From 5daae26a1213b436efc2c1ac0ac0909aad5f8f7c Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 19 May 2020 14:57:35 +0200 Subject: [PATCH] Added button checks --- SDLU/CMakeLists.txt | 2 +- SDLU/SDLU.hpp | 2 ++ SDLU/structures/CMakeLists.txt | 2 ++ SDLU/structures/Mouse.cpp | 16 ++++++++++++ SDLU/structures/Mouse.hpp | 46 ++++++++++++++++++++++++++++++++++ SDLU_Example/main.cpp | 12 ++++++++- 6 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 SDLU/structures/Mouse.cpp create mode 100644 SDLU/structures/Mouse.hpp diff --git a/SDLU/CMakeLists.txt b/SDLU/CMakeLists.txt index 6172643..0cb67e4 100644 --- a/SDLU/CMakeLists.txt +++ b/SDLU/CMakeLists.txt @@ -2,7 +2,7 @@ set(PNAME SDLU) add_library(${PNAME} alibi.cpp SDLU.hpp Util.hpp - "structures/Color.cpp") + "structures/Color.cpp" "structures/Mouse.cpp") set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 17) diff --git a/SDLU/SDLU.hpp b/SDLU/SDLU.hpp index 3e5b116..89acf36 100644 --- a/SDLU/SDLU.hpp +++ b/SDLU/SDLU.hpp @@ -2,4 +2,6 @@ #include "graphics/RenderWindow.hpp" +#include "structures/Mouse.hpp" + #include "exceptions/Exceptions.hpp" \ No newline at end of file diff --git a/SDLU/structures/CMakeLists.txt b/SDLU/structures/CMakeLists.txt index cbc87fe..1f7d68f 100644 --- a/SDLU/structures/CMakeLists.txt +++ b/SDLU/structures/CMakeLists.txt @@ -2,4 +2,6 @@ target_sources(${PNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/Vector2.hpp ${CMAKE_CURRENT_SOURCE_DIR}/Color.hpp ${CMAKE_CURRENT_SOURCE_DIR}/Color.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Mouse.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/Mouse.cpp ) \ No newline at end of file diff --git a/SDLU/structures/Mouse.cpp b/SDLU/structures/Mouse.cpp new file mode 100644 index 0000000..7855dc3 --- /dev/null +++ b/SDLU/structures/Mouse.cpp @@ -0,0 +1,16 @@ +#include "Mouse.hpp" + +#include + +namespace sdlu +{ + Uint32 Mouse::GetButtonState() + { + return SDL_GetMouseState(NULL, NULL); + } + + bool Mouse::IsButtonDown(Button button) + { + return (GetButtonState() & SDL_BUTTON(button)); + } +} \ No newline at end of file diff --git a/SDLU/structures/Mouse.hpp b/SDLU/structures/Mouse.hpp new file mode 100644 index 0000000..5bc6adc --- /dev/null +++ b/SDLU/structures/Mouse.hpp @@ -0,0 +1,46 @@ +/** + * @file Mouse.hpp + * @brief A static class to provide easy handling of the mouse + * @author Lauchmelder23 + * @date 19.05.2020 + */ +#pragma once +#include +#include + +namespace sdlu +{ + /** + * @brief A static class that contains/handles data about + * mouse position and button states + */ + class Mouse + { + public: + /** + * @brief Mouse buttons + */ + enum Button { + Left = SDL_BUTTON_LEFT, + Right = SDL_BUTTON_RIGHT, + Middle = SDL_BUTTON_MIDDLE, + XButton1 = SDL_BUTTON_X1, + XButton2 = SDL_BUTTON_X2 + }; + + /** + * @brief Returns the current mouse button state + * + * @return A 32-bit mask of the current button state + */ + static Uint32 GetButtonState(); + + /** + * @brief Checks if a specific button is pressed + * + * @param[in] button The button to check + * @return True if the button is pressed + */ + static bool IsButtonDown(Button button); + }; +} \ No newline at end of file diff --git a/SDLU_Example/main.cpp b/SDLU_Example/main.cpp index a7217d6..a4e1631 100644 --- a/SDLU_Example/main.cpp +++ b/SDLU_Example/main.cpp @@ -30,8 +30,11 @@ int main(int argc, char** argv) SDL_Event event; float t = 0.f; + + std::string title = ""; while (window.IsOpen()) { + title = ""; while (window.PollEvent(&event)) { switch (event.window.event) @@ -53,7 +56,14 @@ int main(int argc, char** argv) diff = std::chrono::duration_cast (std::chrono::steady_clock::now() - start).count(); - window.SetTitle(std::to_string(1000000 / diff) + " FPS"); + title += (std::to_string(1000000 / diff) + " FPS | Mouse: "); + title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::Left)) ? "L " : "l "; + title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::Middle)) ? "M " : "m "; + title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::Right)) ? "R " : "r "; + title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::XButton1)) ? "X1 " : "x1 "; + title += (sdlu::Mouse::IsButtonDown(sdlu::Mouse::Button::XButton2)) ? "X2" : "x2"; + + window.SetTitle(title); start = std::chrono::steady_clock::now(); }