From: Ingo Ruhnke Date: Thu, 31 Jul 2014 21:51:14 +0000 (+0200) Subject: Cleaned up coordinate translation a little, SDL mouse handling is still broken and... X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=b3ee516a21c499bb089dbca49b571971977a1583;p=supertux.git Cleaned up coordinate translation a little, SDL mouse handling is still broken and SDL_RenderGetViewport() doesn't seem to return proper values --- diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index 48b1c8b6b..e3fce4b5d 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -30,6 +30,7 @@ #include "util/gettext.hpp" #include "video/drawing_context.hpp" #include "video/font.hpp" +#include "video/renderer.hpp" static const float MENU_REPEAT_INITIAL = 0.4f; static const float MENU_REPEAT_RATE = 0.1f; @@ -770,8 +771,9 @@ Menu::event(const SDL_Event& event) case SDL_MOUSEBUTTONDOWN: if(event.button.button == SDL_BUTTON_LEFT) { - int x = int(event.motion.x * float(SCREEN_WIDTH) / PHYSICAL_SCREEN_WIDTH); - int y = int(event.motion.y * float(SCREEN_HEIGHT) / PHYSICAL_SCREEN_HEIGHT); + Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y); + int x = int(mouse_pos.x); + int y = int(mouse_pos.y); if(x > pos.x - get_width()/2 && x < pos.x + get_width()/2 && @@ -785,8 +787,9 @@ Menu::event(const SDL_Event& event) case SDL_MOUSEMOTION: { - float x = event.motion.x * SCREEN_WIDTH / PHYSICAL_SCREEN_WIDTH; - float y = event.motion.y * SCREEN_HEIGHT / PHYSICAL_SCREEN_HEIGHT; + Vector mouse_pos = Renderer::instance()->to_logical(event.motion.x, event.motion.y); + float x = mouse_pos.x; + float y = mouse_pos.y; if(x > pos.x - get_width()/2 && x < pos.x + get_width()/2 && diff --git a/src/gui/mousecursor.cpp b/src/gui/mousecursor.cpp index 74cc075ba..3248b2b71 100644 --- a/src/gui/mousecursor.cpp +++ b/src/gui/mousecursor.cpp @@ -20,6 +20,8 @@ #include "supertux/globals.hpp" #include "video/drawing_context.hpp" +#include "video/renderer.hpp" +#include "video/sdl/sdl_renderer.hpp" MouseCursor* MouseCursor::current_ = 0; @@ -63,8 +65,10 @@ void MouseCursor::draw(DrawingContext& context) int x,y,w,h; Uint8 ispressed = SDL_GetMouseState(&x,&y); - x = int(x * float(SCREEN_WIDTH) / PHYSICAL_SCREEN_WIDTH); - y = int(y * float(SCREEN_HEIGHT) / PHYSICAL_SCREEN_HEIGHT); + Vector mouse_pos = Renderer::instance()->to_logical(x, y, true); + + x = int(mouse_pos.x); + y = int(mouse_pos.y); w = (int) cursor->get_width(); h = (int) (cursor->get_height() / MC_STATES_NB); diff --git a/src/video/gl/gl_renderer.cpp b/src/video/gl/gl_renderer.cpp index 21ab41a79..c71de0adc 100644 --- a/src/video/gl/gl_renderer.cpp +++ b/src/video/gl/gl_renderer.cpp @@ -667,6 +667,13 @@ GLRenderer::apply_video_mode(const Size& size, bool fullscreen) } } +Vector +GLRenderer::to_logical(int physical_x, int physical_y, bool foobar) +{ + return Vector(physical_x * float(SCREEN_WIDTH) / PHYSICAL_SCREEN_WIDTH, + physical_y * float(SCREEN_HEIGHT) / PHYSICAL_SCREEN_HEIGHT); +} + void GLRenderer::set_gamma(float gamma) { diff --git a/src/video/gl/gl_renderer.hpp b/src/video/gl/gl_renderer.hpp index 1a2e10ea5..1352df920 100644 --- a/src/video/gl/gl_renderer.hpp +++ b/src/video/gl/gl_renderer.hpp @@ -129,6 +129,7 @@ public: void resize(int w, int h); void apply_config(); void apply_video_mode(const Size& size, bool fullscreen); + Vector to_logical(int physical_x, int physical_y, bool foobar); void set_gamma(float gamma); SDL_Window* get_window() const { return window; } }; diff --git a/src/video/renderer.hpp b/src/video/renderer.hpp index 4b67f6ee9..5c9c16bfa 100644 --- a/src/video/renderer.hpp +++ b/src/video/renderer.hpp @@ -52,6 +52,7 @@ public: virtual void flip() = 0; virtual void resize(int w, int h) = 0; virtual void apply_config() = 0; + virtual Vector to_logical(int physical_x, int physical_y, bool foobar = false) = 0; virtual void set_gamma(float gamma) = 0; virtual SDL_Window* get_window() const = 0; diff --git a/src/video/sdl/sdl_renderer.cpp b/src/video/sdl/sdl_renderer.cpp index 106e01326..6f194f3fa 100644 --- a/src/video/sdl/sdl_renderer.cpp +++ b/src/video/sdl/sdl_renderer.cpp @@ -93,6 +93,8 @@ SDLRenderer::SDLRenderer() : if(texture_manager == 0) texture_manager = new TextureManager(); + + apply_config(); } SDLRenderer::~SDLRenderer() @@ -289,13 +291,6 @@ SDLRenderer::apply_config() SCREEN_WIDTH = static_cast(SCREEN_WIDTH * scale); SCREEN_HEIGHT = static_cast(SCREEN_HEIGHT * scale); } - - SDL_Rect viewport; - viewport.x = 0; - viewport.y = 0; - viewport.w = screen_size.width; - viewport.h = screen_size.height; - SDL_RenderSetViewport(renderer, &viewport); } else { @@ -317,24 +312,26 @@ SDLRenderer::apply_config() new_size.height = static_cast((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT); SCREEN_HEIGHT = static_cast(max_size.height); } - - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); - SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); - SDL_RenderClear(renderer); - SDL_RenderPresent(renderer); - SDL_RenderClear(renderer); - - SDL_Rect viewport; - viewport.x = std::max(0, (screen_size.width - new_size.width) / 2); - viewport.y = std::max(0, (screen_size.height - new_size.height) / 2); - viewport.w = std::min(new_size.width, screen_size.width); - viewport.h = std::min(new_size.height, screen_size.height); - SDL_RenderSetViewport(renderer, &viewport); } - SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); } +Vector +SDLRenderer::to_logical(int physical_x, int physical_y, bool foobar) +{ + if (foobar) + { + // SDL translates coordinates automatically, except for SDL_GetMouseState(), thus foobar + return Vector(physical_x * float(SCREEN_WIDTH) / (PHYSICAL_SCREEN_WIDTH), + physical_y * float(SCREEN_HEIGHT) / (PHYSICAL_SCREEN_HEIGHT)); + } + else + { + // SDL is doing the translation internally, so we have nothing to do + return Vector(physical_x, physical_y); + } +} + void SDLRenderer::set_gamma(float gamma) { diff --git a/src/video/sdl/sdl_renderer.hpp b/src/video/sdl/sdl_renderer.hpp index 4c8a62c44..197beb40f 100644 --- a/src/video/sdl/sdl_renderer.hpp +++ b/src/video/sdl/sdl_renderer.hpp @@ -35,6 +35,7 @@ public: void flip(); void resize(int w, int h); void apply_config(); + Vector to_logical(int physical_x, int physical_y, bool foobar); void set_gamma(float gamma); SDL_Window* get_window() const { return window; }