forked from yinhui1129754/CppFishingCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoApp.cpp
More file actions
290 lines (257 loc) · 5.97 KB
/
Copy pathDemoApp.cpp
File metadata and controls
290 lines (257 loc) · 5.97 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
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
#include "../stdafx.h"
#include "../Resource.h"
#include "../trigger/createBefore.h"
DemoApp::DemoApp() :
m_hwnd(NULL),
content(NULL)
{
}
DemoApp::~DemoApp()
{
}
void DemoApp::addScene(action * scane) {
this->sceneArr.push_back(scane);
};
//全屏
void DemoApp::fullSreen() {
//int cx = GetSysTemMetrice(SM_CXSCREEN);
if (this->isFull) {
return;
}
this->isFull = true;
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
this->wLong = GetWindowLong(m_hwnd, GWL_STYLE);
SetWindowLong(m_hwnd, GWL_STYLE, WS_POPUP);
SetWindowPos(this->m_hwnd, HWND_TOPMOST, 0, 0, width, height, SWP_SHOWWINDOW);
}
//取消全屏
void DemoApp::cancelFullSreen() {
if (!this->isFull) {
return;
}
this->isFull = false;
SetWindowLong(m_hwnd, GWL_STYLE, this->wLong);
SetWindowPos(this->m_hwnd, HWND_NOTOPMOST, 0, 0, width, height, SWP_SHOWWINDOW);
}
void DemoApp::RunMessageLoop()
{
MSG msg;
BOOL isLoop = TRUE;
QueryPerformanceFrequency(&this->frequency);
while (isLoop)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
{
isLoop = FALSE;
}
}
if (!this->gameBool) {
//窗口处于非激活状态防止cpu爆炸
Sleep(10);
}
this->content->beginDraw();
QueryPerformanceCounter(&this->newtime);
this->msTime = ((float)(newtime.QuadPart - oldtime.QuadPart) / frequency.QuadPart * 1000);
if (this->msTime >=(int)1000/70)
{
this->content->clear();
this->OnRender();
QueryPerformanceCounter(&this->oldtime);
}
this->content->closeDraw();
}
//CloseHandle(this->thread1);
}
HRESULT DemoApp::Initialize()
{
HRESULT hr=S_OK;
// Initialize device-indpendent resources, such
// as the Direct2D factory.
if (SUCCEEDED(hr))
{
// Register the window class.
WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = DemoApp::WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION);
wcex.lpszClassName = L"MXSJ";
wcex.hIcon = LoadIcon(HINST_THISCOMPONENT, (LPCTSTR)IDI_D2DWIN32);
RegisterClassEx(&wcex);
// Create the window.
m_hwnd = CreateWindow(
L"MXSJ",
L"捕鱼人",
WS_OVERLAPPEDWINDOW^WS_MAXIMIZEBOX^WS_THICKFRAME,
CW_USEDEFAULT,
CW_USEDEFAULT,
1200,
750,
NULL,
NULL,
HINST_THISCOMPONENT,
this
);
hr = m_hwnd ? S_OK : E_FAIL;
content = new Content(m_hwnd);
content->strokeStyle(0xff00ff, 1);
content->lineWidth = 2;
content->fillStyle(0xffff00, 1);
createBefore::createWindowBefore(this);
if (SUCCEEDED(hr))
{
ShowWindow(m_hwnd, SW_SHOWNORMAL);
UpdateWindow(m_hwnd);
}
}
createBefore::createWindow(this);
return hr;
}
/*void DemoApp::addScene(action * scane) {
this->sceneArr.push_back(scane);
};*/
void DemoApp::addFun(void (*funs)(DemoApp *)) {
vector<void (*)(DemoApp*)>::iterator it;
it = find(this->fun.begin(), this->fun.end(), (funs));
if (it == this->fun.end()) {
(this->fun).push_back((funs));
}
};
HWND DemoApp::getHwnd() {
return this->m_hwnd;
};
LRESULT CALLBACK DemoApp::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
LRESULT result = 0;
WORD isActive = HIWORD(wParam);
if (message == WM_CREATE)
{
LPCREATESTRUCT pcs = (LPCREATESTRUCT)lParam;
DemoApp *pDemoApp = (DemoApp *)pcs->lpCreateParams;
RECT rc;
GetClientRect(hwnd,&rc);
pDemoApp->width = rc.right - rc.left;
pDemoApp->height = rc.bottom - rc.top;
::SetWindowLongPtrW(
hwnd,
GWLP_USERDATA,
PtrToUlong(pDemoApp)
);
result = 1;
}
else
{
DemoApp *pDemoApp = reinterpret_cast<DemoApp *>(static_cast<LONG_PTR>(
::GetWindowLongPtrW(
hwnd,
GWLP_USERDATA
)));
//
if (pDemoApp == NULL) {
return DefWindowProc(hwnd, message, wParam, lParam);
}
bool wasHandled = false;
if (pDemoApp)
{
switch (message)
{
case WM_SIZE:
{
UINT width = LOWORD(lParam);
UINT height = HIWORD(lParam);
pDemoApp->OnResize(width, height);
}
result = 0;
wasHandled = true;
break;
case WM_KEYUP:
messageMapping::onKeyUp(wParam);
result = 0;
wasHandled = true;
break;
case WM_KEYDOWN:
messageMapping::onKeyDown(wParam);
result = 0;
wasHandled = true;
break;
case WM_MOUSEMOVE:
pDemoApp->mouse->x = LOWORD(lParam);
pDemoApp->mouse->y = HIWORD(lParam);
messageMapping::onMouseMove(pDemoApp->mouse->x, pDemoApp->mouse->y);
result = 0;
wasHandled = true;
break;
case WM_LBUTTONUP:
pDemoApp->mouse->x=LOWORD(lParam);
pDemoApp->mouse->y = HIWORD(lParam);
messageMapping::onMouseUp(pDemoApp->mouse->x, pDemoApp->mouse->y);
messageMapping::onClick(pDemoApp->mouse->x, pDemoApp->mouse->y);
result = 1;
wasHandled = true;
break;
case WM_LBUTTONDOWN:
pDemoApp->mouse->x = LOWORD(lParam);
pDemoApp->mouse->y = HIWORD(lParam);
messageMapping::onMouseDown(pDemoApp->mouse->x, pDemoApp->mouse->y);
result = 1;
wasHandled = true;
break;
case WM_DISPLAYCHANGE:
{
InvalidateRect(hwnd, NULL, FALSE);
}
result = 0;
wasHandled = true;
break;
case WM_ACTIVATE:
if (isActive > 0) {
pDemoApp->gameBool = false;
}
else {
pDemoApp->gameBool = true;
}
result = 0;
wasHandled = true;
break;
case WM_DESTROY:
{
messageMapping::onClose();
PostQuitMessage(0);
}
result = 1;
wasHandled = true;
break;
}
}
if (!wasHandled)
{
result = DefWindowProc(hwnd, message, wParam, lParam);
}
}
return result;
}
HRESULT DemoApp::OnRender()
{
LRESULT hr = S_OK;
for (unsigned int i = 0; i < (this->fun).size();i++)
{
(this->fun)[i](this);
}
return hr;
}
void DemoApp::OnResize(UINT width, UINT height)
{
}
void DemoApp::getSource(vector<string> arr) {
for (unsigned int i=0;i < arr.size();i++) {
this->content->getSoucre(g_chartowchar2(arr[i].c_str()));
}
};