Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mac Menubar  (Read 8506 times)

0 Members and 1 Guest are viewing this topic.

zenroth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Mac Menubar
« Reply #15 on: January 25, 2013, 10:46:36 pm »
#ifdef PLATFORM_MAC
NSDictionary *  aboutOptions = [NSDictionary
                        dictionaryWithObjectsAndKeys:@"Credits", @"Credits.rtf",
                        nil];

@interface MenuNotification : NSObject <NSApplicationDelegate>
- (void) terminateRequest: (id)sender;
- (void) performAbout: (id)sender;
@end

@implementation MenuNotification

- (void) performAbout: (id)sender
{
    [ NSApp orderFrontStandardAboutPanelWithOptions: aboutOptions ];
}

- (void) terminateRequest: (id)sender
{
    //set our main loop to end
    DirectReality->running = false;
}
@end

MenuNotification * menuNotify;
#endif
 

#ifdef PLATFORM_MAC
    menuNotify = [[MenuNotification alloc]init];
    [NSApp setDelegate: menuNotify];
   
    id menubar = [[NSMenu new] autorelease];
    id appMenuItem = [[NSMenuItem new] autorelease];
    [menubar addItem:appMenuItem];
    [NSApp setMainMenu:menubar];
   
    id appMenu = [[NSMenu new] autorelease];
    id appName = [[NSProcessInfo processInfo] processName];
   
    NSString *title;
   
    title = [@"About " stringByAppendingString:appName];
    [appMenu addItemWithTitle:title action:@selector(performAbout:) keyEquivalent:@""];
   
    [appMenu addItem:[NSMenuItem separatorItem]];
   
    id quitTitle = [@"Quit " stringByAppendingString:appName];
    id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
                                                  action:@selector(terminateRequest:) keyEquivalent:@"q"] autorelease];
    [appMenu addItem:quitMenuItem];
    [appMenuItem setSubmenu:appMenu];
#endif
 

Anyways no promises on how great that code is. I really don't touch Obj C unless its just to write something once and then wrap it away so I don't have to see it again, but it seems to work just fine.

« Last Edit: January 26, 2013, 06:24:13 pm by zenroth »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Mac Menubar
« Reply #16 on: January 26, 2013, 02:37:35 pm »
thank you
SFML / OS X developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Mac Menubar
« Reply #17 on: January 26, 2013, 02:40:41 pm »
Hmm... I think
Code: [Select]
menuNotify = [MenuNotification alloc];should be
Code: [Select]
menuNotify = [[MenuNotification alloc] init];
Want to play movies in your SFML application? Check out sfeMovie!

zenroth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Mac Menubar
« Reply #18 on: January 26, 2013, 04:55:00 pm »
Hmm... I think
Code: [Select]
menuNotify = [MenuNotification alloc];should be
Code: [Select]
menuNotify = [[MenuNotification alloc] init];

Reading Apple's documentation I think your right. Though I have no idea what difference it *should* make, given there really shouldn't be much in the way of instance variables I would think, and I'd certainly hope since its a custom class derived just from NSObject that init isn't going to swap me out with something else.

Admittedly though looking at the created object in the debugger does show a whole lot of extra junk there, which I wouldn't personally expect.  However, the same junk seems to get the same values from what I can see regardless of "init" or not.

Ah some more apple documentation "The init method defined in the NSObject class does no initialization; it simply returns self." Since I have no init methods defined myself, it seems like it matters squat in the end. Though programming practice certainly says to do it.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Mac Menubar
« Reply #19 on: January 26, 2013, 05:39:52 pm »
Making assumption that none of the parent classes has anything to initialize is not reliable. Even if the documentation says NSObject doesn't initialize anything, you're not guaranteed it'll remain so. Moreover, your work should be reusable and adaptable, which includes the possibility of changing the parent class without rewriting your class (or at least not dealing with that kind of issue).
Want to play movies in your SFML application? Check out sfeMovie!

zenroth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Mac Menubar
« Reply #20 on: January 26, 2013, 06:23:48 pm »
Making assumption that none of the parent classes has anything to initialize is not reliable. Even if the documentation says NSObject doesn't initialize anything, you're not guaranteed it'll remain so. Moreover, your work should be reusable and adaptable, which includes the possibility of changing the parent class without rewriting your class (or at least not dealing with that kind of issue).

Yeah I agree completely. My ramble was mostly based on trying to discover why the current code seems to work fine, regardless of init or not. I'll edit the pasted code above to include the init.

My disclaimer on the code being complete rubbish still stands though. I write just enough Obj C to accomplish something, and then bury it in a dark place where I hopefully never have to see it again. :) As a consequence my familiarity with Obj C is very minimal. I probably write like 100-200 lines of it a year, and I try to avoid that.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: Mac Menubar
« Reply #21 on: January 26, 2013, 07:19:09 pm »
Oh ok, I admit I was a bit afraid that you keep your code without the -init call. There are often people that do things the way they want to and then complain it's working randomly. Sorry for the "rude" tone.
Want to play movies in your SFML application? Check out sfeMovie!

zenroth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Mac Menubar
« Reply #22 on: January 26, 2013, 08:49:05 pm »
Nah no worries. There is rarely ever a good reason to leave something in a sort of undefined behavior state, especially when its a simple correction and doesn't hurt anything. Better safe than sorry down the road when Apple tweaks something, as they have a habit of doing.

Acrobat

  • Full Member
  • ***
  • Posts: 153
    • View Profile
Re: Mac Menubar
« Reply #23 on: January 28, 2013, 10:48:26 am »
By the way, have you tested Quit, does it close application properly ? I mean finishing the application loop ?

zenroth

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Mac Menubar
« Reply #24 on: January 28, 2013, 07:30:22 pm »
By the way, have you tested Quit, does it close application properly ? I mean finishing the application loop ?
Well in my game, the main loop is just a while loop until a running flag gets set to false, either via a SFML close event, or some other cause. I simply have the Quit menu set my running flag to false, so on the next update the main game loop ends, and it goes off into a normal shutdown flow.

Previously I had tried having the quit option just call Terminate, but this sadly doesn't seem to inject a CLOSE event via SFML.


 

anything