★ TouchArcade needs your help. Click here to support us on Patreon.

Mixing C++ and ObjC

02-23-2012, 11:35 PM
#1
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Mixing C++ and ObjC

Hi guys
I have a question relating to the stucture of prgrams that mix languages.
My development system is 98% C++ with a bare bones ObjC app. My main ViewController file is .mm so it is easy to call C++ functions from Obj C. But sometimes I need to do the opposite.
My way of solving this is to create a singleton MessageManager, which is really just a dynamic list of strings which I scan through in the ObjC side.
So my question is this: how do other people deal with this situation. Is there a better way?

Game source code for PC, Mac, Linux and iOS here.
02-24-2012, 12:24 AM
#2
I think one method is to create a c++ class wrapper. Create a .h file that includes the definitions of everything you need, then depending on your target system, change the implementation file. So, on iOS where you call system specific objective-c functions, have your implementation file be .mm. On Android where you need to use JNI to call Java functions...have a normal cpp file that uses JNI for the implementation. Then, the header is constant, and the functionality should be equal across systems if done properly.

02-24-2012, 01:08 AM
#3
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Quote:
Originally Posted by jhspaybar View Post
I think one method is to create a c++ class wrapper. Create a .h file that includes the definitions of everything you need, then depending on your target system, change the implementation file. So, on iOS where you call system specific objective-c functions, have your implementation file be .mm. On Android where you need to use JNI to call Java functions...have a normal cpp file that uses JNI for the implementation. Then, the header is constant, and the functionality should be equal across systems if done properly.
Thanks for your reply.
Hmmm, actually I have done this with a number of my objects, like texturemanager, audiomanager, etc. But they were easy becasue they were self contained. I guess I neeed to improve the structure of my main app and gameloop.
On the second point, I have avoided android until now because I dont know java and I dont have any hardware to test on. Is there anything equivalent to the iphone simulator for android?

Game source code for PC, Mac, Linux and iOS here.
02-24-2012, 03:22 AM
#4
Quote:
Originally Posted by Charybdis View Post
Hi guys
I have a question relating to the stucture of prgrams that mix languages.
My development system is 98% C++ with a bare bones ObjC app. My main ViewController file is .mm so it is easy to call C++ functions from Obj C. But sometimes I need to do the opposite.
My way of solving this is to create a singleton MessageManager, which is really just a dynamic list of strings which I scan through in the ObjC side.
So my question is this: how do other people deal with this situation. Is there a better way?
There are no clean answers only hacks.

In the simplest method, you can create static C/C++ function wrappers for the Obj-C functions you want to call and include that .h file in the C++ code. Put the implementations of those wrapper functions in a .mm file and have them call the corresponding Obj-C functions.

Things can get more complicated depending on what parameters you want to send across, calling specific instances, etc.
02-24-2012, 05:27 AM
#5
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Quote:
Originally Posted by TouchDeveloper View Post
There are no clean answers only hacks.

In the simplest method, you can create static C/C++ function wrappers for the Obj-C functions you want to call and include that .h file in the C++ code. Put the implementations of those wrapper functions in a .mm file and have them call the corresponding Obj-C functions.

Things can get more complicated depending on what parameters you want to send across, calling specific instances, etc.

Thanks for the reply. This is acceptable way of doing things.
Currently my message manager is satisfactory because it is just used for things like responding to the occasion button press, for example a "more games" button to take the user to a webpage, rather than anything within the game itself. It's just that I dont like the icky feeling of substandard/hacky program structure.

Game source code for PC, Mac, Linux and iOS here.
02-24-2012, 08:51 PM
#6
Quote:
Originally Posted by Charybdis View Post
Thanks for the reply. This is acceptable way of doing things.
Currently my message manager is satisfactory because it is just used for things like responding to the occasion button press, for example a "more games" button to take the user to a webpage, rather than anything within the game itself. It's just that I dont like the icky feeling of substandard/hacky program structure.
In my code the goal is to never have to call ObjC code from the C++ engine. I used to have ObjC code to do texture initialization, and in that case I used the c++ wrapper function approach.

Message-passing seems more appropriate to handling multi-threading situations, and I wouldnt call it a hack as long as it's by design and not just an afterthought to fix the symptom instead of the problem.

Independent developer of Alien Space, Alien Space Retro, and now Annihilate

For more information and latest updates, join us on Facebook: https://www.facebook.com/AlienSpaceGame
02-26-2012, 05:53 PM
#7
About Android, you can use an Emulator,
But for games it is very slow, unlike the iOS Simulator.
It is always best to test and develop on real hardware.
03-04-2012, 02:59 AM
#8
Joined: Apr 2011
Location: Kyoto, Japan
Posts: 281
Quote:
Originally Posted by Obg1 View Post
About Android, you can use an Emulator,
But for games it is very slow, unlike the iOS Simulator.
It is always best to test and develop on real hardware.
Are there multiple emulators to choose from?
Do people have any reccomendations?
What IDE do people like use? I heard Eclipse is pretty good

Game source code for PC, Mac, Linux and iOS here.