Trouble writing a GLUT-like API

Sage
Posts: 1,199
Joined: 2004.10
Post: #1
So, I normally wouldn't bother doing this in the first place, but a friend wants to introduce his kid to C, and wants a (very) simple drawing API. E.g., a window and simple C calls to DrawBox, DrawCircle, etc from basic callbacks.

Now, I pushed him towards PyGame and SDL, but he wants the simplest thing possible, and I've written it. It's dirt simple and works as needed. The trouble is I want it to be easy to build, not just to write.

That is to say, I want to be able to build one of these apps on the command line like so:

# gcc -o demo -framework SimpleDraw demo.c

(SimpleDraw is my framework)

Right now, if I do this I get a window but no menu, it doesn't play well with OSX. User input doesn't work, I can't command-tab to it, etc etc.

If on the other hand, I use Xcode to make a single-window Cocoa app, delete the nib, link against SImpleDraw and add 'demo.c' it works just like I want -- I get a window, keyboard & mouse input work, it shows up on the dock, etc.

I want a simple build system because the goal here is for this to be as easy to a child as it was back when we were kids writing BASIC and the like.

Trouble is that while I'm a Cocoa guy, I've never tried to subvert Cocoa like this. Normally I'm happy to just use Interface Builder and move on.

Here's how I start the application:
Code:
int SDApplicationMain( SDContextDelegate delegate, const char *appName, int argc, const char *argv[] )
{
    //
    //    I'm ignoring argc/argv -- but perhaps someday I'll make use of it
    //

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];    

    {
        NSApp = SDApp = (SDApplication*)[SDApplication sharedApplication];
        
        unsigned int mask = NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
        NSBackingStoreType type = NSBackingStoreBuffered;
        
        NSRect winRect = [NSWindow frameRectForContentRect: NSMakeRect(0, 0, delegate.windowWidth, delegate.windowHeight ) styleMask:mask];
        winRect.origin.x = 0;
        winRect.origin.y = 0;
        
        SDView *view = [[SDView alloc] initWithFrame: winRect];
        
        [SDApp setDisplayName: [NSString stringWithUTF8String:appName]];
        [SDApp setContextDelegate: delegate];
        [SDApp setDrawingView: view];
        
        NSWindow *window = [[NSWindow alloc] initWithContentRect:winRect styleMask:mask backing:type defer:NO];
        [window setTitle: [NSString stringWithUTF8String: appName ]];
        [window setContentView: view];        
        
        [NSApp awakeFromNib];
    }

    [pool release];

    //
    //    enter the application main event loop.
    //

    [NSApp run];
    return 0;
}

The int main() function looks like so:
Code:
int main(int argc, const char *argv[])
{
    SDContextDelegate delegate = SDCreateContextDelegate( 500, 400 );

    delegate.onResize = Resize;
    delegate.onDisplay = Display;
    delegate.onKeyPress = KeyPressed;
    delegate.onKeyRelease = KeyReleased;
    delegate.onMouseButtonPress = MouseButtonPressed;
    delegate.onMouseButtonRelease = MouseButtonReleased;
    delegate.onMouseMove = MouseMoved;
    delegate.onMouseScroll = MouseScroll;
    
    return SDApplicationMain( delegate, "Hello SimpleDraw", argc, argv );
}

And the code for SDApplication ( an NSApplication subclass ) is too long to post here, so here's a link: http://pastie.textmate.org/496433

( It was loosely inspired by OSC's GameShell )

So, for the TL;DR crowd:

1) It works for .app bundle cocoa apps

2) It does not work for command-line apps

Somehow GLUT pulls this off; I'm looking over Apple's GLUT sourcecode, but I haven't figured out what Apple's doing that I'm not doing...

P.S. I want to re-iterate that I've already suggested stuff like PyGame. My friend wants a clean C api, and that's what I've made. I just want it to be easy to compile on the command line.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Code:
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #3
OneSadCookie Wrote:
Code:
ProcessSerialNumber psn;
GetCurrentProcess(&psn);
TransformProcessType(&psn, kProcessTransformToForegroundApplication);

Holy smokes! It worked -- thanks, OSC.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Writing libraries vs writing apps. Najdorf 8 5,008 Nov 13, 2008 02:32 PM
Last Post: backslash
  Trouble with Time and GLUT looping mechanisms Jones 22 7,870 Aug 14, 2006 02:02 PM
Last Post: ermitgilsukaru