Added basic error reporting to shared code.

This commit is contained in:
Camilla Berglund 2010-09-09 21:34:42 +02:00
parent 2c091571d8
commit 922cd1011a
7 changed files with 222 additions and 16 deletions

View File

@ -184,7 +184,10 @@ static void disableKeyRepeat(_GLFWwindow* window)
GLFWAPI void glfwEnable(GLFWwindow window, int token)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
switch (token)
{
@ -216,7 +219,10 @@ GLFWAPI void glfwEnable(GLFWwindow window, int token)
GLFWAPI void glfwDisable(GLFWwindow window, int token)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
switch (token)
{

View File

@ -44,8 +44,17 @@ GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount)
int count, i, swap, res1, res2, depth1, depth2;
GLFWvidmode vm;
if (!_glfwInitialized || maxcount <= 0 || list == NULL)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (maxcount <= 0 || list == NULL)
{
// TODO: Figure out if this is an error
return 0;
}
// Get list of video modes
count = _glfwPlatformGetVideoModes(list, maxcount);
@ -83,8 +92,17 @@ GLFWAPI int glfwGetVideoModes(GLFWvidmode* list, int maxcount)
GLFWAPI void glfwGetDesktopMode(GLFWvidmode* mode)
{
if (!_glfwInitialized || mode == NULL)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (mode == NULL)
{
_glfwSetError(GLFW_INVALID_VALUE);
return;
}
_glfwPlatformGetDesktopMode(mode);
}

View File

@ -132,10 +132,19 @@ GLFWAPI int glfwExtensionSupported(const char* extension)
GLint count;
int i;
if (!_glfwInitialized || !_glfwLibrary.window)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return GL_FALSE;
}
_GLFWwindow* window = _glfwLibrary.window;
if (!_glfwLibrary.currentWindow)
{
_glfwSetError(GLFW_NO_CURRENT_WINDOW);
return GL_FALSE;
}
_GLFWwindow* window = _glfwLibrary.currentWindow;
// Extension names should not have spaces
where = (GLubyte*) strchr(extension, ' ');
@ -184,8 +193,17 @@ GLFWAPI int glfwExtensionSupported(const char* extension)
GLFWAPI void* glfwGetProcAddress(const char* procname)
{
if (!_glfwInitialized || !_glfwLibrary.window)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return NULL;
}
if (!_glfwLibrary.currentWindow)
{
_glfwSetError(GLFW_NO_CURRENT_WINDOW);
return NULL;
}
return _glfwPlatformGetProcAddress(procname);
}
@ -197,10 +215,19 @@ GLFWAPI void* glfwGetProcAddress(const char* procname)
GLFWAPI void glfwGetGLVersion(int* major, int* minor, int* rev)
{
if (!_glfwInitialized || !_glfwLibrary.window)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
_GLFWwindow* window = _glfwLibrary.window;
if (!_glfwLibrary.currentWindow)
{
_glfwSetError(GLFW_NO_CURRENT_WINDOW);
return;
}
_GLFWwindow* window = _glfwLibrary.currentWindow;
if (major != NULL)
*major = window->glMajor;

View File

@ -42,11 +42,18 @@
GLFWAPI int glfwGetKey(GLFWwindow window, int key)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return GLFW_RELEASE;
}
// Is it a valid key?
if (key < 0 || key > GLFW_KEY_LAST)
{
// TODO: Decide whether key is a value or enum
_glfwSetError(GLFW_INVALID_VALUE);
return GLFW_RELEASE;
}
if (window->key[key] == GLFW_STICK)
{
@ -66,11 +73,17 @@ GLFWAPI int glfwGetKey(GLFWwindow window, int key)
GLFWAPI int glfwGetMouseButton(GLFWwindow window, int button)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return GLFW_RELEASE;
}
// Is it a valid mouse button?
if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST)
{
_glfwSetError(GLFW_INVALID_ENUM);
return GLFW_RELEASE;
}
if (window->mouseButton[button] == GLFW_STICK)
{
@ -90,7 +103,10 @@ GLFWAPI int glfwGetMouseButton(GLFWwindow window, int button)
GLFWAPI void glfwGetMousePos(GLFWwindow window, int* xpos, int* ypos)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
// Return mouse position
if (xpos != NULL)
@ -109,7 +125,10 @@ GLFWAPI void glfwGetMousePos(GLFWwindow window, int* xpos, int* ypos)
GLFWAPI void glfwSetMousePos(GLFWwindow window, int xpos, int ypos)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
// Don't do anything if the mouse position did not change
if (xpos == window->mousePosX && ypos == window->mousePosY)
@ -120,7 +139,7 @@ GLFWAPI void glfwSetMousePos(GLFWwindow window, int xpos, int ypos)
window->mousePosY = ypos;
// If we have a locked mouse, do not change cursor position
if (_glfwLibrary.cursorLockWindow)
if (_glfwLibrary.cursorLockWindow == window)
return;
// Update physical cursor position
@ -135,7 +154,10 @@ GLFWAPI void glfwSetMousePos(GLFWwindow window, int xpos, int ypos)
GLFWAPI int glfwGetMouseWheel(GLFWwindow window)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return 0;
}
return window->wheelPos;
}
@ -148,7 +170,10 @@ GLFWAPI int glfwGetMouseWheel(GLFWwindow window)
GLFWAPI void glfwSetMouseWheel(GLFWwindow window, int pos)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->wheelPos = pos;
}
@ -161,7 +186,10 @@ GLFWAPI void glfwSetMouseWheel(GLFWwindow window, int pos)
GLFWAPI void glfwSetKeyCallback(GLFWwindow window, GLFWkeyfun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->keyCallback = cbfun;
}
@ -174,7 +202,10 @@ GLFWAPI void glfwSetKeyCallback(GLFWwindow window, GLFWkeyfun cbfun)
GLFWAPI void glfwSetCharCallback(GLFWwindow window, GLFWcharfun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->charCallback = cbfun;
}
@ -187,7 +218,10 @@ GLFWAPI void glfwSetCharCallback(GLFWwindow window, GLFWcharfun cbfun)
GLFWAPI void glfwSetMouseButtonCallback(GLFWwindow window, GLFWmousebuttonfun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->mouseButtonCallback = cbfun;
}
@ -199,11 +233,14 @@ GLFWAPI void glfwSetMouseButtonCallback(GLFWwindow window, GLFWmousebuttonfun cb
GLFWAPI void glfwSetMousePosCallback(GLFWwindow window, GLFWmouseposfun cbfun)
{
if (!_glfwInitialized || !_glfwLibrary.window)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
// Set callback function
_glfwLibrary.window->mousePosCallback = cbfun;
window->mousePosCallback = cbfun;
// Call the callback function to let the application know the current
// mouse position
@ -219,7 +256,10 @@ GLFWAPI void glfwSetMousePosCallback(GLFWwindow window, GLFWmouseposfun cbfun)
GLFWAPI void glfwSetMouseWheelCallback(GLFWwindow window, GLFWmousewheelfun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
// Set callback function
window->mouseWheelCallback = cbfun;

View File

@ -42,7 +42,10 @@
GLFWAPI int glfwGetJoystickParam(int joy, int param)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return 0;
}
return _glfwPlatformGetJoystickParam(joy, param);
}
@ -57,7 +60,10 @@ GLFWAPI int glfwGetJoystickPos(int joy, float* pos, int numaxes)
int i;
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return 0;
}
// Clear positions
for (i = 0; i < numaxes; i++)
@ -78,7 +84,10 @@ GLFWAPI int glfwGetJoystickButtons(int joy,
int i;
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return 0;
}
// Clear button states
for (i = 0; i < numbuttons; i++)

View File

@ -42,7 +42,10 @@
GLFWAPI double glfwGetTime(void)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return 0.0;
}
return _glfwPlatformGetTime();
}
@ -55,7 +58,10 @@ GLFWAPI double glfwGetTime(void)
GLFWAPI void glfwSetTime(double time)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
_glfwPlatformSetTime(time);
}

View File

@ -375,12 +375,25 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
_GLFWwndconfig wndconfig;
_GLFWwindow* window;
if (!_glfwInitialized || _glfwLibrary.window)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return NULL;
}
if (_glfwLibrary.window)
{
// TODO: Remove this once multi-window is completed
_glfwSetError(GLFW_INTERNAL_ERROR);
return NULL;
}
window = (_GLFWwindow*) malloc(sizeof(_GLFWwindow));
if (!window)
{
_glfwSetError(GLFW_OUT_OF_MEMORY);
return NULL;
}
_glfwLibrary.window = window;
@ -418,18 +431,21 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
{
// OpenGL 1.x series ended with version 1.5
glfwCloseWindow(window);
_glfwSetError(GLFW_INVALID_VALUE);
return GL_FALSE;
}
else if (wndconfig.glMajor == 2 && wndconfig.glMinor > 1)
{
// OpenGL 2.x series ended with version 2.1
glfwCloseWindow(window);
_glfwSetError(GLFW_INVALID_VALUE);
return GL_FALSE;
}
else if (wndconfig.glMajor == 3 && wndconfig.glMinor > 3)
{
// OpenGL 3.x series ended with version 3.3
glfwCloseWindow(window);
_glfwSetError(GLFW_INVALID_VALUE);
return GL_FALSE;
}
else
@ -442,6 +458,7 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
{
// Context profiles are only defined for OpenGL version 3.2 and above
glfwCloseWindow(window);
_glfwSetError(GLFW_INVALID_VALUE);
return GL_FALSE;
}
@ -449,6 +466,7 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
{
// Forward-compatible contexts are only defined for OpenGL version 3.0 and above
glfwCloseWindow(window);
_glfwSetError(GLFW_INVALID_VALUE);
return GL_FALSE;
}
@ -456,6 +474,7 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
{
// Invalid window mode
glfwCloseWindow(window);
_glfwSetError(GLFW_INVALID_ENUM);
return GL_FALSE;
}
@ -506,6 +525,7 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
// The desired OpenGL version is greater than the actual version
// This only happens if the machine lacks {GLX|WGL}_ARB_create_context
glfwCloseWindow(window);
_glfwSetError(GLFW_UNAVAILABLE_VERSION);
return GL_FALSE;
}
@ -515,6 +535,7 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
if (!window->GetStringi)
{
glfwCloseWindow(window);
_glfwSetError(GLFW_INTERNAL_ERROR);
return GL_FALSE;
}
}
@ -538,7 +559,13 @@ GLFWAPI GLFWwindow glfwOpenWindow(int width, int height, int mode)
GLFWAPI void glfwMakeWindowCurrent(GLFWwindow window)
{
if (!_glfwInitialized || _glfwLibrary.currentWindow == window)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (_glfwLibrary.currentWindow == window)
return;
_glfwPlatformMakeWindowCurrent(window);
@ -553,7 +580,10 @@ GLFWAPI void glfwMakeWindowCurrent(GLFWwindow window)
GLFWAPI int glfwIsWindow(GLFWwindow window)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return GL_FALSE;
}
if (window == NULL)
return GL_FALSE;
@ -569,7 +599,10 @@ GLFWAPI int glfwIsWindow(GLFWwindow window)
GLFWAPI void glfwOpenWindowHint(int target, int hint)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
switch (target)
{
@ -646,7 +679,10 @@ GLFWAPI void glfwOpenWindowHint(int target, int hint)
GLFWAPI void glfwCloseWindow(GLFWwindow window)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
// Show mouse pointer again (if hidden)
if (window == _glfwLibrary.cursorLockWindow)
@ -671,7 +707,10 @@ GLFWAPI void glfwCloseWindow(GLFWwindow window)
GLFWAPI void glfwSetWindowTitle(GLFWwindow window, const char* title)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
_glfwPlatformSetWindowTitle(window, title);
}
@ -684,7 +723,10 @@ GLFWAPI void glfwSetWindowTitle(GLFWwindow window, const char* title)
GLFWAPI void glfwGetWindowSize(GLFWwindow window, int* width, int* height)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (width != NULL)
*width = window->width;
@ -700,8 +742,17 @@ GLFWAPI void glfwGetWindowSize(GLFWwindow window, int* width, int* height)
GLFWAPI void glfwSetWindowSize(GLFWwindow window, int width, int height)
{
if (!_glfwInitialized || window->iconified)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (window->iconified)
{
// TODO: Figure out if this is an error
return;
}
// Don't do anything if the window size did not change
if (width == window->width && height == window->height)
@ -724,8 +775,15 @@ GLFWAPI void glfwSetWindowSize(GLFWwindow window, int width, int height)
GLFWAPI void glfwSetWindowPos(GLFWwindow window, int x, int y)
{
if (!_glfwInitialized || window->mode == GLFW_FULLSCREEN || window->iconified)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (window->mode == GLFW_FULLSCREEN || window->iconified)
{
// TODO: Figure out if this is an error
return;
}
@ -739,7 +797,13 @@ GLFWAPI void glfwSetWindowPos(GLFWwindow window, int x, int y)
GLFWAPI void glfwIconifyWindow(GLFWwindow window)
{
if (!_glfwInitialized || window->iconified)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (window->iconified)
return;
_glfwPlatformIconifyWindow(window);
@ -752,7 +816,13 @@ GLFWAPI void glfwIconifyWindow(GLFWwindow window)
GLFWAPI void glfwRestoreWindow(GLFWwindow window)
{
if (!_glfwInitialized || !window->iconified)
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (!window->iconified)
return;
// Restore iconified window
@ -770,7 +840,16 @@ GLFWAPI void glfwRestoreWindow(GLFWwindow window)
GLFWAPI void glfwSwapBuffers(void)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
if (!_glfwLibrary.currentWindow)
{
_glfwSetError(GLFW_NO_CURRENT_WINDOW);
return;
}
if (_glfwLibrary.currentWindow)
_glfwPlatformSwapBuffers();
@ -784,7 +863,10 @@ GLFWAPI void glfwSwapBuffers(void)
GLFWAPI void glfwSwapInterval(int interval)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
_glfwPlatformSwapInterval(interval);
}
@ -797,7 +879,10 @@ GLFWAPI void glfwSwapInterval(int interval)
GLFWAPI int glfwGetWindowParam(GLFWwindow window, int param)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return 0;
}
switch (param)
{
@ -860,7 +945,10 @@ GLFWAPI int glfwGetWindowParam(GLFWwindow window, int param)
GLFWAPI void glfwSetWindowSizeCallback(GLFWwindow window, GLFWwindowsizefun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->windowSizeCallback = cbfun;
@ -877,7 +965,10 @@ GLFWAPI void glfwSetWindowSizeCallback(GLFWwindow window, GLFWwindowsizefun cbfu
GLFWAPI void glfwSetWindowCloseCallback(GLFWwindow window, GLFWwindowclosefun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->windowCloseCallback = cbfun;
}
@ -890,7 +981,10 @@ GLFWAPI void glfwSetWindowCloseCallback(GLFWwindow window, GLFWwindowclosefun cb
GLFWAPI void glfwSetWindowRefreshCallback(GLFWwindow window, GLFWwindowrefreshfun cbfun)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
window->windowRefreshCallback = cbfun;
}
@ -903,7 +997,10 @@ GLFWAPI void glfwSetWindowRefreshCallback(GLFWwindow window, GLFWwindowrefreshfu
GLFWAPI void glfwPollEvents(void)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
_glfwPlatformPollEvents();
}
@ -916,7 +1013,10 @@ GLFWAPI void glfwPollEvents(void)
GLFWAPI void glfwWaitEvents(void)
{
if (!_glfwInitialized)
{
_glfwSetError(GLFW_NOT_INITIALIZED);
return;
}
_glfwPlatformWaitEvents();
}