-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.cpp
245 lines (181 loc) · 5.04 KB
/
simulation.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
/*
* File name: simulation.cpp
* Description: Simulation module
*
* Author: Claude C. Chibelushi
* Date: 04/10/04
* Modified 02/10/05, by CCC: addition of calls to flow-control module
* Modified 19/01/09 by CCC: porting to C++
* Modified 06/02/09 by CCC: implementation of double buffering
* (functions changed: simInit) and of changes required for
* illustrative examples of drawing functionality
* (e.g. worldDataModuleInit()).
* Modified 22/01/10 by CCC: flow-control functionality integrated into Simulation class
*
*/
#include "simulation.h" /* include file for simulation */
/* Convention on function return value: 0 means error; 1 means OK */
/*----------------------------------------------------------------------------\
* |
* INITIALISATION AND CLEAN UP |
* |
*----------------------------------------------------------------------------*/
/*
Constructs and initialises simulation module.
Parameter list
none
*/
Simulation::Simulation()
{
/* TO DO: add relevant code */
}
/*
Destructs simulation module
Parameter list
none
*/
Simulation::~Simulation()
{
/* TO DO: add relevant code */
simCleanUp();
return;
}
/*
Simulation entry point.
Parameter list
hWnd: handle of window
message: pointer to message
*/
int Simulation::simMain(HWND hWnd, MSG * msg)
{
if ( simInit(hWnd) )
{
simLoop(hWnd, msg);
simCleanUp();
return 1;
}
return 0;
}
/*
Simulation initialisation.
Parameter list
hWnd: handle of window
*/
int Simulation::simInit(HWND hWnd)
{
frameRate = 2;
// Initialise: world data module,
// graphics module,
// dynamics / behaviour module,
// flow control module
if( world.worldDataModuleInit(&graphics) &&
graphics.graphicsModuleInit(hWnd) )
{
ShowCursor(FALSE); /* hide mouse cursor */
return 1;
}
return 0;
}
/*
Simulation clean-up prior to shut-down.
Parameter list
none
*/
int Simulation::simCleanUp()
{
world.worldCleanUp(); // release world objects
graphics.graphicsCleanUp(); // release graphics objects
ShowCursor(TRUE); // show mouse cursor
return 1;
}
/*----------------------------------------------------------------------------\
* |
* GAME LOOP |
* |
*----------------------------------------------------------------------------*/
/*
Simulation loop.
Parameter list
hWnd: handle of window
message: pointer to message
*/
int Simulation::simLoop(HWND hWnd, MSG * msg)
{
frameStartTime = GetTickCount();
//Sleep(20);
/* Main loop */
ZeroMemory( msg, sizeof(*msg) );
/* retrieve messages (for e.g. user interaction devices) */
while( msg->message != WM_QUIT ) /* drop out of loop if "quit" message received */
{
/* Windows-part of loop body */
if( PeekMessage( msg, NULL, 0U, 0U, PM_REMOVE ) )
/* Note: PeekMessage() does not wait for message to be placed in queue
before returning (whereas GetMessage() does wait) */
{
TranslateMessage( msg ); /* translate keyboard message */
DispatchMessage( msg ); /* dispatch message to window procedure (via Windows) */
}
gameLoopDelay(frameStartTime);
/* Simulation-part of loop body */
simProc(hWnd);
}
return 1;
}
int Simulation::gameLoopDelay(int frameStartTime)
{
frameProcessingTime = GetTickCount() - frameStartTime;
if(frameProcessingTime < frameRate)
{
Sleep(frameRate - frameProcessingTime);
}
return 1;
}
/*
Simulation processing.
Parameter list
hWnd: handle of window
*/
int Simulation::simProc(HWND hWnd)
{
keyEvent event;
/* Read user input (if any), and activate control logic */
event = kbd.checkUserInput(hWnd);
simControlLogic(hWnd, event);
return 1;
}
/*----------------------------------------------------------------------------\
* |
* CONTROL LOGIC BASED ON USER INPUT |
* |
*----------------------------------------------------------------------------*/
/*
Control logic.
Parameter list
hWnd: handle of window
event: code for user input event
*/
int Simulation::simControlLogic(HWND hWnd, keyEvent event)
{
if (event != QUIT)
{
world.update(event); // update parameters of virtual world
simDisplayFrame(hWnd); // display frame
}
return 1;
}
/*
Draws a frame of the animation (using graphics context of backbuffer).
Parameter list
none
*/
int Simulation::simDisplayFrame(HWND hWnd)
{
// (i.1) display HUD
graphics.drawHUD(); // draw HUD
// (i.2) display world on HUD
world.draw(&graphics);
// (ii) display back buffer
graphics.displayBackBuffer(hWnd);
return 1;
}