-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
492 lines (438 loc) · 11.3 KB
/
main.cpp
File metadata and controls
492 lines (438 loc) · 11.3 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
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
#include "Engine.h"
#include "WindowsX.h"
#include "Menu.h"
#include "UserField.h"
#include "EnemyField.h"
#include "ButtonFieldDeploy.h"
#include "ButtonFieldFire.h"
#include "ButtonFieldConnect.h"
#include "ButtonFieldNewGame.h"
#include "TextureManager.h"
#include "StatisticField.h"
#include "StatusField.h"
#include "CannonField.h"
#include "ClueField.h"
#include "resource.h"
#include "SoundButton.h"
// Windows globals
CHAR WindowClassName[] = { "Windows OpenGL" };
HWND hwnd{};
HDC hDC{};
HGLRC hRC{};
HINSTANCE hInst{};
// Custom globals
bool TimeToRedraw{};
HANDLE TimerFuncHandler{};
const float FrameRate = (float)1000 / 60;
Engine engine;
ButtonFieldDeploy buttonFieldDeploy(ButtonFieldDeployPosX, ButtonFieldDeployPosY);
ButtonFieldFire buttonFieldFire(ButtonFieldFirePosX, ButtonFieldDeployPosY);
ButtonFieldConnect buttonFieldConnect(ButtonFieldConnectPosX, ButtonFieldConnectPosY);
ButtonFieldNewGame buttonFieldNewGame(ButtonFieldNewGamePosX, ButtonFieldNewGamePosY);
UserField userField(UserFieldPosX,UserFieldPosY);
EnemyField enemyField(EnemyFieldPosX, EnemyFieldPosY);
TextureManager textureManager;
StatisticField statisticField(StatisticFieldPosX,StatisticFieldPosY);
StatusField statusField(StatusFieldPosX, StatusFieldPosY);
ClueField clueField(ClueFieldPosX, ClueFieldPosY);
CannonField UserCannonField(UserCannonFieldPosX, UserCannonFieldPosY,3.14);
CannonField OpponentCannonField(EnemyCannonFieldPosX, EnemyCannonFieldPosY,0);
SoundButton soundButton(SoundButtonPosX, SoundButtonPosY);
//Windows prototypes
LONG WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
BOOL B_SetupPixelFormat(HDC);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//OpenGL defines
#define SWAPBUFFERS SwapBuffers(hDC)
// OpenGL prototypes
GLvoid Resize(GLsizei, GLsizei);
GLvoid InitGL(GLsizei, GLsizei);
GLvoid DrawScene(GLvoid);
//Custom prototypes
void TimerFunc(LPVOID);
/// <summary>
/// The starting point of the program.
/// </summary>
/// <param name="hInstance: ">A handle to the current instance of the application.</param>
/// <param name="hPrevInstance: ">A handle to the previous instance of the application. (Always supposed to be NULL)</param>
/// <param name="lpCmdLine: ">The command line for the application, excluding the program name.</param>
/// <param name="nShowCmd: ">Controls how the window is to be shown.</param>
/// <returns>A value of 1 (0x1) when succesful.</returns>
int WINAPI WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nShowCmd)
{
hInst = hInstance;
MSG msg;
WNDCLASS wndclass;
/* Register the frame class */
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wndclass.lpszMenuName = (LPCWSTR)WindowClassName;
wndclass.lpszClassName = (LPCWSTR)WindowClassName;
if (!RegisterClass(&wndclass))
return FALSE;
/* Create the frame */
hwnd = CreateWindow((LPCWSTR)WindowClassName,
L"Warship Battle",
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
CW_USEDEFAULT,
CW_USEDEFAULT,
MinimalWidth,
MinimalHeight,
NULL,
NULL,
hInstance,
NULL);
/* make sure window was created */
if (!hwnd)
return FALSE;
/* show and update main window */
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
UpdateWindow(hwnd);
//Loading Textures into memory
//Create thread to Implement TimerFunc, which tells the program when to redraw the window
TimerFuncHandler = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)TimerFunc, NULL, NULL, NULL);
/* animation loop */
while (true) {
// Process all pending messages
while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
{
if (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
return TRUE;
}
}
if (TimeToRedraw)
{
engine.Event(MSG_VOID);
engine.IncreaseMatchTime();
DrawScene();
TimeToRedraw = false;
}
}
}
/// <summary>
/// Main loop.
/// </summary>
/// <param name="hWnd: ">Handle to the main window.</param>
/// <param name="uMsg: ">Message identifier.</param>
/// <param name="wParam: ">First message parameter.</param>
/// <param name="lParam: ">Second message parameter.</param>
/// <returns>A value of 1 unless the default case is activated.</returns>
LONG WINAPI MainWndProc(
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
LONG lRet = 1;
PAINTSTRUCT ps;
RECT rect;
switch (uMsg) {
case WM_COMMAND:
switch (LOWORD(wParam))
{
case MENU_HELP_ABOUT:
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG1), hWnd, About);
}
break;
case MENU_GAME_EXIT:
SendMessage(hWnd, WM_CLOSE, NULL, NULL);
break;
case MENU_GAME_MAINMENU:
{
engine.StartNewGame();
engine.GameMode = engine.GAMEMODE::Menu;
engine.SetStatus(engine.GAMESTATUS::NewGame);
clueField.startX = ClueFieldPosX;
statusField.startX = StatusFieldPosX;
engine.CloseConnection();
}
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
break;
case WM_CREATE:
hDC = GetDC(hWnd);
if (!B_SetupPixelFormat(hDC))
PostQuitMessage(0);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
GetClientRect(hWnd, &rect);
InitGL(rect.right, rect.bottom);
CreateMenus(hWnd);
break;
case WM_PAINT:
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_SIZE:
GetClientRect(hWnd, &rect);
Resize(rect.right, rect.bottom);
break;
case WM_LBUTTONDOWN:
{
POINT ClickCoordinate{ GET_X_LPARAM(lParam),GET_Y_LPARAM(lParam) };
engine.ConvertPixelsToGL(&ClickCoordinate);
engine.Event(MSG_LBTTNDOWN, ClickCoordinate);
}
break;
case WM_KEYDOWN:
{
engine.Event(MSG_KEYPRESS, { NULL, NULL }, wParam);
}
break;
case WM_CLOSE:
{
if (MessageBox(hWnd, L"Do you really want to quit Warship Battle?", L"Exit?", MB_ICONWARNING | MB_YESNO) == IDYES)
{
engine.CloseConnection();
engine.WaitForDisconnection();
if (hRC)
wglDeleteContext(hRC);
if (hDC)
ReleaseDC(hWnd, hDC);
hRC = 0;
hDC = 0;
DestroyWindow(hWnd);
}
}
break;
case WM_DESTROY:
{
if (hRC)
wglDeleteContext(hRC);
if (hDC)
ReleaseDC(hWnd, hDC);
CloseHandle(TimerFuncHandler);
PostQuitMessage(0);
}
break;
case WM_GETMINMAXINFO:
{
PMINMAXINFO pMinMaxInfo{ (PMINMAXINFO)lParam };
pMinMaxInfo->ptMinTrackSize.x = MinimalWidth;
pMinMaxInfo->ptMinTrackSize.y = MinimalHeight;
}
break;
default:
lRet = DefWindowProc(hWnd, uMsg, wParam, lParam);
break;
}
return lRet;
}
/// <summary>
/// Sets up Pixel Format.
/// </summary>
/// <param name="hdc: ">A handle to the current DC.</param>
/// <returns>True if success, else false.</returns>
BOOL B_SetupPixelFormat(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd, * ppfd;
int pixelformat;
ppfd = &pfd;
ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
ppfd->nVersion = 1;
ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
ppfd->dwLayerMask = PFD_MAIN_PLANE;
ppfd->iPixelType = PFD_TYPE_COLORINDEX;
ppfd->cColorBits = 8;
ppfd->cDepthBits = 16;
ppfd->cAccumBits = 0;
ppfd->cStencilBits = 0;
pixelformat = ChoosePixelFormat(hdc, ppfd);
if ((pixelformat = ChoosePixelFormat(hdc, ppfd)) == 0)
{
MessageBoxA(NULL, "ChoosePixelFormat failed", "Error", MB_OK);
return FALSE;
}
if (SetPixelFormat(hdc, pixelformat, ppfd) == FALSE)
{
MessageBoxA(NULL, "SetPixelFormat failed", "Error", MB_OK);
return FALSE;
}
return TRUE;
}
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;
}
/// <summary>
/// A function that is activated whenever the window is being resized.
/// </summary>
/// <param name="width: ">The desired width of the window.</param>
/// <param name="height: ">The desired Height of the window.</param>
/// <returns>A value of void.</returns>
GLvoid Resize(GLsizei width, GLsizei height)
{
engine.SetWindowGLParam(width, height);
glViewport(0, 0, width, height);
glLoadIdentity(); //Reset Coordinate System
gluOrtho2D(-(engine.GetOffsetW()), OpenGLWidth + engine.GetOffsetW(), -(engine.GetOffsetH()), OpenGLHeight + engine.GetOffsetH()); //Setting Up 2D ORTHOGRAPHIC projection
glMatrixMode(GL_MODELVIEW); //Changing back mode to MODELVIEW mode to start drawing
DrawScene();
}
/// <summary>
/// Initializes OpenGL.
/// </summary>
/// <param name="width: ">The desired starting width of the window.</param>
/// <param name="height: ">The desired starting height of the window.</param>
/// <returns>A value of void.</returns>
GLvoid InitGL(GLsizei width, GLsizei height)
{
//sets backround color
glClearColor(0.3f, 0.3f, 1.0f, 1.0f);
//load textures into memory
textureManager.LoadAllTextures();
//Setting screen offset + size info
engine.SetWindowGLParam(width, height);
glViewport(0, 0, width, height);
glLoadIdentity(); //Reset Coordinate System
gluOrtho2D(-(engine.GetOffsetW()), OpenGLWidth + engine.GetOffsetW(), -(engine.GetOffsetH()), OpenGLHeight + engine.GetOffsetH()); //Setting Up 2D ORTHOGRAPHIC projection
glMatrixMode(GL_MODELVIEW); //Changing back mode to MODELVIEW mode to start drawing
engine.SetStatus(engine.GameStatus);
}
/// <summary>
/// Draws everything onscreen except backround.
/// </summary>
/// /// <param name="">No parameters required.</param>
/// <returns>A value of void</returns>
GLvoid DrawScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT);
userField.Draw();
enemyField.Draw();
clueField.Draw();
statisticField.Draw();
statusField.Draw();
soundButton.Draw();
switch (engine.GameMode)
{
case Engine::GAMEMODE::Menu:
{
switch (engine.GameStatus)
{
case Engine::GAMESTATUS::NewGame:
{
buttonFieldNewGame.Draw();
}
break;
default:
{
buttonFieldConnect.Draw();
}
}
}
break;
case Engine::GAMEMODE::PVE:
{
switch (engine.GameStatus)
{
case Engine::GAMESTATUS::Deploying:
{
buttonFieldDeploy.Draw();
}
break;
case Engine::GAMESTATUS::MainGame:
{
buttonFieldFire.Draw();
}
break;
}
}
break;
case Engine::GAMEMODE::PVP:
{
switch (engine.GameStatus)
{
case Engine::GAMESTATUS::Deploying:
{
buttonFieldDeploy.Draw();
}
break;
case Engine::GAMESTATUS::MainGame:
{
buttonFieldFire.Draw();
}
break;
}
}
break;
default:
break;
}
switch (engine.animation)
{
case Engine::Animation::None:
{
UserCannonField.Draw();
OpponentCannonField.Draw();
}
break;
case Engine::Animation::Rocket:
{
if (engine.UserShot)
{
UserCannonField.Draw(engine.ShootingAngle);
OpponentCannonField.Draw();
}
else
{
OpponentCannonField.Draw(engine.ShootingAngle);
UserCannonField.Draw();
}
engine.rocket.Draw();
}
break;
case Engine::Animation::MainMenu:
{
engine.menuAnimation.Draw();
UserCannonField.Draw();
OpponentCannonField.Draw();
}
break;
default:
break;
}
SWAPBUFFERS;
}
/// <summary>
/// Function that tells the program when to redraw the window.
/// </summary>
/// <param name="">No parameters required.</param>
void TimerFunc(LPVOID)
{
while (true)
{
Sleep(FrameRate);
TimeToRedraw = true;
}
}