Added mouse position getters

This commit is contained in:
Robert 2020-05-19 15:20:25 +02:00
parent 5daae26a12
commit c4c81f35f6
6 changed files with 46 additions and 9 deletions

View file

@ -7,6 +7,8 @@
#pragma once #pragma once
#include <cstdint> #include <cstdint>
#define PI 3.1415926f
#define IS_NULLPTR( x ) (x == nullptr) #define IS_NULLPTR( x ) (x == nullptr)
#define RETURN_IF_NULLPTR( x, ... ) { if(IS_NULLPTR(x)) return __VA_ARGS__; } #define RETURN_IF_NULLPTR( x, ... ) { if(IS_NULLPTR(x)) return __VA_ARGS__; }

View file

@ -61,7 +61,7 @@ namespace sdlu
OnClose(); OnClose();
} }
bool RenderWindow::IsOpen() bool RenderWindow::IsOpen() const
{ {
RETURN_IF_NULLPTR(m_pWindow, false); RETURN_IF_NULLPTR(m_pWindow, false);
return (!SDL_GetWindowID(m_pWindow) ? false : true); return (!SDL_GetWindowID(m_pWindow) ? false : true);
@ -91,7 +91,7 @@ namespace sdlu
return true; return true;
} }
Vector2i RenderWindow::GetPosition() Vector2i RenderWindow::GetPosition() const
{ {
RETURN_IF_NULLPTR(m_pWindow, Vector2i()); RETURN_IF_NULLPTR(m_pWindow, Vector2i());
@ -114,7 +114,7 @@ namespace sdlu
SDL_SetWindowPosition(m_pWindow, x, y); SDL_SetWindowPosition(m_pWindow, x, y);
} }
Vector2u RenderWindow::GetSize() Vector2u RenderWindow::GetSize() const
{ {
RETURN_IF_NULLPTR(m_pWindow, Vector2u()); RETURN_IF_NULLPTR(m_pWindow, Vector2u());
@ -137,7 +137,7 @@ namespace sdlu
SDL_SetWindowSize(m_pWindow, width, height); SDL_SetWindowSize(m_pWindow, width, height);
} }
std::string RenderWindow::GetTitle() std::string RenderWindow::GetTitle() const
{ {
RETURN_IF_NULLPTR(m_pWindow, ""); RETURN_IF_NULLPTR(m_pWindow, "");

View file

@ -14,6 +14,10 @@
namespace sdlu namespace sdlu
{ {
// TODO: Probably break up into sdlu::Window and sdlu::Renderer
// to avoid passing around the Renderer when only the Window is
// needed. (See Mouse::GetPosition for example)
/** /**
* @brief A class that handles window related functionality * @brief A class that handles window related functionality
* *
@ -66,7 +70,7 @@ namespace sdlu
* *
* @return True if the window is open, False if not * @return True if the window is open, False if not
*/ */
bool IsOpen(); bool IsOpen() const;
/** /**
* @brief A non-blocking event polling function * @brief A non-blocking event polling function
@ -90,7 +94,7 @@ namespace sdlu
* *
* @return A vector with the current position relative to the top left corner of the display * @return A vector with the current position relative to the top left corner of the display
*/ */
Vector2i GetPosition(); Vector2i GetPosition() const;
/** /**
* @brief Sets a new window position * @brief Sets a new window position
@ -113,7 +117,7 @@ namespace sdlu
* *
* @return A vector with the windows size * @return A vector with the windows size
*/ */
Vector2u GetSize(); Vector2u GetSize() const;
/** /**
* @brief Sets a new window size * @brief Sets a new window size
@ -136,7 +140,7 @@ namespace sdlu
* *
* @return The title of the widnow * @return The title of the widnow
*/ */
std::string GetTitle(); std::string GetTitle() const;
/** /**
* @brief Sets a new window title * @brief Sets a new window title

View file

@ -13,4 +13,16 @@ namespace sdlu
{ {
return (GetButtonState() & SDL_BUTTON(button)); return (GetButtonState() & SDL_BUTTON(button));
} }
Vector2i Mouse::GetPosition()
{
int x = 0, y = 0;
SDL_GetGlobalMouseState(&x, &y);
return Vector2i(x, y);
}
Vector2i Mouse::GetPosition(const RenderWindow& relativeTo)
{
return GetPosition() - relativeTo.GetPosition();
}
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <SDL_mouse.h> #include <SDL_mouse.h>
#include <structures/Vector2.hpp> #include <structures/Vector2.hpp>
#include <graphics/RenderWindow.hpp>
namespace sdlu namespace sdlu
{ {
@ -42,5 +43,20 @@ namespace sdlu
* @return True if the button is pressed * @return True if the button is pressed
*/ */
static bool IsButtonDown(Button button); static bool IsButtonDown(Button button);
/**
* @brief Gets the absolute position of the mouse
*
* @return Current mouse position relative to screen
*/
static Vector2i GetPosition();
/**
* @brief Gets current relative position of the mouse
*
* @param[in] relativeTo The window the mouse position should be relative to
* @return The position of the mouse relative to the top left of the passed window object
*/
static Vector2i GetPosition(const RenderWindow& relativeTo);
}; };
} }

View file

@ -49,7 +49,10 @@ int main(int argc, char** argv)
} }
} }
window.Clear(sdlu::Color::FromHSV(std::floor(t), 100, 100)); sdlu::Vector2i mousePos = sdlu::Mouse::GetPosition(window) - sdlu::Vector2i(400, 400);
// TODO: Check HSV color conversion, the SV values seem to be ignored
window.Clear(sdlu::Color::FromHSV(std::atan2(mousePos.y, mousePos.x) / PI * 180 + 180,
100, 100));
window.Display(); window.Display();
t += 0.08; t += 0.08;