diff --git a/src/event.c b/src/event.c index 659154e..bc34fb8 100644 --- a/src/event.c +++ b/src/event.c @@ -291,9 +291,25 @@ void event_set_mouse_internal_location(int x, int y) { } SDL_Point event_get_touch_position(const SDL_TouchFingerEvent *e) { +#if XSYSTEM35_SDL_VERSION == 2 int view_w, view_h; gfx_getViewSize(&view_w, &view_h); return (SDL_Point){e->x * view_w, e->y * view_h}; +#else + return (SDL_Point){(int)e->x, (int)e->y}; +#endif +} + +static bool touch_is_on_view_edge(const SDL_TouchFingerEvent *e) { +#if XSYSTEM35_SDL_VERSION == 2 + return e->x == 0.0f || e->x == 1.0f || + e->y == 0.0f || e->y == 1.0f; +#else + int view_w, view_h; + gfx_getViewSize(&view_w, &view_h); + return e->x <= 0.0f || e->x >= view_w || + e->y <= 0.0f || e->y >= view_h; +#endif } // Stores a deferred touch event (valid if .timestamp != 0) to add a delay @@ -345,6 +361,9 @@ static void rance4v2_hack(void) { } void event_handle_event(SDL_Event *e) { + if (!gfx_convertEventCoordinates(e)) + WARNING("Failed to convert event coordinates: %s", SDL_GetError()); + if (modal_handle_event(e)) return; @@ -438,10 +457,10 @@ void event_handle_event(SDL_Event *e) { RawKeyInfo[mouse_to_rawkey(SDL_BUTTON_RIGHT)] = true; send_agsevent(AGSEVENT_BUTTON_PRESS, AGSEVENT_BUTTON_RIGHT); } else { - // SDL_RendererEventWatch clamps touch locations outside of the - // viewport to 0.0-1.0. Treat such events as right-clicks. + // Treat touches on the edge or outside the viewport as + // right-clicks. int button; - if (e->tfinger.x == 0.0f || e->tfinger.x == 1.0f || e->tfinger.y == 0.0f || e->tfinger.y == 1.0f) { + if (touch_is_on_view_edge(&e->tfinger)) { button = SDL_BUTTON_RIGHT; mouseb |= 1 << SDL_BUTTON_RIGHT; RawKeyInfo[mouse_to_rawkey(SDL_BUTTON_RIGHT)] = true; diff --git a/src/gfx.h b/src/gfx.h index 3dc1a2b..d52c5ac 100644 --- a/src/gfx.h +++ b/src/gfx.h @@ -34,6 +34,7 @@ void gfx_setWorldSize(int width, int height, int depth); void gfx_setViewSize(int w, int h); void gfx_getViewSize(int *width, int *height); SDL_Window *gfx_getWindow(void); +bool gfx_convertEventCoordinates(SDL_Event *event); void gfx_warpMouse(int x, int y); SDL_Rect gfx_viewToWindowRect(SDL_Rect rect); void gfx_setWindowTitle(char *name); diff --git a/src/gfx_video.c b/src/gfx_video.c index d0e6471..8a44c57 100644 --- a/src/gfx_video.c +++ b/src/gfx_video.c @@ -180,6 +180,14 @@ SDL_Window *gfx_getWindow(void) { return gfx_window; } +bool gfx_convertEventCoordinates(SDL_Event *event) { +#if XSYSTEM35_SDL_VERSION == 2 + return true; +#else + return SDL_ConvertEventToRenderCoordinates(gfx_renderer, event); +#endif +} + static SDL_Point view_to_window_point(int x, int y) { return sdl_render_coordinates_to_window(gfx_renderer, x, y); } @@ -261,7 +269,7 @@ void gfx_setIntegerScaling(bool enable) { bool EMSCRIPTEN_KEEPALIVE save_screenshot(const char* path) { SDL_Rect *r = &nact->ags.view_area; SDL_Surface *view = gfx_createSurfaceView(main_surface, r->x, r->y, r->w, r->h); - bool ok = SDL_SaveBMP(view, path) == 0; + bool ok = sdl_save_bmp(view, path); sdl_destroy_surface(view); return ok; } diff --git a/src/sdl_compat.h b/src/sdl_compat.h index ff41f26..5e24795 100644 --- a/src/sdl_compat.h +++ b/src/sdl_compat.h @@ -801,6 +801,15 @@ static inline void sdl_destroy_surface(SDL_Surface *surface) #endif } +static inline bool sdl_save_bmp(SDL_Surface *surface, const char *path) +{ +#if XSYSTEM35_SDL_VERSION == 2 + return SDL_SaveBMP(surface, path) == 0; +#else + return SDL_SaveBMP(surface, path); +#endif +} + static inline SDL_Palette *sdl_create_palette(int colors) { #if XSYSTEM35_SDL_VERSION == 2