-
Notifications
You must be signed in to change notification settings - Fork 0
/
winmain.cpp
171 lines (146 loc) · 5.73 KB
/
winmain.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
/*
Group #: 3
Members: Chris Prosser, Jacob Gearhart, Kory Kappel,
Course: COMP 322, Advanced Programming
Date: 28 October 2013
Description: This file is inherited from the skeleton
provided by Charles Kelly.
*/
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Space War winmain.cpp v1.0
#define _CRTDBG_MAP_ALLOC // for detecting memory leaks
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdlib.h> // for detecting memory leaks
#include <crtdbg.h> // for detecting memory leaks
#include "level.h"
// Function prototypes
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
bool CreateMainWindow(HWND &, HINSTANCE, int);
LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
// Game pointer
level *game = NULL;
HWND hwnd = NULL;
//=============================================================================
// Starting point for a Windows application
//=============================================================================
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
// Check for memory leak if debug build
#if defined(DEBUG) | defined(_DEBUG)
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif
MSG msg;
// Create the game, sets up message handler
game = new level;
// Create the window
if (!CreateMainWindow(hwnd, hInstance, nCmdShow))
return 1;
try{
game->initialize(hwnd); // throws GameError
// main message loop
int done = 0;
while (!done)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// look for quit message
if (msg.message == WM_QUIT)
done = 1;
// decode and pass messages on to WinProc
TranslateMessage(&msg);
DispatchMessage(&msg);
} else
game->run(hwnd); // run the game loop
}
SAFE_DELETE (game); // free memory before exit
return msg.wParam;
}
catch(const GameError &err)
{
game->deleteAll();
DestroyWindow(hwnd);
MessageBox(NULL, err.getMessage(), "Error", MB_OK);
}
catch(...)
{
game->deleteAll();
DestroyWindow(hwnd);
MessageBox(NULL, "Unknown error occured in game.", "Error", MB_OK);
}
SAFE_DELETE (game); // free memory before exit
return 0;
}
//=============================================================================
// window event callback function
//=============================================================================
LRESULT WINAPI WinProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
return (game->messageHandler(hwnd, msg, wParam, lParam));
}
//=============================================================================
// Create the window
// Returns: false on error
//=============================================================================
bool CreateMainWindow(HWND &hwnd, HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wcx;
// Fill in the window class structure with parameters
// that describe the main window.
wcx.cbSize = sizeof(wcx); // size of structure
wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes
wcx.lpfnWndProc = WinProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hInstance; // handle to instance
wcx.hIcon = NULL;
wcx.hCursor = LoadCursor(NULL,IDC_ARROW); // predefined arrow
wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // black background
wcx.lpszMenuName = NULL; // name of menu resource
wcx.lpszClassName = CLASS_NAME; // name of window class
wcx.hIconSm = NULL; // small class icon
// Register the window class.
// RegisterClassEx returns 0 on error.
if (RegisterClassEx(&wcx) == 0) // if error
return false;
//set up the screen in windowed or fullscreen mode?
DWORD style;
if (FULLSCREEN)
style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
else
style = WS_OVERLAPPEDWINDOW;
// Create window
hwnd = CreateWindow(
CLASS_NAME, // name of the window class
GAME_TITLE, // title bar text
style, // window style
CW_USEDEFAULT, // default horizontal position of window
CW_USEDEFAULT, // default vertical position of window
GAME_WIDTH, // width of window
GAME_HEIGHT, // height of the window
(HWND) NULL, // no parent window
(HMENU) NULL, // no menu
hInstance, // handle to application instance
(LPVOID) NULL); // no window parameters
// if there was an error creating the window
if (!hwnd)
return false;
if(!FULLSCREEN) // if window
{
// Adjust window size so client area is GAME_WIDTH x GAME_HEIGHT
RECT clientRect;
GetClientRect(hwnd, &clientRect); // get size of client area of window
MoveWindow(hwnd,
0, // Left
0, // Top
GAME_WIDTH+(GAME_WIDTH-clientRect.right), // Right
GAME_HEIGHT+(GAME_HEIGHT-clientRect.bottom), // Bottom
TRUE); // Repaint the window
}
// Show the window
ShowWindow(hwnd, nCmdShow);
return true;
}