OSX Lion fullscreen support

Hi!

I successfully implemented OSX Lion full screen support on my game, instead of the default (I think it looks so much better).

I still need to test on 10.6 to see if it still works OK, but if there is anybody interested in this, I can share the code, or maybe submit to the cocos2d-x trunk.

I’m interested! Thanks! :slight_smile:

Ben

I forked the project and modified the necessary files: https://github.com/bassarisse/cocos2d-x

The files you need are CCWindow.h and .m, and AppController.h and .mm from inside proj.mac.

The only requirement is that your game adapt to the screen resolution.
Please let me know if it works for you.

Hey Bruno, this works great. Thanks very much for sharing! Are you pushing this to master?

One small thing; should this line (AppController.mm):
if (_firstBecomeKey & _startsInFullScreen)

…be this?
if (_firstBecomeKey && _startsInFullScreen)

Also, I’m trying to add dynamic resizing to my app so the user can drag and resize the window to their heart’s content. I’ve added this line to make the window resizable:
[window setStyleMask:window.styleMask | NSResizableWindowMask];
…and this line to preserve the aspect ratio:
[window setContentAspectRatio:NSMakeSize(1280.0f, 960.0f)];
…and then responding to the resize event in this method:
-(void)windowDidResize:(NSNotification *)notification { }

However, I’m not really sure how to resize the game area itself. Should I be calling [eaglView setFrameZoomFactor] in there? Any advice you could give would be very helpful. Thanks so much for your help Bruno.

Ben

Xcode kept asking me to change to one & because _startsInFullScreen was const, so I just gave up =)
I think I’ll push to master once everything is working without problems.

I managed to make the window resizable. The zoom factor is indeed the key, I made it recalculate every time the window gets resized. But, yes, I had to keep the window always locked on the initial aspect ratio (in fullscreen too, btw), so the clicks won’t get messed up (actually, before all these modifications, mouse clicks never worked correctly for me when in fullscreen).
The game screen will only be resized. I don’t know how to show more of the game when the window is resized. I’m fairly new to all this.

I’ll update the fork tomorrow and then I’ll let you know. Gotta sleep now!

Ahhh, yes I had managed to get the thing resizing properly at any aspect ratio, but the clicks were getting messed up as you describe. I wonder if there’s a way of recalculating the clicking area? Hmm…

It’s a shame, as I was hoping to run my game in a 4:3 ratio when windowed, and then stretch to whatever the screen ratio is when it’s in fullscreen. The layers all cope fine, it’s just the windowing and capturing the clicks which is busted. Anyway, I’m looking forward to seeing your code Bruno, thanks for your help It’s much appreciated!

Ben

The code was updated (https://github.com/bassarisse/cocos2d-x), please tell me if its working for you. The only thing now is that you can set your design resolution size on AppController.mm, and the aspect ratio will be set through it. For the mouse clicks to work correctly, the dimensions will be scaled to fit inside your screen (like kResolutionShowAll), I don’t know exactly why, but it works.

I moved all fullscreen code from EAGLView to CCWindow. Also, I removed the extra CCWindow creation on 10.6 fullscreen method, does anybody know why it was like that? Maybe I’m missing something. I’ll appreciate if someone could explain to me.

I’m wondering what should be done to modify the aspect ratio of the game without stretching it. Change the design resolution size from CCEGLView and trigger some events so we can adjust necessary HUD elements? On Paralaxer (http://paralaxer.com), when you go fullscreen, more of the scenery is shown, how is this done?

Hi Bruno,

Sorry for my delayed response. I’ve just found the time to test your new code, and it seems to be working great! Fullscreen works perfectly on my Mountain Lion machine, and window resizing also seems to be working too. The only thing I’ve added to my implementation is a call to:

[window setMinSize:NSMakeSize(designResolutionSize.width*0.25f, designResolutionSize.height*0.25f)];

…to avoid the user accidentally resizing the window to some minuscule size.

I’ve also set the windowedFrameZoomFactor to 0.9f in my implementation, as 0.5f seems a little too small initially (although obviously this will vary depending on the game I suppose).

Anyway, this is great and should definitely be submitted to the main branch IMO. Thank you so much for sharing!

Ben

Glad it worked for you.
I added the minimum size code to CCWindow.mm and removed the windowedFrameZoomFactor from it, as it was only used on AppController.mm. I also changed the default zoom factor to 0.75f.

I’m hoping to submit the code by this week.
Thanks for the help!

Hey Bruno,

Just a heads up - your windowing code doesn’t seem to work in Snow Leopard. I’m not sure why - it simply does not show the window on that operating system. I’m having a look into it now and will report back if I can find the cause…

Ben

Sorry Bruno, I was wrong about the previous post - the problem wasn’t with your windowing code but rather a connected system. Unfortunately it had the symptoms of looking like it was to do with your code, hence my (rash) conclusion. Once I had patched up the other system it all appears to be working fine.

Sorry for the false alarm, my mistake!

Ben

Sorry to necro-bump this. I’ve gotten this working in fullscreen, but have the mouse issues whereby the mouse click is registered at the desktop resolution (1680x1050) instead of the OpenGL resolution (1280x768). Now, I’m sure I could do some fancy conversions, but is there a better way of doing this within Cocos2dx?

Sorry, that https://github.com/bassarisse/cocos2d-x link doesn’t contains the modifications for the OSX fullscreen anymore. As they’re using GLFW on cocos2d-x 3.0, my code stopped working. I attached the modified files if you want to mess with them, but note that I haven’t tested with the latest cocos2d-x 2.x.

When I tested, after those modifications, the clicks were registered correctly.

Thanks :slight_smile: So what I actually ended up doing was reworking the default code for applicationDidFinishLaunching that’s generated from the multi-projects python script in version 2.2 like so:

    -(void) applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        NSRect displayRect = [[NSScreen mainScreen] frame];
        window = [[CCWindow alloc] initWithFrame:displayRect fullscreen:YES];
        glView = [[EAGLView alloc] initWithFrame:displayRect];
        [glView setFullScreen:YES];
        [glView becomeFirstResponder];
        [window setContentView:glView];
        [window makeKeyAndOrderFront:self];
        [window makeMainWindow];

        cocos2d::CCApplication::sharedApplication()->run();
    }

I also took out the toggleFullscreen support and dug into CCWindow.m to change it like so (though really I should have extended this on my own and overridden the function — was in a hurry):

- (void) keyDown:(NSEvent *)event
{
    // Escape exits the program
    if([event keyCode] == 53)
    {
        exit(0);
    }
}

This is with 2.2. I tried 3.0 but there were a lot of funky memory access errors I hadn’t encountered with any versions prior to 3.0. I dropped back to 2.2 until that stabilized a bit more. Seems like they’re reworking the way allocations are handled.

As to 3.0 fullscreen support under OSX… is that coming or are they getting rid of it entirely? I’m not a graphics engineer by trade, so I love relying on the engine to handle all of that fuzzy work for me.

Well, I think with GLFW this will improve a bit. As long as they use a version that supports this “new” method of OSX fullscreen, it will be fine.

Does anyone know if this code can possibly make Mac OS X fullscreen support work in Cocos2d-x 2.x?

I have been trying for a few hours with the provided .zip file but have had no luck resolving the core issue around touch mapping.

I’d love to have all the fancy extra features, of course, but I just cannot seem to figure out what in the attached zip file fixes the touch coordinates bug.

@corytrese wrote:

Does anyone know if this code can possibly make Mac OS X fullscreen support work in Cocos2d-x 2.x?

I have been trying for a few hours with the provided .zip file but have had no luck resolving the core issue around touch mapping.

I’d love to have all the fancy extra features, of course, but I just cannot seem to figure out what in the attached zip file fixes the touch coordinates bug.

After some more hacking, I have it working. The main point of contention is the differences in naming of the EGLView, EAGLView, CCEAGLView classes. Once you make the zip file’s content match the variations used by your Cocos2d-x version (2.2 in my case) it will work fine.

Now there is a “GLViewImpl::createWithFullScreen” method, that allows creating a fullscreen window on desktop platforms.

Does it work on Mac? If so where to call this function???

In your AppDelegate.cpp replace this line: “glview = GLViewImpl::create(“YourApp”);” with this one: “glview = GLViewImpl::createWithFullScreen(“YourApp”);”