Merge branch 'master' of github.com:elmindreda/glfw

This commit is contained in:
Camilla Berglund 2012-02-01 14:27:43 +01:00
commit 05173690d0
2 changed files with 174 additions and 121 deletions

View File

@ -232,11 +232,13 @@ static long getElementValue(_glfwJoystick* joystick, _glfwJoystickElement* eleme
static void removeJoystick(_glfwJoystick* joystick) static void removeJoystick(_glfwJoystick* joystick)
{ {
int i;
if (joystick->present) if (joystick->present)
{ {
joystick->present = GL_FALSE; joystick->present = GL_FALSE;
for (int i = 0; i < joystick->numAxes; i++) for (i = 0; i < joystick->numAxes; i++)
{ {
_glfwJoystickElement* axes = _glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i); (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i);
@ -245,7 +247,7 @@ static void removeJoystick(_glfwJoystick* joystick)
CFArrayRemoveAllValues(joystick->axes); CFArrayRemoveAllValues(joystick->axes);
joystick->numAxes = 0; joystick->numAxes = 0;
for (int i = 0; i < joystick->numButtons; i++) for (i = 0; i < joystick->numButtons; i++)
{ {
_glfwJoystickElement* button = _glfwJoystickElement* button =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i); (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i);
@ -254,7 +256,7 @@ static void removeJoystick(_glfwJoystick* joystick)
CFArrayRemoveAllValues(joystick->buttons); CFArrayRemoveAllValues(joystick->buttons);
joystick->numButtons = 0; joystick->numButtons = 0;
for (int i = 0; i < joystick->numHats; i++) for (i = 0; i < joystick->numHats; i++)
{ {
_glfwJoystickElement* hat = _glfwJoystickElement* hat =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, i); (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->hats, i);
@ -287,23 +289,26 @@ static void removalCallback(void* target, IOReturn result, void* refcon, void* s
static void pollJoystickEvents(void) 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]; _glfwJoystick* joystick = &_glfwJoysticks[i];
if (joystick->present) if (joystick->present)
{ {
for (CFIndex i = 0; i < joystick->numButtons; i++) for (j = 0; j < joystick->numButtons; j++)
{ {
_glfwJoystickElement* button = _glfwJoystickElement* button =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, i); (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->buttons, j);
button->value = getElementValue(joystick, button); button->value = getElementValue(joystick, button);
} }
for (CFIndex i = 0; i < joystick->numAxes; i++) for (j = 0; j < joystick->numAxes; j++)
{ {
_glfwJoystickElement* axes = _glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, i); (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick->axes, j);
axes->value = getElementValue(joystick, axes); axes->value = getElementValue(joystick, axes);
} }
} }
@ -449,7 +454,9 @@ void _glfwInitJoysticks(void)
void _glfwTerminateJoysticks(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]; _glfwJoystick* joystick = &_glfwJoysticks[i];
removeJoystick(joystick); removeJoystick(joystick);
@ -498,6 +505,8 @@ int _glfwPlatformGetJoystickParam(int joy, int param)
int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes) int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes)
{ {
int i;
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST) if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0; return 0;
@ -514,7 +523,7 @@ int _glfwPlatformGetJoystickPos(int joy, float* pos, int numaxes)
// Update joystick state // Update joystick state
pollJoystickEvents(); pollJoystickEvents();
for (int i = 0; i < numaxes; i++) for (i = 0; i < numaxes; i++)
{ {
_glfwJoystickElement* axes = _glfwJoystickElement* axes =
(_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.axes, i); (_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 _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
int numbuttons) int numbuttons)
{ {
int i;
if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST) if (joy < GLFW_JOYSTICK_1 || joy > GLFW_JOYSTICK_LAST)
return 0; return 0;
@ -559,7 +570,7 @@ int _glfwPlatformGetJoystickButtons(int joy, unsigned char* buttons,
// Update joystick state // Update joystick state
pollJoystickEvents(); pollJoystickEvents();
for (int i = 0; i < numbuttons; i++) for (i = 0; i < numbuttons; i++)
{ {
_glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.buttons, i); _glfwJoystickElement* button = (_glfwJoystickElement*) CFArrayGetValueAtIndex(joystick.buttons, i);
buttons[i] = button->value ? GLFW_PRESS : GLFW_RELEASE; buttons[i] = button->value ? GLFW_PRESS : GLFW_RELEASE;

View File

@ -147,7 +147,7 @@ static const unsigned int MAC_TO_GLFW_KEYCODE_MAPPING[128] =
/* 07 */ GLFW_KEY_X, /* 07 */ GLFW_KEY_X,
/* 08 */ GLFW_KEY_C, /* 08 */ GLFW_KEY_C,
/* 09 */ GLFW_KEY_V, /* 09 */ GLFW_KEY_V,
/* 0a */ -1, /* 0a */ GLFW_KEY_GRAVE_ACCENT,
/* 0b */ GLFW_KEY_B, /* 0b */ GLFW_KEY_B,
/* 0c */ GLFW_KEY_Q, /* 0c */ GLFW_KEY_Q,
/* 0d */ GLFW_KEY_W, /* 0d */ GLFW_KEY_W,
@ -187,7 +187,7 @@ static const unsigned int MAC_TO_GLFW_KEYCODE_MAPPING[128] =
/* 2f */ GLFW_KEY_PERIOD, /* 2f */ GLFW_KEY_PERIOD,
/* 30 */ GLFW_KEY_TAB, /* 30 */ GLFW_KEY_TAB,
/* 31 */ GLFW_KEY_SPACE, /* 31 */ GLFW_KEY_SPACE,
/* 32 */ GLFW_KEY_GRAVE_ACCENT, /* 32 */ GLFW_KEY_WORLD_1,
/* 33 */ GLFW_KEY_BACKSPACE, /* 33 */ GLFW_KEY_BACKSPACE,
/* 34 */ -1, /* 34 */ -1,
/* 35 */ GLFW_KEY_ESCAPE, /* 35 */ GLFW_KEY_ESCAPE,
@ -443,20 +443,68 @@ static int convertMacKeyCode(unsigned int macKeyCode)
@end @end
//////////////////////////////////////////////////////////////////////////
////// GLFW platform API //////
//////////////////////////////////////////////////////////////////////////
//======================================================================== //========================================================================
// Here is where the window is created, and the OpenGL rendering context is // Create the Cocoa window
// created
//======================================================================== //========================================================================
int _glfwPlatformOpenWindow(_GLFWwindow* window, static GLboolean createWindow(_GLFWwindow* window,
const _GLFWwndconfig *wndconfig, const _GLFWwndconfig* wndconfig)
const _GLFWfbconfig *fbconfig)
{ {
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 #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
// Fail if any OpenGL version above 2.1 other than 3.2 was requested // Fail if any OpenGL version above 2.1 other than 3.2 was requested
if (wndconfig->glMajor > 3 || if (wndconfig->glMajor > 3 ||
@ -499,104 +547,7 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
return GL_FALSE; return GL_FALSE;
} }
// We can only have one application delegate, but we only allocate it the #define ADD_ATTR(x) { attributes[attributeCount++] = x; }
// 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_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); } #define ADD_ATTR2(x, y) { ADD_ATTR(x); ADD_ATTR(y); }
// Arbitrary array size here // Arbitrary array size here
@ -675,9 +626,100 @@ int _glfwPlatformOpenWindow(_GLFWwindow* window,
return GL_FALSE; 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->NS.window makeKeyAndOrderFront:nil];
[window->NSGL.context setView:[window->NS.window contentView]]; [window->NSGL.context setView:[window->NS.window contentView]];
if (wndconfig->mode == GLFW_FULLSCREEN)
{
CGCaptureAllDisplays();
CGDisplaySwitchToMode(CGMainDisplayID(), fullscreenMode);
}
if (wndconfig->mode == GLFW_FULLSCREEN) if (wndconfig->mode == GLFW_FULLSCREEN)
{ {
// TODO: Make this work on pre-Leopard systems // TODO: Make this work on pre-Leopard systems