Calling Obj-C from C
Hi,
I want to implement online rankings for my shoote'm up game (check this tread to know more about it) and I'm planning to use cocoslive.
My game is implemented in C and the only Obj-C code I have right now is for interacting with the OS. Once the window, view and everything else is setup, I pass the control to the game itself. No problems so far. Everything works as expected.
But now I want the score to be sent to a server, right after the GAME OVER screen. The problem I'm having is that because cocoslive is Obj-C, I don't know how to call the functions I need to call from my C code.
Any ideas or suggestions? Anything trivial I'm missing?
I want to implement online rankings for my shoote'm up game (check this tread to know more about it) and I'm planning to use cocoslive.
My game is implemented in C and the only Obj-C code I have right now is for interacting with the OS. Once the window, view and everything else is setup, I pass the control to the game itself. No problems so far. Everything works as expected.
But now I want the score to be sent to a server, right after the GAME OVER screen. The problem I'm having is that because cocoslive is Obj-C, I don't know how to call the functions I need to call from my C code.
Any ideas or suggestions? Anything trivial I'm missing?
Wait, I think I know how to do it. It's probably not the cleanest way but I think it should work.
Because in Obj-C I set my main loop function which is Obj-C as well, I can ask the game (C code) every frame if it's time to send a new score or retrieve it from the server. If that happens, I can call the functions I need.
Because in Obj-C I set my main loop function which is Obj-C as well, I can ask the game (C code) every frame if it's time to send a new score or retrieve it from the server. If that happens, I can call the functions I need.
...or you could just change the file extension of the C file in question to .m and go knock yourself out.
Any standard C code will compile as Objective-C without changes, and you can use the extra bits of Objective-C without having to put stuff in classes. As long as you aren't running a thread outside of your main obj-C loop you shouldn't even need to worry about NSAutorelease pools.
Any standard C code will compile as Objective-C without changes, and you can use the extra bits of Objective-C without having to put stuff in classes. As long as you aren't running a thread outside of your main obj-C loop you shouldn't even need to worry about NSAutorelease pools.
Another option is to define a simple C interface for the Objective-C functions you need to call, and write a small amount of glue code that lets you call them from a .c file. So, file.c #includes glue.h, which defines a set of C functions. glue.m calls through to Objective-C code in the function implementations. I use this approach when I want to keep Objective-C code out of the core of my program.
Your approaches are clearly better than mine. I could rename my C file to .m as backslash suggests, but I like the idea of having a wrapper for Obj-C functions better, just in case I decide to port my game to another platform sometime later.
Thanks guys!
Thanks guys!
ThemsAllTook Wrote:Another option is to define a simple C interface for the Objective-C functions you need to call, and write a small amount of glue code that lets you call them from a .c file. So, file.c #includes glue.h, which defines a set of C functions. glue.m calls through to Objective-C code in the function implementations. I use this approach when I want to keep Objective-C code out of the core of my program.
Could you give an example of what the glue looks like? I just did a whole game in C (submitted and waiting). I had several times where I wanted to call Objective C from C, and was confused enough that I ended up with solutions I wasn't really happy with.
Nosredna Wrote:Could you give an example of what the glue looks like? I just did a whole game in C (submitted and waiting). I had several times where I wanted to call Objective C from C, and was confused enough that I ended up with solutions I wasn't really happy with.I haven't implemented it yet, but from what I understand it would look like:
yourFile.c (file from where you'd ideally call Obj-C functions but you end up calling the glue ones instead)
Code:
#include "glue.h"
void yourFunction(...)
{
.
.
.
glueDoSomething(...);
.
.
.
}glue.h
Code:
void glueDoSomething(...);glue.m
Code:
#import "glue.h"
#import "yourObjCClass.h"
void glueDoSomething(...)
{
objCFunction(...); // The Obj-C function you want to call, implemented in yourObjCClass.m
}
Pretty much what's posted above, though the Objective-C method call would more likely look more like [objCObject doSomething]; than objcFunction(...);.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| __fastcall calling convention on iPhone | Dimka | 3 | 3,828 |
Aug 21, 2010 03:35 PM Last Post: OneSadCookie |
|
| removeFromSuperview not calling dealloc of view - SOLVED | BeyondCloister | 5 | 9,730 |
Aug 11, 2010 01:18 PM Last Post: BeyondCloister |
|
| release memory calling HTMLs files | imaumac | 3 | 2,493 |
Dec 23, 2008 07:49 AM Last Post: imaumac |
|

