Added button checks

This commit is contained in:
Robert 2020-05-19 14:57:35 +02:00
parent 970d6a89ae
commit 5daae26a12
6 changed files with 78 additions and 2 deletions

View file

@ -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)

View file

@ -2,4 +2,6 @@
#include "graphics/RenderWindow.hpp"
#include "structures/Mouse.hpp"
#include "exceptions/Exceptions.hpp"

View file

@ -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
)

16
SDLU/structures/Mouse.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "Mouse.hpp"
#include <SDL_mouse.h>
namespace sdlu
{
Uint32 Mouse::GetButtonState()
{
return SDL_GetMouseState(NULL, NULL);
}
bool Mouse::IsButtonDown(Button button)
{
return (GetButtonState() & SDL_BUTTON(button));
}
}

46
SDLU/structures/Mouse.hpp Normal file
View file

@ -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 <SDL_mouse.h>
#include <structures/Vector2.hpp>
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);
};
}

View file

@ -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::microseconds>
(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();
}