Mouse coordinates are floating point

You can get sub-pixel mouse coordinates and motion depending on the platform and display scaling.

Fixes https://github.com/libsdl-org/SDL/issues/2999
main
Sam Lantinga 2022-12-29 19:31:12 -08:00
parent 8c3239dee5
commit cefbeb582f
52 changed files with 564 additions and 654 deletions

View File

@ -82,6 +82,8 @@ The timestamp_us member of the sensor events has been renamed sensor_timestamp a
You should set the event.common.timestamp field before passing an event to SDL_PushEvent(). If the timestamp is 0 it will be filled in with SDL_GetTicksNS().
Mouse events use floating point values for mouse coordinates and relative motion values. You can get sub-pixel motion depending on the platform and display scaling.
The SDL_DISPLAYEVENT_* events have been moved to top level events, and SDL_DISPLAYEVENT has been removed. In general, handling this change just means checking for the individual events instead of first checking for SDL_DISPLAYEVENT and then checking for display events. You can compare the event >= SDL_DISPLAYEVENT_FIRST and <= SDL_DISPLAYEVENT_LAST if you need to see whether it's a display event.
The SDL_WINDOWEVENT_* events have been moved to top level events, and SDL_WINDOWEVENT has been removed. In general, handling this change just means checking for the individual events instead of first checking for SDL_WINDOWEVENT and then checking for window events. You can compare the event >= SDL_WINDOWEVENT_FIRST and <= SDL_WINDOWEVENT_LAST if you need to see whether it's a window event.
@ -411,6 +413,8 @@ used by additional platforms that didn't have a SDL_RunApp-like function before)
SDL_ShowCursor() has been split into three functions: SDL_ShowCursor(), SDL_HideCursor(), and SDL_CursorVisible()
SDL_GetMouseState(), SDL_GetGlobalMouseState(), SDL_GetRelativeMouseState(), SDL_WarpMouseInWindow(), and SDL_WarpMouseGlobal() all use floating point mouse positions, to provide sub-pixel precision on platforms that support it.
The following functions have been renamed:
* SDL_FreeCursor() => SDL_DestroyCursor()
@ -463,6 +467,8 @@ which index is the "opengl" or whatnot driver, you can just pass that string dir
here, now. Passing NULL is the same as passing -1 here in SDL2, to signify you want SDL
to decide for you.
SDL_RenderWindowToLogical() and SDL_RenderLogicalToWindow() take floating point coordinates in both directions.
The following functions have been renamed:
* SDL_RenderCopy() => SDL_RenderTexture()
* SDL_RenderCopyEx() => SDL_RenderTextureRotated()

View File

@ -297,12 +297,12 @@ typedef struct SDL_MouseMotionEvent
Uint32 type; /**< ::SDL_MOUSEMOTION */
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_MouseID which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
SDL_MouseID which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
Uint32 state; /**< The current button state */
Sint32 x; /**< X coordinate, relative to window */
Sint32 y; /**< Y coordinate, relative to window */
Sint32 xrel; /**< The relative motion in the X direction */
Sint32 yrel; /**< The relative motion in the Y direction */
float x; /**< X coordinate, relative to window */
float y; /**< Y coordinate, relative to window */
float xrel; /**< The relative motion in the X direction */
float yrel; /**< The relative motion in the Y direction */
} SDL_MouseMotionEvent;
/**
@ -313,13 +313,13 @@ typedef struct SDL_MouseButtonEvent
Uint32 type; /**< ::SDL_MOUSEBUTTONDOWN or ::SDL_MOUSEBUTTONUP */
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_MouseID which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
SDL_MouseID which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
Uint8 button; /**< The mouse button index */
Uint8 state; /**< ::SDL_PRESSED or ::SDL_RELEASED */
Uint8 clicks; /**< 1 for single-click, 2 for double-click, etc. */
Uint8 padding1;
Sint32 x; /**< X coordinate, relative to window */
Sint32 y; /**< Y coordinate, relative to window */
Uint8 padding;
float x; /**< X coordinate, relative to window */
float y; /**< Y coordinate, relative to window */
} SDL_MouseButtonEvent;
/**
@ -330,14 +330,12 @@ typedef struct SDL_MouseWheelEvent
Uint32 type; /**< ::SDL_MOUSEWHEEL */
Uint64 timestamp; /**< In nanoseconds, populated using SDL_GetTicksNS() */
Uint32 windowID; /**< The window with mouse focus, if any */
SDL_MouseID which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
Sint32 x; /**< The amount scrolled horizontally, positive to the right and negative to the left */
Sint32 y; /**< The amount scrolled vertically, positive away from the user and negative toward the user */
SDL_MouseID which; /**< The mouse instance id, or SDL_TOUCH_MOUSEID */
float x; /**< The amount scrolled horizontally, positive to the right and negative to the left */
float y; /**< The amount scrolled vertically, positive away from the user and negative toward the user */
Uint32 direction; /**< Set to one of the SDL_MOUSEWHEEL_* defines. When FLIPPED the values in X and Y will be opposite. Multiply by -1 to change them back */
float preciseX; /**< The amount scrolled horizontally, positive to the right and negative to the left, with float precision (added in 2.0.18) */
float preciseY; /**< The amount scrolled vertically, positive away from the user and negative toward the user, with float precision (added in 2.0.18) */
Sint32 mouseX; /**< X coordinate, relative to window (added in 2.26.0) */
Sint32 mouseY; /**< Y coordinate, relative to window (added in 2.26.0) */
float mouseX; /**< X coordinate, relative to window */
float mouseY; /**< Y coordinate, relative to window */
} SDL_MouseWheelEvent;
/**

View File

@ -103,7 +103,7 @@ extern DECLSPEC SDL_Window * SDLCALL SDL_GetMouseFocus(void);
* \sa SDL_GetRelativeMouseState
* \sa SDL_PumpEvents
*/
extern DECLSPEC Uint32 SDLCALL SDL_GetMouseState(int *x, int *y);
extern DECLSPEC Uint32 SDLCALL SDL_GetMouseState(float *x, float *y);
/**
* Get the current state of the mouse in relation to the desktop.
@ -132,7 +132,7 @@ extern DECLSPEC Uint32 SDLCALL SDL_GetMouseState(int *x, int *y);
*
* \sa SDL_CaptureMouse
*/
extern DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(int *x, int *y);
extern DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(float *x, float *y);
/**
* Retrieve the relative state of the mouse.
@ -151,7 +151,7 @@ extern DECLSPEC Uint32 SDLCALL SDL_GetGlobalMouseState(int *x, int *y);
*
* \sa SDL_GetMouseState
*/
extern DECLSPEC Uint32 SDLCALL SDL_GetRelativeMouseState(int *x, int *y);
extern DECLSPEC Uint32 SDLCALL SDL_GetRelativeMouseState(float *x, float *y);
/**
* Move the mouse cursor to the given position within the window.
@ -173,7 +173,7 @@ extern DECLSPEC Uint32 SDLCALL SDL_GetRelativeMouseState(int *x, int *y);
* \sa SDL_WarpMouseGlobal
*/
extern DECLSPEC void SDLCALL SDL_WarpMouseInWindow(SDL_Window * window,
int x, int y);
float x, float y);
/**
* Move the mouse to the given position in global screen space.
@ -195,7 +195,7 @@ extern DECLSPEC void SDLCALL SDL_WarpMouseInWindow(SDL_Window * window,
*
* \sa SDL_WarpMouseInWindow
*/
extern DECLSPEC int SDLCALL SDL_WarpMouseGlobal(int x, int y);
extern DECLSPEC int SDLCALL SDL_WarpMouseGlobal(float x, float y);
/**
* Set relative mouse mode.

View File

@ -1034,10 +1034,10 @@ extern DECLSPEC void SDLCALL SDL_GetRenderScale(SDL_Renderer * renderer,
* \sa SDL_GetRenderLogicalSize
* \sa SDL_SetRenderLogicalSize
*/
extern DECLSPEC void SDLCALL SDL_RenderWindowToLogical(SDL_Renderer * renderer,
int windowX, int windowY,
float *logicalX, float *logicalY);
extern DECLSPEC void SDLCALL SDL_RenderWindowToLogical(SDL_Renderer *renderer,
float windowX, float windowY,
float *logicalX, float *logicalY);
/**
* Get real coordinates of point in window when given logical coordinates of
@ -1060,9 +1060,9 @@ extern DECLSPEC void SDLCALL SDL_RenderWindowToLogical(SDL_Renderer * renderer,
* \sa SDL_GetRenderLogicalSize
* \sa SDL_SetRenderLogicalSize
*/
extern DECLSPEC void SDLCALL SDL_RenderLogicalToWindow(SDL_Renderer * renderer,
float logicalX, float logicalY,
int *windowX, int *windowY);
extern DECLSPEC void SDLCALL SDL_RenderLogicalToWindow(SDL_Renderer *renderer,
float logicalX, float logicalY,
float *windowX, float *windowY);
/**
* Set the color used for drawing operations (Rect, Line and Clear).

View File

@ -254,12 +254,12 @@ class SDL_BApp : public BApplication
SDL_GetWindowPosition(win, &winPosX, &winPosY);
int dx = x - (winWidth / 2);
int dy = y - (winHeight / 2);
SDL_SendMouseMotion(0, win, 0, SDL_GetMouse()->relative_mode, dx, dy);
SDL_SendMouseMotion(0, win, 0, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
if (!be_app->IsCursorHidden())
be_app->HideCursor();
} else {
SDL_SendMouseMotion(0, win, 0, 0, x, y);
SDL_SendMouseMotion(0, win, 0, 0, (float)x, (float)y);
if (SDL_CursorVisible() && be_app->IsCursorHidden())
be_app->ShowCursor();
}

View File

@ -448,7 +448,7 @@ void SDL_EVDEV_Poll(void)
case SYN_REPORT:
/* Send mouse axis changes together to ensure consistency and reduce event processing overhead */
if (item->mouse_x != 0 || item->mouse_y != 0) {
SDL_SendMouseMotion(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse, item->mouse_x, item->mouse_y);
SDL_SendMouseMotion(SDL_EVDEV_GetEventTimestamp(event), mouse->focus, (SDL_MouseID)item->fd, item->relative_mouse, (float)item->mouse_x, (float)item->mouse_y);
item->mouse_x = item->mouse_y = 0;
}
if (item->mouse_wheel != 0 || item->mouse_hwheel != 0) {

View File

@ -100,12 +100,12 @@ void updateMouse(SDL_WSCONS_mouse_input_data *inputData)
} break;
case WSCONS_EVENT_MOUSE_DELTA_X:
{
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, events[i].value, 0);
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, (float)events[i].value, 0.0f);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_Y:
{
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, 0, -events[i].value);
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, 0.0f, -(float)events[i].value);
break;
}
case WSCONS_EVENT_MOUSE_DELTA_W:

View File

@ -547,7 +547,7 @@ void SDL_WinRTApp::OnWindowActivated(CoreWindow ^ sender, WindowActivatedEventAr
*/
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION >= NTDDI_WINBLUE)
Point cursorPos = WINRT_TransformCursorPosition(window, sender->PointerPosition, TransformToSDLWindowSize);
SDL_SendMouseMotion(0, window, 0, 0, (int)cursorPos.X, (int)cursorPos.Y);
SDL_SendMouseMotion(0, window, 0, 0, cursorPos.X, cursorPos.Y);
#endif
/* TODO, WinRT: see if the Win32 bugfix from https://hg.libsdl.org/SDL/rev/d278747da408 needs to be applied (on window activation) */

View File

@ -317,7 +317,7 @@ SDL_DYNAPI_PROC(int,SDL_GetGamepadTouchpadFinger,(SDL_Gamepad *a, int b, int c,
SDL_DYNAPI_PROC(SDL_GamepadType,SDL_GetGamepadType,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(Uint16,SDL_GetGamepadVendor,(SDL_Gamepad *a),(a),return)
SDL_DYNAPI_PROC(SDL_JoystickID*,SDL_GetGamepads,(int *a),(a),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetGlobalMouseState,(int *a, int *b),(a,b),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetGlobalMouseState,(float *a, float *b),(a,b),return)
SDL_DYNAPI_PROC(SDL_Window*,SDL_GetGrabbedWindow,(void),(),return)
SDL_DYNAPI_PROC(const char*,SDL_GetHint,(const char *a),(a),return)
SDL_DYNAPI_PROC(SDL_bool,SDL_GetHintBoolean,(const char *a, SDL_bool b),(a,b),return)
@ -360,7 +360,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GetMasksForPixelFormatEnum,(Uint32 a, int *b, Uint3
SDL_DYNAPI_PROC(void,SDL_GetMemoryFunctions,(SDL_malloc_func *a, SDL_calloc_func *b, SDL_realloc_func *c, SDL_free_func *d),(a,b,c,d),)
SDL_DYNAPI_PROC(SDL_Keymod,SDL_GetModState,(void),(),return)
SDL_DYNAPI_PROC(SDL_Window*,SDL_GetMouseFocus,(void),(),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetMouseState,(int *a, int *b),(a,b),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetMouseState,(float *a, float *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_GetNumAllocations,(void),(),return)
SDL_DYNAPI_PROC(int,SDL_GetNumAudioDevices,(int a),(a),return)
SDL_DYNAPI_PROC(int,SDL_GetNumAudioDrivers,(void),(),return)
@ -396,7 +396,7 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GetRectIntersectionFloat,(const SDL_FRect *a, const
SDL_DYNAPI_PROC(void,SDL_GetRectUnion,(const SDL_Rect *a, const SDL_Rect *b, SDL_Rect *c),(a,b,c),)
SDL_DYNAPI_PROC(void,SDL_GetRectUnionFloat,(const SDL_FRect *a, const SDL_FRect *b, SDL_FRect *c),(a,b,c),)
SDL_DYNAPI_PROC(SDL_bool,SDL_GetRelativeMouseMode,(void),(),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetRelativeMouseState,(int *a, int *b),(a,b),return)
SDL_DYNAPI_PROC(Uint32,SDL_GetRelativeMouseState,(float *a, float *b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_GetRenderClipRect,(SDL_Renderer *a, SDL_Rect *b),(a,b),)
SDL_DYNAPI_PROC(int,SDL_GetRenderDrawBlendMode,(SDL_Renderer *a, SDL_BlendMode *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_GetRenderDrawColor,(SDL_Renderer *a, Uint8 *b, Uint8 *c, Uint8 *d, Uint8 *e),(a,b,c,d,e),return)
@ -628,7 +628,7 @@ SDL_DYNAPI_PROC(int,SDL_RenderLine,(SDL_Renderer *a, int b, int c, int d, int e)
SDL_DYNAPI_PROC(int,SDL_RenderLineFloat,(SDL_Renderer *a, float b, float c, float d, float e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(int,SDL_RenderLines,(SDL_Renderer *a, const SDL_Point *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_RenderLinesFloat,(SDL_Renderer *a, const SDL_FPoint *b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(void,SDL_RenderLogicalToWindow,(SDL_Renderer *a, float b, float c, int *d, int *e),(a,b,c,d,e),)
SDL_DYNAPI_PROC(void,SDL_RenderLogicalToWindow,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
SDL_DYNAPI_PROC(int,SDL_RenderPoint,(SDL_Renderer *a, int b, int c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_RenderPointFloat,(SDL_Renderer *a, float b, float c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_RenderPoints,(SDL_Renderer *a, const SDL_Point *b, int c),(a,b,c),return)
@ -644,7 +644,7 @@ SDL_DYNAPI_PROC(int,SDL_RenderTexture,(SDL_Renderer *a, SDL_Texture *b, const SD
SDL_DYNAPI_PROC(int,SDL_RenderTextureFloat,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d),(a,b,c,d),return)
SDL_DYNAPI_PROC(int,SDL_RenderTextureRotated,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_Rect *d, const double e, const SDL_Point *f, const SDL_RendererFlip g),(a,b,c,d,e,f,g),return)
SDL_DYNAPI_PROC(int,SDL_RenderTextureRotatedFloat,(SDL_Renderer *a, SDL_Texture *b, const SDL_Rect *c, const SDL_FRect *d, const double e, const SDL_FPoint *f, const SDL_RendererFlip g),(a,b,c,d,e,f,g),return)
SDL_DYNAPI_PROC(void,SDL_RenderWindowToLogical,(SDL_Renderer *a, int b, int c, float *d, float *e),(a,b,c,d,e),)
SDL_DYNAPI_PROC(void,SDL_RenderWindowToLogical,(SDL_Renderer *a, float b, float c, float *d, float *e),(a,b,c,d,e),)
SDL_DYNAPI_PROC(SDL_AssertState,SDL_ReportAssertion,(SDL_AssertData *a, const char *b, const char *c, int d),(a,b,c,d),return)
SDL_DYNAPI_PROC(void,SDL_ResetAssertionReport,(void),(),)
SDL_DYNAPI_PROC(SDL_bool,SDL_ResetHint,(const char *a),(a),return)
@ -781,8 +781,8 @@ SDL_DYNAPI_PROC(void,SDL_Vulkan_UnloadLibrary,(void),(),)
SDL_DYNAPI_PROC(int,SDL_WaitEvent,(SDL_Event *a),(a),return)
SDL_DYNAPI_PROC(int,SDL_WaitEventTimeout,(SDL_Event *a, Sint32 b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_WaitThread,(SDL_Thread *a, int *b),(a,b),)
SDL_DYNAPI_PROC(int,SDL_WarpMouseGlobal,(int a, int b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_WarpMouseInWindow,(SDL_Window *a, int b, int c),(a,b,c),)
SDL_DYNAPI_PROC(int,SDL_WarpMouseGlobal,(float a, float b),(a,b),return)
SDL_DYNAPI_PROC(void,SDL_WarpMouseInWindow,(SDL_Window *a, float b, float c),(a,b,c),)
SDL_DYNAPI_PROC(Uint32,SDL_WasInit,(Uint32 a),(a),return)
SDL_DYNAPI_PROC(size_t,SDL_WriteBE16,(SDL_RWops *a, Uint16 b),(a,b),return)
SDL_DYNAPI_PROC(size_t,SDL_WriteBE32,(SDL_RWops *a, Uint32 b),(a,b),return)

View File

@ -300,19 +300,19 @@ static void SDL_LogEvent(const SDL_Event *event)
break;
SDL_EVENT_CASE(SDL_MOUSEMOTION)
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u state=%u x=%d y=%d xrel=%d yrel=%d)",
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u state=%u x=%g y=%g xrel=%g yrel=%g)",
(uint)event->motion.timestamp, (uint)event->motion.windowID,
(uint)event->motion.which, (uint)event->motion.state,
(int)event->motion.x, (int)event->motion.y,
(int)event->motion.xrel, (int)event->motion.yrel);
event->motion.x, event->motion.y,
event->motion.xrel, event->motion.yrel);
break;
#define PRINT_MBUTTON_EVENT(event) \
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u button=%u state=%s clicks=%u x=%d y=%d)", \
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u button=%u state=%s clicks=%u x=%g y=%g)", \
(uint)event->button.timestamp, (uint)event->button.windowID, \
(uint)event->button.which, (uint)event->button.button, \
event->button.state == SDL_PRESSED ? "pressed" : "released", \
(uint)event->button.clicks, (int)event->button.x, (int)event->button.y)
(uint)event->button.clicks, event->button.x, event->button.y)
SDL_EVENT_CASE(SDL_MOUSEBUTTONDOWN)
PRINT_MBUTTON_EVENT(event);
break;
@ -322,10 +322,9 @@ static void SDL_LogEvent(const SDL_Event *event)
#undef PRINT_MBUTTON_EVENT
SDL_EVENT_CASE(SDL_MOUSEWHEEL)
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%d y=%d preciseX=%f preciseY=%f direction=%s)",
(void)SDL_snprintf(details, sizeof(details), " (timestamp=%u windowid=%u which=%u x=%g y=%g direction=%s)",
(uint)event->wheel.timestamp, (uint)event->wheel.windowID,
(uint)event->wheel.which, (int)event->wheel.x, (int)event->wheel.y,
event->wheel.preciseX, event->wheel.preciseY,
(uint)event->wheel.which, event->wheel.x, event->wheel.y,
event->wheel.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped");
break;

View File

@ -37,7 +37,7 @@ static SDL_Mouse SDL_mouse;
/* for mapping mouse events to touch */
static SDL_bool track_mouse_down = SDL_FALSE;
static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y);
static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, float x, float y);
static void SDLCALL SDL_MouseDoubleClickTimeChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
@ -305,15 +305,13 @@ void SDL_SetMouseFocus(SDL_Window *window)
}
/* Check to see if we need to synthesize focus events */
static SDL_bool SDL_UpdateMouseFocus(SDL_Window *window, int x, int y, Uint32 buttonstate, SDL_bool send_mouse_motion)
static SDL_bool SDL_UpdateMouseFocus(SDL_Window *window, float x, float y, Uint32 buttonstate, SDL_bool send_mouse_motion)
{
SDL_Mouse *mouse = SDL_GetMouse();
SDL_bool inWindow = SDL_TRUE;
if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
int w, h;
SDL_GetWindowSize(window, &w, &h);
if (x < 0 || y < 0 || x >= w || y >= h) {
if (x < 0.0f || y < 0.0f || x >= (float)window->w || y >= (float)window->h) {
inWindow = SDL_FALSE;
}
}
@ -343,7 +341,7 @@ static SDL_bool SDL_UpdateMouseFocus(SDL_Window *window, int x, int y, Uint32 bu
return SDL_TRUE;
}
int SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
int SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, float x, float y)
{
if (window && !relative) {
SDL_Mouse *mouse = SDL_GetMouse();
@ -355,24 +353,7 @@ int SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseI
return SDL_PrivateSendMouseMotion(timestamp, window, mouseID, relative, x, y);
}
static int GetScaledMouseDelta(float scale, int value, float *accum)
{
if (value && scale != 1.0f) {
if ((value > 0) != (*accum > 0)) {
*accum = 0.0f;
}
*accum += scale * value;
if (*accum >= 0.0f) {
value = (int)SDL_floor(*accum);
} else {
value = (int)SDL_ceil(*accum);
}
*accum -= value;
}
return value;
}
static float CalculateSystemScale(SDL_Mouse *mouse, const int *x, const int *y)
static float CalculateSystemScale(SDL_Mouse *mouse, const float *x, const float *y)
{
int i;
int n = mouse->num_system_scale_values;
@ -384,7 +365,7 @@ static float CalculateSystemScale(SDL_Mouse *mouse, const int *x, const int *y)
return v[0];
}
speed = SDL_sqrtf((float)(*x * *x) + (*y * *y));
speed = SDL_sqrtf((*x * *x) + (*y * *y));
for (i = 0; i < (n - 2); i += 2) {
if (speed < v[i + 2]) {
break;
@ -398,7 +379,6 @@ static float CalculateSystemScale(SDL_Mouse *mouse, const int *x, const int *y)
coef = (speed - v[i]) / (v[i + 2] - v[i]);
scale = v[i + 1] + (coef * (v[i + 3] - v[i + 1]));
}
SDL_Log("speed = %.2f, scale = %.2f\n", speed, scale);
return scale;
}
@ -444,39 +424,39 @@ int SDL_SetMouseSystemScale(int num_values, const float *values)
return 0;
}
static void GetScaledMouseDeltas(SDL_Mouse *mouse, int *x, int *y)
static void GetScaledMouseDeltas(SDL_Mouse *mouse, float *x, float *y)
{
if (mouse->relative_mode) {
if (mouse->enable_relative_speed_scale) {
*x = GetScaledMouseDelta(mouse->relative_speed_scale, *x, &mouse->scale_accum_x);
*y = GetScaledMouseDelta(mouse->relative_speed_scale, *y, &mouse->scale_accum_y);
*x *= mouse->relative_speed_scale;
*y *= mouse->relative_speed_scale;
} else if (mouse->enable_relative_system_scale && mouse->num_system_scale_values > 0) {
float relative_system_scale = CalculateSystemScale(mouse, x, y);
*x = GetScaledMouseDelta(relative_system_scale, *x, &mouse->scale_accum_x);
*y = GetScaledMouseDelta(relative_system_scale, *y, &mouse->scale_accum_y);
*x *= relative_system_scale;
*y *= relative_system_scale;
}
} else {
if (mouse->enable_normal_speed_scale) {
*x = GetScaledMouseDelta(mouse->normal_speed_scale, *x, &mouse->scale_accum_x);
*y = GetScaledMouseDelta(mouse->normal_speed_scale, *y, &mouse->scale_accum_y);
*x *= mouse->normal_speed_scale;
*y *= mouse->normal_speed_scale;
}
}
}
static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y)
static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, float x, float y)
{
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
int xrel = 0;
int yrel = 0;
float xrel = 0.0f;
float yrel = 0.0f;
/* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
if (mouse->mouse_touch_events) {
if (mouseID != SDL_TOUCH_MOUSEID && !relative && track_mouse_down) {
if (window) {
float fx = (float)x / (float)window->w;
float fy = (float)y / (float)window->h;
SDL_SendTouchMotion(timestamp, SDL_MOUSE_TOUCHID, 0, window, fx, fy, 1.0f);
float normalized_x = x / (float)window->w;
float normalized_y = y / (float)window->h;
SDL_SendTouchMotion(timestamp, SDL_MOUSE_TOUCHID, 0, window, normalized_x, normalized_y, 1.0f);
}
}
}
@ -489,11 +469,13 @@ static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_
}
if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
int center_x = 0, center_y = 0;
SDL_GetWindowSize(window, &center_x, &center_y);
center_x /= 2;
center_y /= 2;
if (x == center_x && y == center_y) {
int w = 0, h = 0;
float center_x, center_y;
SDL_GetWindowSize(window, &w, &h);
center_x = (float)w / 2.0f;
center_y = (float)h / 2.0f;
if (x >= SDL_floorf(center_x) && x <= SDL_ceilf(center_x) &&
y >= SDL_floorf(center_y) && y <= SDL_ceilf(center_y)) {
mouse->last_x = center_x;
mouse->last_y = center_y;
if (!mouse->relative_mode_warp_motion) {
@ -535,8 +517,8 @@ static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_
/* Ignore relative motion positioning the first touch */
if (mouseID == SDL_TOUCH_MOUSEID && !GetButtonState(mouse, SDL_TRUE)) {
xrel = 0;
yrel = 0;
xrel = 0.0f;
yrel = 0.0f;
}
/* Update internal mouse coordinates */
@ -551,14 +533,10 @@ static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_
/* make sure that the pointers find themselves inside the windows,
unless we have the mouse captured. */
if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
int x_min = 0, x_max = 0;
int y_min = 0, y_max = 0;
int x_min = 0, x_max = window->w - 1;
int y_min = 0, y_max = window->h - 1;
const SDL_Rect *confine = SDL_GetWindowMouseRect(window);
SDL_GetWindowSize(window, &x_max, &y_max);
--x_max;
--y_max;
if (confine) {
SDL_Rect window_rect;
SDL_Rect mouse_rect;
@ -575,18 +553,18 @@ static int SDL_PrivateSendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_
}
}
if (mouse->x > x_max) {
mouse->x = x_max;
if (mouse->x > (float)x_max) {
mouse->x = (float)x_max;
}
if (mouse->x < x_min) {
mouse->x = x_min;
if (mouse->x < (float)x_min) {
mouse->x = (float)x_min;
}
if (mouse->y > y_max) {
mouse->y = y_max;
if (mouse->y > (float)y_max) {
mouse->y = (float)y_max;
}
if (mouse->y < y_min) {
mouse->y = y_min;
if (mouse->y < (float)y_min) {
mouse->y = (float)y_min;
}
}
@ -692,9 +670,9 @@ static int SDL_PrivateSendMouseButton(Uint64 timestamp, SDL_Window *window, SDL_
track_mouse_down = SDL_FALSE;
}
if (window) {
float fx = (float)mouse->x / (float)window->w;
float fy = (float)mouse->y / (float)window->h;
SDL_SendTouch(timestamp, SDL_MOUSE_TOUCHID, 0, window, track_mouse_down, fx, fy, 1.0f);
float normalized_x = mouse->x / (float)window->w;
float normalized_y = mouse->y / (float)window->h;
SDL_SendTouch(timestamp, SDL_MOUSE_TOUCHID, 0, window, track_mouse_down, normalized_x, normalized_y, 1.0f);
}
}
}
@ -739,8 +717,8 @@ static int SDL_PrivateSendMouseButton(Uint64 timestamp, SDL_Window *window, SDL_
Uint64 now = SDL_GetTicks();
if (now >= (clickstate->last_timestamp + mouse->double_click_time) ||
SDL_abs(mouse->x - clickstate->last_x) > mouse->double_click_radius ||
SDL_abs(mouse->y - clickstate->last_y) > mouse->double_click_radius) {
SDL_fabs(mouse->x - clickstate->last_x) > mouse->double_click_radius ||
SDL_fabs(mouse->y - clickstate->last_y) > mouse->double_click_radius) {
clickstate->click_count = 0;
}
clickstate->last_timestamp = now;
@ -800,7 +778,6 @@ int SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID
{
SDL_Mouse *mouse = SDL_GetMouse();
int posted;
int integral_x, integral_y;
if (window) {
SDL_SetMouseFocus(window);
@ -810,44 +787,6 @@ int SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID
return 0;
}
if (x > 0.0f) {
if (mouse->accumulated_wheel_x < 0.0f) {
mouse->accumulated_wheel_x = 0.0f;
}
} else if (x < 0.0f) {
if (mouse->accumulated_wheel_x > 0.0f) {
mouse->accumulated_wheel_x = 0.0f;
}
}
mouse->accumulated_wheel_x += x;
if (mouse->accumulated_wheel_x > 0.0f) {
integral_x = (int)SDL_floor(mouse->accumulated_wheel_x);
} else if (mouse->accumulated_wheel_x < 0.0f) {
integral_x = (int)SDL_ceil(mouse->accumulated_wheel_x);
} else {
integral_x = 0;
}
mouse->accumulated_wheel_x -= integral_x;
if (y > 0.0f) {
if (mouse->accumulated_wheel_y < 0.0f) {
mouse->accumulated_wheel_y = 0.0f;
}
} else if (y < 0.0f) {
if (mouse->accumulated_wheel_y > 0.0f) {
mouse->accumulated_wheel_y = 0.0f;
}
}
mouse->accumulated_wheel_y += y;
if (mouse->accumulated_wheel_y > 0.0f) {
integral_y = (int)SDL_floor(mouse->accumulated_wheel_y);
} else if (mouse->accumulated_wheel_y < 0.0f) {
integral_y = (int)SDL_ceil(mouse->accumulated_wheel_y);
} else {
integral_y = 0;
}
mouse->accumulated_wheel_y -= integral_y;
/* Post the event, if desired */
posted = 0;
if (SDL_EventEnabled(SDL_MOUSEWHEEL)) {
@ -856,10 +795,8 @@ int SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID
event.common.timestamp = timestamp;
event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
event.wheel.which = mouseID;
event.wheel.x = integral_x;
event.wheel.y = integral_y;
event.wheel.preciseX = x;
event.wheel.preciseY = y;
event.wheel.x = x;
event.wheel.y = y;
event.wheel.direction = (Uint32)direction;
event.wheel.mouseX = mouse->x;
event.wheel.mouseY = mouse->y;
@ -935,7 +872,7 @@ void SDL_QuitMouse(void)
}
Uint32
SDL_GetMouseState(int *x, int *y)
SDL_GetMouseState(float *x, float *y)
{
SDL_Mouse *mouse = SDL_GetMouse();
@ -949,7 +886,7 @@ SDL_GetMouseState(int *x, int *y)
}
Uint32
SDL_GetRelativeMouseState(int *x, int *y)
SDL_GetRelativeMouseState(float *x, float *y)
{
SDL_Mouse *mouse = SDL_GetMouse();
@ -959,18 +896,18 @@ SDL_GetRelativeMouseState(int *x, int *y)
if (y) {
*y = mouse->ydelta;
}
mouse->xdelta = 0;
mouse->ydelta = 0;
mouse->xdelta = 0.0f;
mouse->ydelta = 0.0f;
return GetButtonState(mouse, SDL_TRUE);
}
Uint32
SDL_GetGlobalMouseState(int *x, int *y)
SDL_GetGlobalMouseState(float *x, float *y)
{
SDL_Mouse *mouse = SDL_GetMouse();
if (mouse->GetGlobalMouseState) {
int tmpx, tmpy;
float tmpx, tmpy;
/* make sure these are never NULL for the backend implementations... */
if (x == NULL) {
@ -980,7 +917,7 @@ SDL_GetGlobalMouseState(int *x, int *y)
y = &tmpy;
}
*x = *y = 0;
*x = *y = 0.0f;
return mouse->GetGlobalMouseState(x, y);
} else {
@ -988,7 +925,7 @@ SDL_GetGlobalMouseState(int *x, int *y)
}
}
void SDL_PerformWarpMouseInWindow(SDL_Window *window, int x, int y, SDL_bool ignore_relative_mode)
void SDL_PerformWarpMouseInWindow(SDL_Window *window, float x, float y, SDL_bool ignore_relative_mode)
{
SDL_Mouse *mouse = SDL_GetMouse();
@ -1033,12 +970,12 @@ void SDL_PerformWarpMouseInWindow(SDL_Window *window, int x, int y, SDL_bool ign
}
}
void SDL_WarpMouseInWindow(SDL_Window *window, int x, int y)
void SDL_WarpMouseInWindow(SDL_Window *window, float x, float y)
{
SDL_PerformWarpMouseInWindow(window, x, y, SDL_FALSE);
}
int SDL_WarpMouseGlobal(int x, int y)
int SDL_WarpMouseGlobal(float x, float y)
{
SDL_Mouse *mouse = SDL_GetMouse();
@ -1083,8 +1020,6 @@ int SDL_SetRelativeMouseMode(SDL_bool enabled)
}
}
mouse->relative_mode = enabled;
mouse->scale_accum_x = 0.0f;
mouse->scale_accum_y = 0.0f;
if (enabled) {
/* Update cursor visibility before we potentially warp the mouse */
@ -1095,7 +1030,9 @@ int SDL_SetRelativeMouseMode(SDL_bool enabled)
SDL_SetMouseFocus(focusWindow);
if (mouse->relative_mode_warp) {
SDL_PerformWarpMouseInWindow(focusWindow, focusWindow->w / 2, focusWindow->h / 2, SDL_TRUE);
float center_x = (float)focusWindow->w / 2.0f;
float center_y = (float)focusWindow->h / 2.0f;
SDL_PerformWarpMouseInWindow(focusWindow, center_x, center_y, SDL_TRUE);
}
}

View File

@ -37,7 +37,7 @@ typedef struct
typedef struct
{
int last_x, last_y;
float last_x, last_y;
Uint64 last_timestamp;
Uint8 click_count;
} SDL_MouseClickState;
@ -60,10 +60,10 @@ typedef struct
void (*FreeCursor)(SDL_Cursor *cursor);
/* Warp the mouse to (x,y) within a window */
void (*WarpMouse)(SDL_Window *window, int x, int y);
void (*WarpMouse)(SDL_Window *window, float x, float y);
/* Warp the mouse to (x,y) in screen space */
int (*WarpMouseGlobal)(int x, int y);
int (*WarpMouseGlobal)(float x, float y);
/* Set relative mode */
int (*SetRelativeMouseMode)(SDL_bool enabled);
@ -72,18 +72,16 @@ typedef struct
int (*CaptureMouse)(SDL_Window *window);
/* Get absolute mouse coordinates. (x) and (y) are never NULL and set to zero before call. */
Uint32 (*GetGlobalMouseState)(int *x, int *y);
Uint32 (*GetGlobalMouseState)(float *x, float *y);
/* Data common to all mice */
SDL_MouseID mouseID;
SDL_Window *focus;
int x;
int y;
int xdelta;
int ydelta;
int last_x, last_y; /* the last reported x and y coordinates */
float accumulated_wheel_x;
float accumulated_wheel_y;
float x;
float y;
float xdelta;
float ydelta;
float last_x, last_y; /* the last reported x and y coordinates */
SDL_bool has_position;
SDL_bool relative_mode;
SDL_bool relative_mode_warp;
@ -95,8 +93,6 @@ typedef struct
SDL_bool enable_relative_system_scale;
int num_system_scale_values;
float *system_scale_values;
float scale_accum_x;
float scale_accum_y;
Uint32 double_click_time;
int double_click_radius;
SDL_bool touch_mouse_events;
@ -145,7 +141,7 @@ extern int SDL_UpdateMouseCapture(SDL_bool force_release);
extern int SDL_SetMouseSystemScale(int num_values, const float *values);
/* Send a mouse motion event */
extern int SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, int x, int y);
extern int SDL_SendMouseMotion(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, int relative, float x, float y);
/* Send a mouse button event */
extern int SDL_SendMouseButton(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, Uint8 state, Uint8 button);
@ -157,7 +153,7 @@ extern int SDL_SendMouseButtonClicks(Uint64 timestamp, SDL_Window *window, SDL_M
extern int SDL_SendMouseWheel(Uint64 timestamp, SDL_Window *window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction);
/* Warp the mouse within the window, potentially overriding relative mode */
extern void SDL_PerformWarpMouseInWindow(SDL_Window *window, int x, int y, SDL_bool ignore_relative_mode);
extern void SDL_PerformWarpMouseInWindow(SDL_Window *window, float x, float y, SDL_bool ignore_relative_mode);
/* TODO RECONNECT: Set mouse state to "zero" */
#if 0

View File

@ -260,19 +260,19 @@ int SDL_SendTouch(Uint64 timestamp, SDL_TouchID id, SDL_FingerID fingerid, SDL_W
if (window) {
if (down) {
if (finger_touching == SDL_FALSE) {
int pos_x = (int)(x * (float)window->w);
int pos_y = (int)(y * (float)window->h);
float pos_x = (x * (float)window->w);
float pos_y = (y * (float)window->h);
if (pos_x < 0) {
pos_x = 0;
}
if (pos_x > window->w - 1) {
pos_x = window->w - 1;
if (pos_x > (float)(window->w - 1)) {
pos_x = (float)(window->w - 1);
}
if (pos_y < 0) {
pos_y = 0;
if (pos_y < 0.0f) {
pos_y = 0.0f;
}
if (pos_y > window->h - 1) {
pos_y = window->h - 1;
if (pos_y > (float)(window->h - 1)) {
pos_y = (float)(window->h - 1);
}
SDL_SendMouseMotion(timestamp, window, SDL_TOUCH_MOUSEID, 0, pos_x, pos_y);
SDL_SendMouseButton(timestamp, window, SDL_TOUCH_MOUSEID, SDL_PRESSED, SDL_BUTTON_LEFT);
@ -384,19 +384,19 @@ int SDL_SendTouchMotion(Uint64 timestamp, SDL_TouchID id, SDL_FingerID fingerid,
if (id != SDL_MOUSE_TOUCHID) {
if (window) {
if (finger_touching == SDL_TRUE && track_touchid == id && track_fingerid == fingerid) {
int pos_x = (int)(x * (float)window->w);
int pos_y = (int)(y * (float)window->h);
if (pos_x < 0) {
pos_x = 0;
float pos_x = (x * (float)window->w);
float pos_y = (y * (float)window->h);
if (pos_x < 0.0f) {
pos_x = 0.0f;
}
if (pos_x > window->w - 1) {
pos_x = window->w - 1;
if (pos_x > (float)(window->w - 1)) {
pos_x = (float)(window->w - 1);
}
if (pos_y < 0) {
pos_y = 0;
if (pos_y < 0.0f) {
pos_y = 0.0f;
}
if (pos_y > window->h - 1) {
pos_y = window->h - 1;
if (pos_y > (float)(window->h - 1)) {
pos_y = (float)(window->h - 1);
}
SDL_SendMouseMotion(timestamp, window, SDL_TOUCH_MOUSEID, 0, pos_x, pos_y);
}

View File

@ -921,7 +921,7 @@ static void IOS_MFIJoystickUpdate(SDL_Joystick *joystick)
for (id key in controller.physicalInputProfile.axes) {
GCControllerAxisInput *axis = controller.physicalInputProfile.axes[key];
if (axis.value != 0.0f)
NSLog(@"Axis %@ = %.2f\n", key, axis.value);
NSLog(@"Axis %@ = %g\n", key, axis.value);
}
}
}

View File

@ -759,21 +759,15 @@ static int SDLCALL SDL_RendererEventWatch(void *userdata, SDL_Event *event)
SDL_FPoint scale;
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
if (logical_w) {
event->motion.x -= (int)(viewport.x * renderer->dpi_scale.x);
event->motion.y -= (int)(viewport.y * renderer->dpi_scale.y);
event->motion.x = (int)(event->motion.x / (scale.x * renderer->dpi_scale.x));
event->motion.y = (int)(event->motion.y / (scale.y * renderer->dpi_scale.y));
if (event->motion.xrel != 0 && renderer->relative_scaling) {
float rel = renderer->xrel + event->motion.xrel / (scale.x * renderer->dpi_scale.x);
float truncated = SDL_truncf(rel);
renderer->xrel = rel - truncated;
event->motion.xrel = (Sint32)truncated;
event->motion.x -= (float)(viewport.x * renderer->dpi_scale.x);
event->motion.y -= (float)(viewport.y * renderer->dpi_scale.y);
event->motion.x /= (scale.x * renderer->dpi_scale.x);
event->motion.y /= (scale.y * renderer->dpi_scale.y);
if (event->motion.xrel != 0.0f && renderer->relative_scaling) {
event->motion.xrel /= (scale.x * renderer->dpi_scale.x);
}
if (event->motion.yrel != 0 && renderer->relative_scaling) {
float rel = renderer->yrel + event->motion.yrel / (scale.y * renderer->dpi_scale.y);
float truncated = SDL_truncf(rel);
renderer->yrel = rel - truncated;
event->motion.yrel = (Sint32)truncated;
if (event->motion.yrel != 0.0f && renderer->relative_scaling) {
event->motion.yrel /= (scale.y * renderer->dpi_scale.y);
}
}
}
@ -786,10 +780,24 @@ static int SDLCALL SDL_RendererEventWatch(void *userdata, SDL_Event *event)
SDL_FPoint scale;
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
if (logical_w) {
event->button.x -= (int)(viewport.x * renderer->dpi_scale.x);
event->button.y -= (int)(viewport.y * renderer->dpi_scale.y);
event->button.x = (int)(event->button.x / (scale.x * renderer->dpi_scale.x));
event->button.y = (int)(event->button.y / (scale.y * renderer->dpi_scale.y));
event->button.x -= (float)(viewport.x * renderer->dpi_scale.x);
event->button.y -= (float)(viewport.y * renderer->dpi_scale.y);
event->button.x /= (scale.x * renderer->dpi_scale.x);
event->button.y /= (scale.y * renderer->dpi_scale.y);
}
}
} else if (event->type == SDL_MOUSEWHEEL) {
SDL_Window *window = SDL_GetWindowFromID(event->button.windowID);
if (window == renderer->window) {
int logical_w, logical_h;
SDL_DRect viewport;
SDL_FPoint scale;
GetWindowViewportValues(renderer, &logical_w, &logical_h, &viewport, &scale);
if (logical_w) {
event->wheel.mouseX -= (float)(viewport.x * renderer->dpi_scale.x);
event->wheel.mouseY -= (float)(viewport.y * renderer->dpi_scale.y);
event->wheel.mouseX /= (scale.x * renderer->dpi_scale.x);
event->wheel.mouseY /= (scale.y * renderer->dpi_scale.y);
}
}
} else if (event->type == SDL_FINGERDOWN ||
@ -2510,14 +2518,14 @@ void SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY)
}
}
void SDL_RenderWindowToLogical(SDL_Renderer *renderer, int windowX, int windowY, float *logicalX, float *logicalY)
void SDL_RenderWindowToLogical(SDL_Renderer *renderer, float windowX, float windowY, float *logicalX, float *logicalY)
{
float window_physical_x, window_physical_y;
CHECK_RENDERER_MAGIC(renderer, );
window_physical_x = ((float)windowX) / renderer->dpi_scale.x;
window_physical_y = ((float)windowY) / renderer->dpi_scale.y;
window_physical_x = (windowX / renderer->dpi_scale.x);
window_physical_y = (windowY / renderer->dpi_scale.y);
if (logicalX) {
*logicalX = (float)((window_physical_x - renderer->viewport.x) / renderer->scale.x);
@ -2527,7 +2535,7 @@ void SDL_RenderWindowToLogical(SDL_Renderer *renderer, int windowX, int windowY,
}
}
void SDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float logicalY, int *windowX, int *windowY)
void SDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float logicalY, float *windowX, float *windowY)
{
float window_physical_x, window_physical_y;
@ -2537,10 +2545,10 @@ void SDL_RenderLogicalToWindow(SDL_Renderer *renderer, float logicalX, float log
window_physical_y = (float)((logicalY * renderer->scale.y) + renderer->viewport.y);
if (windowX) {
*windowX = (int)(window_physical_x * renderer->dpi_scale.x);
*windowX = (window_physical_x * renderer->dpi_scale.x);
}
if (windowY) {
*windowY = (int)(window_physical_y * renderer->dpi_scale.y);
*windowY = (window_physical_y * renderer->dpi_scale.y);
}
}

View File

@ -249,10 +249,6 @@ struct SDL_Renderer
/* List of triangle indices to draw rects */
int rect_index_order[6];
/* Remainder from scaled relative motion */
float xrel;
float yrel;
/* The list of textures */
SDL_Texture *textures;
SDL_Texture *target;

View File

@ -1528,23 +1528,23 @@ static void SDLTest_PrintEvent(SDL_Event *event)
SDL_Log("SDL EVENT: Keymap changed");
break;
case SDL_MOUSEMOTION:
SDL_Log("SDL EVENT: Mouse: moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (%" SDL_PRIs32 ",%" SDL_PRIs32 ") in window %" SDL_PRIu32,
SDL_Log("SDL EVENT: Mouse: moved to %g,%g (%g,%g) in window %" SDL_PRIu32,
event->motion.x, event->motion.y,
event->motion.xrel, event->motion.yrel,
event->motion.windowID);
break;
case SDL_MOUSEBUTTONDOWN:
SDL_Log("SDL EVENT: Mouse: button %d pressed at %" SDL_PRIs32 ",%" SDL_PRIs32 " with click count %d in window %" SDL_PRIu32,
SDL_Log("SDL EVENT: Mouse: button %d pressed at %g,%g with click count %d in window %" SDL_PRIu32,
event->button.button, event->button.x, event->button.y, event->button.clicks,
event->button.windowID);
break;
case SDL_MOUSEBUTTONUP:
SDL_Log("SDL EVENT: Mouse: button %d released at %" SDL_PRIs32 ",%" SDL_PRIs32 " with click count %d in window %" SDL_PRIu32,
SDL_Log("SDL EVENT: Mouse: button %d released at %g,%g with click count %d in window %" SDL_PRIu32,
event->button.button, event->button.x, event->button.y, event->button.clicks,
event->button.windowID);
break;
case SDL_MOUSEWHEEL:
SDL_Log("SDL EVENT: Mouse: wheel scrolled %" SDL_PRIs32 " in x and %" SDL_PRIs32 " in y (reversed: %" SDL_PRIu32 ") in window %" SDL_PRIu32,
SDL_Log("SDL EVENT: Mouse: wheel scrolled %g in x and %g in y (reversed: %" SDL_PRIu32 ") in window %" SDL_PRIu32,
event->wheel.x, event->wheel.y, event->wheel.direction, event->wheel.windowID);
break;
case SDL_JOYDEVICEADDED:
@ -2067,9 +2067,9 @@ void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done
case SDLK_a:
if (withControl) {
/* Ctrl-A reports absolute mouse position. */
int x, y;
float x, y;
const Uint32 mask = SDL_GetGlobalMouseState(&x, &y);
SDL_Log("ABSOLUTE MOUSE: (%d, %d)%s%s%s%s%s\n", x, y,
SDL_Log("ABSOLUTE MOUSE: (%g, %g)%s%s%s%s%s\n", x, y,
(mask & SDL_BUTTON_LMASK) ? " [LBUTTON]" : "",
(mask & SDL_BUTTON_MMASK) ? " [MBUTTON]" : "",
(mask & SDL_BUTTON_RMASK) ? " [RBUTTON]" : "",
@ -2101,7 +2101,7 @@ void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done
char message[256];
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
(void)SDL_snprintf(message, sizeof message, "(%" SDL_PRIs32 ", %" SDL_PRIs32 "), rel (%" SDL_PRIs32 ", %" SDL_PRIs32 ")\n",
(void)SDL_snprintf(message, sizeof message, "(%g, %g), rel (%g, %g)\n",
lastEvent.x, lastEvent.y, lastEvent.xrel, lastEvent.yrel);
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Last mouse position", message, window);
break;
@ -2170,6 +2170,7 @@ void SDLTest_CommonDrawWindowInfo(SDL_Renderer *renderer, SDL_Window *window, in
int textY = 0;
const int lineHeight = 10;
int x, y, w, h;
float fx, fy;
SDL_Rect rect;
SDL_DisplayMode mode;
float ddpi, hdpi, vdpi;
@ -2314,14 +2315,14 @@ void SDLTest_CommonDrawWindowInfo(SDL_Renderer *renderer, SDL_Window *window, in
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
flags = SDL_GetMouseState(&x, &y);
(void)SDL_snprintf(text, sizeof text, "SDL_GetMouseState: %d,%d ", x, y);
flags = SDL_GetMouseState(&fx, &fy);
(void)SDL_snprintf(text, sizeof text, "SDL_GetMouseState: %g,%g ", fx, fy);
SDLTest_PrintButtonMask(text, sizeof text, flags);
SDLTest_DrawString(renderer, 0, textY, text);
textY += lineHeight;
flags = SDL_GetGlobalMouseState(&x, &y);
(void)SDL_snprintf(text, sizeof text, "SDL_GetGlobalMouseState: %d,%d ", x, y);
flags = SDL_GetGlobalMouseState(&fx, &fy);
(void)SDL_snprintf(text, sizeof text, "SDL_GetGlobalMouseState: %g,%g ", fx, fy);
SDLTest_PrintButtonMask(text, sizeof text, flags);
SDLTest_DrawString(renderer, 0, textY, text);
textY += lineHeight;

View File

@ -1304,7 +1304,7 @@ SDL_GetWindowPixelFormat(SDL_Window *window)
static void SDL_RestoreMousePosition(SDL_Window *window)
{
int x, y;
float x, y;
if (window == SDL_GetMouseFocus()) {
SDL_GetMouseState(&x, &y);
@ -2992,7 +2992,7 @@ void SDL_OnWindowFocusGained(SDL_Window *window)
if (mouse && mouse->relative_mode) {
SDL_SetMouseFocus(window);
if (mouse->relative_mode_warp) {
SDL_PerformWarpMouseInWindow(window, window->w / 2, window->h / 2, SDL_TRUE);
SDL_PerformWarpMouseInWindow(window, (float)window->w / 2.0f, (float)window->h / 2.0f, SDL_TRUE);
}
}

View File

@ -224,7 +224,7 @@ void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y
changes = state & ~last_state;
button = TranslateButton(changes);
last_state = state;
SDL_SendMouseMotion(0, window, 0, relative, (int)x, (int)y);
SDL_SendMouseMotion(0, window, 0, relative, x, y);
SDL_SendMouseButton(0, window, 0, SDL_PRESSED, button);
break;
@ -232,13 +232,13 @@ void Android_OnMouse(SDL_Window *window, int state, int action, float x, float y
changes = last_state & ~state;
button = TranslateButton(changes);
last_state = state;
SDL_SendMouseMotion(0, window, 0, relative, (int)x, (int)y);
SDL_SendMouseMotion(0, window, 0, relative, x, y);
SDL_SendMouseButton(0, window, 0, SDL_RELEASED, button);
break;
case ACTION_MOVE:
case ACTION_HOVER_MOVE:
SDL_SendMouseMotion(0, window, 0, relative, (int)x, (int)y);
SDL_SendMouseMotion(0, window, 0, relative, x, y);
break;
case ACTION_SCROLL:

View File

@ -230,13 +230,13 @@ static int Cocoa_ShowCursor(SDL_Cursor *cursor)
}
}
static SDL_Window *SDL_FindWindowAtPoint(const int x, const int y)
static SDL_Window *SDL_FindWindowAtPoint(const float x, const float y)
{
const SDL_Point pt = { x, y };
const SDL_FPoint pt = { x, y };
SDL_Window *i;
for (i = SDL_GetVideoDevice()->windows; i; i = i->next) {
const SDL_Rect r = { i->x, i->y, i->w, i->h };
if (SDL_PointInRect(&pt, &r)) {
const SDL_FRect r = { (float)i->x, (float)i->y, (float)i->w, (float)i->h };
if (SDL_PointInRectFloat(&pt, &r)) {
return i;
}
}
@ -244,7 +244,7 @@ static SDL_Window *SDL_FindWindowAtPoint(const int x, const int y)
return NULL;
}
static int Cocoa_WarpMouseGlobal(int x, int y)
static int Cocoa_WarpMouseGlobal(float x, float y)
{
CGPoint point;
SDL_Mouse *mouse = SDL_GetMouse();
@ -256,7 +256,7 @@ static int Cocoa_WarpMouseGlobal(int x, int y)
return 0;
}
}
point = CGPointMake((float)x, (float)y);
point = CGPointMake(x, y);
Cocoa_HandleMouseWarp(point.x, point.y);
@ -285,7 +285,7 @@ static int Cocoa_WarpMouseGlobal(int x, int y)
return 0;
}
static void Cocoa_WarpMouse(SDL_Window *window, int x, int y)
static void Cocoa_WarpMouse(SDL_Window *window, float x, float y)
{
Cocoa_WarpMouseGlobal(window->x + x, window->y + y);
}
@ -340,14 +340,14 @@ static int Cocoa_CaptureMouse(SDL_Window *window)
return 0;
}
static Uint32 Cocoa_GetGlobalMouseState(int *x, int *y)
static Uint32 Cocoa_GetGlobalMouseState(float *x, float *y)
{
const NSUInteger cocoaButtons = [NSEvent pressedMouseButtons];
const NSPoint cocoaLocation = [NSEvent mouseLocation];
Uint32 retval = 0;
*x = (int)cocoaLocation.x;
*y = (int)(CGDisplayPixelsHigh(kCGDirectMainDisplay) - cocoaLocation.y);
*x = cocoaLocation.x;
*y = (CGDisplayPixelsHigh(kCGDirectMainDisplay) - cocoaLocation.y);
retval |= (cocoaButtons & (1 << 0)) ? SDL_BUTTON_LMASK : 0;
retval |= (cocoaButtons & (1 << 1)) ? SDL_BUTTON_RMASK : 0;
@ -495,7 +495,7 @@ void Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
DLog("Motion was (%g, %g), offset to (%g, %g)", [event deltaX], [event deltaY], deltaX, deltaY);
}
SDL_SendMouseMotion(Cocoa_GetEventTimestamp([event timestamp]), mouse->focus, mouseID, 1, (int)deltaX, (int)deltaY);
SDL_SendMouseMotion(Cocoa_GetEventTimestamp([event timestamp]), mouse->focus, mouseID, 1, deltaX, deltaY);
}
void Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)

View File

@ -53,7 +53,7 @@ typedef enum
PendingWindowOperation pendingWindowOperation;
BOOL isMoving;
NSInteger focusClickPending;
int pendingWindowWarpX, pendingWindowWarpY;
float pendingWindowWarpX, pendingWindowWarpY;
BOOL isDragAreaRunning;
}
@ -71,7 +71,7 @@ typedef enum
- (BOOL)isMovingOrFocusClickPending;
- (void)setFocusClickPending:(NSInteger)button;
- (void)clearFocusClickPending:(NSInteger)button;
- (void)setPendingMoveX:(int)x Y:(int)y;
- (void)setPendingMoveX:(float)x Y:(float)y;
- (void)windowDidFinishMoving;
- (void)onMovingOrFocusClickPendingStateCleared;

View File

@ -26,6 +26,8 @@
#error SDL for macOS must be built with a 10.7 SDK or above.
#endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1070 */
#include <float.h> /* For FLT_MAX */
#include "../SDL_sysvideo.h"
#include "../../events/SDL_keyboard_c.h"
#include "../../events/SDL_mouse_c.h"
@ -170,7 +172,7 @@
NSArray *array;
NSPoint point;
SDL_Mouse *mouse;
int x, y;
float x, y;
if (desiredType == nil) {
return NO; /* can't accept anything that's being dropped here. */
@ -187,9 +189,9 @@
/* Code addon to update the mouse location */
point = [sender draggingLocation];
mouse = SDL_GetMouse();
x = (int)point.x;
y = (int)(sdlwindow->h - point.y);
if (x >= 0 && x < sdlwindow->w && y >= 0 && y < sdlwindow->h) {
x = point.x;
y = (sdlwindow->h - point.y);
if (x >= 0.0f && x < (float)sdlwindow->w && y >= 0.0f && y < (float)sdlwindow->h) {
SDL_SendMouseMotion(0, sdlwindow, mouse->mouseID, 0, x, y);
}
/* Code addon to update the mouse location */
@ -370,7 +372,7 @@ static SDL_bool ShouldAdjustCoordinatesForGrab(SDL_Window *window)
return SDL_FALSE;
}
static SDL_bool AdjustCoordinatesForGrab(SDL_Window *window, int x, int y, CGPoint *adjusted)
static SDL_bool AdjustCoordinatesForGrab(SDL_Window *window, float x, float y, CGPoint *adjusted)
{
if (window->mouse_rect.w > 0 && window->mouse_rect.h > 0) {
SDL_Rect window_rect;
@ -382,10 +384,10 @@ static SDL_bool AdjustCoordinatesForGrab(SDL_Window *window, int x, int y, CGPoi
window_rect.h = window->h;
if (SDL_GetRectIntersection(&window->mouse_rect, &window_rect, &mouse_rect)) {
int left = window->x + mouse_rect.x;
int right = left + mouse_rect.w - 1;
int top = window->y + mouse_rect.y;
int bottom = top + mouse_rect.h - 1;
float left = (float)window->x + mouse_rect.x;
float right = left + mouse_rect.w - 1;
float top = (float)window->y + mouse_rect.y;
float bottom = top + mouse_rect.h - 1;
if (x < left || x > right || y < top || y > bottom) {
adjusted->x = SDL_clamp(x, left, right);
adjusted->y = SDL_clamp(y, top, bottom);
@ -396,10 +398,10 @@ static SDL_bool AdjustCoordinatesForGrab(SDL_Window *window, int x, int y, CGPoi
}
if ((window->flags & SDL_WINDOW_MOUSE_GRABBED) != 0) {
int left = window->x;
int right = left + window->w - 1;
int top = window->y;
int bottom = top + window->h - 1;
float left = (float)window->x;
float right = left + window->w - 1;
float top = (float)window->y;
float bottom = top + window->h - 1;
if (x < left || x > right || y < top || y > bottom) {
adjusted->x = SDL_clamp(x, left, right);
adjusted->y = SDL_clamp(y, top, bottom);
@ -450,7 +452,7 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
} else {
/* Move the cursor to the nearest point in the window */
if (ShouldAdjustCoordinatesForGrab(window)) {
int x, y;
float x, y;
CGPoint cgpoint;
SDL_GetGlobalMouseState(&x, &y);
@ -479,7 +481,7 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
pendingWindowOperation = PENDING_OPERATION_NONE;
isMoving = NO;
isDragAreaRunning = NO;
pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
center = [NSNotificationCenter defaultCenter];
@ -672,7 +674,7 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
}
}
- (void)setPendingMoveX:(int)x Y:(int)y
- (void)setPendingMoveX:(float)x Y:(float)y
{
pendingWindowWarpX = x;
pendingWindowWarpY = y;
@ -690,14 +692,14 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
{
if (![self isMovingOrFocusClickPending]) {
SDL_Mouse *mouse = SDL_GetMouse();
if (pendingWindowWarpX != INT_MAX && pendingWindowWarpY != INT_MAX) {
if (pendingWindowWarpX != FLT_MAX && pendingWindowWarpY != FLT_MAX) {
mouse->WarpMouseGlobal(pendingWindowWarpX, pendingWindowWarpY);
pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
}
if (mouse->relative_mode && !mouse->relative_mode_warp && mouse->focus == _data.window) {
/* Move the cursor to the nearest point in the window */
{
int x, y;
float x, y;
CGPoint cgpoint;
SDL_GetMouseState(&x, &y);
@ -731,7 +733,7 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
- (void)windowWillMove:(NSNotification *)aNotification
{
if ([_data.nswindow isKindOfClass:[SDLWindow class]]) {
pendingWindowWarpX = pendingWindowWarpY = INT_MAX;
pendingWindowWarpX = pendingWindowWarpY = FLT_MAX;
isMoving = YES;
}
}
@ -848,13 +850,13 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
/* If we just gained focus we need the updated mouse position */
if (!mouse->relative_mode) {
NSPoint point;
int x, y;
float x, y;
point = [_data.nswindow mouseLocationOutsideOfEventStream];
x = (int)point.x;
y = (int)(window->h - point.y);
x = point.x;
y = (window->h - point.y);
if (x >= 0 && x < window->w && y >= 0 && y < window->h) {
if (x >= 0.0f && x < (float)window->w && y >= 0.0f && y < (float)window->h) {
SDL_SendMouseMotion(0, window, mouse->mouseID, 0, x, y);
}
}
@ -1317,7 +1319,7 @@ static int Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_
SDL_Mouse *mouse = SDL_GetMouse();
SDL_MouseID mouseID;
NSPoint point;
int x, y;
float x, y;
SDL_Window *window;
if (!mouse) {
@ -1337,8 +1339,8 @@ static int Cocoa_SendMouseButtonClicks(SDL_Mouse *mouse, NSEvent *theEvent, SDL_
}
point = [theEvent locationInWindow];
x = (int)point.x;
y = (int)(window->h - point.y);
x = point.x;
y = (window->h - point.y);
if (NSAppKitVersionNumber >= NSAppKitVersionNumber10_13_2) {
/* Mouse grab is taken care of by the confinement rect */

View File

@ -613,8 +613,7 @@ static EM_BOOL Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEv
{
SDL_WindowData *window_data = userData;
const int isPointerLocked = window_data->has_pointer_lock;
int mx, my;
static double residualx = 0, residualy = 0;
float mx, my;
/* rescale (in case canvas is being scaled)*/
double client_w, client_h, xscale, yscale;
@ -623,16 +622,11 @@ static EM_BOOL Emscripten_HandleMouseMove(int eventType, const EmscriptenMouseEv
yscale = window_data->window->h / client_h;
if (isPointerLocked) {
residualx += mouseEvent->movementX * xscale;
residualy += mouseEvent->movementY * yscale;
/* Let slow sub-pixel motion accumulate. Don't lose it. */
mx = residualx;
residualx -= mx;
my = residualy;
residualy -= my;
mx = (float)(mouseEvent->movementX * xscale);
my = (float)(mouseEvent->movementY * yscale);
} else {
mx = mouseEvent->targetX * xscale;
my = mouseEvent->targetY * yscale;
mx = (float)(mouseEvent->targetX * xscale);
my = (float)(mouseEvent->targetY * yscale);
}
SDL_SendMouseMotion(0, window_data->window, 0, isPointerLocked, mx, my);
@ -687,16 +681,16 @@ static EM_BOOL Emscripten_HandleMouseFocus(int eventType, const EmscriptenMouseE
{
SDL_WindowData *window_data = userData;
int mx = mouseEvent->targetX, my = mouseEvent->targetY;
const int isPointerLocked = window_data->has_pointer_lock;
if (!isPointerLocked) {
/* rescale (in case canvas is being scaled)*/
float mx, my;
double client_w, client_h;
emscripten_get_element_css_size(window_data->canvas_id, &client_w, &client_h);
mx = mx * (window_data->window->w / client_w);
my = my * (window_data->window->h / client_h);
mx = (float)(mouseEvent->targetX * (window_data->window->w / client_w));
my = (float)(mouseEvent->targetY * (window_data->window->h / client_h));
SDL_SendMouseMotion(0, window_data->window, 0, isPointerLocked, mx, my);
}

View File

@ -204,11 +204,6 @@ static int Emscripten_ShowCursor(SDL_Cursor *cursor)
return 0;
}
static void Emscripten_WarpMouse(SDL_Window *window, int x, int y)
{
SDL_Unsupported();
}
static int Emscripten_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_Window *window;
@ -241,7 +236,6 @@ void Emscripten_InitMouse()
mouse->CreateCursor = Emscripten_CreateCursor;
mouse->ShowCursor = Emscripten_ShowCursor;
mouse->FreeCursor = Emscripten_FreeCursor;
mouse->WarpMouse = Emscripten_WarpMouse;
mouse->CreateSystemCursor = Emscripten_CreateSystemCursor;
mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;

View File

@ -254,12 +254,12 @@ class SDL_BApp : public BApplication
SDL_GetWindowPosition(win, &winPosX, &winPosY);
int dx = x - (winWidth / 2);
int dy = y - (winHeight / 2);
SDL_SendMouseMotion(0, win, 0, SDL_GetMouse()->relative_mode, dx, dy);
SDL_SendMouseMotion(0, win, 0, SDL_GetMouse()->relative_mode, (float)dx, (float)dy);
set_mouse_position((winPosX + winWidth / 2), (winPosY + winHeight / 2));
if (!be_app->IsCursorHidden())
be_app->HideCursor();
} else {
SDL_SendMouseMotion(0, win, 0, 0, x, y);
SDL_SendMouseMotion(0, win, 0, 0, (float)x, (float)y);
if (SDL_CursorVisible() && be_app->IsCursorHidden())
be_app->ShowCursor();
}

View File

@ -37,8 +37,6 @@ static SDL_Cursor *KMSDRM_CreateCursor(SDL_Surface *surface, int hot_x, int hot_
static int KMSDRM_ShowCursor(SDL_Cursor *cursor);
static void KMSDRM_MoveCursor(SDL_Cursor *cursor);
static void KMSDRM_FreeCursor(SDL_Cursor *cursor);
static void KMSDRM_WarpMouse(SDL_Window *window, int x, int y);
static int KMSDRM_WarpMouseGlobal(int x, int y);
/**************************************************************************************/
/* BEFORE CODING ANYTHING MOUSE/CURSOR RELATED, REMEMBER THIS. */
@ -354,15 +352,7 @@ static int KMSDRM_ShowCursor(SDL_Cursor *cursor)
return ret;
}
/* Warp the mouse to (x,y) */
static void KMSDRM_WarpMouse(SDL_Window *window, int x, int y)
{
/* Only one global/fullscreen window is supported */
KMSDRM_WarpMouseGlobal(x, y);
}
/* Warp the mouse to (x,y) */
static int KMSDRM_WarpMouseGlobal(int x, int y)
static int KMSDRM_WarpMouseGlobal(float x, float y)
{
SDL_Mouse *mouse = SDL_GetMouse();
@ -378,7 +368,7 @@ static int KMSDRM_WarpMouseGlobal(int x, int y)
if (dispdata->cursor_bo) {
int ret = 0;
ret = KMSDRM_drmModeMoveCursor(dispdata->cursor_bo_drm_fd, dispdata->crtc->crtc_id, x, y);
ret = KMSDRM_drmModeMoveCursor(dispdata->cursor_bo_drm_fd, dispdata->crtc->crtc_id, (int)x, (int)y);
if (ret) {
SDL_SetError("drmModeMoveCursor() failed.");
@ -395,6 +385,12 @@ static int KMSDRM_WarpMouseGlobal(int x, int y)
}
}
static void KMSDRM_WarpMouse(SDL_Window *window, float x, float y)
{
/* Only one global/fullscreen window is supported */
KMSDRM_WarpMouseGlobal(x, y);
}
void KMSDRM_InitMouse(_THIS, SDL_VideoDisplay *display)
{
SDL_Mouse *mouse = SDL_GetMouse();

View File

@ -44,8 +44,6 @@ static SDL_Cursor *RPI_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y);
static int RPI_ShowCursor(SDL_Cursor *cursor);
static void RPI_MoveCursor(SDL_Cursor *cursor);
static void RPI_FreeCursor(SDL_Cursor *cursor);
static void RPI_WarpMouse(SDL_Window *window, int x, int y);
static int RPI_WarpMouseGlobal(int x, int y);
static SDL_Cursor *global_cursor;
@ -223,14 +221,61 @@ static void RPI_FreeCursor(SDL_Cursor *cursor)
}
}
/* Warp the mouse to (x,y) */
static void RPI_WarpMouse(SDL_Window *window, int x, int y)
static int RPI_WarpMouseGlobalGraphically(float x, float y)
{
RPI_WarpMouseGlobal(x, y);
RPI_CursorData *curdata;
DISPMANX_UPDATE_HANDLE_T update;
int ret;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
SDL_Mouse *mouse = SDL_GetMouse();
if (mouse == NULL || mouse->cur_cursor == NULL || mouse->cur_cursor->driverdata == NULL) {
return 0;
}
curdata = (RPI_CursorData *)mouse->cur_cursor->driverdata;
if (curdata->element == DISPMANX_NO_HANDLE) {
return 0;
}
update = vc_dispmanx_update_start(0);
if (!update) {
return 0;
}
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = curdata->w << 16;
src_rect.height = curdata->h << 16;
dst_rect.x = (int)x - curdata->hot_x;
dst_rect.y = (int)y - curdata->hot_y;
dst_rect.width = curdata->w;
dst_rect.height = curdata->h;
ret = vc_dispmanx_element_change_attributes(
update,
curdata->element,
0,
0,
0,
&dst_rect,
&src_rect,
DISPMANX_NO_HANDLE,
DISPMANX_NO_ROTATE);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
}
/* Submit asynchronously, otherwise the peformance suffers a lot */
ret = vc_dispmanx_update_submit(update, 0, NULL);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_update_submit() failed");
}
return 0;
}
/* Warp the mouse to (x,y) */
static int RPI_WarpMouseGlobal(int x, int y)
static int RPI_WarpMouseGlobal(float x, float y)
{
RPI_CursorData *curdata;
DISPMANX_UPDATE_HANDLE_T update;
@ -246,100 +291,12 @@ static int RPI_WarpMouseGlobal(int x, int y)
/* Update internal mouse position. */
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 0, x, y);
curdata = (RPI_CursorData *)mouse->cur_cursor->driverdata;
if (curdata->element == DISPMANX_NO_HANDLE) {
return 0;
}
update = vc_dispmanx_update_start(0);
if (!update) {
return 0;
}
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = curdata->w << 16;
src_rect.height = curdata->h << 16;
dst_rect.x = x - curdata->hot_x;
dst_rect.y = y - curdata->hot_y;
dst_rect.width = curdata->w;
dst_rect.height = curdata->h;
ret = vc_dispmanx_element_change_attributes(
update,
curdata->element,
0,
0,
0,
&dst_rect,
&src_rect,
DISPMANX_NO_HANDLE,
DISPMANX_NO_ROTATE);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
}
/* Submit asynchronously, otherwise the peformance suffers a lot */
ret = vc_dispmanx_update_submit(update, 0, NULL);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_update_submit() failed");
}
return 0;
return RPI_WarpMouseGlobalGraphically(x, y);
}
/* Warp the mouse to (x,y) */
static int RPI_WarpMouseGlobalGraphicOnly(int x, int y)
static void RPI_WarpMouse(SDL_Window *window, float x, float y)
{
RPI_CursorData *curdata;
DISPMANX_UPDATE_HANDLE_T update;
int ret;
VC_RECT_T dst_rect;
VC_RECT_T src_rect;
SDL_Mouse *mouse = SDL_GetMouse();
if (mouse == NULL || mouse->cur_cursor == NULL || mouse->cur_cursor->driverdata == NULL) {
return 0;
}
curdata = (RPI_CursorData *)mouse->cur_cursor->driverdata;
if (curdata->element == DISPMANX_NO_HANDLE) {
return 0;
}
update = vc_dispmanx_update_start(0);
if (!update) {
return 0;
}
src_rect.x = 0;
src_rect.y = 0;
src_rect.width = curdata->w << 16;
src_rect.height = curdata->h << 16;
dst_rect.x = x - curdata->hot_x;
dst_rect.y = y - curdata->hot_y;
dst_rect.width = curdata->w;
dst_rect.height = curdata->h;
ret = vc_dispmanx_element_change_attributes(
update,
curdata->element,
0,
0,
0,
&dst_rect,
&src_rect,
DISPMANX_NO_HANDLE,
DISPMANX_NO_ROTATE);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_element_change_attributes() failed");
}
/* Submit asynchronously, otherwise the peformance suffers a lot */
ret = vc_dispmanx_update_submit(update, 0, NULL);
if (ret != DISPMANX_SUCCESS) {
return SDL_SetError("vc_dispmanx_update_submit() failed");
}
return 0;
RPI_WarpMouseGlobal(x, y);
}
void RPI_InitMouse(_THIS)
@ -369,7 +326,7 @@ static void RPI_MoveCursor(SDL_Cursor *cursor)
SDL_Mouse *mouse = SDL_GetMouse();
/* We must NOT call SDL_SendMouseMotion() on the next call or we will enter recursivity,
* so we create a version of WarpMouseGlobal without it. */
RPI_WarpMouseGlobalGraphicOnly(mouse->x, mouse->y);
RPI_WarpMouseGlobalGraphically(mouse->x, mouse->y);
}
#endif /* SDL_VIDEO_DRIVER_RPI */

View File

@ -126,7 +126,7 @@ void RISCOS_PollMouse(_THIS)
buttons = regs.r[2];
if (mouse->x != x || mouse->y != y) {
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 0, x, y);
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 0, (float)x, (float)y);
}
if (driverdata->last_mouse_buttons != buttons) {

View File

@ -320,7 +320,7 @@ static void OnGCMouseConnected(GCMouse *mouse) API_AVAILABLE(macos(11.0), ios(14
mouse.mouseInput.mouseMovedHandler = ^(GCMouseInput *mouseInput, float deltaX, float deltaY) {
if (SDL_GCMouseRelativeMode()) {
SDL_SendMouseMotion(0, SDL_GetMouseFocus(), mouseID, 1, (int)deltaX, -(int)deltaY);
SDL_SendMouseMotion(0, SDL_GetMouseFocus(), mouseID, 1, deltaX, -deltaY);
}
};

View File

@ -158,7 +158,7 @@ extern int SDL_AppleTVRemoteOpenedAsJoystick;
point.x -= origin.x;
point.y -= origin.y;
SDL_SendMouseMotion(0, sdlwindow, 0, 0, (int)point.x, (int)point.y);
SDL_SendMouseMotion(0, sdlwindow, 0, 0, point.x, point.y);
}
return [UIPointerRegion regionWithRect:self.bounds identifier:nil];
}

View File

@ -74,7 +74,7 @@ void VITA_PollMouse(void)
prev_buttons = m_reports[i].buttons;
if (m_reports[i].rel_x || m_reports[i].rel_y) {
SDL_SendMouseMotion(0, Vita_Window, 0, 1, m_reports[i].rel_x, m_reports[i].rel_y);
SDL_SendMouseMotion(0, Vita_Window, 0, 1, (float)m_reports[i].rel_x, (float)m_reports[i].rel_y);
}
}
}

View File

@ -500,10 +500,8 @@ static void pointer_handle_motion(void *data, struct wl_pointer *pointer,
input->sx_w = sx_w;
input->sy_w = sy_w;
if (input->pointer_focus) {
const float sx_f = (float)wl_fixed_to_double(sx_w);
const float sy_f = (float)wl_fixed_to_double(sy_w);
const int sx = (int)SDL_floorf(sx_f * window->pointer_scale_x);
const int sy = (int)SDL_floorf(sy_f * window->pointer_scale_y);
float sx = (float)(wl_fixed_to_double(sx_w) * window->pointer_scale_x);
float sy = (float)(wl_fixed_to_double(sy_w) * window->pointer_scale_y);
SDL_SendMouseMotion(Wayland_GetPointerTimestamp(input, time), window->sdlwindow, 0, 0, sx, sy);
}
}
@ -2215,10 +2213,8 @@ static void tablet_tool_handle_motion(void *data, struct zwp_tablet_tool_v2 *too
input->sx_w = sx_w;
input->sy_w = sy_w;
if (input->tool_focus) {
const float sx_f = (float)wl_fixed_to_double(sx_w);
const float sy_f = (float)wl_fixed_to_double(sy_w);
const int sx = (int)SDL_floorf(sx_f * window->pointer_scale_x);
const int sy = (int)SDL_floorf(sy_f * window->pointer_scale_y);
float sx = (float)(wl_fixed_to_double(sx_w) * window->pointer_scale_x);
float sy = (float)(wl_fixed_to_double(sy_w) * window->pointer_scale_y);
SDL_SendMouseMotion(0, window->sdlwindow, 0, 0, sx, sy);
}
}
@ -2572,21 +2568,12 @@ static void relative_pointer_handle_relative_motion(void *data,
SDL_WindowData *window = input->pointer_focus;
double dx_unaccel;
double dy_unaccel;
double dx;
double dy;
dx_unaccel = wl_fixed_to_double(dx_unaccel_w);
dy_unaccel = wl_fixed_to_double(dy_unaccel_w);
/* Add left over fraction from last event. */
dx_unaccel += input->dx_frac;
dy_unaccel += input->dy_frac;
input->dx_frac = modf(dx_unaccel, &dx);
input->dy_frac = modf(dy_unaccel, &dy);
if (input->pointer_focus && d->relative_mouse_mode) {
SDL_SendMouseMotion(0, window->sdlwindow, 0, 1, (int)dx, (int)dy);
SDL_SendMouseMotion(0, window->sdlwindow, 0, 1, (float)dx_unaccel, (float)dy_unaccel);
}
}

View File

@ -110,9 +110,6 @@ struct SDL_WaylandInput
uint32_t buttons_pressed;
double dx_frac;
double dy_frac;
struct
{
struct xkb_keymap *keymap;

View File

@ -525,7 +525,7 @@ static int Wayland_ShowCursor(SDL_Cursor *cursor)
return 0;
}
static void Wayland_WarpMouse(SDL_Window *window, int x, int y)
static void Wayland_WarpMouse(SDL_Window *window, float x, float y)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = vd->driverdata;
@ -543,11 +543,6 @@ static void Wayland_WarpMouse(SDL_Window *window, int x, int y)
}
}
static int Wayland_WarpMouseGlobal(int x, int y)
{
return SDL_Unsupported();
}
static int Wayland_SetRelativeMouseMode(SDL_bool enabled)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
@ -646,7 +641,6 @@ void Wayland_InitMouse(void)
mouse->ShowCursor = Wayland_ShowCursor;
mouse->FreeCursor = Wayland_FreeCursor;
mouse->WarpMouse = Wayland_WarpMouse;
mouse->WarpMouseGlobal = Wayland_WarpMouseGlobal;
mouse->SetRelativeMouseMode = Wayland_SetRelativeMouseMode;
input->relative_mode_override = SDL_FALSE;

View File

@ -517,12 +517,10 @@ static void WIN_UpdateFocus(SDL_Window *window, SDL_bool expect_focus)
/* In relative mode we are guaranteed to have mouse focus if we have keyboard focus */
if (!SDL_GetMouse()->relative_mode) {
SDL_Point point;
SDL_FPoint point;
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
point.x = cursorPos.x;
point.y = cursorPos.y;
WIN_ClientPointToSDL(data->window, &point.x, &point.y);
WIN_ClientPointToSDLFloat(data->window, cursorPos.x, cursorPos.y, &point.x, &point.y);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), window, 0, 0, point.x, point.y);
}
@ -830,10 +828,9 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
/* Only generate mouse events for real mouse */
if (GetMouseMessageSource() != SDL_MOUSE_EVENT_SOURCE_TOUCH &&
lParam != data->last_pointer_update) {
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
float x, y;
WIN_ClientPointToSDL(data->window, &x, &y);
WIN_ClientPointToSDLFloat(data->window, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam), &x, &y);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, 0, 0, x, y);
}
@ -897,7 +894,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
rawmouse = &inp.data.mouse;
if ((rawmouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE) {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (int)rawmouse->lLastX, (int)rawmouse->lLastY);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (float)rawmouse->lLastX, (float)rawmouse->lLastY);
} else if (rawmouse->lLastX || rawmouse->lLastY) {
/* This is absolute motion, either using a tablet or mouse over RDP
@ -953,7 +950,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
const int MAX_RELATIVE_MOTION = (h / 6);
if (SDL_abs(relX) < MAX_RELATIVE_MOTION &&
SDL_abs(relY) < MAX_RELATIVE_MOTION) {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, relX, relY);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (float)relX, (float)relY);
}
}
}
@ -963,7 +960,7 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
SDL_abs(relY) > MAXIMUM_TABLET_RELATIVE_MOTION) {
/* Ignore this motion, probably a pen lift and drop */
} else {
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, relX, relY);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 1, (float)relX, (float)relY);
}
}
@ -991,12 +988,10 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
if (SDL_GetMouseFocus() == data->window && !SDL_GetMouse()->relative_mode && !IsIconic(hwnd)) {
SDL_Mouse *mouse;
POINT cursorPos;
SDL_Point point;
SDL_FPoint point;
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
point.x = cursorPos.x;
point.y = cursorPos.y;
WIN_ClientPointToSDL(data->window, &point.x, &point.y);
WIN_ClientPointToSDLFloat(data->window, cursorPos.x, cursorPos.y, &point.x, &point.y);
mouse = SDL_GetMouse();
if (!mouse->was_touch_mouse_events) { /* we're not a touch handler causing a mouse leave? */
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, 0, 0, point.x, point.y);
@ -1783,10 +1778,8 @@ static void WIN_UpdateMouseCapture()
if (GetCursorPos(&cursorPos) && ScreenToClient(data->hwnd, &cursorPos)) {
SDL_bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
SDL_MouseID mouseID = SDL_GetMouse()->mouseID;
SDL_Point point;
point.x = cursorPos.x;
point.y = cursorPos.y;
WIN_ClientPointToSDL(data->window, &point.x, &point.y);
SDL_FPoint point;
WIN_ClientPointToSDLFloat(data->window, cursorPos.x, cursorPos.y, &point.x, &point.y);
SDL_SendMouseMotion(WIN_GetEventTimestamp(), data->window, mouseID, 0, point.x, point.y);
SDL_SendMouseButton(WIN_GetEventTimestamp(), data->window, mouseID, GetAsyncKeyState(VK_LBUTTON) & 0x8000 ? SDL_PRESSED : SDL_RELEASED, !swapButtons ? SDL_BUTTON_LEFT : SDL_BUTTON_RIGHT);

View File

@ -585,39 +585,47 @@ int WIN_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect)
* Returns the DPI of the monitor that was closest to x, y and used for the conversion.
*/
void WIN_ScreenPointFromSDL(int *x, int *y, int *dpiOut)
{
POINT pt = { 0, 0 };
WIN_ScreenPointFromSDLFloat((float)*x, (float)*y, &pt.x, &pt.y, dpiOut);
*x = pt.x;
*y = pt.y;
}
void WIN_ScreenPointFromSDLFloat(float x, float y, LONG *xOut, LONG *yOut, int *dpiOut)
{
const SDL_VideoDevice *videodevice = SDL_GetVideoDevice();
const SDL_VideoData *videodata;
int displayIndex;
SDL_Rect bounds;
float ddpi, hdpi, vdpi;
int x_sdl, y_sdl;
SDL_Point point;
point.x = *x;
point.y = *y;
point.x = (int)x;
point.y = (int)y;
if (dpiOut) {
*dpiOut = 96;
}
if (videodevice == NULL || !videodevice->driverdata) {
return;
goto passthrough;
}
videodata = (SDL_VideoData *)videodevice->driverdata;
if (!videodata->dpi_scaling_enabled) {
return;
goto passthrough;
}
/* Can't use MonitorFromPoint for this because we currently have SDL coordinates, not pixels */
displayIndex = SDL_GetDisplayIndexForPoint(&point);
if (displayIndex < 0) {
return;
goto passthrough;
}
if (SDL_GetDisplayBounds(displayIndex, &bounds) < 0 || SDL_GetDisplayDPI(displayIndex, &ddpi, &hdpi, &vdpi) < 0) {
return;
goto passthrough;
}
if (dpiOut) {
@ -625,15 +633,18 @@ void WIN_ScreenPointFromSDL(int *x, int *y, int *dpiOut)
}
/* Undo the DPI-scaling within the monitor bounds to convert back to pixels */
x_sdl = *x;
y_sdl = *y;
*x = bounds.x + MulDiv(x_sdl - bounds.x, (int)ddpi, 96);
*y = bounds.y + MulDiv(y_sdl - bounds.y, (int)ddpi, 96);
*xOut = bounds.x + SDL_lroundf(((x - bounds.x) * ddpi) / 96.0f);
*yOut = bounds.y + SDL_lroundf(((y - bounds.y) * ddpi) / 96.0f);
#ifdef HIGHDPI_DEBUG_VERBOSE
SDL_Log("WIN_ScreenPointFromSDL: (%d, %d) points -> (%d x %d) pixels, using %d DPI monitor",
x_sdl, y_sdl, *x, *y, (int)ddpi);
SDL_Log("WIN_ScreenPointFromSDL: (%g, %g) points -> (%d x %d) pixels, using %g DPI monitor",
x, y, *xOut, *yOut, ddpi);
#endif
return;
passthrough:
*xOut = SDL_lroundf(x);
*yOut = SDL_lroundf(y);
}
/**
@ -643,6 +654,14 @@ void WIN_ScreenPointFromSDL(int *x, int *y, int *dpiOut)
* No-op if DPI scaling is not enabled.
*/
void WIN_ScreenPointToSDL(int *x, int *y)
{
SDL_FPoint pt;
WIN_ScreenPointToSDLFloat(*x, *y, &pt.x, &pt.y);
*x = SDL_lroundf(pt.x);
*y = SDL_lroundf(pt.y);
}
void WIN_ScreenPointToSDLFloat(LONG x, LONG y, float *xOut, float *yOut)
{
const SDL_VideoDevice *videodevice = SDL_GetVideoDevice();
const SDL_VideoData *videodata;
@ -651,7 +670,6 @@ void WIN_ScreenPointToSDL(int *x, int *y)
int i, displayIndex;
SDL_Rect bounds;
float ddpi, hdpi, vdpi;
int x_pixels, y_pixels;
if (videodevice == NULL || !videodevice->driverdata) {
return;
@ -659,11 +677,13 @@ void WIN_ScreenPointToSDL(int *x, int *y)
videodata = (SDL_VideoData *)videodevice->driverdata;
if (!videodata->dpi_scaling_enabled) {
*xOut = (float)x;
*yOut = (float)y;
return;
}
point.x = *x;
point.y = *y;
point.x = x;
point.y = y;
monitor = MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST);
/* Search for the corresponding SDL monitor */
@ -684,14 +704,12 @@ void WIN_ScreenPointToSDL(int *x, int *y)
}
/* Convert the point's offset within the monitor from pixels to DPI-scaled points */
x_pixels = *x;
y_pixels = *y;
*x = bounds.x + MulDiv(x_pixels - bounds.x, 96, (int)ddpi);
*y = bounds.y + MulDiv(y_pixels - bounds.y, 96, (int)ddpi);
*xOut = (float)bounds.x + ((float)(x - bounds.x) * 96.0f) / ddpi;
*yOut = (float)bounds.y + ((float)(y - bounds.y) * 96.0f) / ddpi;
#ifdef HIGHDPI_DEBUG_VERBOSE
SDL_Log("WIN_ScreenPointToSDL: (%d, %d) pixels -> (%d x %d) points, using %d DPI monitor",
x_pixels, y_pixels, *x, *y, (int)ddpi);
SDL_Log("WIN_ScreenPointToSDL: (%d, %d) pixels -> (%g x %g) points, using %g DPI monitor",
x, y, *xOut, *yOut, ddpi);
#endif
}

View File

@ -40,7 +40,9 @@ extern int WIN_InitModes(_THIS);
extern int WIN_GetDisplayBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect);
extern int WIN_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay *display, SDL_Rect *rect);
extern void WIN_ScreenPointFromSDL(int *x, int *y, int *dpiOut);
extern void WIN_ScreenPointFromSDLFloat(float x, float y, LONG *xOut, LONG *yOut, int *dpiOut);
extern void WIN_ScreenPointToSDL(int *x, int *y);
extern void WIN_ScreenPointToSDLFloat(LONG x, LONG y, float *xOut, float *yOut);
extern int WIN_GetDisplayDPI(_THIS, SDL_VideoDisplay *display, float *ddpi, float *hdpi, float *vdpi);
extern void WIN_GetDisplayModes(_THIS, SDL_VideoDisplay *display);
extern int WIN_SetDisplayMode(_THIS, SDL_VideoDisplay *display, SDL_DisplayMode *mode);

View File

@ -277,7 +277,7 @@ void WIN_SetCursorPos(int x, int y)
}
}
static void WIN_WarpMouse(SDL_Window *window, int x, int y)
static void WIN_WarpMouse(SDL_Window *window, float x, float y)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
HWND hwnd = data->hwnd;
@ -288,8 +288,7 @@ static void WIN_WarpMouse(SDL_Window *window, int x, int y)
return;
}
pt.x = x;
pt.y = y;
WIN_ClientPointFromSDLFloat(window, x, y, &pt.x, &pt.y);
ClientToScreen(hwnd, &pt);
WIN_SetCursorPos(pt.x, pt.y);
@ -297,13 +296,11 @@ static void WIN_WarpMouse(SDL_Window *window, int x, int y)
SDL_SendMouseMotion(0, window, SDL_GetMouse()->mouseID, 0, x, y);
}
static int WIN_WarpMouseGlobal(int x, int y)
static int WIN_WarpMouseGlobal(float x, float y)
{
POINT pt;
WIN_ScreenPointFromSDL(&x, &y, NULL);
pt.x = x;
pt.y = y;
WIN_ScreenPointFromSDLFloat(x, y, &pt.x, &pt.y, NULL);
SetCursorPos(pt.x, pt.y);
return 0;
}
@ -333,16 +330,14 @@ static int WIN_CaptureMouse(SDL_Window *window)
return 0;
}
static Uint32 WIN_GetGlobalMouseState(int *x, int *y)
static Uint32 WIN_GetGlobalMouseState(float *x, float *y)
{
Uint32 retval = 0;
POINT pt = { 0, 0 };
SDL_bool swapButtons = GetSystemMetrics(SM_SWAPBUTTON) != 0;
GetCursorPos(&pt);
*x = (int)pt.x;
*y = (int)pt.y;
WIN_ScreenPointToSDL(x, y);
WIN_ScreenPointToSDLFloat(pt.x, pt.y, x, y);
retval |= GetAsyncKeyState(!swapButtons ? VK_LBUTTON : VK_RBUTTON) & 0x8000 ? SDL_BUTTON_LMASK : 0;
retval |= GetAsyncKeyState(!swapButtons ? VK_RBUTTON : VK_LBUTTON) & 0x8000 ? SDL_BUTTON_RMASK : 0;

View File

@ -1357,6 +1357,20 @@ void WIN_ClientPointToSDL(const SDL_Window *window, int *x, int *y)
*y = MulDiv(*y, 96, data->scaling_dpi);
}
void WIN_ClientPointToSDLFloat(const SDL_Window *window, LONG x, LONG y, float *xOut, float *yOut)
{
const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
const SDL_VideoData *videodata = data->videodata;
if (videodata->dpi_scaling_enabled) {
*xOut = (float)(x * 96) / data->scaling_dpi;
*yOut = (float)(y * 96) / data->scaling_dpi;
} else {
*xOut = (float)x;
*yOut = (float)y;
}
}
/**
* Convert a point in the client area from DPI-scaled points to pixels.
*
@ -1375,6 +1389,20 @@ void WIN_ClientPointFromSDL(const SDL_Window *window, int *x, int *y)
*y = MulDiv(*y, data->scaling_dpi, 96);
}
void WIN_ClientPointFromSDLFloat(const SDL_Window *window, float x, float y, LONG *xOut, LONG *yOut)
{
const SDL_WindowData *data = ((SDL_WindowData *)window->driverdata);
const SDL_VideoData *videodata = data->videodata;
if (videodata->dpi_scaling_enabled) {
*xOut = (LONG)SDL_roundf((x * data->scaling_dpi) / 96.0f);
*yOut = (LONG)SDL_roundf((y * data->scaling_dpi) / 96.0f);
} else {
*xOut = (LONG)SDL_roundf(x);
*yOut = (LONG)SDL_roundf(y);
}
}
#if !defined(__XBOXONE__) && !defined(__XBOXSERIES__)
void WIN_AcceptDragAndDrop(SDL_Window *window, SDL_bool accept)
{

View File

@ -104,7 +104,9 @@ extern void WIN_OnWindowEnter(_THIS, SDL_Window *window);
extern void WIN_UpdateClipCursor(SDL_Window *window);
extern int WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled);
extern void WIN_ClientPointToSDL(const SDL_Window *window, int *x, int *y);
extern void WIN_ClientPointToSDLFloat(const SDL_Window *window, LONG x, LONG y, float *xOut, float *yOut);
extern void WIN_ClientPointFromSDL(const SDL_Window *window, int *x, int *y);
extern void WIN_ClientPointFromSDLFloat(const SDL_Window *window, float x, float y, LONG *xOut, LONG *yOut);
extern void WIN_AcceptDragAndDrop(SDL_Window *window, SDL_bool accept);
extern int WIN_FlashWindow(_THIS, SDL_Window *window, SDL_FlashOperation operation);

View File

@ -253,7 +253,7 @@ void WINRT_ProcessPointerMovedEvent(SDL_Window *window, Windows::UI::Input::Poin
SDL_SendMouseButton(0, window, 0, pressed, button);
}
SDL_SendMouseMotion(0, window, 0, 0, (int)windowPoint.X, (int)windowPoint.Y);
SDL_SendMouseMotion(0, window, 0, 0, windowPoint.X, windowPoint.Y);
} else {
SDL_SendTouchMotion(0,
WINRT_TouchID,
@ -390,8 +390,8 @@ void WINRT_ProcessMouseMovedEvent(SDL_Window *window, Windows::Devices::Input::M
window,
0,
1,
SDL_lroundf(mouseDeltaInSDLWindowCoords.X),
SDL_lroundf(mouseDeltaInSDLWindowCoords.Y));
mouseDeltaInSDLWindowCoords.X,
mouseDeltaInSDLWindowCoords.Y);
}
#endif // SDL_VIDEO_DRIVER_WINRT

View File

@ -932,7 +932,7 @@ static void X11_DispatchEvent(_THIS, XEvent *xevent)
#endif
if (!mouse->relative_mode) {
SDL_SendMouseMotion(0, data->window, 0, 0, xevent->xcrossing.x, xevent->xcrossing.y);
SDL_SendMouseMotion(0, data->window, 0, 0, (float)xevent->xcrossing.x, (float)xevent->xcrossing.y);
}
/* We ungrab in LeaveNotify, so we may need to grab again here */
@ -954,7 +954,7 @@ static void X11_DispatchEvent(_THIS, XEvent *xevent)
}
#endif
if (!SDL_GetMouse()->relative_mode) {
SDL_SendMouseMotion(0, data->window, 0, 0, xevent->xcrossing.x, xevent->xcrossing.y);
SDL_SendMouseMotion(0, data->window, 0, 0, (float)xevent->xcrossing.x, (float)xevent->xcrossing.y);
}
if (xevent->xcrossing.mode != NotifyGrab &&
@ -1311,7 +1311,7 @@ static void X11_DispatchEvent(_THIS, XEvent *xevent)
printf("window %p: X11 motion: %d,%d\n", data, xevent->xmotion.x, xevent->xmotion.y);
#endif
SDL_SendMouseMotion(0, data->window, 0, 0, xevent->xmotion.x, xevent->xmotion.y);
SDL_SendMouseMotion(0, data->window, 0, 0, (float)xevent->xmotion.x, (float)xevent->xmotion.y);
}
} break;

View File

@ -324,7 +324,7 @@ static int X11_ShowCursor(SDL_Cursor *cursor)
return 0;
}
static void X11_WarpMouseInternal(Window xwindow, const int x, const int y)
static void X11_WarpMouseInternal(Window xwindow, float x, float y)
{
SDL_VideoData *videodata = (SDL_VideoData *)SDL_GetVideoDevice()->driverdata;
Display *display = videodata->display;
@ -337,17 +337,17 @@ static void X11_WarpMouseInternal(Window xwindow, const int x, const int y)
X11_XIGetClientPointer(display, None, &deviceid);
}
if (deviceid != 0) {
X11_XIWarpPointer(display, deviceid, None, xwindow, 0.0, 0.0, 0, 0, (double)x, (double)y);
X11_XIWarpPointer(display, deviceid, None, xwindow, 0.0, 0.0, 0, 0, x, y);
} else
#endif
{
X11_XWarpPointer(display, None, xwindow, 0, 0, 0, 0, x, y);
X11_XWarpPointer(display, None, xwindow, 0, 0, 0, 0, (int)x, (int)y);
}
X11_XSync(display, False);
videodata->global_mouse_changed = SDL_TRUE;
}
static void X11_WarpMouse(SDL_Window *window, int x, int y)
static void X11_WarpMouse(SDL_Window *window, float x, float y)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
@ -361,7 +361,7 @@ static void X11_WarpMouse(SDL_Window *window, int x, int y)
#endif
}
static int X11_WarpMouseGlobal(int x, int y)
static int X11_WarpMouseGlobal(float x, float y)
{
X11_WarpMouseInternal(DefaultRootWindow(GetDisplay()), x, y);
return 0;
@ -405,7 +405,7 @@ static int X11_CaptureMouse(SDL_Window *window)
return 0;
}
static Uint32 X11_GetGlobalMouseState(int *x, int *y)
static Uint32 X11_GetGlobalMouseState(float *x, float *y)
{
SDL_VideoData *videodata = (SDL_VideoData *)SDL_GetVideoDevice()->driverdata;
Display *display = GetDisplay();
@ -452,8 +452,8 @@ static Uint32 X11_GetGlobalMouseState(int *x, int *y)
SDL_assert(!videodata->global_mouse_changed); /* The pointer wasn't on any X11 screen?! */
*x = videodata->global_mouse_position.x;
*y = videodata->global_mouse_position.y;
*x = (float)videodata->global_mouse_position.x;
*y = (float)videodata->global_mouse_position.y;
return videodata->global_mouse_buttons;
}

View File

@ -306,7 +306,7 @@ int X11_HandleXinput2Event(SDL_VideoData *videodata, XGenericEventCookie *cookie
}
}
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, (int)processed_coords[0], (int)processed_coords[1]);
SDL_SendMouseMotion(0, mouse->focus, mouse->mouseID, 1, (float)processed_coords[0], (float)processed_coords[1]);
devinfo->prev_coords[0] = coords[0];
devinfo->prev_coords[1] = coords[1];
devinfo->prev_time = rawev->time;
@ -347,7 +347,7 @@ int X11_HandleXinput2Event(SDL_VideoData *videodata, XGenericEventCookie *cookie
if (!mouse->relative_mode || mouse->relative_mode_warp) {
SDL_Window *window = xinput2_get_sdlwindow(videodata, xev->event);
if (window) {
SDL_SendMouseMotion(0, window, 0, 0, xev->event_x, xev->event_y);
SDL_SendMouseMotion(0, window, 0, 0, (float)xev->event_x, (float)xev->event_y);
}
}
}

View File

@ -2,6 +2,7 @@
* Mouse test suite
*/
#include <limits.h>
#include <float.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_test.h>
@ -27,8 +28,8 @@ static int mouseStateCheck(Uint32 state)
*/
int mouse_getMouseState(void *arg)
{
int x;
int y;
float x;
float y;
Uint32 state;
/* Pump some events to update mouse state */
@ -41,26 +42,26 @@ int mouse_getMouseState(void *arg)
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
/* Case where x pointer is not NULL */
x = INT_MIN;
x = FLT_MIN;
state = SDL_GetMouseState(&x, NULL);
SDLTest_AssertPass("Call to SDL_GetMouseState(&x, NULL)");
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
/* Case where y pointer is not NULL */
y = INT_MIN;
y = FLT_MIN;
state = SDL_GetMouseState(NULL, &y);
SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, &y)");
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
/* Case where x and y pointer is not NULL */
x = INT_MIN;
y = INT_MIN;
x = FLT_MIN;
y = FLT_MIN;
state = SDL_GetMouseState(&x, &y);
SDLTest_AssertPass("Call to SDL_GetMouseState(&x, &y)");
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
return TEST_COMPLETED;
@ -72,8 +73,8 @@ int mouse_getMouseState(void *arg)
*/
int mouse_getRelativeMouseState(void *arg)
{
int x;
int y;
float x;
float y;
Uint32 state;
/* Pump some events to update mouse state */
@ -86,26 +87,26 @@ int mouse_getRelativeMouseState(void *arg)
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
/* Case where x pointer is not NULL */
x = INT_MIN;
x = FLT_MIN;
state = SDL_GetRelativeMouseState(&x, NULL);
SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, NULL)");
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
/* Case where y pointer is not NULL */
y = INT_MIN;
y = FLT_MIN;
state = SDL_GetRelativeMouseState(NULL, &y);
SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, &y)");
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
/* Case where x and y pointer is not NULL */
x = INT_MIN;
y = INT_MIN;
x = FLT_MIN;
y = FLT_MIN;
state = SDL_GetRelativeMouseState(&x, &y);
SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, &y)");
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
return TEST_COMPLETED;
@ -431,23 +432,24 @@ int mouse_warpMouseInWindow(void *arg)
{
const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
int numPositions = 6;
int xPositions[6];
int yPositions[6];
int x, y, i, j;
float xPositions[6];
float yPositions[6];
float x, y;
int i, j;
SDL_Window *window;
xPositions[0] = -1;
xPositions[1] = 0;
xPositions[2] = 1;
xPositions[3] = w - 1;
xPositions[4] = w;
xPositions[5] = w + 1;
xPositions[3] = (float)w - 1;
xPositions[4] = (float)w;
xPositions[5] = (float)w + 1;
yPositions[0] = -1;
yPositions[1] = 0;
yPositions[2] = 1;
yPositions[3] = h - 1;
yPositions[4] = h;
yPositions[5] = h + 1;
yPositions[3] = (float)h - 1;
yPositions[4] = (float)h;
yPositions[5] = (float)h + 1;
/* Create test window */
window = createMouseSuiteTestWindow();
if (window == NULL) {
@ -455,14 +457,14 @@ int mouse_warpMouseInWindow(void *arg)
}
/* Mouse to random position inside window */
x = SDLTest_RandomIntegerInRange(1, w - 1);
y = SDLTest_RandomIntegerInRange(1, h - 1);
x = (float)SDLTest_RandomIntegerInRange(1, w - 1);
y = (float)SDLTest_RandomIntegerInRange(1, h - 1);
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
/* Same position again */
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
/* Mouse to various boundary positions */
for (i = 0; i < numPositions; i++) {
@ -470,7 +472,7 @@ int mouse_warpMouseInWindow(void *arg)
x = xPositions[i];
y = yPositions[j];
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
/* TODO: add tracking of events and check that each call generates a mouse motion event */
SDL_PumpEvents();
@ -492,7 +494,7 @@ int mouse_warpMouseInWindow(void *arg)
int mouse_getMouseFocus(void *arg)
{
const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
int x, y;
float x, y;
SDL_Window *window;
SDL_Window *focusWindow;
@ -507,10 +509,10 @@ int mouse_getMouseFocus(void *arg)
}
/* Mouse to random position inside window */
x = SDLTest_RandomIntegerInRange(1, w - 1);
y = SDLTest_RandomIntegerInRange(1, h - 1);
x = (float)SDLTest_RandomIntegerInRange(1, w - 1);
y = (float)SDLTest_RandomIntegerInRange(1, h - 1);
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
/* Pump events to update focus state */
SDL_Delay(100);
@ -524,10 +526,10 @@ int mouse_getMouseFocus(void *arg)
SDLTest_AssertCheck(focusWindow == window, "Check returned window value is test window");
/* Mouse to random position outside window */
x = SDLTest_RandomIntegerInRange(-9, -1);
y = SDLTest_RandomIntegerInRange(-9, -1);
x = (float)SDLTest_RandomIntegerInRange(-9, -1);
y = (float)SDLTest_RandomIntegerInRange(-9, -1);
SDL_WarpMouseInWindow(window, x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
/* Clean up test window */
destroyMouseSuiteTestWindow(window);
@ -568,18 +570,18 @@ int mouse_getDefaultCursor(void *arg)
*/
int mouse_getGlobalMouseState(void *arg)
{
int x;
int y;
float x;
float y;
Uint32 state;
x = INT_MIN;
y = INT_MIN;
x = FLT_MIN;
y = FLT_MIN;
/* Get current cursor */
state = SDL_GetGlobalMouseState(&x, &y);
SDLTest_AssertPass("Call to SDL_GetGlobalMouseState()");
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %.f", x);
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %.f", y);
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
return TEST_COMPLETED;

View File

@ -95,8 +95,8 @@ static SDL_Gamepad **gamepads;
static int num_gamepads = 0;
static SDL_Joystick *virtual_joystick = NULL;
static SDL_GamepadAxis virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
static int virtual_axis_start_x;
static int virtual_axis_start_y;
static float virtual_axis_start_x;
static float virtual_axis_start_y;
static SDL_GamepadButton virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
static void UpdateWindowTitle()
@ -424,9 +424,9 @@ static void CloseVirtualGamepad()
}
}
static SDL_GamepadButton FindButtonAtPosition(int x, int y)
static SDL_GamepadButton FindButtonAtPosition(float x, float y)
{
SDL_Point point;
SDL_FPoint point;
int i;
SDL_bool showing_front = ShowingFront();
@ -435,12 +435,12 @@ static SDL_GamepadButton FindButtonAtPosition(int x, int y)
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
SDL_bool on_front = (i < SDL_GAMEPAD_BUTTON_PADDLE1 || i > SDL_GAMEPAD_BUTTON_PADDLE4);
if (on_front == showing_front) {
SDL_Rect rect;
rect.x = button_positions[i].x;
rect.y = button_positions[i].y;
rect.w = BUTTON_SIZE;
rect.h = BUTTON_SIZE;
if (SDL_PointInRect(&point, &rect)) {
SDL_FRect rect;
rect.x = (float)button_positions[i].x;
rect.y = (float)button_positions[i].y;
rect.w = (float)BUTTON_SIZE;
rect.h = (float)BUTTON_SIZE;
if (SDL_PointInRectFloat(&point, &rect)) {
return (SDL_GamepadButton)i;
}
}
@ -448,9 +448,9 @@ static SDL_GamepadButton FindButtonAtPosition(int x, int y)
return SDL_GAMEPAD_BUTTON_INVALID;
}
static SDL_GamepadAxis FindAxisAtPosition(int x, int y)
static SDL_GamepadAxis FindAxisAtPosition(float x, float y)
{
SDL_Point point;
SDL_FPoint point;
int i;
SDL_bool showing_front = ShowingFront();
@ -458,12 +458,12 @@ static SDL_GamepadAxis FindAxisAtPosition(int x, int y)
point.y = y;
for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
if (showing_front) {
SDL_Rect rect;
rect.x = axis_positions[i].x;
rect.y = axis_positions[i].y;
rect.w = AXIS_SIZE;
rect.h = AXIS_SIZE;
if (SDL_PointInRect(&point, &rect)) {
SDL_FRect rect;
rect.x = (float)axis_positions[i].x;
rect.y = (float)axis_positions[i].y;
rect.w = (float)AXIS_SIZE;
rect.h = (float)AXIS_SIZE;
if (SDL_PointInRectFloat(&point, &rect)) {
return (SDL_GamepadAxis)i;
}
}
@ -471,13 +471,13 @@ static SDL_GamepadAxis FindAxisAtPosition(int x, int y)
return SDL_GAMEPAD_AXIS_INVALID;
}
static void VirtualGamepadMouseMotion(int x, int y)
static void VirtualGamepadMouseMotion(float x, float y)
{
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
if (virtual_axis_active != SDL_GAMEPAD_AXIS_INVALID) {
const int MOVING_DISTANCE = 2;
if (SDL_abs(x - virtual_axis_start_x) >= MOVING_DISTANCE ||
SDL_abs(y - virtual_axis_start_y) >= MOVING_DISTANCE) {
const float MOVING_DISTANCE = 2.0f;
if (SDL_fabs(x - virtual_axis_start_x) >= MOVING_DISTANCE ||
SDL_fabs(y - virtual_axis_start_y) >= MOVING_DISTANCE) {
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
}
@ -488,12 +488,12 @@ static void VirtualGamepadMouseMotion(int x, int y)
if (virtual_axis_active == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
virtual_axis_active == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
int range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
float distance = SDL_clamp(((float)y - virtual_axis_start_y) / AXIS_SIZE, 0.0f, 1.0f);
float distance = SDL_clamp((y - virtual_axis_start_y) / AXIS_SIZE, 0.0f, 1.0f);
Sint16 value = (Sint16)(SDL_JOYSTICK_AXIS_MIN + (distance * range));
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, value);
} else {
float distanceX = SDL_clamp(((float)x - virtual_axis_start_x) / AXIS_SIZE, -1.0f, 1.0f);
float distanceY = SDL_clamp(((float)y - virtual_axis_start_y) / AXIS_SIZE, -1.0f, 1.0f);
float distanceX = SDL_clamp((x - virtual_axis_start_x) / AXIS_SIZE, -1.0f, 1.0f);
float distanceY = SDL_clamp((y - virtual_axis_start_y) / AXIS_SIZE, -1.0f, 1.0f);
Sint16 valueX, valueY;
if (distanceX >= 0) {
@ -512,7 +512,7 @@ static void VirtualGamepadMouseMotion(int x, int y)
}
}
static void VirtualGamepadMouseDown(int x, int y)
static void VirtualGamepadMouseDown(float x, float y)
{
SDL_GamepadButton button;
SDL_GamepadAxis axis;
@ -531,7 +531,7 @@ static void VirtualGamepadMouseDown(int x, int y)
}
}
static void VirtualGamepadMouseUp(int x, int y)
static void VirtualGamepadMouseUp(float x, float y)
{
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);

View File

@ -72,18 +72,18 @@ void loop()
if (event.type == SDL_MOUSEMOTION) {
if (event.motion.state) {
int xrel, yrel;
float xrel, yrel;
int window_w, window_h;
SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID);
SDL_GetWindowSize(window, &window_w, &window_h);
xrel = event.motion.xrel;
yrel = event.motion.yrel;
if (event.motion.y < window_h / 2) {
if (event.motion.y < (float)window_h / 2.0f) {
angle += xrel;
} else {
angle -= xrel;
}
if (event.motion.x < window_w / 2) {
if (event.motion.x < (float)window_w / 2.0f) {
angle -= yrel;
} else {
angle += yrel;

View File

@ -39,7 +39,7 @@ static int current_alpha = 255;
static int current_color = 255;
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
int mouse_begin_x = -1, mouse_begin_y = -1;
float mouse_begin_x = -1.0f, mouse_begin_y = -1.0f;
int done;
void DrawPoints(SDL_Renderer *renderer)
@ -86,9 +86,9 @@ void DrawPoints(SDL_Renderer *renderer)
#define MAX_LINES 16
int num_lines = 0;
SDL_Rect lines[MAX_LINES];
SDL_FRect lines[MAX_LINES];
static int
add_line(int x1, int y1, int x2, int y2)
add_line(float x1, float y1, float x2, float y2)
{
if (num_lines >= MAX_LINES) {
return 0;
@ -97,7 +97,7 @@ add_line(int x1, int y1, int x2, int y2)
return 0;
}
SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
SDL_Log("adding line (%g, %g), (%g, %g)\n", x1, y1, x2, y2);
lines[num_lines].x = x1;
lines[num_lines].y = y1;
lines[num_lines].w = x2;
@ -123,16 +123,16 @@ void DrawLines(SDL_Renderer *renderer)
SDL_RenderLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
SDL_RenderLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
} else {
SDL_RenderLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
SDL_RenderLineFloat(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
}
}
}
#define MAX_RECTS 16
int num_rects = 0;
SDL_Rect rects[MAX_RECTS];
SDL_FRect rects[MAX_RECTS];
static int
add_rect(int x1, int y1, int x2, int y2)
add_rect(float x1, float y1, float x2, float y2)
{
if (num_rects >= MAX_RECTS) {
return 0;
@ -142,13 +142,13 @@ add_rect(int x1, int y1, int x2, int y2)
}
if (x1 > x2) {
SWAP(int, x1, x2);
SWAP(float, x1, x2);
}
if (y1 > y2) {
SWAP(int, y1, y2);
SWAP(float, y1, y2);
}
SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
SDL_Log("adding rect (%g, %g), (%g, %g) [%gx%g]\n", x1, y1, x2, y2,
x2 - x1, y2 - y1);
rects[num_rects].x = x1;
@ -163,7 +163,7 @@ static void
DrawRects(SDL_Renderer *renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
SDL_RenderFillRects(renderer, rects, num_rects);
SDL_RenderFillRectsFloat(renderer, rects, num_rects);
}
static void
@ -175,8 +175,8 @@ DrawRectLineIntersections(SDL_Renderer *renderer)
for (i = 0; i < num_rects; i++) {
for (j = 0; j < num_lines; j++) {
int x1, y1, x2, y2;
SDL_Rect r;
float x1, y1, x2, y2;
SDL_FRect r;
r = rects[i];
x1 = lines[j].x;
@ -184,8 +184,8 @@ DrawRectLineIntersections(SDL_Renderer *renderer)
x2 = lines[j].w;
y2 = lines[j].h;
if (SDL_GetRectAndLineIntersection(&r, &x1, &y1, &x2, &y2)) {
SDL_RenderLine(renderer, x1, y1, x2, y2);
if (SDL_GetRectAndLineIntersectionFloat(&r, &x1, &y1, &x2, &y2)) {
SDL_RenderLineFloat(renderer, x1, y1, x2, y2);
}
}
}
@ -200,9 +200,9 @@ DrawRectRectIntersections(SDL_Renderer *renderer)
for (i = 0; i < num_rects; i++) {
for (j = i + 1; j < num_rects; j++) {
SDL_Rect r;
if (SDL_GetRectIntersection(&rects[i], &rects[j], &r)) {
SDL_RenderFillRect(renderer, &r);
SDL_FRect r;
if (SDL_GetRectIntersectionFloat(&rects[i], &rects[j], &r)) {
SDL_RenderFillRectFloat(renderer, &r);
}
}
}
@ -235,16 +235,22 @@ void loop()
if (event.key.keysym.mod & SDL_KMOD_SHIFT) {
num_lines = 0;
} else {
add_line(rand() % 640, rand() % 480, rand() % 640,
rand() % 480);
add_line(
(float)(rand() % 640),
(float)(rand() % 480),
(float)(rand() % 640),
(float)(rand() % 480));
}
break;
case 'r':
if (event.key.keysym.mod & SDL_KMOD_SHIFT) {
num_rects = 0;
} else {
add_rect(rand() % 640, rand() % 480, rand() % 640,
rand() % 480);
add_rect(
(float)(rand() % 640),
(float)(rand() % 480),
(float)(rand() % 640),
(float)(rand() % 480));
}
break;
}

View File

@ -33,7 +33,7 @@ typedef struct _Object
{
struct _Object *next;
int x1, y1, x2, y2;
float x1, y1, x2, y2;
Uint8 r, g, b;
SDL_bool isRect;
@ -56,7 +56,7 @@ void DrawObject(SDL_Renderer *renderer, Object *object)
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
if (object->isRect) {
SDL_Rect rect;
SDL_FRect rect;
if (object->x1 > object->x2) {
rect.x = object->x2;
@ -74,10 +74,9 @@ void DrawObject(SDL_Renderer *renderer, Object *object)
rect.h = object->y2 - object->y1;
}
/* SDL_RenderRect(renderer, &rect); */
SDL_RenderFillRect(renderer, &rect);
SDL_RenderFillRectFloat(renderer, &rect);
} else {
SDL_RenderLine(renderer, object->x1, object->y1, object->x2, object->y2);
SDL_RenderLineFloat(renderer, object->x1, object->y1, object->x2, object->y2);
}
}
@ -113,20 +112,18 @@ void loop(void *arg)
switch (event.type) {
case SDL_MOUSEWHEEL:
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
event.wheel.preciseX *= -1.0f;
event.wheel.preciseY *= -1.0f;
event.wheel.x *= -1;
event.wheel.y *= -1;
}
if (event.wheel.preciseX != 0.0f) {
if (event.wheel.x != 0.0f) {
wheel_x_active = SDL_TRUE;
/* "positive to the right and negative to the left" */
wheel_x += event.wheel.preciseX * 10.0f;
wheel_x += event.wheel.x * 10.0f;
}
if (event.wheel.preciseY != 0.0f) {
if (event.wheel.x != 0.0f) {
wheel_y_active = SDL_TRUE;
/* "positive away from the user and negative towards the user" */
wheel_y -= event.wheel.preciseY * 10.0f;
wheel_y -= event.wheel.x * 10.0f;
}
break;
@ -229,10 +226,10 @@ void loop(void *arg)
/* Mouse wheel */
SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
if (wheel_x_active) {
SDL_RenderLine(renderer, (int)wheel_x, 0, (int)wheel_x, SCREEN_HEIGHT);
SDL_RenderLineFloat(renderer, wheel_x, 0.0f, wheel_x, (float)SCREEN_HEIGHT);
}
if (wheel_y_active) {
SDL_RenderLine(renderer, 0, (int)wheel_y, SCREEN_WIDTH, (int)wheel_y);
SDL_RenderLineFloat(renderer, 0.0f, wheel_y, (float)SCREEN_WIDTH, wheel_y);
}
/* Objects from mouse clicks */

View File

@ -149,7 +149,7 @@ static const Uint32 fps_check_delay = 5000;
SDL_Surface *MooseYUVSurfaces[MOOSEFRAMES_COUNT];
SDL_Texture *MooseTexture = NULL;
SDL_Rect displayrect;
SDL_FRect displayrect;
int window_w;
int window_h;
int paused = 0;
@ -192,7 +192,7 @@ void MoveSprites(SDL_Renderer *renderer)
SDL_UpdateTexture(MooseTexture, NULL, MooseYUVSurfaces[i]->pixels, MooseYUVSurfaces[i]->pitch);
}
SDL_RenderClear(renderer);
SDL_RenderTexture(renderer, MooseTexture, NULL, &displayrect);
SDL_RenderTextureFloat(renderer, MooseTexture, NULL, &displayrect);
SDL_RenderPresent(renderer);
} else {
SDL_Texture *tmp;
@ -209,7 +209,7 @@ void MoveSprites(SDL_Renderer *renderer)
}
SDL_RenderClear(renderer);
SDL_RenderTexture(renderer, tmp, NULL, &displayrect);
SDL_RenderTextureFloat(renderer, tmp, NULL, &displayrect);
SDL_RenderPresent(renderer);
SDL_DestroyTexture(tmp);
}
@ -231,8 +231,10 @@ void loop()
switch (event.type) {
case SDL_WINDOWEVENT_RESIZED:
SDL_SetRenderViewport(renderer, NULL);
displayrect.w = window_w = event.window.data1;
displayrect.h = window_h = event.window.data2;
window_w = event.window.data1;
window_h = event.window.data2;
displayrect.w = (float)window_w;
displayrect.h = (float)window_h;
break;
case SDL_MOUSEBUTTONDOWN:
displayrect.x = event.button.x - window_w / 2;
@ -488,8 +490,8 @@ int main(int argc, char **argv)
displayrect.x = 0;
displayrect.y = 0;
displayrect.w = window_w;
displayrect.h = window_h;
displayrect.w = (float)window_w;
displayrect.h = (float)window_h;
/* Ignore key up events, they don't even get filtered */
SDL_SetEventEnabled(SDL_KEYUP, SDL_FALSE);

View File

@ -23,14 +23,17 @@
#endif
static SDLTest_CommonState *state;
int i, done;
SDL_Rect rect;
SDL_Event event;
static int i, done;
static float mouseX, mouseY;
static SDL_Rect rect;
static SDL_Event event;
static void
DrawRects(SDL_Renderer *renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
rect.x = (int)mouseX;
rect.y = (int)mouseY;
SDL_RenderFillRect(renderer, &rect);
}
@ -43,8 +46,8 @@ loop()
switch (event.type) {
case SDL_MOUSEMOTION:
{
rect.x += event.motion.xrel;
rect.y += event.motion.yrel;
mouseX += event.motion.xrel;
mouseY += event.motion.yrel;
} break;
}
}

View File

@ -64,18 +64,18 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
int text_length;
int x, y;
int table_top;
SDL_Point mouse_pos = { -1, -1 };
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
/* Get mouse position */
if (SDL_GetMouseFocus() == window) {
int window_x, window_y;
float window_x, window_y;
float logical_x, logical_y;
SDL_GetMouseState(&window_x, &window_y);
SDL_RenderWindowToLogical(renderer, window_x, window_y, &logical_x, &logical_y);
mouse_pos.x = (int)logical_x;
mouse_pos.y = (int)logical_y;
mouse_pos.x = logical_x;
mouse_pos.y = logical_y;
}
x = 0;
@ -101,7 +101,7 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
}
for (i = 0; i < num_modes; ++i) {
SDL_Rect cell_rect;
SDL_FRect cell_rect;
if (0 != SDL_GetDisplayMode(display_index, i, &mode)) {
return;
@ -115,12 +115,12 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
column_chars = SDL_max(column_chars, text_length);
/* Check if under mouse */
cell_rect.x = x;
cell_rect.y = y;
cell_rect.w = text_length * FONT_CHARACTER_SIZE;
cell_rect.h = lineHeight;
cell_rect.x = (float)x;
cell_rect.y = (float)y;
cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
cell_rect.h = (float)lineHeight;
if (SDL_PointInRect(&mouse_pos, &cell_rect)) {
if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
/* Update cached mode under the mouse */