From c4c81f35f6e9745b85ef0d8f82a6daec2aaeebf0 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 19 May 2020 15:20:25 +0200 Subject: [PATCH] Added mouse position getters --- SDLU/Util.hpp | 2 ++ SDLU/graphics/RenderWindow.cpp | 8 ++++---- SDLU/graphics/RenderWindow.hpp | 12 ++++++++---- SDLU/structures/Mouse.cpp | 12 ++++++++++++ SDLU/structures/Mouse.hpp | 16 ++++++++++++++++ SDLU_Example/main.cpp | 5 ++++- 6 files changed, 46 insertions(+), 9 deletions(-) diff --git a/SDLU/Util.hpp b/SDLU/Util.hpp index eba6604..bff7acf 100644 --- a/SDLU/Util.hpp +++ b/SDLU/Util.hpp @@ -7,6 +7,8 @@ #pragma once #include +#define PI 3.1415926f + #define IS_NULLPTR( x ) (x == nullptr) #define RETURN_IF_NULLPTR( x, ... ) { if(IS_NULLPTR(x)) return __VA_ARGS__; } diff --git a/SDLU/graphics/RenderWindow.cpp b/SDLU/graphics/RenderWindow.cpp index 9393333..a41dc67 100644 --- a/SDLU/graphics/RenderWindow.cpp +++ b/SDLU/graphics/RenderWindow.cpp @@ -61,7 +61,7 @@ namespace sdlu OnClose(); } - bool RenderWindow::IsOpen() + bool RenderWindow::IsOpen() const { RETURN_IF_NULLPTR(m_pWindow, false); return (!SDL_GetWindowID(m_pWindow) ? false : true); @@ -91,7 +91,7 @@ namespace sdlu return true; } - Vector2i RenderWindow::GetPosition() + Vector2i RenderWindow::GetPosition() const { RETURN_IF_NULLPTR(m_pWindow, Vector2i()); @@ -114,7 +114,7 @@ namespace sdlu SDL_SetWindowPosition(m_pWindow, x, y); } - Vector2u RenderWindow::GetSize() + Vector2u RenderWindow::GetSize() const { RETURN_IF_NULLPTR(m_pWindow, Vector2u()); @@ -137,7 +137,7 @@ namespace sdlu SDL_SetWindowSize(m_pWindow, width, height); } - std::string RenderWindow::GetTitle() + std::string RenderWindow::GetTitle() const { RETURN_IF_NULLPTR(m_pWindow, ""); diff --git a/SDLU/graphics/RenderWindow.hpp b/SDLU/graphics/RenderWindow.hpp index eee6972..301db47 100644 --- a/SDLU/graphics/RenderWindow.hpp +++ b/SDLU/graphics/RenderWindow.hpp @@ -14,6 +14,10 @@ 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 * @@ -66,7 +70,7 @@ namespace sdlu * * @return True if the window is open, False if not */ - bool IsOpen(); + bool IsOpen() const; /** * @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 */ - Vector2i GetPosition(); + Vector2i GetPosition() const; /** * @brief Sets a new window position @@ -113,7 +117,7 @@ namespace sdlu * * @return A vector with the windows size */ - Vector2u GetSize(); + Vector2u GetSize() const; /** * @brief Sets a new window size @@ -136,7 +140,7 @@ namespace sdlu * * @return The title of the widnow */ - std::string GetTitle(); + std::string GetTitle() const; /** * @brief Sets a new window title diff --git a/SDLU/structures/Mouse.cpp b/SDLU/structures/Mouse.cpp index 7855dc3..87efd1c 100644 --- a/SDLU/structures/Mouse.cpp +++ b/SDLU/structures/Mouse.cpp @@ -13,4 +13,16 @@ namespace sdlu { 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(); + } } \ No newline at end of file diff --git a/SDLU/structures/Mouse.hpp b/SDLU/structures/Mouse.hpp index 5bc6adc..6f536f2 100644 --- a/SDLU/structures/Mouse.hpp +++ b/SDLU/structures/Mouse.hpp @@ -7,6 +7,7 @@ #pragma once #include #include +#include namespace sdlu { @@ -42,5 +43,20 @@ namespace sdlu * @return True if the button is pressed */ 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); }; } \ No newline at end of file diff --git a/SDLU_Example/main.cpp b/SDLU_Example/main.cpp index a4e1631..b9036ef 100644 --- a/SDLU_Example/main.cpp +++ b/SDLU_Example/main.cpp @@ -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(); t += 0.08;