Skip to content

Commit 606cdcf

Browse files
allefantelias
andauthored
prevent crash when using al_get_mouse_state in the SDL port (liballeg#1425)
* prevent crash * store ratio * -> --------- Co-authored-by: elias <[email protected]>
1 parent 06522b6 commit 606cdcf

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/sdl/sdl_mouse.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ typedef struct ALLEGRO_MOUSE_SDL
2323
ALLEGRO_MOUSE mouse;
2424
ALLEGRO_MOUSE_STATE state;
2525
ALLEGRO_DISPLAY *display;
26+
float ratio;
2627
} ALLEGRO_MOUSE_SDL;
2728

2829
static ALLEGRO_MOUSE_DRIVER *vt;
@@ -63,12 +64,12 @@ void _al_sdl_mouse_event(SDL_Event *e)
6364

6465
if (e->type == SDL_WINDOWEVENT) {
6566
d = find_display(e->window.windowID);
66-
float ratio = _al_sdl_get_display_pixel_ratio(d);
67+
mouse->ratio = _al_sdl_get_display_pixel_ratio(d);
6768
if (e->window.event == SDL_WINDOWEVENT_ENTER) {
6869
event.mouse.type = ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY;
6970
SDL_GetMouseState(&event.mouse.x, &event.mouse.y);
70-
event.mouse.x *= ratio;
71-
event.mouse.y *= ratio;
71+
event.mouse.x *= mouse->ratio;
72+
event.mouse.y *= mouse->ratio;
7273
}
7374
else {
7475
event.mouse.type = ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY;
@@ -155,6 +156,7 @@ static bool sdl_init_mouse(void)
155156
{
156157
mouse = al_calloc(1, sizeof *mouse);
157158
_al_event_source_init(&mouse->mouse.es);
159+
mouse->ratio = 1.0f;
158160
return true;
159161
}
160162

@@ -195,12 +197,11 @@ static bool sdl_set_mouse_axis(int which, int value)
195197
static void sdl_get_mouse_state(ALLEGRO_MOUSE_STATE *ret_state)
196198
{
197199
int x, y, i;
198-
float ratio = _al_sdl_get_display_pixel_ratio(mouse->display);
199200
ALLEGRO_SYSTEM_INTERFACE *sdl = _al_sdl_system_driver();
200201
sdl->heartbeat();
201202
SDL_GetMouseState(&x, &y);
202-
ret_state->x = x * ratio;
203-
ret_state->y = y * ratio;
203+
ret_state->x = x * mouse->ratio;
204+
ret_state->y = y * mouse->ratio;
204205
ret_state->z = 0;
205206
ret_state->w = 0;
206207
for (i = 0; i < ALLEGRO_MOUSE_MAX_EXTRA_AXES; i++)

0 commit comments

Comments
 (0)