Calling Obj-C from C

Member
Posts: 36
Joined: 2009.02
Post: #1
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?
Quote this message in a reply
Member
Posts: 36
Joined: 2009.02
Post: #2
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.
Quote this message in a reply
Member
Posts: 245
Joined: 2005.11
Post: #3
...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.
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #4
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.
Quote this message in a reply
Member
Posts: 36
Joined: 2009.02
Post: #5
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!
Quote this message in a reply
Member
Posts: 51
Joined: 2009.02
Post: #6
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.
Quote this message in a reply
Member
Posts: 36
Joined: 2009.02
Post: #7
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
}
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #8
Pretty much what's posted above, though the Objective-C method call would more likely look more like [objCObject doSomething]; than objcFunction(...);.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  __fastcall calling convention on iPhone Dimka 3 4,840 Aug 21, 2010 03:35 PM
Last Post: OneSadCookie
  removeFromSuperview not calling dealloc of view - SOLVED BeyondCloister 5 14,151 Aug 11, 2010 01:18 PM
Last Post: BeyondCloister
  release memory calling HTMLs files imaumac 3 3,002 Dec 23, 2008 07:49 AM
Last Post: imaumac