Skip to content

Commit

Permalink
Cleaned up coordinate translation a little, SDL mouse handling is sti…
Browse files Browse the repository at this point in the history
…ll broken and SDL_RenderGetViewport() doesn't seem to return proper values
  • Loading branch information
Grumbel committed Jul 31, 2014
1 parent 48a97f3 commit b3ee516
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 27 deletions.
11 changes: 7 additions & 4 deletions src/gui/menu.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 &&
Expand All @@ -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 &&
Expand Down
8 changes: 6 additions & 2 deletions src/gui/mousecursor.cpp
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/video/gl/gl_renderer.cpp
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions src/video/gl/gl_renderer.hpp
Expand Up @@ -129,6 +129,7 @@ class GLRenderer : public Renderer
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; }
};
Expand Down
1 change: 1 addition & 0 deletions src/video/renderer.hpp
Expand Up @@ -52,6 +52,7 @@ class Renderer
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;

Expand Down
39 changes: 18 additions & 21 deletions src/video/sdl/sdl_renderer.cpp
Expand Up @@ -93,6 +93,8 @@ SDLRenderer::SDLRenderer() :

if(texture_manager == 0)
texture_manager = new TextureManager();

apply_config();
}

SDLRenderer::~SDLRenderer()
Expand Down Expand Up @@ -289,13 +291,6 @@ SDLRenderer::apply_config()
SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
SCREEN_HEIGHT = static_cast<int>(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
{
Expand All @@ -317,24 +312,26 @@ SDLRenderer::apply_config()
new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
SCREEN_HEIGHT = static_cast<int>(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)
{
Expand Down
1 change: 1 addition & 0 deletions src/video/sdl/sdl_renderer.hpp
Expand Up @@ -35,6 +35,7 @@ class SDLRenderer : public Renderer
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; }

Expand Down

0 comments on commit b3ee516

Please sign in to comment.