Cross Platform Mobile Layer

Started by
2 comments, last by Kimmi 12 years, 11 months ago
I am currently working on an iPhone game which uses OpenGL ES where most of the code is in C++ so that I can port to Android later on. My problem is I'm not quite sure where to start in building a cross platform layer between the C++ of my engine and the Objective-C of the OS.

An example would be accessing the native keyboard of the device. I'd like to be able to call a function in the wrapper which would return a keyboard object which handles the keyboard of that particular OS. I did try once with function pointers but ran into a nasty issue with Objective-C that wouldn't have been trivial to solve.

Does anyone either have any suggestions, or a good source of material to read about the subject? Ideally I'd like to write something nicely object oriented and reusable for future projects.
Advertisement
Simple way is to create 2 Keyboard class files, iphone/keyboard.hpp and android/keyboard.hpp and then just select the one which contains the correct implementation when compiling via an #ifdef. If there is common code between the two classes you can have a base interface for that.




keyboard.hpp

#ifndef KEYBOARD_HPP

#define KEYBOARD_HPP

/*abstract*/ class KeyboardBase { ... };

#ifdef PLATFORM_IOS

#include "iphone/keyboard.hpp"

typedef IPhoneKeyboard Keyboard;

#elif PLATFORM_ANDROID

#include "android/keyboard.hpp"

typedef AndroidKeyboard Keyboard;

#endif

#endif

Thanks for the response. I guess it is pretty much what I expected.

I think you would probably need some sort of Shell based class in which you would derive an AndroidShell and IOSShell from, again using #ifndefs. These two classes would be where the OS specific Keyboard object is created and used from. So the Shell would be the interfacing layer between the engine/app and the OS. In other words, the app never needs to know about which OS is on the other side. Am I on the right track? Or is there a better way?
You should also try to declare a pure abstract Interface for each os-specific Subsystem / component / module. For instance you can define a keyboard-interface and the classes IPhoneKeyboard and AndroidKeyboard are derived from this. Using this the build will fail if you missed the implementation for a subsystem.

Kimmi
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]

This topic is closed to new replies.

Advertisement