Scale mouse coordinates by window scale (#1630)

* Update window scale on resize

* Scale mouse coordinates by window scale

* Avoid scaling mouse coordinates while using `LITE_USE_SDL_RENDERER`
This commit is contained in:
Guldoman 2023-11-29 16:21:58 +01:00 committed by takase1121
parent 9272f5ef2d
commit c31a8ae0f6
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
4 changed files with 26 additions and 11 deletions

View File

@ -225,8 +225,8 @@ top:
SDL_GetMouseState(&mx, &my); SDL_GetMouseState(&mx, &my);
lua_pushstring(L, "filedropped"); lua_pushstring(L, "filedropped");
lua_pushstring(L, e.drop.file); lua_pushstring(L, e.drop.file);
lua_pushinteger(L, mx); lua_pushinteger(L, mx * window_renderer.scale_x);
lua_pushinteger(L, my); lua_pushinteger(L, my * window_renderer.scale_y);
SDL_free(e.drop.file); SDL_free(e.drop.file);
return 4; return 4;
@ -283,8 +283,8 @@ top:
if (e.button.button == 1) { SDL_CaptureMouse(1); } if (e.button.button == 1) { SDL_CaptureMouse(1); }
lua_pushstring(L, "mousepressed"); lua_pushstring(L, "mousepressed");
lua_pushstring(L, button_name(e.button.button)); lua_pushstring(L, button_name(e.button.button));
lua_pushinteger(L, e.button.x); lua_pushinteger(L, e.button.x * window_renderer.scale_x);
lua_pushinteger(L, e.button.y); lua_pushinteger(L, e.button.y * window_renderer.scale_y);
lua_pushinteger(L, e.button.clicks); lua_pushinteger(L, e.button.clicks);
return 5; return 5;
@ -292,8 +292,8 @@ top:
if (e.button.button == 1) { SDL_CaptureMouse(0); } if (e.button.button == 1) { SDL_CaptureMouse(0); }
lua_pushstring(L, "mousereleased"); lua_pushstring(L, "mousereleased");
lua_pushstring(L, button_name(e.button.button)); lua_pushstring(L, button_name(e.button.button));
lua_pushinteger(L, e.button.x); lua_pushinteger(L, e.button.x * window_renderer.scale_x);
lua_pushinteger(L, e.button.y); lua_pushinteger(L, e.button.y * window_renderer.scale_y);
return 4; return 4;
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
@ -305,10 +305,10 @@ top:
e.motion.yrel += event_plus.motion.yrel; e.motion.yrel += event_plus.motion.yrel;
} }
lua_pushstring(L, "mousemoved"); lua_pushstring(L, "mousemoved");
lua_pushinteger(L, e.motion.x); lua_pushinteger(L, e.motion.x * window_renderer.scale_x);
lua_pushinteger(L, e.motion.y); lua_pushinteger(L, e.motion.y * window_renderer.scale_y);
lua_pushinteger(L, e.motion.xrel); lua_pushinteger(L, e.motion.xrel * window_renderer.scale_x);
lua_pushinteger(L, e.motion.yrel); lua_pushinteger(L, e.motion.yrel * window_renderer.scale_y);
return 5; return 5;
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:

View File

@ -520,6 +520,7 @@ void ren_init(SDL_Window *win) {
void ren_resize_window(RenWindow *window_renderer) { void ren_resize_window(RenWindow *window_renderer) {
renwin_resize_surface(window_renderer); renwin_resize_surface(window_renderer);
renwin_update_scale(window_renderer);
} }

View File

@ -29,7 +29,8 @@ static void setup_renderer(RenWindow *ren, int w, int h) {
#endif #endif
void renwin_init_surface(UNUSED RenWindow *ren) { void renwin_init_surface(RenWindow *ren) {
ren->scale_x = ren->scale_y = 1;
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
if (ren->rensurface.surface) { if (ren->rensurface.surface) {
SDL_FreeSurface(ren->rensurface.surface); SDL_FreeSurface(ren->rensurface.surface);
@ -95,6 +96,16 @@ void renwin_resize_surface(UNUSED RenWindow *ren) {
#endif #endif
} }
void renwin_update_scale(RenWindow *ren) {
#ifndef LITE_USE_SDL_RENDERER
SDL_Surface *surface = SDL_GetWindowSurface(ren->window);
int window_w = surface->w, window_h = surface->h;
SDL_GetWindowSize(ren->window, &window_w, &window_h);
ren->scale_x = (float)surface->w / window_w;
ren->scale_y = (float)surface->h / window_h;
#endif
}
void renwin_show_window(RenWindow *ren) { void renwin_show_window(RenWindow *ren) {
SDL_ShowWindow(ren->window); SDL_ShowWindow(ren->window);
} }

View File

@ -6,6 +6,8 @@ struct RenWindow {
uint8_t *command_buf; uint8_t *command_buf;
size_t command_buf_idx; size_t command_buf_idx;
size_t command_buf_size; size_t command_buf_size;
float scale_x;
float scale_y;
#ifdef LITE_USE_SDL_RENDERER #ifdef LITE_USE_SDL_RENDERER
SDL_Renderer *renderer; SDL_Renderer *renderer;
SDL_Texture *texture; SDL_Texture *texture;
@ -19,6 +21,7 @@ void renwin_init_command_buf(RenWindow *ren);
void renwin_clip_to_surface(RenWindow *ren); void renwin_clip_to_surface(RenWindow *ren);
void renwin_set_clip_rect(RenWindow *ren, RenRect rect); void renwin_set_clip_rect(RenWindow *ren, RenRect rect);
void renwin_resize_surface(RenWindow *ren); void renwin_resize_surface(RenWindow *ren);
void renwin_update_scale(RenWindow *ren);
void renwin_show_window(RenWindow *ren); void renwin_show_window(RenWindow *ren);
void renwin_update_rects(RenWindow *ren, RenRect *rects, int count); void renwin_update_rects(RenWindow *ren, RenRect *rects, int count);
void renwin_free(RenWindow *ren); void renwin_free(RenWindow *ren);