From f7282e86c0e8f2924f47203372fa4a6169872b61 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Tue, 30 Jul 2013 14:43:01 +0200 Subject: [PATCH] Added pointer swap macro for callback setters. --- src/init.c | 5 ++--- src/input.c | 48 +++++++++++-------------------------------- src/internal.h | 9 ++++++++ src/monitor.c | 7 ++----- src/window.c | 56 +++++++++++++------------------------------------- 5 files changed, 39 insertions(+), 86 deletions(-) diff --git a/src/init.c b/src/init.c index 588364c9..064bef8c 100644 --- a/src/init.c +++ b/src/init.c @@ -192,8 +192,7 @@ GLFWAPI const char* glfwGetVersionString(void) GLFWAPI GLFWerrorfun glfwSetErrorCallback(GLFWerrorfun cbfun) { - GLFWerrorfun previous = _glfwErrorCallback; - _glfwErrorCallback = cbfun; - return previous; + _GLFW_SWAP_POINTERS(_glfwErrorCallback, cbfun); + return cbfun; } diff --git a/src/input.c b/src/input.c index 85982419..0f728bea 100644 --- a/src/input.c +++ b/src/input.c @@ -349,76 +349,52 @@ GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos) GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* handle, GLFWkeyfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWkeyfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.key; - window->callbacks.key = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.key, cbfun); + return cbfun; } GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* handle, GLFWcharfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWcharfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.character; - window->callbacks.character = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.character, cbfun); + return cbfun; } GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle, GLFWmousebuttonfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWmousebuttonfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.mouseButton; - window->callbacks.mouseButton = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.mouseButton, cbfun); + return cbfun; } GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle, GLFWcursorposfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWcursorposfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.cursorPos; - window->callbacks.cursorPos = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.cursorPos, cbfun); + return cbfun; } GLFWAPI GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow* handle, GLFWcursorenterfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWcursorenterfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.cursorEnter; - window->callbacks.cursorEnter = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.cursorEnter, cbfun); + return cbfun; } GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow* handle, GLFWscrollfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWscrollfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.scroll; - window->callbacks.scroll = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.scroll, cbfun); + return cbfun; } diff --git a/src/internal.h b/src/internal.h index 677b8c67..3e2b30a4 100644 --- a/src/internal.h +++ b/src/internal.h @@ -120,6 +120,15 @@ typedef struct _GLFWmonitor _GLFWmonitor; return x; \ } +// Swaps the provided pointers +#define _GLFW_SWAP_POINTERS(x, y) \ + { \ + void* t; \ + t = x; \ + x = y; \ + y = t; \ + } + //======================================================================== // Internal types diff --git a/src/monitor.c b/src/monitor.c index 3c143564..629e6a85 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -328,12 +328,9 @@ GLFWAPI const char* glfwGetMonitorName(GLFWmonitor* handle) GLFWAPI GLFWmonitorfun glfwSetMonitorCallback(GLFWmonitorfun cbfun) { - GLFWmonitorfun previous; _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = _glfw.monitorCallback; - _glfw.monitorCallback = cbfun; - return previous; + _GLFW_SWAP_POINTERS(_glfw.monitorCallback, cbfun); + return cbfun; } GLFWAPI const GLFWvidmode* glfwGetVideoModes(GLFWmonitor* handle, int* count) diff --git a/src/window.c b/src/window.c index cb9064ce..e1784925 100644 --- a/src/window.c +++ b/src/window.c @@ -604,91 +604,63 @@ GLFWAPI GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* handle, GLFWwindowposfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWwindowposfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.pos; - window->callbacks.pos = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.pos, cbfun); + return cbfun; } GLFWAPI GLFWwindowsizefun glfwSetWindowSizeCallback(GLFWwindow* handle, GLFWwindowsizefun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWwindowsizefun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.size; - window->callbacks.size = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.size, cbfun); + return cbfun; } GLFWAPI GLFWwindowclosefun glfwSetWindowCloseCallback(GLFWwindow* handle, GLFWwindowclosefun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWwindowclosefun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.close; - window->callbacks.close = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.close, cbfun); + return cbfun; } GLFWAPI GLFWwindowrefreshfun glfwSetWindowRefreshCallback(GLFWwindow* handle, GLFWwindowrefreshfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWwindowrefreshfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.refresh; - window->callbacks.refresh = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.refresh, cbfun); + return cbfun; } GLFWAPI GLFWwindowfocusfun glfwSetWindowFocusCallback(GLFWwindow* handle, GLFWwindowfocusfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWwindowfocusfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.focus; - window->callbacks.focus = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.focus, cbfun); + return cbfun; } GLFWAPI GLFWwindowiconifyfun glfwSetWindowIconifyCallback(GLFWwindow* handle, GLFWwindowiconifyfun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWwindowiconifyfun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.iconify; - window->callbacks.iconify = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.iconify, cbfun); + return cbfun; } GLFWAPI GLFWframebuffersizefun glfwSetFramebufferSizeCallback(GLFWwindow* handle, GLFWframebuffersizefun cbfun) { _GLFWwindow* window = (_GLFWwindow*) handle; - GLFWframebuffersizefun previous; - _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - - previous = window->callbacks.fbsize; - window->callbacks.fbsize = cbfun; - return previous; + _GLFW_SWAP_POINTERS(window->callbacks.fbsize, cbfun); + return cbfun; } GLFWAPI void glfwPollEvents(void)