Mixing C++ and ObjC

Discussion in 'Public Game Developers Forum' started by Charybdis, Feb 24, 2012.

  1. Charybdis

    Charybdis Well-Known Member

    Apr 2, 2011
    281
    1
    0
    Programmer
    Kyoto, Japan
    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?
     
  2. jhspaybar

    jhspaybar Active Member

    Sep 29, 2011
    44
    0
    0
    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.
     
  3. Charybdis

    Charybdis Well-Known Member

    Apr 2, 2011
    281
    1
    0
    Programmer
    Kyoto, Japan
    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?
     
  4. TouchDeveloper

    TouchDeveloper Active Member

    Feb 19, 2012
    27
    0
    0
    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.
     
  5. Charybdis

    Charybdis Well-Known Member

    Apr 2, 2011
    281
    1
    0
    Programmer
    Kyoto, Japan

    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. :cool:
     
  6. AlienSpace

    AlienSpace Well-Known Member

    May 28, 2010
    416
    0
    16
    Independent developer
    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.
     
  7. Obg1

    Obg1 Well-Known Member

    Feb 17, 2012
    48
    0
    6
    Developer
    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.
     
  8. Charybdis

    Charybdis Well-Known Member

    Apr 2, 2011
    281
    1
    0
    Programmer
    Kyoto, Japan
    Are there multiple emulators to choose from?
    Do people have any reccomendations?
    What IDE do people like use? I heard Eclipse is pretty good
     

Share This Page