-
Notifications
You must be signed in to change notification settings - Fork 0
/
Win32Project1.cpp
554 lines (472 loc) · 14.3 KB
/
Win32Project1.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// Win32Project1.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "Win32Project1.h"
#include "Box2D/Box2D.h"
#include "PhysicWorld.h"
#include "MyBodysStruct.h"
#include "bodyRenderer.h"
#include "Stick.h"
#include "GameBall.h"
#include <Leap.h>
#include <Windows.h>
#include <mmsystem.h>
#include <gdiplus.h>
#include <time.h>
#include <mutex>
#include <cmath>
#include <math.h>
#include <iostream>
#include <string>
#include <string.h>
using namespace Gdiplus;
using namespace Leap;
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE hInst = 0; // 当前实例
HWND g_hWnd = 0;
WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
ULONG_PTR m_gdiplusToken;
//物理世界相关全局变量,贾神定的
static float ground_gravity = -10;
const float GLOBAL_HEIGHT = 700;
const float GLOBAL_WIDTH = 400;
//[jlc]: gaming variable, HP and score.
int HP;
int score;
PhysicWorld box2DWorld;
Gdiplus::Image main(L"main.png");
Gdiplus::Image start(L"start.png");
Gdiplus::Image end(L"end.png");
Gdiplus::Image senpai(L"senpai.png");
class SampleListener : public Leap::Listener {
public:
virtual void onConnect(const Leap::Controller&);
virtual void onInit(const Leap::Controller&);
virtual void onDisconnect(const Leap::Controller&);
virtual void onExit(const Leap::Controller&);
virtual void onFocusGained(const Leap::Controller&);
virtual void onFocusLost(const Leap::Controller&);
// main detect function
virtual void onFrame(const Leap::Controller&);
};
void SampleListener::onConnect(const Leap::Controller& controller) {
std::cout << "Connected" << std::endl;
//controller.enableGesture(Gesture::TYPE_CIRCLE);
controller.enableGesture(Leap::Gesture::TYPE_KEY_TAP);
controller.enableGesture(Leap::Gesture::TYPE_SCREEN_TAP);
controller.enableGesture(Leap::Gesture::TYPE_SWIPE);
}
void SampleListener::onInit(const Leap::Controller& controller) {
std::cout << "Initialized" << std::endl;
}
void SampleListener::onDisconnect(const Leap::Controller& controller) {
//Note: not dispatched when running in a debugger.
std::cout << "Disconnected" << std::endl;
}
void SampleListener::onExit(const Leap::Controller& controller) {
std::cout << "Exited" << std::endl;
}
void SampleListener::onFocusGained(const Leap::Controller& controller) {
std::cout << "Focus Gained" << std::endl;
}
void SampleListener::onFocusLost(const Leap::Controller& controller) {
std::cout << "Focus Lost" << std::endl;
}
void SampleListener::onFrame(const Leap::Controller& controller) {
const Leap::Frame frame = controller.frame();
/*
std::cout << "frame id: " << frame.id() << std::endl;
std::cout << "frame timestamp: " << frame.timestamp() << std::endl;
std::cout << "frame hands: " << frame.hands().count() << std::endl;
std::cout << "frame fingers: " << frame.fingers().count() << std::endl;
std::cout << "frame tools: " << frame.tools().count() << std::endl;
std::cout << "frame gestures: " << frame.gestures().count() << std::endl;
*/
const Leap::GestureList gestures = frame.gestures();
if (gestures.count()) {
std::cout << "---------------------------------------" << std::endl;
for (int i = 0; i < gestures.count(); i++) {
Leap::Gesture gesture = gestures[i];
switch (gesture.type()) {
case Leap::Gesture::TYPE_SWIPE: {
std::cout << "Catch SWIPE\t";
Leap::SwipeGesture sg = Leap::SwipeGesture(gesture);
Leap::Vector direction = sg.direction();
float x = direction.x;
int motion = 0;
if (sg.state() == sg.STATE_UPDATE) {
if (x < 0) {
motion = MOTION_LEFT;
}
else if (x > 0) {
motion = MOTION_RIGHT;
}
}
float speed = sg.speed();
speed = sqrt(speed);
applyMotion(motion, (int)speed);
break;
}
case Leap::Gesture::TYPE_KEY_TAP:
//break;
case Leap::Gesture::TYPE_SCREEN_TAP: {
int motion = 3;
Leap::HandList hl = gesture.hands();
if (hl.count() != 1)
return;
Leap::Hand hand = hl[0];
if (hand.isLeft())
applyMotion(motion, -1);
else if (hand.isRight())
applyMotion(motion, -2);
break;
}
default: break;
}
}
}
}
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
HWND InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void createBall(b2World *world) {
b2BodyDef centerCircleBodyDef;
centerCircleBodyDef.type = b2_dynamicBody;
int posX = rand() % 300 - 150;
centerCircleBodyDef.position.Set(posX, 350);
b2Body* ball = world->CreateBody(¢erCircleBodyDef);
b2CircleShape circle;
circle.m_p.Set(0.0f, 0.0f);
circle.m_radius = GLOBAL_WIDTH/18;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 0.1f;
fixtureDef.friction = 0.3f;
ball->CreateFixture(&fixtureDef);
int type = rand() % 2;
printf("create ball!\n");
MyBodys.vec.push_back(GameBall(ball, type, circle.m_radius));
}
void gameInit() {
//[jlc]: init HP and score.
HP = 5;
score = 0;
srand(time(0));
//circle in the center.
b2BodyDef centerCircleBodyDef;
centerCircleBodyDef.position.Set(0.0f, 0.0f);
MyBodys.centerCircle = box2DWorld.world.CreateBody(¢erCircleBodyDef);
b2CircleShape circle;
circle.m_p.Set(0.0f, 0.0f);
circle.m_radius = GLOBAL_WIDTH/18;
b2FixtureDef fixtureDef;
fixtureDef.shape = &circle;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
MyBodys.centerCircle->CreateFixture(&fixtureDef);
//rect for rotating
b2BodyDef rotateRectDef;
rotateRectDef.type = b2_dynamicBody;
rotateRectDef.position.Set(GLOBAL_WIDTH * 3 / 18, 0.0f);
b2Body* rotateRect;
rotateRect = box2DWorld.world.CreateBody(&rotateRectDef);
b2PolygonShape polygon;
polygon.SetAsBox(GLOBAL_WIDTH * 2 / 9, GLOBAL_WIDTH * 1 / 18);
fixtureDef.shape = &polygon;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
rotateRect->CreateFixture(&fixtureDef);
MyBodys.stick = new Stick(rotateRect, GLOBAL_WIDTH * 4 / 9, GLOBAL_WIDTH * 1 / 9);
b2RevoluteJointDef jointDef;
//joint
jointDef.Initialize( MyBodys.stick->rect, MyBodys.centerCircle, MyBodys.centerCircle->GetWorldCenter());
MyBodys.joint = (b2RevoluteJoint*)box2DWorld.world.CreateJoint(&jointDef);
}
void Logic(DWORD elapse) {
switch (GAME)
{
case STARTING: {
gameInit();
GAME = PLAYING;
break;
}
case WAITING: {
// draw main menu
break;
}
case PLAYING: {
float32 timeStep = 1.0f / 60.0f;
int32 velocityIterations = 6;
int32 positionIterations = 2;
box2DWorld.world.Step(timeStep, velocityIterations, positionIterations);
b2Vec2 position = MyBodys.stick->rect->GetPosition();
//printf("bar: %4.2f %4.2f\n", position.x, position.y);
for (int i = 0; i < MyBodys.vec.size(); i++) {
position = MyBodys.vec.at(i).ball->GetPosition();
//[jlc]: remove the balls out of range, update HP and score.
if (position.x<-GLOBAL_WIDTH / 2-30 || position.x>GLOBAL_WIDTH / 2+30
|| position.y<-GLOBAL_HEIGHT / 2-30 || position.y>GLOBAL_HEIGHT / 2+30) {
// RED ball goes down from the screen
if (MyBodys.vec.at(i).type == TYPE_RED&&position.y < -GLOBAL_HEIGHT / 2-30) {
score += 5;
}
if (MyBodys.vec.at(i).type == TYPE_BLACK || position.y < -GLOBAL_HEIGHT / 2-30) {
HP -= 1;
}
box2DWorld.world.DestroyBody(MyBodys.vec.at(i).ball);
MyBodys.vec.erase(MyBodys.vec.begin() + i);
i--;
}
//printf("ball %d: %4.2f %4.2f\n", i, position.x, position.y);
}
if (HP <= 0) {
//fail,destroy Everything
//box2DWorld.world.DestroyJoint(MyBodys.joint);
box2DWorld.world.DestroyBody(MyBodys.centerCircle);
box2DWorld.world.DestroyBody(MyBodys.stick->rect);
for (int i = 0; i < MyBodys.vec.size(); i++) {
box2DWorld.world.DestroyBody(MyBodys.vec.at(i).ball);
MyBodys.vec.erase(MyBodys.vec.begin() + i);
i--;
}
GAME = FAILED;
}
if (elapse % 400<=45) {
createBall(&(box2DWorld.world));
}
break;
}
default:
break;
}
}
void Render(HWND hwnd) {
int gwidth, gheight;
// TODO: 在此处添加使用 hdc 的任何绘图代码...
//Gdiplus::Image main(L"main.png");
Gdiplus::Image *image=NULL;
Gdiplus::Image *senpai = NULL;
HDC hdc = GetDC(hwnd);
RECT rt;
GetClientRect(hwnd, &rt);
gwidth = rt.right - rt.left;
gheight = rt.bottom - rt.top;
HDC hMdc = CreateCompatibleDC(hdc);//创建内存dc
HBITMAP hMbitmap = CreateCompatibleBitmap(hdc, gwidth, gheight);//创建内存绘图图片
SelectObject(hMdc, hMbitmap);//关联dc和图片
//取得宽度和高度
//width = image.GetWidth();
//height = image.GetHeight();
//绘图
Graphics graphics(hMdc);
//graphics.DrawImage(&main, 0, 0, gwidth, gheight);
switch (GAME)
{
case STARTING:
{
//image = Gdiplus::Image::FromFile(L"main.png");
//graphics.DrawImage(image, 0, 0, gwidth, gheight);
}break;
case WAITING:
{
image = Gdiplus::Image::FromFile(L"start.png");
graphics.DrawImage(image, 0, 0, gwidth, gheight);
}break;
case PLAYING:
{
image = Gdiplus::Image::FromFile(L"main.png");
senpai= Gdiplus::Image::FromFile(L"senpai.png");
graphics.DrawImage(image, 0, 0, gwidth, gheight);
graphics.DrawImage(senpai, 700, 580, 100, 120);
SolidBrush solidBrush(Color::Red);
FontFamily fontFamily(L"Arial");
Gdiplus::Font font(&fontFamily, 40, FontStyleRegular, UnitPixel);
PointF pointf(10.0f, 100.0f);
//显示字体显示大小
WCHAR infoString[100];
swprintf(infoString,100, L"得分:%d", score);
graphics.DrawString(infoString, wcslen(infoString), &font, pointf, &solidBrush);
bodyRenderer::RenderStick(&graphics, MyBodys.stick);
for (int i = 0; i < MyBodys.vec.size(); i++) {
bodyRenderer::RenderBall(&graphics, &MyBodys.vec[i]);
}
}break;
case FAILED: {
image = Gdiplus::Image::FromFile(_T("end.png"));
graphics.DrawImage(image, 0, 0, gwidth, gheight);
}break;
default:
break;
}
if(image) delete image;
if (senpai) delete senpai;
BitBlt(hdc, 0, 0, gwidth, gheight, hMdc, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdc);
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_WIN32PROJECT1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
HDC hdc;
MSG msg;
HWND hwnd = InitInstance(hInstance, nCmdShow);
// 执行应用程序初始化:
if (!(hwnd))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32PROJECT1));
PlaySound(L"3.wav", NULL, SND_LOOP | SND_ASYNC);
// 主消息循环:
ZeroMemory(&msg, sizeof(msg));
//gameInit();
SampleListener listener;
Controller controller;
controller.addListener(listener);
while (TRUE)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
static DWORD dwTime = timeGetTime();
DWORD dwCurrentTime = timeGetTime();
DWORD dwElapsedTime = dwCurrentTime - dwTime;
float fElapsedTime = dwElapsedTime * 0.001f;
Logic(dwElapsedTime);
Render(hwnd);
if (dwElapsedTime < 1000 / 60)
Sleep(1000 / 60 - dwElapsedTime);
dwTime = dwCurrentTime;
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32PROJECT1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = NULL;
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WIN32PROJECT1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // 将实例句柄存储在全局变量中
HDC hdc;
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPED,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return hWnd;
}
//onInit();
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return hWnd;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
int width, height;
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: 在此处添加使用 hdc 的任何绘图代码...
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
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;
}