SDLPP: mouse position functions

This commit is contained in:
zvon 2021-04-29 19:48:05 +02:00
parent 90dc26251b
commit 4f20274f11
3 changed files with 51 additions and 1 deletions

View File

@ -28,7 +28,7 @@ SDLPPLIBRARY = libsdlpp.dll
LIBRARYFLAGS = LIBRARYFLAGS =
endif endif
OBJECTFILES = sdlpp_circlecolider.${OBJEXT} sdlpp_circlerenderer.${OBJEXT} sdlpp_collision.${OBJEXT} sdlpp_common.${OBJEXT} sdlpp_font.${OBJEXT} sdlpp_linerenderer.${OBJEXT} sdlpp_rectcolider.${OBJEXT} sdlpp_rectrenderer.${OBJEXT} sdlpp_renderer.${OBJEXT} sdlpp_renderobject.${OBJEXT} sdlpp_scene.${OBJEXT} sdlpp_textrenderer.${OBJEXT} sdlpp_texture.${OBJEXT} sdlpp_window.${OBJEXT} sdlpp_fontconfiguration.${OBJEXT} OBJECTFILES = sdlpp_circlecolider.${OBJEXT} sdlpp_circlerenderer.${OBJEXT} sdlpp_collision.${OBJEXT} sdlpp_common.${OBJEXT} sdlpp_font.${OBJEXT} sdlpp_linerenderer.${OBJEXT} sdlpp_rectcolider.${OBJEXT} sdlpp_rectrenderer.${OBJEXT} sdlpp_renderer.${OBJEXT} sdlpp_renderobject.${OBJEXT} sdlpp_scene.${OBJEXT} sdlpp_textrenderer.${OBJEXT} sdlpp_texture.${OBJEXT} sdlpp_window.${OBJEXT} sdlpp_fontconfiguration.${OBJEXT} sdlpp_mouse.${OBJEXT}
all: ${SDLPPLIBRARY} all: ${SDLPPLIBRARY}

31
sdlpp/sdlpp_mouse.cpp Normal file
View File

@ -0,0 +1,31 @@
#include "sdlpp_mouse.hpp"
#include "sdlpp_renderobject.hpp"
SDLPP::Vec2D< int > SDLPP::Mouse::getMousePosition() {
int x, y;
SDL_GetMouseState( &x, &y );
return { x, y };
}
SDLPP::Vec2D< double >
SDLPP::Mouse::getMousePositionDouble( const SDLPP::Renderer &r,
SDLPP::ObjectAlignment alignment_x,
SDLPP::ObjectAlignment alignment_y ) {
auto pixel_pos = getMousePosition();
auto smaller = r.getSmallerSide();
Vec2D< double > ret = { static_cast< double >( pixel_pos.getX() ) / smaller,
static_cast< double >( pixel_pos.getY() ) /
smaller };
Vec2D< double > additions =
( r.getDoubleDimensions() - Vec2D< double >( 1.0, 1.0 ) ) / 2;
switch ( alignment_x ) {
case SDLPP::OBJ_CENTER:
ret -= additions;
break;
case SDLPP::OBJ_END:
ret -= 2 * additions;
default:
break;
}
return ret;
}

19
sdlpp/sdlpp_mouse.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef SDLPP_HPP_MOUSE
#define SDLPP_HPP_MOUSE
#include "sdlpp_common.hpp"
#include "sdlpp_renderer.hpp"
#include "sdlpp_renderobject.hpp"
#include "sdlpp_vector.hpp"
namespace SDLPP {
class SDLPPSCOPE Mouse {
public:
static Vec2D< int > getMousePosition();
static Vec2D< double >
getMousePositionDouble( const Renderer &r, ObjectAlignment alignment_x,
ObjectAlignment alignment_y );
};
} // end of namespace SDLPP
#endif