Repository navigation
Expand file tree
/
Copy pathgame.cpp
More file actions
373 lines (313 loc) · 13.3 KB
/
Copy pathgame.cpp
File metadata and controls
373 lines (313 loc) · 13.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
#include "game.h"
#include "memory.cpp"
#include "transform_2d.cpp"
#include "physics_body_2d.cpp"
#include "collision_2d.cpp"
#include "camera.cpp"
internal void ExportAndRenderDebugRecords(game_memory* Memory, game_render_commands* RenderCommands);
vec4
GenerateDarkRandomColor()
{
real32 R = 0.5f * RandomUnilateral();
real32 G = 0.5f * RandomUnilateral();
real32 B = 0.5f * RandomUnilateral();
return vec(R, G, B, 1.0f);
}
void
GenerateDarkRandomColors(vec4* ColorArray, int ArrayCount)
{
for(int i=0;
i < ArrayCount;
i++)
{
ColorArray[i] = GenerateDarkRandomColor();
}
}
void
UpdatePhysicsWorld2d(physics_world2D* World, real32 ElapsedTime, int Iterations)
{
TIMED_BLOCK(PhysicsUpdateTotal);
Iterations = Clamp(Iterations, World->MinIteration, World->MaxIteration);
real32 SubStepTime = ElapsedTime / Iterations;
for(int it = 0;
it < Iterations;
it++)
{
// NOTE: Movement step
for(uint32 i = 0;
i < World->BodyCount;
i++)
{
ApplyGravity(&World->Bodies[i],
World->Gravity,
World->ForceMultiplier,
SubStepTime);
ApplyForce(&World->Bodies[i], SubStepTime);
RotatePhysicsBody(&World->Bodies[i], SubStepTime);
MovePhysicsBody(&World->Bodies[i], SubStepTime);
// NOTE: Reset the force to zero
World->Bodies[i].Force = vec(0.0f, 0.0f);
}
// NOTE: Reset collision state
for(uint32 i = 0;
i < World->BodyCount;
i++)
{
World->Bodies[i].IsCollided = false;
}
World->CollisionManifoldsCount = 0;
World->ContactPairsCount = 0;
World->ContactPointsCount = 0;
CollisionResolutionBroadPhase(World);
CollisionResolutionNarrowPhase(World);
}
}
// TODO: Pass only camera and bodies array? Or maybe entities
void
DeleteOutofReachPhysicsBodies(game_state* GameState)
{
TIMED_FUNCTION;
// TODO: Only the delete the body from fixed position of the screen, not relative to camera position?
real32 MaxViewWidth = GameState->Camera.Width / GameState->Camera.MinZoom;
real32 MaxViewHeight = GameState->Camera.Height / GameState->Camera.MinZoom;
real32 BoundLeft = GameState->Camera.Position.x - (MaxViewWidth / 2.0f);
real32 BoundRight = GameState->Camera.Position.x + (MaxViewWidth / 2.0f);
real32 BoundTop = GameState->Camera.Position.y - (MaxViewHeight / 2.0f);
real32 BoundBottom = GameState->Camera.Position.y + (MaxViewHeight / 2.0f);
for(uint32 i = 0;
i < GameState->World->BodyCount;)
{
if(!GameState->World->Bodies[i].IsStatic)
{
AABB aabb = GetAABBFromPhysicsBody(&GameState->World->Bodies[i]);
if(aabb.Max.x < BoundLeft || aabb.Min.x > BoundRight || aabb.Max.y < BoundTop || aabb.Min.y > BoundBottom)
{
// TODO: Replace normal array with linkedlist for deletion
for(uint32 j = i; j < GameState->World->BodyCount - 1; j++)
{
GameState->World->Bodies[j] = GameState->World->Bodies[j+1];
GameState->BodyColors[j] = GameState->BodyColors[j+1];
}
GameState->World->BodyCount -= 1;
GameState->BodyColorsCount -= 1;
continue;
}
}
i++;
}
}
void
InitGameState(game_memory* Memory, game_state* GameState)
{
if(!Memory->IsInitialized)
{
InitializeRandomNumbers();
MemoryArenaInit(&GameState->WorldArena,
(uint8*)Memory->PermanentStorage + sizeof(game_state),
Memory->PermanentStorageSize - sizeof(game_state));
GameState->Camera = {};
GameState->Camera.Zoom = 1.0f;
GameState->Camera.Width = 1300.0f;
GameState->Camera.Height = 800.0f;
GameState->Camera.Position = vec(650.0f, 400.0f);
GameState->Camera.MaxZoom = 10.0f;
GameState->Camera.MinZoom = 1.0f;
GameState->HighlightCollisions = false;
GameState->WireframeMode = false;
GameState->BodyColors = PushArray(&GameState->WorldArena, MAX_BODIES, vec4);
GameState->BodyColorsCount = 0;
GameState->World = PushStruct(&GameState->WorldArena, physics_world2D);
GameState->World->Bodies = PushArray(&GameState->WorldArena, MAX_BODIES, physics_body2D);
GameState->World->BodyCount = 0;
GameState->World->ContactPairs = PushArray(&GameState->WorldArena, MAX_CONTACT_PAIRS, contact_pair);
GameState->World->ContactPairsCount = 0;
GameState->World->ContactPoints = PushArray(&GameState->WorldArena, MAX_CONTACT_POINTS, vec2);
GameState->World->ContactPointsCount = 0;
GameState->World->CollisionManifolds = PushArray(&GameState->WorldArena, MAX_COLLISION_MANIFOLDS, collision_manifold);
GameState->World->CollisionManifoldsCount = 0;
// Spawn default platforms
real32 ScreenWidth = 1300.0f;
real32 ScreenHeight = 800.0f;
real32 Padding20 = 20.0f;
physics_body2D BottomPlatform = CreateBoxPhysicsBody2D(GameState->World,
vec(ScreenWidth / 2.0f, ScreenHeight - 6.0f * Padding20),
ScreenWidth / 2.0f,
Padding20 + 1.0f,
0.5f,
0.2f,
true);
BottomPlatform.Angle = PI;
GameState->World->Bodies[GameState->World->BodyCount++] = BottomPlatform;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
physics_body2D LeftPaddle = CreateBoxPhysicsBody2D(GameState->World,
vec(ScreenWidth / 2.5f, ScreenHeight / 2.0f),
ScreenWidth / 4.0f,
Padding20 + 1.0f,
0.5f,
0.5f,
true);
LeftPaddle.Angle = PI / 4.0f;
GameState->World->Bodies[GameState->World->BodyCount++] = LeftPaddle;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
physics_body2D RightPaddle = CreateBoxPhysicsBody2D(GameState->World,
vec(ScreenWidth / 1.5f, ScreenHeight / 2.0f),
ScreenWidth / 4.0f,
Padding20 + 1.0f,
0.5f,
0.5f,
true);
RightPaddle.Angle = -PI / 4.0f;
GameState->World->Bodies[GameState->World->BodyCount++] = RightPaddle;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
// Spawn a couple of demo falling bodies
physics_body2D DemoCircle = CreateCirclePhysicsBody2D(GameState->World,
vec(ScreenWidth / 2.0f - 60.0f, 150.0f),
16.0f, 0.6f, 0.85f, false);
GameState->World->Bodies[GameState->World->BodyCount++] = DemoCircle;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
physics_body2D DemoBox = CreateBoxPhysicsBody2D(GameState->World,
vec(ScreenWidth / 2.0f + 60.0f, 120.0f),
32.0f, 32.0f, 0.6f, 0.60f, false);
GameState->World->Bodies[GameState->World->BodyCount++] = DemoBox;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
Memory->IsInitialized = true;
}
}
// TODO: Optimize this function?
void
HandleInput(game_input* Input, game_state* GameState)
{
if(Input->MouseButtons[1].EndedDown)
{
PanCamera(&GameState->Camera, Input->MouseRel.x, Input->MouseRel.y);
}
if(Input->MouseWheelDelta != 0)
{
ZoomCamera(&GameState->Camera, Input->MousePos.x, Input->MousePos.y, Input->MouseWheelDelta);
}
vec2 MouseWorldPos = GetRelativeWorldPosition(&GameState->Camera, Input->MousePos.x, Input->MousePos.y);
if(WasPressed(Input->SpawnCircle) || WasPressed(Input->MouseButtons[0]))
{
physics_body2D Body = CreateCirclePhysicsBody2D(GameState->World, MouseWorldPos, 14.0f, 0.6f, 0.85f, false);
GameState->World->Bodies[GameState->World->BodyCount++] = Body;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
}
if(WasPressed(Input->SpawnBox))
{
physics_body2D Body = CreateBoxPhysicsBody2D(GameState->World, MouseWorldPos, 28.0f, 28.0f, 0.6f, 0.60f, false);
GameState->World->Bodies[GameState->World->BodyCount++] = Body;
GameState->BodyColors[GameState->BodyColorsCount++] = GenerateDarkRandomColor();
}
if(WasPressed(Input->Reset))
{
GameState->World->BodyCount = 3;
GameState->BodyColorsCount = 3;
}
if(WasPressed(Input->ToggleWireframe))
{
GameState->WireframeMode = !GameState->WireframeMode;
}
if(WasPressed(Input->HighlightCollisions))
{
GameState->HighlightCollisions = !GameState->HighlightCollisions;
}
if(WasPressed(Input->ChangeProfileMode))
{
if(GameState->ProfilerMode == NO_PROFILING)
{
GameState->ProfilerMode = HOST_PROFILE;
}
else if(GameState->ProfilerMode == HOST_PROFILE)
{
GameState->ProfilerMode = GAME_PROFILE;
}
else
{
GameState->ProfilerMode = NO_PROFILING;
}
}
}
extern "C"
#ifdef _WIN32
__declspec(dllexport)
#else
__attribute__((visibility("default")))
#endif
GAME_UPDATE_AND_RENDER(GameUpdateAndRender)
{
Assert(sizeof(game_state) <= Memory->PermanentStorageSize);
game_state* GameState = (game_state*)Memory->PermanentStorage;
InitGameState(Memory, GameState);
HandleInput(Input, GameState);
// TODO: Handle physics in a seperate thread?
UpdatePhysicsWorld2d(GameState->World, Input->dtForFrame, 128);
DeleteOutofReachPhysicsBodies(GameState);
PushClear(RenderCommands, vec(0.00f, 0.00f, 0.00f, 1.0f));
// TODO: register render commands in separate thread?
for(uint32 i = 0;
i < GameState->World->BodyCount;
i++)
{
physics_body2D* Body = &GameState->World->Bodies[i];
vec4 OutlineColor = WHITE;
if(GameState->HighlightCollisions && Body->IsCollided)
{
OutlineColor = RED;
}
if(Body->Shape == BOX)
{
vec2* Verts = GetPhysicsBodyTransformedVertices(Body);
vec2 ScreenVerts[4];
for(int v = 0; v < 4; v++)
{
ScreenVerts[v] = WorldToScreen(&GameState->Camera, Verts[v]);
}
PushQuad(RenderCommands, ScreenVerts, GameState->BodyColors[i], OutlineColor, true);
}
else if(Body->Shape == CIRCLE)
{
vec2 ScreenPos = WorldToScreen(&GameState->Camera, Body->Position);
real32 ScaledRadius = Body->Radius * GameState->Camera.Zoom;
PushCircle(RenderCommands, ScreenPos, ScaledRadius,
Body->Angle, GameState->BodyColors[i], OutlineColor, true);
}
}
// TODO: Use temporary memory?
char Buf[128];
int FPS = (Input->dtForFrame > 0.0001f) ? (int)(1.0f / Input->dtForFrame) : 0;
snprintf(Buf, sizeof(Buf), "FPS: %d | Bodies: %d | Zoom: %0.2f [C/Click: Circle, B: Box, R: Reset]",
FPS, GameState->World->BodyCount, GameState->Camera.Zoom);
PushText(RenderCommands, vec(20.0f, 20.0f), Buf, WHITE, 24);
Memory->ProfilerMode = GameState->ProfilerMode;
if(Memory->ProfilerMode == GAME_PROFILE)
{
ExportAndRenderDebugRecords(Memory, RenderCommands);
}
}
debug_record DebugRecordArray[__COUNTER__];
// TODO: Create a debug ui framework, or integrate Dear-Imgui
internal void
ExportAndRenderDebugRecords(game_memory* Memory, game_render_commands* RenderCommands)
{
Memory->DebugRecordCount = (uint32)ARRAY_COUNT(DebugRecordArray);
Memory->DebugRecords = DebugRecordArray;
real32 DebugY = 50.0f;
for(uint32 RecordIndex = 0; RecordIndex < (uint32)ARRAY_COUNT(DebugRecordArray); ++RecordIndex)
{
debug_record* Record = DebugRecordArray + RecordIndex;
if(Record->BlockName)
{
// TODO: Use temporary memory?
char RecordBuf[256];
real32 MegaCycles = (real32)Record->CycleCount / (1000.0f * 1000.0f);
uint32 CyclesPerHit = (Record->HitCount > 0) ? (uint32)(Record->CycleCount / Record->HitCount) : 0;
snprintf(RecordBuf, sizeof(RecordBuf),
"%s: %.2f Mcy (%u hits, %u cy/hit)",
Record->BlockName, MegaCycles, Record->HitCount, CyclesPerHit);
PushText(RenderCommands, vec(20.0f, DebugY), RecordBuf, YELLOW, 10);
DebugY += 24.0f;
Record->CycleCount = 0;
Record->HitCount = 0;
}
}
}