-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_asteroids.cpp
More file actions
550 lines (506 loc) · 14.4 KB
/
Copy path13_asteroids.cpp
File metadata and controls
550 lines (506 loc) · 14.4 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <glm/glm.hpp>
#include <imgui.h>
#include <imgui_impl_sdl3.h>
#include <imgui_impl_sdlrenderer3.h>
#include <savepoint/imgui.hpp>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <format>
#include <numbers>
#include <random>
#include <string>
#include <vector>
static constexpr int kWidth = 960;
static constexpr int kHeight = 720;
static glm::vec2 Direction(float rotation);
static void Wrap(glm::vec2& position);
static glm::vec2 Offset(glm::vec2 a, glm::vec2 b);
static float RandomFloat();
static int RandomInt();
static void Lines(const std::vector<glm::vec2>& points, glm::vec2 position, float rotation);
void Visit(SavepointVisitor& visitor, glm::vec2& vector)
{
visitor(vector.x);
visitor(vector.y);
}
struct Entity
{
glm::vec2 Position;
glm::vec2 Velocity;
void Visit(SavepointVisitor& visitor)
{
visitor(Position);
visitor(Velocity);
}
};
struct Bullet : Entity
{
float Life = 0.0f;
void Update(float deltaTime)
{
Position += Velocity * deltaTime;
Life -= deltaTime;
Wrap(Position);
}
void Draw(SDL_Renderer* renderer) const
{
SDL_FRect rect{Position.x - 2.0f, Position.y - 2.0f, 4.0f, 4.0f};
SDL_SetRenderDrawColor(renderer, 255, 225, 125, 255);
SDL_RenderFillRect(renderer, &rect);
}
void Visit(SavepointVisitor& visitor)
{
Entity::Visit(visitor);
visitor(Life);
}
};
struct Player : Entity
{
float Rotation = -std::numbers::pi_v<float> * 0.5f;
float ShotTimer = 0.0f;
float InvincibilityTimer = 2.0f;
float Radius = 9.0f;
int Lives = 3;
bool IsAccelerating = false;
std::vector<Bullet> Bullets;
Player()
{
Position = {kWidth * 0.5f, kHeight * 0.5f};
}
void Fire()
{
if (ShotTimer > 0.0f || Bullets.size() >= 8 || Lives == 0)
{
return;
}
glm::vec2 direction = Direction(Rotation);
Bullet bullet;
bullet.Position = Position + direction * 14.0f;
bullet.Velocity = Velocity + direction * 390.0f;
bullet.Life = 1.2f;
Bullets.push_back(bullet);
ShotTimer = 0.18f;
}
void Update(float deltaTime)
{
const bool* keys = SDL_GetKeyboardState(nullptr);
Rotation += (keys[SDL_SCANCODE_D] - keys[SDL_SCANCODE_A]) * 3.8f * deltaTime;
IsAccelerating = keys[SDL_SCANCODE_W];
if (IsAccelerating)
{
Velocity += Direction(Rotation) * 190.0f * deltaTime;
}
if (keys[SDL_SCANCODE_SPACE])
{
Fire();
}
Velocity *= std::pow(0.72f, deltaTime);
if (glm::length(Velocity) > 300.0f)
{
Velocity = glm::normalize(Velocity) * 300.0f;
}
Position += Velocity * deltaTime;
Wrap(Position);
ShotTimer = std::max(0.0f, ShotTimer - deltaTime);
InvincibilityTimer = std::max(0.0f, InvincibilityTimer - deltaTime);
for (Bullet& bullet : Bullets)
{
bullet.Update(deltaTime);
}
std::erase_if(Bullets, [](const Bullet& bullet) { return bullet.Life <= 0.0f; });
}
void Destroy()
{
Lives--;
Position = {kWidth * 0.5f, kHeight * 0.5f};
Velocity = {};
Rotation = -std::numbers::pi_v<float> * 0.5f;
InvincibilityTimer = 2.5f;
}
void Draw() const
{
if (InvincibilityTimer == 0.0f || int(InvincibilityTimer * 10.0f) % 2)
{
Lines({{15.0f, 0.0f}, {-11.0f, -9.0f}, {-7.0f, 0.0f}, {-11.0f, 9.0f}}, Position, Rotation);
if (IsAccelerating)
{
Lines({{-8.0f, -5.0f}, {-19.0f, 0.0f}, {-8.0f, 5.0f}}, Position, Rotation);
}
}
}
void Visit(SavepointVisitor& visitor)
{
Entity::Visit(visitor);
visitor(Rotation);
visitor(ShotTimer);
visitor(InvincibilityTimer);
visitor(Lives);
visitor(Bullets);
}
};
struct Asteroid : Entity
{
float Radius = 40.0f;
float Rotation = 0.0f;
float RotationRate = 0.0f;
uint32_t Seed = 0;
void Spawn(glm::vec2 position, float radius)
{
float rotation = RandomFloat() * std::numbers::pi_v<float> * 2.0f;
Position = position;
Velocity = Direction(rotation) * (35.0f + RandomFloat() * 35.0f);
Radius = radius;
Rotation = RandomFloat() * std::numbers::pi_v<float> * 2.0f;
RotationRate = (RandomFloat() * 2.0f - 1.0f) * 0.8f;
Seed = RandomInt();
}
void Update(float deltaTime)
{
Position += Velocity * deltaTime;
Rotation += RotationRate * deltaTime;
Wrap(Position);
}
int Split(std::vector<Asteroid>& asteroids) const
{
if (Radius <= 20.0f)
{
return 100;
}
float radius = Radius * 0.56f;
asteroids.emplace_back().Spawn(Position, radius);
asteroids.emplace_back().Spawn(Position, radius);
return Radius > 30.0f ? 20 : 50;
}
void Draw() const
{
std::minstd_rand generator{Seed};
std::vector<glm::vec2> points;
for (int i = 0; i < 10; i++)
{
float rotation = i / 10.0f * std::numbers::pi_v<float> * 2.0f;
float radius = Radius * (0.78f + generator() % 23 * 0.01f);
points.push_back(Direction(rotation) * radius);
}
Lines(points, Position, Rotation);
}
void Visit(SavepointVisitor& visitor)
{
Entity::Visit(visitor);
visitor(Radius);
visitor(Rotation);
visitor(RotationRate);
visitor(Seed);
}
};
struct State
{
std::minstd_rand Generator;
Player User;
std::vector<Asteroid> Asteroids;
int Score = 0;
int Round = 0;
State()
{
Generator.seed(SDL_GetTicks());
}
void Spawn()
{
Round++;
Asteroids.clear();
User.Bullets.clear();
int count = std::min(10, Round + 3);
for (int i = 0; i < count; i++)
{
glm::vec2 position;
do
{
position = {RandomFloat() * kWidth, RandomFloat() * kHeight};
}
while (glm::length(Offset(position, User.Position)) < 180.0f);
Asteroid& asteroid = Asteroids.emplace_back();
asteroid.Spawn(position, 42.0f);
}
}
void Visit(SavepointVisitor& visitor)
{
visitor(User);
visitor(Generator);
visitor(Score);
visitor(Round);
visitor(Asteroids);
}
};
static SDL_Window* window;
static SDL_Renderer* renderer;
static Savepoint savepoint;
static State state;
static SavepointDebugger debugger;
static bool showDebugger = false;
static void Wrap(glm::vec2& position)
{
if (position.x < 0.0f)
{
position.x += kWidth;
}
if (position.x >= kWidth)
{
position.x -= kWidth;
}
if (position.y < 0.0f)
{
position.y += kHeight;
}
if (position.y >= kHeight)
{
position.y -= kHeight;
}
}
static glm::vec2 Offset(glm::vec2 a, glm::vec2 b)
{
glm::vec2 offset = a - b;
if (offset.x > kWidth / 2)
{
offset.x -= kWidth;
}
if (offset.x < -kWidth / 2)
{
offset.x += kWidth;
}
if (offset.y > kHeight / 2)
{
offset.y -= kHeight;
}
if (offset.y < -kHeight / 2)
{
offset.y += kHeight;
}
return offset;
}
static glm::vec2 Direction(float rotation)
{
return {std::cos(rotation), std::sin(rotation)};
}
static float RandomFloat()
{
return std::generate_canonical<float, 24>(state.Generator);
}
static int RandomInt()
{
return state.Generator();
}
static void Lines(const std::vector<glm::vec2>& points, glm::vec2 position, float rotation)
{
float c = std::cos(rotation);
float s = std::sin(rotation);
glm::mat2 rotationMatrix{{c, s}, {-s, c}};
SDL_SetRenderDrawColor(renderer, 210, 227, 225, 255);
for (size_t i = 0; i < points.size(); i++)
{
glm::vec2 a = rotationMatrix * points[i] + position;
glm::vec2 b = rotationMatrix * points[(i + 1) % points.size()] + position;
SDL_RenderLine(renderer, a.x, a.y, b.x, b.y);
}
}
static void Text(float x, float y, std::string text, float size)
{
SDL_SetRenderDrawColor(renderer, 220, 235, 232, 255);
SDL_SetRenderScale(renderer, size, size);
SDL_RenderDebugText(renderer, x / size, y / size, text.c_str());
SDL_SetRenderScale(renderer, 1.0f, 1.0f);
}
static void CenteredText(float y, std::string text, float size)
{
Text((kWidth - text.size() * 8.0f * size) * 0.5f, y, text, size);
}
static void Restart()
{
state = {};
state.Spawn();
}
static void Save()
{
savepoint.Write(state);
savepoint.Save();
debugger.Refresh();
}
static void Load()
{
savepoint.Read(state);
debugger.Refresh();
}
static void Update(float deltaTime)
{
Player& player = state.User;
if (player.Lives == 0)
{
return;
}
player.Update(deltaTime);
for (Asteroid& asteroid : state.Asteroids)
{
asteroid.Update(deltaTime);
}
for (int bullet = 0; bullet < player.Bullets.size();)
{
bool hit = false;
for (int asteroid = 0; asteroid < state.Asteroids.size(); asteroid++)
{
glm::vec2 offset = Offset(player.Bullets[bullet].Position, state.Asteroids[asteroid].Position);
if (glm::length(offset) < state.Asteroids[asteroid].Radius)
{
player.Bullets.erase(player.Bullets.begin() + bullet);
Asteroid hitAsteroid = state.Asteroids[asteroid];
state.Asteroids.erase(state.Asteroids.begin() + asteroid);
state.Score += hitAsteroid.Split(state.Asteroids);
hit = true;
break;
}
}
if (!hit)
{
bullet++;
}
}
if (player.InvincibilityTimer == 0.0f)
{
for (const Asteroid& asteroid : state.Asteroids)
{
glm::vec2 offset = Offset(player.Position, asteroid.Position);
if (glm::length(offset) < asteroid.Radius + player.Radius)
{
player.Destroy();
break;
}
}
}
if (state.Asteroids.empty())
{
state.Spawn();
}
}
static void Draw()
{
const Player& player = state.User;
SDL_SetRenderDrawColor(renderer, 2, 7, 11, 255);
SDL_RenderClear(renderer);
std::minstd_rand generator{12345};
for (int i = 0; i < 100; i++)
{
uint32_t star = generator();
SDL_SetRenderDrawColor(renderer, 100, 120, 120, 255);
SDL_RenderPoint(renderer, float(star % kWidth), float((star >> 12) % kHeight));
}
for (const Asteroid& asteroid : state.Asteroids)
{
asteroid.Draw();
}
for (const Bullet& bullet : player.Bullets)
{
bullet.Draw(renderer);
}
player.Draw();
Text(20.0f, 18.0f, std::format("SCORE {:06}", state.Score), 1.5f);
CenteredText(18.0f, std::format("WAVE {}", state.Round), 1.5f);
Text(20.0f, 52.0f, std::format("LIVES {}", player.Lives), 1.0f);
Text(20.0f, kHeight - 28.0f, "A/D TURN W ACCELERATE SPACE FIRE F5 SAVE F9 LOAD N NEW F10 DEBUG", 1.0f);
if (player.Lives == 0)
{
SDL_FRect rect{0.0f, 0.0f, kWidth, kHeight};
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 2, 7, 11, 210);
SDL_RenderFillRect(renderer, &rect);
CenteredText(250.0f, "GAME OVER", 3.0f);
CenteredText(315.0f, "N FOR NEW GAME", 1.0f);
}
}
static void DrawDebugger()
{
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();
ImGui::NewFrame();
const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBringToFrontOnFocus;
if (ImGui::Begin("13_SavepointDebugger", &showDebugger, flags))
{
debugger.Render(savepoint);
}
ImGui::End();
ImGui::Render();
SDL_SetRenderLogicalPresentation(renderer, 0, 0, SDL_LOGICAL_PRESENTATION_DISABLED);
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer);
SDL_SetRenderLogicalPresentation(renderer, kWidth, kHeight, SDL_LOGICAL_PRESENTATION_LETTERBOX);
}
int main(int argc, char** argv)
{
SavepointSetLogFunction([](std::string_view string) { SDL_Log("%.*s", int(string.size()), string.data()); });
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer("Asteroids", kWidth, kHeight, SDL_WINDOW_RESIZABLE, &window, &renderer);
SDL_SetRenderLogicalPresentation(renderer, kWidth, kHeight, SDL_LOGICAL_PRESENTATION_LETTERBOX);
SDL_SetRenderVSync(renderer, 1);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer3_Init(renderer);
switch (savepoint.Open(SavepointDriver::SQLite3, "asteroids.sqlite3", SavepointVersion{}))
{
case SavepointStatus::New:
Restart();
break;
case SavepointStatus::Existing:
Load();
break;
}
uint64_t ticks1 = SDL_GetTicks();
bool running = true;
while (running)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
ImGui_ImplSDL3_ProcessEvent(&event);
switch (event.type)
{
case SDL_EVENT_QUIT:
running = false;
break;
case SDL_EVENT_KEY_DOWN:
switch (event.key.scancode)
{
case SDL_SCANCODE_N:
Restart();
break;
case SDL_SCANCODE_F5:
Save();
break;
case SDL_SCANCODE_F9:
Load();
break;
case SDL_SCANCODE_F10:
showDebugger = !showDebugger;
break;
}
break;
}
}
uint64_t ticks2 = SDL_GetTicks();
Update(float(ticks2 - ticks1) / 1000.0f);
ticks1 = ticks2;
Draw();
if (showDebugger)
{
DrawDebugger();
}
SDL_RenderPresent(renderer);
}
Save();
savepoint.Close();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}