forked from SunXLei/SRender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32.cpp
272 lines (236 loc) · 7.02 KB
/
win32.cpp
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
#include "./win32.h"
#include <cassert>
#include <cstdio>
window_t* window = NULL;
static LRESULT CALLBACK msg_callback(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
window->is_close = 1;
break;
case WM_KEYDOWN:
window->keys[wParam & 511] = 1;
break;
case WM_KEYUP:
window->keys[wParam & 511] = 0;
break;
case WM_LBUTTONDOWN:
window->mouse_info.orbit_pos = get_mouse_pos();
window->buttons[0] = 1; break;
case WM_LBUTTONUP:
window->buttons[0] = 0;
break;
case WM_RBUTTONDOWN:
window->mouse_info.fv_pos = get_mouse_pos();
window->buttons[1] = 1;
break;
case WM_RBUTTONUP:
window->buttons[1] = 0;
break;
case WM_MOUSEWHEEL:
window->mouse_info.wheel_delta = GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
break;
default: return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
/*
UINT style;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
HBRUSH hbrBackground;
LPCSTR lpszMenuName;
LPCSTR lpszClassName;
*/
static void register_window_class()
{
ATOM atom;
//初始化结构体
WNDCLASS wc;
wc.style = CS_BYTEALIGNCLIENT; //窗口风格
wc.lpfnWndProc = (WNDPROC)msg_callback; //回调函数
wc.cbClsExtra = 0; //紧跟在窗口类尾部的一块额外空间,不用则设为0
wc.cbWndExtra = 0; //紧跟在窗口实例尾部的一块额外空间,不用则设为0
wc.hInstance = GetModuleHandle(NULL); //当前实例句柄
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); //任务栏图标
wc.hCursor = LoadCursor(NULL, IDC_ARROW); //光标样式
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); //背景样式
wc.lpszMenuName = NULL; //菜单
wc.lpszClassName = "SRender_window"; //该窗口类的名字
atom = RegisterClass(&wc); //注册窗口类
assert(atom != 0);
}
/*
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
*/
static void init_bm_header(BITMAPINFOHEADER &bi,int width,int height)
{
memset(&bi, 0, sizeof(BITMAPINFOHEADER));
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = width;
bi.biHeight = -height; //从上到下
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = width * height * 4;
}
int window_init(int width, int height, const char *title)
{
window = (window_t*)malloc(sizeof(window_t));
memset(window, 0, sizeof(window_t));
window->is_close = 0;
RECT rect = { 0, 0, width, height }; //一个矩形范围 左上右下
int wx, wy, sx, sy;
LPVOID ptr; //就是void *
HDC hDC; //设备环境,h代表句柄,handle
BITMAPINFOHEADER bi;
//注册窗口类
register_window_class();
//创建窗口
window->h_window = CreateWindow(("SRender_window"), title,
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
0, 0, 0, 0, NULL, NULL, GetModuleHandle(NULL), NULL);
assert(window->h_window != NULL);
//初始化位图头格式
init_bm_header(bi, width, height);
//获得兼容性DC
hDC = GetDC(window->h_window);
window->mem_dc = CreateCompatibleDC(hDC);
ReleaseDC(window->h_window, hDC);
//创建位图
window->bm_dib = CreateDIBSection(window->mem_dc, (BITMAPINFO*)&bi, DIB_RGB_COLORS, &ptr, 0, 0); //创建设备无关句柄
assert(window->bm_dib !=NULL);
window->bm_old = (HBITMAP)SelectObject(window->mem_dc, window->bm_dib);//把新创建的位图句柄写入mem_dc
window->window_fb = (unsigned char*)ptr;
window->width = width;
window->height = height;
AdjustWindowRect(&rect, GetWindowLong(window->h_window, GWL_STYLE), 0);//调整窗口大小
wx = rect.right - rect.left;
wy = rect.bottom - rect.top;
sx = (GetSystemMetrics(SM_CXSCREEN) - wx) / 2; // GetSystemMetrics(SM_CXSCREEN)获取你屏幕的分片率
sy = (GetSystemMetrics(SM_CYSCREEN) - wy) / 2; // 计算出中心位置
if (sy < 0) sy = 0;
SetWindowPos(window->h_window, NULL, sx, sy, wx, wy, (SWP_NOCOPYBITS | SWP_NOZORDER | SWP_SHOWWINDOW));
SetForegroundWindow(window->h_window);
ShowWindow(window->h_window, SW_NORMAL);
//消息分派
msg_dispatch();
//初始化keys, window_fb全为0
memset(window->window_fb, 0, width * height * 4);
memset(window->keys, 0, sizeof(char) * 512);
return 0;
}
int window_destroy()
{
if (window->mem_dc)
{
if (window->bm_old)
{
SelectObject(window->mem_dc, window->bm_old); // 写入原来的bitmap,才能释放DC!
window->bm_old = NULL;
}
DeleteDC(window->mem_dc);
window->mem_dc = NULL;
}
if (window->bm_dib)
{
DeleteObject(window->bm_dib);
window->bm_dib = NULL;
}
if (window->h_window)
{
CloseWindow(window->h_window);
window->h_window = NULL;
}
free(window);
return 0;
}
void msg_dispatch()
{
MSG msg;
while (1)
{
// Peek不阻塞,Get会阻塞,PM_NOREMOVE表示如果有消息不处理(留给接下来的Get处理)
if (!PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) break; //没消息就溜,确定有消息再用Get
if (!GetMessage(&msg, NULL, 0, 0)) break;
TranslateMessage(&msg); //转换消息 虚拟按键->字符
DispatchMessage(&msg); //传送消息给回调
}
}
static void window_display()
{
LOGFONT logfont; //改变输出字体
ZeroMemory(&logfont, sizeof(LOGFONT));
logfont.lfCharSet = ANSI_CHARSET;
logfont.lfHeight = 20; //设置字体的大小
HFONT hFont = CreateFontIndirect(&logfont);
HDC hDC = GetDC(window->h_window);
//目标举行的左上角(x,y), 宽度,高度,上下文指针
SelectObject(window->mem_dc, hFont);
SetTextColor(window->mem_dc, RGB(190, 190, 190));
SetBkColor(window->mem_dc, RGB(80, 80, 80));
//TextOut(window->mem_dc, 300, 50, "Project Name:SRender", strlen("Project Name:SRender"));
//TextOut(window->mem_dc, 300, 80, "Author:Lei", strlen("Author:Lei Sun"));
TextOut(window->mem_dc, 20, 20,
"control:hold left buttion to rotate, right button to pan",
strlen("Control:hold left buttion to rotate, right button to pan"));
// 把兼容性DC的数据传到真正的DC上
BitBlt(hDC, 0, 0, window->width, window->height, window->mem_dc, 0, 0, SRCCOPY);
ReleaseDC(window->h_window, hDC);
}
void window_draw(unsigned char *framebuffer)
{
int i, j;
for (int i = 0; i < window->height; i++)
{
for (int j = 0; j < window->width; j++)
{
int index = (i * window->width + j) * 4;
window->window_fb[index] = framebuffer[index + 2];
window->window_fb[index + 1] = framebuffer[index + 1];
window->window_fb[index + 2] = framebuffer[index];
}
}
window_display();
}
vec2 get_mouse_pos()
{
POINT point;
GetCursorPos(&point);
ScreenToClient(window->h_window, &point); // 从屏幕空间转到窗口空间
return vec2((float)point.x, (float)point.y);
}
/* misc platform functions */
static double get_native_time(void) {
static double period = -1;
LARGE_INTEGER counter;
if (period < 0) {
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
period = 1 / (double)frequency.QuadPart;
}
QueryPerformanceCounter(&counter);
return counter.QuadPart * period;
}
float platform_get_time(void) {
static double initial = -1;
if (initial < 0) {
initial = get_native_time();
}
return (float)(get_native_time() - initial);
}