CG-Project2
Loading...
Searching...
No Matches
InputManager.hpp
1#pragma once
2
3#include "Utils.hpp"
4
5#include <array>
6#include <mutex>
7#include <vector>
8
9const int MAX_KEY_QUEUE = 20;
10const int MAX_MOUSE_KEY_QUEUE = 8;
11
12struct Key {
13 unsigned int key;
14 unsigned int status;
15};
16
17namespace ogl {
18 class InputManager {
19 public:
20 InputManager(InputManager &other) = delete;
21
22 void operator=(const InputManager &other) = delete;
23
24 /*
25 * Retrieves the instance of the InputManager if it's not created.
26 * This function is thread safe using a simple `std::mutex`.
27 *
28 * @return `InputManager` unique object.
29 */
30 inline static Shared<InputManager> instance() {
31 std::lock_guard<std::mutex> lock(s_mutex);
32 if (s_pointer == nullptr) {
33 Shared<InputManager> copy(new InputManager());
34 copy.swap(s_pointer);
35 }
36
37 return s_pointer;
38 }
39
40 void keyPressed(const unsigned int &key);
41
42 void keyReleased(const unsigned int &key);
43
44 bool isKeyPressed(const unsigned int &key) const;
45
46 Key getKeyStatus(const unsigned int &key) const;
47
48 std::vector<Key> getKeys();
49
50 private:
51 inline static Shared<InputManager> s_pointer = nullptr;
52 inline static std::mutex s_mutex{};
53
54 std::vector<Key> m_keys = std::vector<Key>();
55 std::array<Key, MAX_MOUSE_KEY_QUEUE> m_mouse{};
56
57 InputManager() = default;
58 };
59} // namespace ogl
Definition InputManager.hpp:12