From fa17b7e6166c75edd0fd0b563feb4051b58dc84c Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Mon, 30 Jan 2012 00:02:54 +0100 Subject: [PATCH 1/2] Compatibility fixes. --- src/cocoa_joystick.m | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/cocoa_joystick.m b/src/cocoa_joystick.m index 5e3a6bf8..0aa10a4a 100644 --- a/src/cocoa_joystick.m +++ b/src/cocoa_joystick.m @@ -232,11 +232,13 @@ static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* eleme static void removeJoystick(_glfwJoystick* joystick) { + int i; + if (joystick->present) { joystick->present = GL_FALSE; - for (int i = 0; i < joystick->numAxes; i++) + for (i = 0; i < joystick->numAxes; i++) { _glfwJoystickElement* axes = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i); @@ -245,7 +247,7 @@ static void removeJoystick(_glfwJoystick* joystick) CFArrayRemoveAllValues(joystick->axes); joystick->numAxes = 0; - for (int i = 0; i < joystick->numButtons; i++) + for (i = 0; i < joystick->numButtons; i++) { _glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i); @@ -254,7 +256,7 @@ static void removeJoystick(_glfwJoystick* joystick) CFArrayRemoveAllValues(joystick->buttons); joystick->numButtons = 0; - for (int i = 0; i < joystick->numHats; i++) + for (i = 0; i < joystick->numHats; i++) { _glfwJoystickElement* hat = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, i); @@ -287,23 +289,26 @@ static void removalCallback(void* target, IOReturn result, void* refcon, void* s static void pollJoystickEvents(void) { - for (int i = 0; i < GLFW_JOYSTICK_LAST + 1; i++) + int i; + CFIndex j; + + for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++) { _glfwJoystick* joystick = &_glfwJoysticks[i]; if (joystick->present) { - for (CFIndex i = 0; i < joystick->numButtons; i++) + for (j = 0; j < joystick->numButtons; j++) { _glfwJoystickElement* button = - (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i); + (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, j); button->value = getElementValue(joystick, button); } - for (CFIndex i = 0; i < joystick->numAxes; i++) + for (j = 0; j < joystick->numAxes; j++) { _glfwJoystickElement* axes = - (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i); + (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, j); axes->value = getElementValue(joystick, axes); } } @@ -449,7 +454,9 @@ void _glfwInitJoysticks(void) void _glfwTerminateJoysticks(void) { - for (int i = 0; i < GLFW_JOYSTICK_LAST + 1; i++) + int i; + + for (i = 0; i < GLFW_JOYSTICK_LAST + 1; i++) { _glfwJoystick* joystick = &_glfwJoysticks[i]; removeJoystick(joystick); @@ -498,6 +505,8 @@ int _glfwPlatformGetJoystickParam(int joy, int param) int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) { + int i; + if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST) return 0; @@ -514,7 +523,7 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) // Update joystick state pollJoystickEvents(); - for (int i = 0; i < numaxes; i++) + for (i = 0; i < numaxes; i++) { _glfwJoystickElement* axes = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.axes, i); @@ -543,6 +552,8 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, int numbuttons) { + int i; + if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST) return 0; @@ -559,7 +570,7 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons, // Update joystick state pollJoystickEvents(); - for (int i = 0; i < numbuttons; i++) + for (i = 0; i < numbuttons; i++) { _glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.buttons, i); buttons[i] = button->value ? GLFW_PRESS : GLFW_RELEASE; From b6c510c9d1b1d5c05e696c835b997f6a75b6f05e Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Tue, 31 Jan 2012 17:53:19 +0100 Subject: [PATCH 2/2] Split window and context creation into separate functions. --- src/cocoa_window.m | 258 ++++++++++++++++++++++++++------------------- 1 file changed, 150 insertions(+), 108 deletions(-) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index ce95217d..ffec23b0 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -443,20 +443,68 @@ static int convertMacKeyCode(unsigned int macKeyCode) @end - -////////////////////////////////////////////////////////////////////////// -////// GLFW platform API ////// -////////////////////////////////////////////////////////////////////////// - //======================================================================== -// Here is where the window is created, and the OpenGL rendering context is -// created +// Create the Cocoa window //======================================================================== -int _glfwPlatformOpenWindow(_GLFWwindow* window, - const _GLFWwndconfig *wndconfig, - const _GLFWfbconfig *fbconfig) +static GLboolean createWindow(_GLFWwindow* window, + const _GLFWwndconfig* wndconfig) { + unsigned int styleMask = 0; + + if (wndconfig->mode == GLFW_WINDOWED) + { + styleMask = NSTitledWindowMask | NSClosableWindowMask | + NSMiniaturizableWindowMask; + + if (wndconfig->resizable) + styleMask |= NSResizableWindowMask; + } + else + styleMask = NSBorderlessWindowMask; + + window->NS.window = [[NSWindow alloc] + initWithContentRect:NSMakeRect(0, 0, window->width, window->height) + styleMask:styleMask + backing:NSBackingStoreBuffered + defer:NO]; + + if (window->NS.window == nil) + { + _glfwSetError(GLFW_PLATFORM_ERROR, + "Cocoa/NSOpenGL: Failed to create window"); + return GL_FALSE; + } + + [window->NS.window setTitle:[NSString stringWithCString:wndconfig->title + encoding:NSISOLatin1StringEncoding]]; + + [window->NS.window setContentView:[[GLFWContentView alloc] + initWithGlfwWindow:window]]; + [window->NS.window setDelegate:window->NS.delegate]; + [window->NS.window setAcceptsMouseMovedEvents:YES]; + [window->NS.window center]; + + return GL_TRUE; +} + +//======================================================================== +// Create the OpenGL context +//======================================================================== + +static GLboolean createContext(_GLFWwindow* window, + const _GLFWwndconfig* wndconfig, + const _GLFWfbconfig* fbconfig) +{ + unsigned int attributeCount = 0; + + // Mac OS X needs non-zero color size, so set resonable values + int colorBits = fbconfig->redBits + fbconfig->greenBits + fbconfig->blueBits; + if (colorBits == 0) + colorBits = 24; + else if (colorBits < 15) + colorBits = 15; + #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 // Fail if any OpenGL version above 2.1 other than 3.2 was requested if (wndconfig->glMajor > 3 || @@ -499,104 +547,7 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window, return GL_FALSE; } - // We can only have one application delegate, but we only allocate it the - // first time we create a window to keep all window code in this file - if (_glfwLibrary.NS.delegate == nil) - { - _glfwLibrary.NS.delegate = [[GLFWApplicationDelegate alloc] init]; - if (_glfwLibrary.NS.delegate == nil) - { - _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create application " - "delegate"); - return GL_FALSE; - } - - [NSApp setDelegate:_glfwLibrary.NS.delegate]; - } - - window->NS.delegate = [[GLFWWindowDelegate alloc] initWithGlfwWindow:window]; - if (window->NS.delegate == nil) - { - _glfwSetError(GLFW_PLATFORM_ERROR, - "Cocoa/NSOpenGL: Failed to create window delegate"); - return GL_FALSE; - } - - // Mac OS X needs non-zero color size, so set resonable values - int colorBits = fbconfig->redBits + fbconfig->greenBits + fbconfig->blueBits; - if (colorBits == 0) - colorBits = 24; - else if (colorBits < 15) - colorBits = 15; - - // Ignored hints: - // OpenGLMajor, OpenGLMinor, OpenGLForward: - // pending Mac OS X support for OpenGL 3.x - // OpenGLDebug - // pending it meaning anything on Mac OS X - - // Don't use accumulation buffer support; it's not accelerated - // Aux buffers probably aren't accelerated either - - CFDictionaryRef fullscreenMode = NULL; - if (wndconfig->mode == GLFW_FULLSCREEN) - { - // I think it's safe to pass 0 to the refresh rate for this function - // rather than conditionalizing the code to call the version which - // doesn't specify refresh... - fullscreenMode = - CGDisplayBestModeForParametersAndRefreshRateWithProperty( - CGMainDisplayID(), - colorBits + fbconfig->alphaBits, - window->width, window->height, - wndconfig->refreshRate, - // Controversial, see macosx_fullscreen.m for discussion - kCGDisplayModeIsSafeForHardware, - NULL); - - window->width = - [[(id)fullscreenMode objectForKey:(id)kCGDisplayWidth] intValue]; - window->height = - [[(id)fullscreenMode objectForKey:(id)kCGDisplayHeight] intValue]; - } - - unsigned int styleMask = 0; - - if (wndconfig->mode == GLFW_WINDOWED) - { - styleMask = NSTitledWindowMask | NSClosableWindowMask | - NSMiniaturizableWindowMask; - - if (wndconfig->resizable) - styleMask |= NSResizableWindowMask; - } - else - styleMask = NSBorderlessWindowMask; - - window->NS.window = [[NSWindow alloc] - initWithContentRect:NSMakeRect(0, 0, window->width, window->height) - styleMask:styleMask - backing:NSBackingStoreBuffered - defer:NO]; - - [window->NS.window setTitle:[NSString stringWithCString:wndconfig->title - encoding:NSISOLatin1StringEncoding]]; - - [window->NS.window setContentView:[[GLFWContentView alloc] initWithGlfwWindow:window]]; - [window->NS.window setDelegate:window->NS.delegate]; - [window->NS.window setAcceptsMouseMovedEvents:YES]; - [window->NS.window center]; - - if (wndconfig->mode == GLFW_FULLSCREEN) - { - CGCaptureAllDisplays(); - CGDisplaySwitchToMode(CGMainDisplayID(), fullscreenMode); - } - - unsigned int attribute_count = 0; - -#define ADD_ATTR(x) { attributes[attribute_count++] = x; } +#define ADD_ATTR(x) { attributes[attributeCount++] = x; } #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); } // Arbitrary array size here @@ -675,9 +626,100 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window, return GL_FALSE; } + return GL_TRUE; +} + + +////////////////////////////////////////////////////////////////////////// +////// GLFW platform API ////// +////////////////////////////////////////////////////////////////////////// + +//======================================================================== +// Here is where the window is created, and the OpenGL rendering context is +// created +//======================================================================== + +int _glfwPlatformOpenWindow(_GLFWwindow* window, + const _GLFWwndconfig *wndconfig, + const _GLFWfbconfig *fbconfig) +{ + // We can only have one application delegate, but we only allocate it the + // first time we create a window to keep all window code in this file + if (_glfwLibrary.NS.delegate == nil) + { + _glfwLibrary.NS.delegate = [[GLFWApplicationDelegate alloc] init]; + if (_glfwLibrary.NS.delegate == nil) + { + _glfwSetError(GLFW_PLATFORM_ERROR, + "Cocoa/NSOpenGL: Failed to create application " + "delegate"); + return GL_FALSE; + } + + [NSApp setDelegate:_glfwLibrary.NS.delegate]; + } + + window->NS.delegate = [[GLFWWindowDelegate alloc] initWithGlfwWindow:window]; + if (window->NS.delegate == nil) + { + _glfwSetError(GLFW_PLATFORM_ERROR, + "Cocoa/NSOpenGL: Failed to create window delegate"); + return GL_FALSE; + } + + // Mac OS X needs non-zero color size, so set resonable values + int colorBits = fbconfig->redBits + fbconfig->greenBits + fbconfig->blueBits; + if (colorBits == 0) + colorBits = 24; + else if (colorBits < 15) + colorBits = 15; + + // Ignored hints: + // OpenGLMajor, OpenGLMinor, OpenGLForward: + // pending Mac OS X support for OpenGL 3.x + // OpenGLDebug + // pending it meaning anything on Mac OS X + + // Don't use accumulation buffer support; it's not accelerated + // Aux buffers probably aren't accelerated either + + CFDictionaryRef fullscreenMode = NULL; + if (wndconfig->mode == GLFW_FULLSCREEN) + { + // I think it's safe to pass 0 to the refresh rate for this function + // rather than conditionalizing the code to call the version which + // doesn't specify refresh... + fullscreenMode = + CGDisplayBestModeForParametersAndRefreshRateWithProperty( + CGMainDisplayID(), + colorBits + fbconfig->alphaBits, + window->width, window->height, + wndconfig->refreshRate, + // Controversial, see macosx_fullscreen.m for discussion + kCGDisplayModeIsSafeForHardware, + NULL); + + window->width = + [[(id)fullscreenMode objectForKey:(id)kCGDisplayWidth] intValue]; + window->height = + [[(id)fullscreenMode objectForKey:(id)kCGDisplayHeight] intValue]; + } + + if (!createWindow(window, wndconfig)) + return GL_FALSE; + + if (!createContext(window, wndconfig, fbconfig)) + return GL_FALSE; + [window->NS.window makeKeyAndOrderFront:nil]; [window->NSGL.context setView:[window->NS.window contentView]]; + if (wndconfig->mode == GLFW_FULLSCREEN) + { + CGCaptureAllDisplays(); + CGDisplaySwitchToMode(CGMainDisplayID(), fullscreenMode); + } + if (wndconfig->mode == GLFW_FULLSCREEN) { // TODO: Make this work on pre-Leopard systems