-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwin32gl1.cpp
508 lines (397 loc) · 10.7 KB
/
win32gl1.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// win32gl1.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "win32gl1.h"
#include "printText.h"
#include "loadpng.h"
#include "gamelogic.h"
#include "director.h"
#include <cassert>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
HDC hDC;
PIXELFORMATDESCRIPTOR pfd;
HGLRC ghRC;
bool pauseFlag;
bool openglInited = false;
LPDWORD DrawingThread = NULL;
HANDLE DrawingThreadHANDLE = NULL;
HWND hWnd;
float FPS = 60;
float LastCycleTime = 0.01;//上一帧的时间
POINT mousepos;//鼠标位置
char lastPressedKey = '\0';
bool shouldRun = false;
// 此代码模块中包含的函数的前向声明:
void SceneShow(GLvoid);
void SceneInit(int w, int h);
void openglInit();//初始化OPENGL
void showFPS();//显示FPS
void setFPS(float fps);//设置FPS
void getLastCycleTime();//获得上一帧的时间(注意!每个循环只能调用一次!!!)
void getKeyBoardState();
bool lessSprite(Sprite* s1, Sprite* s2)
{
return s1->getlayer() < s2->getlayer();
}
DWORD WINAPI DrawingLoop(LPVOID lpParameter);
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
pauseFlag = false;
MSG msg;
HACCEL hAccelTable;
// 初始化全局字符串
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32GL1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32GL1));
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_SAVEBITS;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32GL1));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_WIN32GL1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
Director::create();
hInst = hInstance; // 将实例句柄存储在全局变量中
//设置窗口大小
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW&~WS_THICKFRAME&~WS_MAXIMIZEBOX,
200, 200, WINDOW_WIDTH + 18, WINDOW_HEIGHT+61, NULL, NULL, hInstance, NULL);
//~WS_THICKFRAME禁止改变窗口大小
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
shouldRun = true;
if (!DrawingThreadHANDLE)
DrawingThreadHANDLE = CreateThread(NULL, 0, DrawingLoop, NULL, 0, DrawingThread);
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
switch (message)
{
case WM_COMMAND:
SceneShow();
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_PAUSE:
Director::getTheInstance()->pause();
break;
case IDM_RESUME:
Director::getTheInstance()->resume();
break;
case IDM_START:
Director::getTheInstance()->start();
break;
case IDM_END:
Director::getTheInstance()->end();
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
// TODO: 在此添加任意绘图代码...
break;
case WM_DESTROY:
shouldRun = false;
WaitForSingleObject(DrawingThreadHANDLE, INFINITE);
CloseHandle(DrawingThreadHANDLE);
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
break;
case WM_CHAR:
lastPressedKey = wParam;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
//OpenGL 初始化开始
void SceneInit(int w, int h)
{
BuildFont(hDC);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景
glColor3f(1.0f, 1.0f, 1.0f);
//glShadeModel(GL_FLAT);
//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
//glOrtho(-50.0f, 50.0f, -50.0f, 50.0f, -10.0f, 10.0f);
}
// 这里进行所有的绘图工作
void SceneShow(GLvoid)
{
Director::getTheInstance()->CheckFlag();
switch (Director::getTheInstance()->getState())
{
case SLEEPING:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
SwapBuffers(hDC);
break;
case RUNNING:
GetCursorPos(&mousepos);//获得鼠标位置
getLastCycleTime();//每帧重置一下计时器
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
update(0.1f);
if (Director::getTheInstance()->getTotalScene())
{
Director::getTheInstance()->getKeyState();
Director::getTheInstance()->tickTimers();
Scene* thescene = Director::getTheInstance()->getCurrentScene();
Director::getTheInstance()->update();
sort(thescene->getSpriteList().begin(), thescene->getSpriteList().end(), lessSprite);
for (vector<Sprite*>::iterator it = thescene->getSpriteList().begin(); it != thescene->getSpriteList().end(); it++)
{
Point origin = (*it)->getpos();
Point anchorPoint = (*it)->getAnchorPoint();
Size size = (*it)->getsize();
swap(size.height, size.width);
float rot = (*it)->getrotation();
glLoadIdentity();
float r = sqrt(size.width*size.width + size.height*size.height);
Point bl, br, tr, tl;
bl.x = -(size.width) / r;
bl.y = -(size.height) / r;
br.x = (size.width) / r;
br.y = -(size.height) / r;
tr.x = (size.width) / r;
tr.y = (size.height) / r;
tl.x = -(size.width) / r;
tl.y = (size.height) / r;
glRotatef(90.0f + rot / M_PI * 180, 0.0f, 0.0f, 1.0f);
//TODO:FIX IT
glViewport(origin.x - r *anchorPoint.x, origin.y - r*anchorPoint.y, r, r);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
if ((*it)->getflipX()) //左右翻转
{
swap(bl.x, br.x);
swap(bl.y, br.y);
swap(tl.x, tr.x);
swap(tl.y, tr.y);
}
if ((*it)->getflipY()) //上下翻转
{
swap(bl.x, tl.x);
swap(bl.y, tl.y);
swap(br.x, tr.x);
swap(br.y, tr.y);;
}
glBindTexture(GL_TEXTURE_2D, (*it)->getTex()); //与纹理绑定
glBegin(GL_POLYGON);
glTexCoord2f(0.0f, 0.0f); glVertex3f(bl.x, bl.y, 0.0f); //左下
glTexCoord2f(0.0f, 1.0f); glVertex3f(br.x, br.y, 0.0f); //右下
glTexCoord2f(1.0f, 1.0f); glVertex3f(tr.x, tr.y, 0.0f); //右上
glTexCoord2f(1.0f, 0.0f); glVertex3f(tl.x, tl.y, 0.0f); //左上
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
}
}
///////////////////////////////////////////////////////////
//任何glPrint应在此行之后执行(不然看不到)
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glLoadIdentity();
glRasterPos2f(-1.0f, -0.8f);
ScreenToClient(hWnd, &mousepos);
Director::getTheInstance()->setMousePos(mousepos);
glPrint("mouse pos(%ld,%ld)", mousepos.x, mousepos.y);
glRasterPos2f(-1.0f, -0.6f);
glPrint("Keyboard: %c", lastPressedKey);
// 交换缓冲区
getKeyBoardState();
showFPS();
glRasterPos2f(-1.0f, -0.5f);
glPrint("aswd control");
SwapBuffers(hDC);
break;
}
}
void initDrawing()
{
int iFormat;
openglInited = true;
hDC = GetDC(hWnd);
assert(hDC);
ZeroMemory(&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1; // 版本,一般设为
// 一组表明象素缓冲特性的标志位
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA; // 明象素数据类型是RGBA 还是颜色索引;
pfd.cColorBits = 32; // 每个颜色缓冲区中颜色位平面的数目,对颜色索引方式是缓冲区大小
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE; // 被忽略,为了一致性而包含的
iFormat = ChoosePixelFormat(hDC, &pfd); // 选择一个像素格式
assert(SetPixelFormat(hDC, iFormat, &pfd)); // 设置到DC 中
ghRC = wglCreateContext(hDC); // 创建绘图描述表
assert(wglMakeCurrent(hDC, ghRC)); // 使之成为当前绘图描述表
openglInit();
}
DWORD WINAPI DrawingLoop(LPVOID lpParameter)
{
if (!openglInited)
initDrawing();
while (shouldRun)
{
SceneShow();
Sleep(1000 / 60);
}
Director::getTheInstance()->reset();
assert(wglMakeCurrent(NULL, NULL));
assert(wglDeleteContext(ghRC));
assert(ReleaseDC(hWnd, hDC));
ghRC = NULL;
hDC = NULL;
return 0;
}
//OpenGL 初始化开始
void openglInit()
{
BuildFont(hDC);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑色背景
glColor3f(1.0f, 1.0f, 1.0f);
glShadeModel(GL_FLAT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void showFPS()
{
glLoadIdentity();
glRasterPos2f(-1.0f, -1.0f);
static float lastFPS = 0;
lastFPS = 1.0 / LastCycleTime;
glPrint("FPS:%f", lastFPS);
}
void setFPS(float fps)
{
FPS = fps;
}
void getLastCycleTime()
{
LARGE_INTEGER LARGE;
static LONGLONG lasttimeL = 0;
LONGLONG nowL = 0;
double freq = 0;
QueryPerformanceFrequency(&LARGE);
freq = (double)LARGE.QuadPart;//获得频率
QueryPerformanceCounter(&LARGE);
nowL = LARGE.QuadPart;
LastCycleTime = ((float)(nowL - lasttimeL)) / freq;
lasttimeL = nowL;
}
void getKeyBoardState()
{
if (true)
{
glRasterPos2f(-1.0f, -0.2f);
//glPrint("L button Pressed %d", GetAsyncKeyState(VK_LBUTTON) & 0x8000);
}
}