Engine²
Open-source game engine written in C++.
Loading...
Searching...
No Matches
InputUtils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <GLFW/glfw3.h>
4#include <glm/vec2.hpp>
5
6#include "Logger.hpp"
7#include "core/Core.hpp"
9
10namespace Input::Utils {
11using JoystickAxes = std::vector<float>;
12using JoystickButtons = std::vector<unsigned char>;
13
20inline bool IsKeyPressed(int key) noexcept { return glfwGetKey(glfwGetCurrentContext(), key) == GLFW_PRESS; }
21
28inline bool IsMouseButtonPressed(int button) noexcept
29{
30 return glfwGetMouseButton(glfwGetCurrentContext(), button) == GLFW_PRESS;
31}
32
38inline glm::vec2 GetMousePosition() noexcept
39{
40 double x;
41 double y;
42 glfwGetCursorPos(glfwGetCurrentContext(), &x, &y);
43 return {x, y};
44}
45
49void PrintAvailableControllers() noexcept;
50
57inline bool IsJoystickPresent(int jid) noexcept { return glfwJoystickPresent(jid); }
58
65inline std::string GetJoystickName(int jid) noexcept
66{
67 if (IsJoystickPresent(jid))
68 {
69 return std::string(glfwGetJoystickName(jid));
70 }
71 return "Unknown";
72}
73
82
91} // namespace Input::Utils
Definition InputUtils.hpp:10
std::vector< float > JoystickAxes
Definition InputUtils.hpp:11
std::string GetJoystickName(int jid) noexcept
Get the name of a joystick.
Definition InputUtils.hpp:65
JoystickButtons GetJoystickButtons(int jid)
Get the joystick button states.
Definition InputUtils.cpp:30
void PrintAvailableControllers() noexcept
Prints the available controllers that glfw can handle.
Definition InputUtils.cpp:5
bool IsKeyPressed(int key) noexcept
Check if a key is pressed.
Definition InputUtils.hpp:20
bool IsMouseButtonPressed(int button) noexcept
Check if a mouse button is pressed.
Definition InputUtils.hpp:28
bool IsJoystickPresent(int jid) noexcept
Check if a joystick is present.
Definition InputUtils.hpp:57
glm::vec2 GetMousePosition() noexcept
Get the current mouse position.
Definition InputUtils.hpp:38
JoystickAxes GetJoystickAxes(int jid)
Get the joystick axis values.
Definition InputUtils.cpp:17
std::vector< unsigned char > JoystickButtons
Definition InputUtils.hpp:12