I’d like to use SDL for my iOS games, but all my code is in Objective C. I’ve got SDL3 up and running on Xcode, but I can’t figure out how to call my Objective C functions from SDL’s main.c file.
You can just rename main.c to main.m. Objective-C is a strict superset of C, so all valid C code is valid Objective-C code. The entry point for Objective-C applications is int main(int argc, char **argv) just like in C.
Paired with the fact that Objective-C doesn’t do name mangling, it also means any plain functions in Objective-C can be called from C, even if the implementation of that function is in Objective-C.
So if you had to keep main.c as C code, you could implement a wrapper like
/* wrapper.m */
void setPosition(id spriteBridgedToCCode, CGPoint position)
{
/* might have to bridge spriteBridgedToCCode back, I haven't used ObjC in a
while so I don't remember the specifics of using bridging references to
pass ObjC objects back and forth between C and ObjC. It's probably
easier if your ObjC code isn't using ARC. */
[(ObjCSprite *)spriteBridgedToCCode setPosition:position];
}
My memory is a little hazy on exactly how to pass Objective-C objects back and forth with C code. Perhaps someone else can fill that part in.
If you really want to get hairy, you could call the C function objc_msgSend() or one of its variants, which is what calls like [someObject pleaseDo:whatever withThing:thing] get turned into.
Oh, wow, thanks so much, that was so simple!!! Looks like I’m finally ready to ditch my own cobbled together SDL-like OpenGL code on iOS and switch to SDL full time (I’ve been using SDL for years on the Android and Windows ports of my games - but I love developing in Objective C)