-
Notifications
You must be signed in to change notification settings - Fork 0
/
EBG.h
305 lines (251 loc) · 6.72 KB
/
EBG.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#pragma once
#include <Windows.h>
#include <iostream>
#pragma comment(lib, "winmm.lib")
#include "EBG_graphics.h"
std::string GetLastErrorAsString()
{
//Get the error message ID, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0) {
return std::string(); //No error message has been recorded
}
LPSTR messageBuffer = nullptr;
//Ask Win32 to give us the string version of that message ID.
//The parameters we pass in, tell Win32 to create the buffer that holds the message for us (because we don't yet know how long the message string will be).
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
//Copy the error message into a std::string.
std::string message(messageBuffer, size);
//Free the Win32's string's buffer.
LocalFree(messageBuffer);
return message;
}
namespace ebg
{
struct mouse_s
{
POINT win_pos;
ipoint pos, old_pos, delta;
bool in_screen;
bool left, right, middle;
bool tleft, tright, tmiddle;
inline bool update_clicks(UINT u_msg)
{
switch (u_msg)
{
case WM_LBUTTONDOWN:
tleft = true;
left = true;
return true;
case WM_LBUTTONUP:
tleft = true;
left = false;
return true;
case WM_RBUTTONDOWN:
tright = true;
right = true;
return true;
case WM_RBUTTONUP:
tright = true;
right = false;
return true;
case WM_MBUTTONDOWN:
tmiddle = true;
middle = true;
return true;
case WM_MBUTTONUP:
tmiddle = true;
middle = false;
return true;
default:
return false;
}
}
};
struct abc_keyboard
{
bool keys[26];
inline bool get_key(char c) const
{
return keys[c - 'a'];
}
inline bool update_key_down(WPARAM w_param)
{
if (w_param >= 'A' && w_param <= 'Z')
{
keys[w_param - 'A'] = true;
return true;
}
return false;
}
inline bool update_key_up(WPARAM w_param)
{
if (w_param >= 'A' && w_param <= 'Z')
{
keys[w_param - 'A'] = false;
return true;
}
return false;
}
};
constexpr float dt_multipler = 1000.0f, inv_dt_multipler = 0.001f;
constexpr ipoint extra_size(16, 39);
struct win_data
{
BITMAPINFO bitmap_info;
WNDCLASSA wndc;
HDC hdc;
FILE* console;
MSG msg;
};
struct basic_engine
{
fpoint fdim, fhdim;
ipoint idim;
upoint hdim;
float ratio, inv_ratio;
bool running;
unsigned target_fps;
float delta_time;
mouse_s mouse;
abc_keyboard keyboard;
float* depth_buffer;
graphics::surface surface;
HWND window;
win_data data;
bool has_console;
float target_delta_time;
unsigned target_frame_time, tick, real_dt, start_time;
// unsigned delta time
unsigned udt;
void update_mouse()
{
GetCursorPos(&mouse.win_pos);
ScreenToClient(window, &mouse.win_pos);
mouse.old_pos = mouse.pos;
mouse.pos = ipoint(mouse.win_pos.x, surface.dim.y - mouse.win_pos.y);
mouse.delta = mouse.pos - mouse.old_pos;
mouse.in_screen = is_inside(mouse.pos, surface.dim);
}
inline void refresh_mouse_ticks()
{
mouse.tleft = false;
mouse.tmiddle = false;
mouse.tright = false;
}
// auto updates mouse
void start_tick()
{
start_time = timeGetTime();
while (PeekMessageW(&data.msg, window, 0, 0, PM_REMOVE))
{
TranslateMessage(&data.msg);
DispatchMessageW(&data.msg);
}
update_mouse();
}
void end_tick()
{
StretchDIBits(
data.hdc,
0, 0, idim.x, idim.y,
0, 0, idim.x, idim.y,
surface.buffer, &data.bitmap_info,
DIB_RGB_COLORS, SRCCOPY
);
refresh_mouse_ticks();
real_dt = timeGetTime() - start_time;
if (real_dt < target_frame_time)
{
Sleep(target_frame_time - real_dt);
udt = target_frame_time;
delta_time = target_delta_time;
}
else
{
udt = real_dt;
delta_time = static_cast<float>(udt) * inv_dt_multipler;
}
tick++;
}
basic_engine() {}
basic_engine(const char* title, upoint window_dimension, bool console, int fps,
WNDPROC event_handler, HINSTANCE hInstance, bool alloc_depth_buffer = false)
{
tick = real_dt = start_time = udt = 0L;
delta_time = .0f;
has_console = console;
running = false;
target_fps = fps;
target_frame_time = 1000 / fps;
target_delta_time = dt_multipler / static_cast<float>(fps);
memset(keyboard.keys, 0, 26);
memset(&mouse.left, 0, 6);
mouse.pos = ipoint(0, 0);
idim = window_dimension;
hdim = window_dimension >> 1U;
fdim = window_dimension;
fhdim = fdim * 0.5f;
ratio = fdim.x / fdim.y;
inv_ratio = fdim.y / fdim.x;
surface = graphics::surface(window_dimension);
depth_buffer = alloc_depth_buffer == true ? TYPE_MALLOC(float, surface.buffer_size) : nullptr;
window = nullptr;
if (console)
{
AllocConsole();
freopen_s(&data.console, "CONOUT$", "w", stdout);
}
data.wndc = { };
data.wndc.lpfnWndProc = event_handler;
data.wndc.hInstance = hInstance;
data.wndc.lpszClassName = title;
if (RegisterClassA(&data.wndc) == 0)
{
std::cout << GetLastErrorAsString();
MessageBoxA(nullptr, "Failed to register Window Class!", "Error", MB_OK);
return;
}
window = CreateWindowExA(
0,
data.wndc.lpszClassName,
data.wndc.lpszClassName,
WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
idim.x + extra_size.x, idim.y + extra_size.y,
nullptr, nullptr, hInstance, nullptr
);
if (window == nullptr)
{
UnregisterClassA(data.wndc.lpszClassName, hInstance);
std::cout << GetLastErrorAsString();
MessageBoxA(nullptr, "Failed to register Window Class!", "Error", MB_OK);
return;
}
data.bitmap_info.bmiHeader.biSize = sizeof(data.bitmap_info.bmiHeader);
data.bitmap_info.bmiHeader.biWidth = window_dimension.x;
data.bitmap_info.bmiHeader.biHeight = window_dimension.y;
data.bitmap_info.bmiHeader.biPlanes = 1;
data.bitmap_info.bmiHeader.biBitCount = 32;
data.bitmap_info.bmiHeader.biCompression = BI_RGB;
data.hdc = GetDC(window);
ShowWindow(window, 1);
UpdateWindow(window);
running = true;
}
};
void delete_basic_engine(basic_engine* be)
{
be->running = false;
if (be->has_console)
{
fclose(be->data.console);
FreeConsole();
}
ReleaseDC(be->window, be->data.hdc);
DestroyWindow(be->window);
UnregisterClassA(be->data.wndc.lpszClassName, be->data.wndc.hInstance);
graphics::delete_surface(&be->surface);
}
}