From 129e94da2e58b678d5440f803d6679d086bb092b Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Thu, 4 Apr 2013 16:16:21 +0200 Subject: [PATCH] Changed cursor pos to double. --- README.md | 1 + examples/splitview.c | 16 ++++++++-------- examples/wave.c | 2 +- include/GL/glfw3.h | 6 +++--- src/cocoa_window.m | 6 +----- src/input.c | 15 ++++++++------- src/internal.h | 6 +++--- src/win32_window.c | 4 ++-- src/x11_window.c | 5 +++-- tests/accuracy.c | 4 ++-- tests/events.c | 4 ++-- tests/peter.c | 10 +++++----- 12 files changed, 39 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 76b650c2..f95cbb2d 100644 --- a/README.md +++ b/README.md @@ -308,6 +308,7 @@ GLFW. * Changed `glfwGetProcAddress` to return a (generic) function pointer * Changed `glfwGetVideoModes` to return a dynamic, unlimited number of video modes for the specified monitor + * Changed cursor position to double-precision floating-point * Renamed `glfw.h` to `glfw3.h` to avoid conflicts with 2.x series * Renamed `glfwOpenWindowHint` to `glfwWindowHint` * Renamed `GLFW_ACTIVE` to `GLFW_FOCUSED` diff --git a/examples/splitview.c b/examples/splitview.c index 6d2a17fb..ea2c7eac 100644 --- a/examples/splitview.c +++ b/examples/splitview.c @@ -27,7 +27,7 @@ //======================================================================== // Mouse position -static int xpos = 0, ypos = 0; +static double xpos = 0, ypos = 0; // Window size static int width, height; @@ -379,24 +379,24 @@ static void windowRefreshFun(GLFWwindow* window) // Mouse position callback function //======================================================================== -static void cursorPosFun(GLFWwindow* window, int x, int y) +static void cursorPosFun(GLFWwindow* window, double x, double y) { // Depending on which view was selected, rotate around different axes switch (active_view) { case 1: - rot_x += y - ypos; - rot_z += x - xpos; + rot_x += (int) y - ypos; + rot_z += (int) x - xpos; do_redraw = 1; break; case 3: - rot_x += y - ypos; - rot_y += x - xpos; + rot_x += (int) y - ypos; + rot_y += (int) x - xpos; do_redraw = 1; break; case 4: - rot_y += x - xpos; - rot_z += y - ypos; + rot_y += (int) x - xpos; + rot_z += (int) y - ypos; do_redraw = 1; break; default: diff --git a/examples/wave.c b/examples/wave.c index 067e99a0..fc1ca0db 100644 --- a/examples/wave.c +++ b/examples/wave.c @@ -335,7 +335,7 @@ void mouse_button_callback(GLFWwindow* window, int button, int action) // Callback function for cursor motion events //======================================================================== -void cursor_position_callback(GLFWwindow* window, int x, int y) +void cursor_position_callback(GLFWwindow* window, double x, double y) { if (locked) { diff --git a/include/GL/glfw3.h b/include/GL/glfw3.h index c1653e48..df84486c 100644 --- a/include/GL/glfw3.h +++ b/include/GL/glfw3.h @@ -641,7 +641,7 @@ typedef void (* GLFWmousebuttonfun)(GLFWwindow*,int,int); * * @sa glfwSetCursorPosCallback */ -typedef void (* GLFWcursorposfun)(GLFWwindow*,int,int); +typedef void (* GLFWcursorposfun)(GLFWwindow*,double,double); /*! @brief The function signature for cursor enter/exit callbacks. * @param[in] window The window that received the event. @@ -1730,7 +1730,7 @@ GLFWAPI int glfwGetMouseButton(GLFWwindow* window, int button); * * @ingroup input */ -GLFWAPI void glfwGetCursorPos(GLFWwindow* window, int* xpos, int* ypos); +GLFWAPI void glfwGetCursorPos(GLFWwindow* window, double* xpos, double* ypos); /*! @brief Sets the position of the cursor, relative to the client area of the window. * @@ -1748,7 +1748,7 @@ GLFWAPI void glfwGetCursorPos(GLFWwindow* window, int* xpos, int* ypos); * * @ingroup input */ -GLFWAPI void glfwSetCursorPos(GLFWwindow* window, int xpos, int ypos); +GLFWAPI void glfwSetCursorPos(GLFWwindow* window, double xpos, double ypos); /*! @brief Sets the key callback. * diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 9edbb9e5..5205579f 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -365,11 +365,7 @@ static int convertMacKeyCode(unsigned int macKeyCode) [window->ns.object contentRectForFrameRect:[window->ns.object frame]]; const NSPoint p = [event locationInWindow]; - // Cocoa coordinate system has origin at lower left - const int x = lround(floor(p.x)); - const int y = contentRect.size.height - lround(ceil(p.y)); - - _glfwInputCursorMotion(window, x, y); + _glfwInputCursorMotion(window, p.x, contentRect.size.height - p.y); } } diff --git a/src/input.c b/src/input.c index 0d38da4f..962d5294 100644 --- a/src/input.c +++ b/src/input.c @@ -38,7 +38,8 @@ // static void setCursorMode(_GLFWwindow* window, int newMode) { - int width, height, oldMode, centerPosX, centerPosY; + int width, height, oldMode; + double centerPosX, centerPosY; if (newMode != GLFW_CURSOR_NORMAL && newMode != GLFW_CURSOR_HIDDEN && @@ -54,8 +55,8 @@ static void setCursorMode(_GLFWwindow* window, int newMode) _glfwPlatformGetWindowSize(window, &width, &height); - centerPosX = width / 2; - centerPosY = height / 2; + centerPosX = width / 2.0; + centerPosY = height / 2.0; if (oldMode == GLFW_CURSOR_CAPTURED || newMode == GLFW_CURSOR_CAPTURED) _glfwPlatformSetCursorPos(window, centerPosX, centerPosY); @@ -171,11 +172,11 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action) window->callbacks.mouseButton((GLFWwindow*) window, button, action); } -void _glfwInputCursorMotion(_GLFWwindow* window, int x, int y) +void _glfwInputCursorMotion(_GLFWwindow* window, double x, double y) { if (window->cursorMode == GLFW_CURSOR_CAPTURED) { - if (!x && !y) + if (x == 0.0 && y == 0.0) return; window->cursorPosX += x; @@ -297,7 +298,7 @@ GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button) return (int) window->mouseButton[button]; } -GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, int* xpos, int* ypos) +GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos) { _GLFWwindow* window = (_GLFWwindow*) handle; @@ -310,7 +311,7 @@ GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, int* xpos, int* ypos) *ypos = window->cursorPosY; } -GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, int xpos, int ypos) +GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos) { _GLFWwindow* window = (_GLFWwindow*) handle; diff --git a/src/internal.h b/src/internal.h index 9c49a5f0..c7b45afb 100644 --- a/src/internal.h +++ b/src/internal.h @@ -225,7 +225,7 @@ struct _GLFWwindow // Window input state GLboolean stickyKeys; GLboolean stickyMouseButtons; - int cursorPosX, cursorPosY; + double cursorPosX, cursorPosY; int cursorMode; char mouseButton[GLFW_MOUSE_BUTTON_LAST + 1]; char key[GLFW_KEY_LAST + 1]; @@ -344,7 +344,7 @@ const char* _glfwPlatformGetVersionString(void); /*! @copydoc glfwSetCursorPos * @ingroup platform */ -void _glfwPlatformSetCursorPos(_GLFWwindow* window, int xpos, int ypos); +void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos); /*! @brief Sets up the specified cursor mode for the specified window. * @param[in] window The window whose cursor mode to change. @@ -614,7 +614,7 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action); * of the client area of the window. * @ingroup event */ -void _glfwInputCursorMotion(_GLFWwindow* window, int x, int y); +void _glfwInputCursorMotion(_GLFWwindow* window, double x, double y); /*! @brief Notifies shared code of a cursor enter/leave event. * @param[in] window The window that received the event. diff --git a/src/win32_window.c b/src/win32_window.c index 772e253d..fb1b9a2e 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1049,9 +1049,9 @@ void _glfwPlatformWaitEvents(void) _glfwPlatformPollEvents(); } -void _glfwPlatformSetCursorPos(_GLFWwindow* window, int xpos, int ypos) +void _glfwPlatformSetCursorPos(_GLFWwindow* window, double xpos, double ypos) { - POINT pos = { xpos, ypos }; + POINT pos = { (int) xpos, (int) ypos }; ClientToScreen(window->win32.handle, &pos); SetCursorPos(pos.x, pos.y); } diff --git a/src/x11_window.c b/src/x11_window.c index f58cd66f..74fa3093 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -1004,13 +1004,14 @@ void _glfwPlatformWaitEvents(void) _glfwPlatformPollEvents(); } -void _glfwPlatformSetCursorPos(_GLFWwindow* window, int x, int y) +void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) { // Store the new position so it can be recognized later window->x11.cursorPosX = x; window->x11.cursorPosY = y; - XWarpPointer(_glfw.x11.display, None, window->x11.handle, 0,0,0,0, x, y); + XWarpPointer(_glfw.x11.display, None, window->x11.handle, + 0,0,0,0, (int) x, (int) y); } void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) diff --git a/tests/accuracy.c b/tests/accuracy.c index 755d5dd6..caefb474 100644 --- a/tests/accuracy.c +++ b/tests/accuracy.c @@ -35,7 +35,7 @@ #include #include -static int cursor_x = 0, cursor_y = 0; +static double cursor_x = 0.0, cursor_y = 0.0; static int window_width = 640, window_height = 480; static int swap_interval = 1; @@ -68,7 +68,7 @@ static void window_size_callback(GLFWwindow* window, int width, int height) gluOrtho2D(0.f, window_width, 0.f, window_height); } -static void cursor_position_callback(GLFWwindow* window, int x, int y) +static void cursor_position_callback(GLFWwindow* window, double x, double y) { cursor_x = x; cursor_y = y; diff --git a/tests/events.c b/tests/events.c index 96756f88..25c90908 100644 --- a/tests/events.c +++ b/tests/events.c @@ -290,9 +290,9 @@ static void mouse_button_callback(GLFWwindow* window, int button, int action) printf(" was %s\n", get_action_name(action)); } -static void cursor_position_callback(GLFWwindow* window, int x, int y) +static void cursor_position_callback(GLFWwindow* window, double x, double y) { - printf("%08x at %0.3f: Cursor position: %i %i\n", counter++, glfwGetTime(), x, y); + printf("%08x at %0.3f: Cursor position: %f %f\n", counter++, glfwGetTime(), x, y); } static void cursor_enter_callback(GLFWwindow* window, int entered) diff --git a/tests/peter.c b/tests/peter.c index 110cdb8c..4bee8218 100644 --- a/tests/peter.c +++ b/tests/peter.c @@ -36,8 +36,8 @@ #include static GLboolean reopen = GL_FALSE; -static int cursor_x; -static int cursor_y; +static double cursor_x; +static double cursor_y; static void toggle_cursor(GLFWwindow* window) { @@ -58,9 +58,9 @@ static void error_callback(int error, const char* description) fprintf(stderr, "Error: %s\n", description); } -static void cursor_position_callback(GLFWwindow* window, int x, int y) +static void cursor_position_callback(GLFWwindow* window, double x, double y) { - printf("Cursor moved to: %i %i (%i %i)\n", x, y, x - cursor_x, y - cursor_y); + printf("Cursor moved to: %f %f (%f %f)\n", x, y, x - cursor_x, y - cursor_y); cursor_x = x; cursor_y = y; } @@ -102,7 +102,7 @@ static GLFWwindow* open_window(void) glfwSwapInterval(1); glfwGetCursorPos(window, &cursor_x, &cursor_y); - printf("Cursor position: %i %i\n", cursor_x, cursor_y); + printf("Cursor position: %f %f\n", cursor_x, cursor_y); glfwSetWindowSizeCallback(window, window_size_callback); glfwSetCursorPosCallback(window, cursor_position_callback);