forked from 42henry/herb-engine-C-3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindows-platform.c
More file actions
161 lines (142 loc) · 4.62 KB
/
windows-platform.c
File metadata and controls
161 lines (142 loc) · 4.62 KB
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
#include <windows.h>
#include <mmsystem.h>
#include "herbengineC3D.c"
#define FRAME_LENGTH (FRAME_TIME_NS / 1000000)
static HBITMAP bitmap = NULL;
static HDC memDC = NULL;
HWND hwnd;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
BitBlt(hdc, 0, 0, WIDTH, HEIGHT, memDC, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps);
return 0;
}
case WM_KEYDOWN: {
keys[wParam] = 1;
return 0;
}
case WM_KEYUP: {
keys[wParam] = 0;
return 0;
}
case WM_LBUTTONDOWN: mouse_left_click = 1; return 0;
case WM_RBUTTONDOWN: mouse_right_click = 1; return 0;
case WM_LBUTTONUP: mouse_left_click = 0; return 0;
case WM_RBUTTONUP: mouse_right_click = 0; return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
(void)hPrevInstance;
(void)lpCmdLine;
// --- Create pixel bitmap ---
BITMAPINFO bmi = {0};
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = WIDTH;
bmi.bmiHeader.biHeight = -HEIGHT; // top-down
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bitmap = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void**)&pixels, NULL, 0);
memDC = CreateCompatibleDC(NULL);
SelectObject(memDC, bitmap);
// --- Register & create window ---
WNDCLASS wc = {0};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "DIBWindow";
RegisterClass(&wc);
hwnd = CreateWindowEx(0, wc.lpszClassName, "DigMake",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, WIDTH, HEIGHT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
init_stuff();
// --- Map keys to Windows Virtual Key codes ---
w = 'W';
a = 'A';
s = 'S';
d = 'D';
shift = VK_SHIFT;
space = VK_SPACE;
control = VK_CONTROL;
escape = VK_ESCAPE;
one = '1';
two = '2';
three = '3';
four = '4';
five = '5';
six = '6';
seven = '7';
eight = '8';
nine = '9';
// --- Bump timer resolution to 1ms so Sleep() is accurate ---
timeBeginPeriod(1);
clock_gettime(CLOCK_MONOTONIC, &last);
int prev_hold_mouse = 0;
MSG msg;
while (1) {
// --- Handle all pending messages (non-blocking) ---
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
timeEndPeriod(1);
cleanup();
return 0;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// --- Mouse ---
if (holding_mouse) {
if (!prev_hold_mouse) {
POINT center = { WIDTH / 2, HEIGHT / 2 };
ClientToScreen(hwnd, ¢er);
SetCursorPos(center.x, center.y);
mouse.x = WIDTH / 2;
mouse.y = HEIGHT / 2;
mouse_left_click = 0;
mouse_right_click = 0;
prev_hold_mouse = 1;
while (ShowCursor(FALSE) >= 0);
continue;
}
POINT p;
GetCursorPos(&p);
ScreenToClient(hwnd, &p);
mouse.x = p.x;
mouse.y = p.y;
POINT center = { WIDTH / 2, HEIGHT / 2 };
ClientToScreen(hwnd, ¢er);
SetCursorPos(center.x, center.y);
} else {
if (prev_hold_mouse) {
while (ShowCursor(TRUE) < 0);
}
prev_hold_mouse = 0;
}
// --- Update ---
update();
// --- Render ---
InvalidateRect(hwnd, NULL, FALSE);
UpdateWindow(hwnd);
// --- Frame timing (mirrors Linux) ---
clock_gettime(CLOCK_MONOTONIC, &now);
long elapsed = (now.tv_sec - last.tv_sec) * 1000000000L
+ (now.tv_nsec - last.tv_nsec);
if (elapsed < FRAME_TIME_NS) {
DWORD sleep_ms = (DWORD)((FRAME_TIME_NS - elapsed) / 1000000);
if (sleep_ms > 0) Sleep(sleep_ms);
}
clock_gettime(CLOCK_MONOTONIC, &last);
}
timeEndPeriod(1);
cleanup();
return 0;
}