-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathengine.h
167 lines (143 loc) · 4.31 KB
/
engine.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#ifndef ENGINE_HDR
#define ENGINE_HDR
#include <stdint.h>
#include "u_map.h"
#include "u_string.h"
#include "u_misc.h"
#include "u_stack.h"
#include "a_system.h"
/// Frame timer
struct FrameTimer {
static constexpr size_t kMaxFPS = 0; ///< For capping framerate (0 = disabled)
static constexpr float kDampenEpsilon = 0.00001f; ///< Dampening to stabilize framerate readings
FrameTimer();
void cap(float maxFps); ///< Cap framerate to `maxFps' 0 to disable
float mspf() const; ///< Milliseconds per frame
int fps() const; ///< Frames per second
float delta() const; ///< Frame delta
uint32_t ticks() const; ///< Ticks
void reset();
bool update();
void lock();
void unlock();
private:
float m_maxFrameTicks;
uint32_t m_lastSecondTicks;
int m_frameCount;
uint32_t m_minTicks;
uint32_t m_maxTicks;
float m_averageTicks;
float m_deltaTime;
uint32_t m_lastFrameTicks;
uint32_t m_currentTicks;
uint32_t m_targetTicks;
uint32_t m_frameMin;
uint32_t m_frameMax;
float m_frameAverage;
int m_framesPerSecond;
bool m_lock;
};
/// Types of frame synchronization methods
enum {
kSyncTear = -1, ///< Late swap tearing
kSyncNone, ///< No vertical syncronization
kSyncEnabled, ///< Vertical syncronization
kSyncRefresh ///< No vertical syncronization, cap framerate to monitor refresh rate
};
/// Represents the given mouse state
struct MouseState {
MouseState();
enum {
kMouseButtonLeft = 1 << 0, ///< left mouse button
kMouseButtonRight = 1 << 1 ///< right mouse button
};
int x; ///< x position
int y; ///< y position
int wheel; ///< wheel value
int button; ///< flags
};
inline MouseState::MouseState()
: x(0)
, y(0)
, wheel(0)
, button(0)
{
}
enum class TextState {
kInactive,
kInputting,
kFinished
};
/// Engine object
struct Engine {
Engine();
~Engine();
bool init(int &argc, char **argv);
typedef void (*bindFunction)();
u::map<u::string, int> &keyState(const u::string &key = "", bool keyDown = false, bool keyUp = false);
void mouseDelta(int *deltaX, int *deltaY);
MouseState mouse() const;
void bindSet(const u::string &what, bindFunction handler);
void swap();
size_t width() const;
size_t height() const;
void relativeMouse(bool state);
bool relativeMouse();
void centerMouse();
void setWindowTitle(const char *title);
void resize(size_t width, size_t height);
void setVSyncOption(int option);
void screenShot();
void deleteConfig();
const u::string &userPath() const;
const u::string &gamePath() const;
TextState textInput(u::string &what);
FrameTimer m_frameTimer; // TODO: private
protected:
bool initContext();
bool initTimers();
bool initData(int &argc, char **argv);
void deleteConfigRecursive(const u::string &pathName);
private:
u::map<u::string, int> m_keyMap;
u::map<u::string, bindFunction> m_binds;
u::stack<u::string, 32> m_textInputHistory;
size_t m_textInputHistoryCursor;
u::vector<u::string> m_autoComplete;
size_t m_autoCompleteCursor;
u::string m_userPath;
u::string m_gamePath;
MouseState m_mouseState;
size_t m_screenWidth;
size_t m_screenHeight;
size_t m_refreshRate;
void *m_context; ///< pimpl for context
};
[[noreturn]] void neoFatalError(const char *error);
void *neoGetProcAddress(const char *proc);
template <typename... Ts>
[[noreturn]] inline void neoFatal(const char *fmt, const Ts&... ts) {
neoFatalError(u::format(fmt, ts...).c_str());
}
// Wrapper
u::map<u::string, int> &neoKeyState(const u::string &key = "", bool keyDown = false, bool keyUp = false);
const FrameTimer &neoFrameTimer();
MouseState neoMouseState();
TextState neoTextState(u::string &what);
void neoMouseDelta(int *deltaX, int *deltaY);
void neoSwap();
size_t neoWidth();
size_t neoHeight();
void neoRelativeMouse(bool state);
bool neoRelativeMouse();
void neoCenterMouse();
void neoSetWindowTitle(const char *title);
void neoResize(size_t width, size_t height);
const u::string &neoUserPath();
const u::string &neoGamePath();
void neoBindSet(const u::string &what, void (*handler)());
void (*neoBindGet(const u::string &what))();
void neoSetVSyncOption(int option);
void neoScreenShot();
void neoDeleteConfig();
#endif