-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.h
49 lines (41 loc) · 1.13 KB
/
window.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#pragma once
#include <chrono>
#include <functional>
#include <glm/vec2.hpp>
#include "gfx.h"
using UpdateFn = std::function<void()>;
using RenderFn = std::function<void()>;
using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
struct Key {
bool down; // is the key pressed now
bool before; // was the key pressed last time we checked
};
class Keyboard {
Key keys[GLFW_KEY_LAST]{};
public:
bool pressed_once(const int key) {
const bool pressed = keys[key].down && !keys[key].before;
keys[key].before = keys[key].down;
return pressed;
}
[[nodiscard]] bool held(const int key) const { return keys[key].down; }
void press(const int key) { keys[key].down = true; }
void release(const int key) { keys[key].down = false; }
};
class Window {
glm::vec2 size;
UpdateFn update;
RenderFn render;
TimePoint time_prev;
TimePoint time_now;
public:
Keyboard keyboard;
float time_delta;
GLFWwindow *handle;
Window(float width, float height, const char *title, UpdateFn update,
RenderFn render);
~Window();
[[nodiscard]] glm::vec2 dimensions() const;
void loop();
};