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
|
#include "games/Game.hpp"
#include "games/tetris/Tetromino.hpp"
#include "games/tetris/Tetris.hpp"
#include "renderer/Renderer.hpp"
#include "common/MemoryManager.hpp"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_timer.h>
#include <imgui.h>
#include <fstream>
#include <iostream>
#include <string>
Tetris::Tetris()
: m_font{s_dejavu_sans_mono_filepath, 22}
, m_active_tetromino{m_board.m_bitmap}
, m_highscore {ReadHighscore()}
{
Start();
}
void
Tetris::Start()
{
m_tlast_milliseconds = SDL_GetTicks();
m_dt_remaining_seconds = 0.0f;
m_board.Reset();
m_active_tetromino.Reset(Tetromino::GenerateRandomId());
m_next_tetromino_id = Tetromino::GenerateRandomId();
memset(m_tetromino_counters, 0, sizeof(m_tetromino_counters));
m_tetromino_counters[m_active_tetromino.GetId()] += 1;
m_score = 0;
m_line_counter = 0;
m_starting_level = 0;
m_level = 0;
m_softdrop_counter = 0;
m_game_status = game_resume;
}
void
Tetris::FinishUpdate(float dt)
{
if (m_game_status == game_resume) {
uint32_t softdrop_count = GetSoftdropCount(dt);
for (uint32_t i{0}; i < softdrop_count; i++) {
bool moved_down = m_active_tetromino.MaybeMoveDown();
if (!moved_down) {
HandleTetrominoPlacement();
}
}
}
}
void
Tetris::ProcessEvent(SDL_Event& event)
{
switch (event.type) {
case SDL_EVENT_KEY_DOWN: {
auto key = event.key.key;
if (key == SDLK_RIGHT) {
m_active_tetromino.MaybeMoveHorizontally(Tetromino::right);
}
else if (key == SDLK_LEFT) {
m_active_tetromino.MaybeMoveHorizontally(Tetromino::left);
}
else if (key == SDLK_DOWN) {
bool moved_down = m_active_tetromino.MaybeMoveDown();
if (!moved_down) {
HandleTetrominoPlacement();
}
else {
m_softdrop_counter++;
}
}
else if (key == SDLK_X) {
m_active_tetromino.MaybeRotate(Tetromino::rotate_clockwise);
}
else if (key == SDLK_Z || key == SDLK_Y) {
m_active_tetromino.MaybeRotate(Tetromino::rotate_counter_clockwise);
}
}
default:;
}
}
void
Tetris::HandleTetrominoPlacement()
{
int32_t rows_cleared = m_board.PlaceTetromino(m_active_tetromino);
m_active_tetromino.Reset(m_next_tetromino_id);
m_next_tetromino_id = Tetromino::GenerateRandomId();
if (m_active_tetromino.IsCollisionWithBoard()) {
m_game_status = game_over;
if (m_score > m_highscore) {
m_highscore = m_score;
WriteHighscore();
}
}
m_line_counter += rows_cleared;
m_tetromino_counters[m_active_tetromino.GetId()] += 1;
if (rows_cleared == 1) {
m_score += 40 * (m_level + 1);
}
else if (rows_cleared == 2) {
m_score += 100 * (m_level + 1);
}
else if (rows_cleared == 3) {
m_score += 300 * (m_level + 1);
}
else if (rows_cleared == 4) {
m_score += 1200 * (m_level + 1);
}
m_score += m_softdrop_counter;
m_softdrop_counter = 0;
m_level = m_starting_level + m_line_counter / 10;
}
uint32_t
Tetris::GetSoftdropCount(float dt)
{
float nes_frame_time = 1.0f / 60;
int32_t nes_frames_per_cell;
if (m_level <= 8) nes_frames_per_cell = 48 - m_level * 5;
else if (m_level == 9) nes_frames_per_cell = 6;
else if (m_level <= 12) nes_frames_per_cell = 5;
else if (m_level <= 15) nes_frames_per_cell = 4;
else if (m_level <= 18) nes_frames_per_cell = 3;
else if (m_level <= 28) nes_frames_per_cell = 2;
else nes_frames_per_cell = 1;
float dt_level = static_cast<float>(nes_frames_per_cell) * nes_frame_time;
uint32_t softdrop_count = 0;
while (dt > dt_level) {
softdrop_count += 1;
dt -= dt_level;
}
m_dt_remaining_seconds = dt;
return softdrop_count;
}
int32_t
Tetris::ReadHighscore()
{
int32_t highscore = 0;
std::ifstream highscore_file_in {s_tetris_highscore_path};
if (highscore_file_in) {
highscore_file_in >> highscore;
highscore_file_in.close();
}
return highscore;
}
void
Tetris::WriteHighscore()
{
std::ofstream highscore_file_out {s_tetris_highscore_path};
if (highscore_file_out) {
highscore_file_out << m_highscore << std::endl;
highscore_file_out.close();
}
else {
SDL_LogInfo(0, "Tetris: cannot open %s for writing", s_tetris_highscore_path);
}
}
void
Tetris::Draw()
{
m_board.Draw(m_level);
m_active_tetromino.Draw();
DrawNextTetromino();
DrawStatistics();
DrawLineCounter();
DrawLevel();
DrawScore();
}
void
Tetris::DrawGameOverMenu()
{
ImGui::Begin("TetrisGameOver", nullptr, s_imgui_window_flags_menu);
ImGui::Text("Score = %d", m_score);
ImGui::Text("HighScore = %d", m_highscore);
if (ImGui::Button("Restart")) {
printf("restarted\n");
Start();
}
if (ImGui::Button("Exit")) {
m_game_status = game_exit;
}
ImGui::End();
}
void
Tetris::DrawLineCounter()
{
V2F32 pos = {0.5f, 2.6f};
Color color = {0.9f, 0.9f, 0.9f, 1.0f};
String32Id str_id = MemoryManager::EmplaceString32_Frame(U"Lines: xxx");
std::u32string& str = MemoryManager::GetString32(str_id);
int line_count = std::min(m_line_counter, 999);
str[9] = U'0' + char32_t(line_count % 10);
line_count /= 10;
str[8] = U'0' + char32_t(line_count % 10);
line_count /= 10;
str[7] = U'0' + char32_t(line_count % 10);
line_count /= 10;
g_renderer.PushString32(str_id, m_font, pos, color, z_text);
pos.x += 0.2f;
}
void
Tetris::DrawStatistics()
{
V2F32 pos = {0.4f, 0.5f};
String32Id title_text = MemoryManager::EmplaceString32_Frame(U"Statistics");
V2F32 title_pos = {pos.x + 0.02f, pos.y + 1.64f};
g_renderer.PushString32(title_text, m_font, title_pos, s_text_color, z_text);
float yadvance = -0.2f;
float tetrominoes_x0 = pos.x;
float tetrominoes_y0 = pos.y - (float)Tetromino::id_count * yadvance;
V2F32 tetromino_pos = {tetrominoes_x0, tetrominoes_y0};
Tetromino::Draw(Tetromino::t_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
Tetromino::Draw(Tetromino::j_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
Tetromino::Draw(Tetromino::z_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
Tetromino::Draw(Tetromino::o_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
Tetromino::Draw(Tetromino::s_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
Tetromino::Draw(Tetromino::l_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
Tetromino::Draw(Tetromino::i_piece, 0, tetromino_pos, 0.5f);
tetromino_pos.y += yadvance;
// Todo: reorder tetrominoes' bitmaps and just for-loop this?
float counters_x0 = pos.x + 0.4f;
float counters_y0 = pos.y + 0.05f - yadvance * (float)Tetromino::id_count;
V2F32 counters_pos = {counters_x0, counters_y0};
String32Id t_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::t_piece]));
String32Id j_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::j_piece]));
String32Id z_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::z_piece]));
String32Id o_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::o_piece]));
String32Id s_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::s_piece]));
String32Id l_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::l_piece]));
String32Id i_count = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_tetromino_counters[Tetromino::i_piece]));
g_renderer.PushString32(t_count, m_font, counters_pos, s_text_color, z_text);
counters_pos.y += yadvance;
g_renderer.PushString32(j_count, m_font, counters_pos, s_text_color, z_text);
counters_pos.y += yadvance;
g_renderer.PushString32(z_count, m_font, counters_pos, s_text_color, z_text);
counters_pos.y += yadvance;
g_renderer.PushString32(o_count, m_font, counters_pos, s_text_color, z_text);
counters_pos.y += yadvance;
g_renderer.PushString32(s_count, m_font, counters_pos, s_text_color, z_text);
counters_pos.y += yadvance;
g_renderer.PushString32(l_count, m_font, counters_pos, s_text_color, z_text);
counters_pos.y += yadvance;
g_renderer.PushString32(i_count, m_font, counters_pos, s_text_color, z_text);
}
void
Tetris::DrawScore()
{
V2F32 pos = {3.0f, 2.6f};
String32Id top_label = MemoryManager::EmplaceString32_Frame(U"Top");
String32Id top_value = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_highscore));
String32Id score_label = MemoryManager::EmplaceString32_Frame(U"Score");
String32Id score_value = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_score));
g_renderer.PushString32(top_label, m_font, pos, s_text_color, z_text);
pos.y -= 0.1f;
g_renderer.PushString32(top_value, m_font, pos, s_text_color, z_text);
pos.y -= 0.2f;
g_renderer.PushString32(score_label, m_font, pos, s_text_color, z_text);
pos.y -= 0.1f;
g_renderer.PushString32(score_value, m_font, pos, s_text_color, z_text);
}
void
Tetris::DrawNextTetromino()
{
V2F32 pos = {3.0f, 1.4f};
V2F32 label_pos = {pos.x, pos.y + 0.4f};
String32Id label_text = MemoryManager::EmplaceString32_Frame(U"Next:");
g_renderer.PushString32(label_text, m_font, label_pos, s_text_color, z_layer1);
V2F32 tetromino_pos = {pos.x, pos.y};
Tetromino::Draw(m_next_tetromino_id, 0, tetromino_pos, 0.5f);
}
void
Tetris::DrawLevel()
{
V2F32 pos = {3.0f, 1.1f};
String32Id label = MemoryManager::EmplaceString32_Frame(U"Level");
g_renderer.PushString32(label, m_font, pos, s_text_color, z_text);
pos.y -= 0.1f;
String32Id level = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_level));
g_renderer.PushString32(level, m_font, pos, s_text_color, z_text);
}
|