SDL and menu bar on OS X

Nibbie
Posts: 3
Joined: 2009.05
Post: #1
I want to make a working menu bar for my SDL program on Mac OS X. I've done Googling on how it's done, but to little avail. I tried the latest SDLMain.m/.h, but don't know how to work with it from there (also Quit doesn't even work on it, shouldn't it work?), and I've tried setting SDL_USE_NIB_FILE to 1 with the latest copy of SDLMain.nib/ in the same place, but the program doesn't even start and XCode reports it exited with status 1.

It's very important that I at least get the Quit option to work, from the menu, not just the shortcut, as even I can't get used to not using it, and then I need to add a whole bunch of menu items and connect it to my already existing C code (works by setting an array of flags). (Disclaimer: I'm not very experienced with Cocoa/ObjC)

Can anyone point me to a definitive solution to how to fix the Quit problem, and perhaps pointers on how to go from there with the rest of the menu? The problem has been around for so long surely someone has it all figured out by now?
Quote this message in a reply
Member
Posts: 34
Joined: 2010.01
Post: #2
Last I recall, quit worked for me, just not the shortcut in the SDL templates. There's a solution here how to get the shortcuts to work: http://lists.libsdl.org/pipermail/sdl-li...46674.html
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2009.05
Post: #3
zgcoder Wrote:Last I recall, quit worked for me, just not the shortcut in the SDL templates. There's a solution here how to get the shortcuts to work: http://lists.libsdl.org/pipermail/sdl-li...46674.html

Thanks. I've found that the default SDLMain.m works, except that the SDL_QUIT event is sent but not correctly processed. So at the end of the terminate function I added a exit(EXIT_SUCCESS); (good enough for me).

So it seems a good starting point. The shortcut doesn't work right out the box so I'll look into that now.
Quote this message in a reply
Member
Posts: 34
Joined: 2010.01
Post: #4
Here is the file I use (I got rid of SDLMain.h and put the interface inside of SDLMain.m). I don't recall having issues with processing the SDL_QUIT event. Are you processing it correctly in your code (Basically, break out of the game loop and let your program terminate on its own)? The shortcuts, command Q or H or M, seem to work for me too with my SDLMain.m
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2009.05
Post: #5
zgcoder Wrote:Here is the file I use (I got rid of SDLMain.h and put the interface inside of SDLMain.m). I don't recall having issues with processing the SDL_QUIT event. Are you processing it correctly in your code (Basically, break out of the game loop and let your program terminate on its own)? The shortcuts, command Q or H or M, seem to work for me too with my SDLMain.m

I'm not sure about that, but basically I use a flagging system I made (an array of flags) so now when I click a menu entry it sets the flag and does what it's supposed to do, even for quitting. Also I found a version of SDLMain.m where shortcuts work. So everything's working fine now. What puzzles me is why in SVN the latest SDLMain.m doesn't support the keyboard shortcuts.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #6
I just modified the quartz event handling routines to let the menu bar claim the event before passing them on to SDL. Works like a charm. I'd have to look up how I did it though and my main computer is out of service due to a dead battery and a power adapter that was forgotten at work. Sad

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 34
Joined: 2010.01
Post: #7
This is how processing quit is suppose to work. This is partially (modified a bit) taken from the SDL template:

Code:
SDL_Init (SDL_INIT_VIDEO);

//...

SDL_Event event;
SDL_bool done = SDL_FALSE;

while ( !done )
{
    /* Check for events */
    while ( SDL_PollEvent (&event) )
    {
        switch (event.type)
        {
            case SDL_KEYDOWN:
                    // Handle key presses
                                break;
            case SDL_QUIT:
                    // we're quitting
                done = SDL_TRUE;
                break;
            default:
                break;
        }
    }
    
    // Do game stuff
    //...
}

//...

SDL_Quit();

If I wanted to add more menu items, I would add them by using cocoa methods for adding menus (for example, see -setupWindowMenu in SDLMain.m) and set up actions that pass events using SDL's system (for example, see -terminate: in SDLMain.m which handles quitting). So if, say, I wanted to implement a fullscreen menu option, the action would be something like this:

Code:
- (void)fullscreen:(id)sender
{
    /* Post an event */
    SDL_Event event;
    event.type = 27; // Random number I picked, check events.h for possible values to choose from
    SDL_PushEvent(&event);
}

And in the game, I'd check event.type against 27 (this should probably be a constant) to handle fullscreen.

I'm not sure if this is how Skorche does it (no idea what he means by quartz event handling).
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #8
I had made a trivial modification (added a single line) to the OS X SDL event handling code. The file name was something like QuartzEvent.m or something, though without being able to boot my other computer I can't easily get the specifics.

All I did was to pass the event to the main menu bar object and return early if it did trap the event.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #9
Somebody asked about it, so I looked it up.

All I did was add:
Code:
    // Pass key events on to the menu to handle cmd-keys
    if([[NSApp menu] performKeyEquivalent:event])
        return;

To the top of the QZ_DoKey() function in video/quartz/SDL_QuartzEvents.m. It gives the menu a chance to process and swallow the event before it's passed to SDL. Cmd-q will "just work" and other things like Cmd-h or Cmd-m will work and the keypresses that trigger them wont make it to the game to screw things up.

I haven't used SDL in a couple years though. Hopefully it still actually works. Wink

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2012.02
Post: #10
@Skorche

Thanks a million!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Can't get menu bars to work. XSTNX 0 2,357 Jun 26, 2009 09:08 AM
Last Post: XSTNX
  GLUT apple menu close application Rasterman 31 15,317 Aug 15, 2007 04:48 PM
Last Post: AnotherJake
  OpenGL Menu engine? BinarySpike 4 7,322 Jun 11, 2007 09:43 PM
Last Post: JeroMiya
  Remove the File Menu from app guvidu 2 3,394 Mar 23, 2007 12:43 PM
Last Post: PowerMacX
  Enabling the "quit" menu function in an SDL program ferum 2 4,143 Sep 10, 2006 02:35 PM
Last Post: PowerMacX