aboutsummaryrefslogtreecommitdiff
path: root/src/games/tetris/Tetris.cpp
blob: 2413dd1dda0210d0025dbd803e35bb5db8bd8d65 (plain)
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
#include <games/Game.hpp>
#include <games/tetris/Tetromino.hpp>
#include <games/tetris/Tetris.hpp>
#include <renderer/Renderer.hpp>

#include <SDL3/SDL_events.h>
#include <SDL3/SDL_timer.h>
#include <imgui.h>

#include <fstream>
#include <iostream>


static 
std::u32string
int32_to_u32string(int32_t value)
{
    std::string str = std::to_string(value);
    return std::u32string(str.begin(), str.end());
}


Tetris::Tetris()
    : m_font{s_dejavu_sans_mono_filepath, 22}
    , m_active_tetromino{m_board.m_bitmap}
    , m_highscore {ReadHighscore()}
{
    m_frame_strings.reserve(s_frame_strings_capacity);
}

void
Tetris::Start()
{
    m_game_status = game_resuming;
    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;
}

bool
Tetris::Update(std::vector<SDL_Event>& events)
{
    Color clear_color = {0.2f, 0.2f, 0.2f, 1.0f};
    g_renderer.SetCameraSize(4.0f, 3.0f);
    g_renderer.Clear(clear_color);

    m_frame_strings.clear();


    if (m_game_status == game_starting) {
        Start();
    }


    float dt = ProcessDt();


    if (m_game_status == game_resuming) {
        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();
            }
        }
    }


    for (auto& event : events) {
        switch (m_game_status) {
        case game_resuming: UpdateResumeState(event); break;
        case game_paused: UpdatePauseState(event); break;
        default:;
        }
    }


    if (m_game_status == game_exit) {
        return false;
    }


    Draw();

    assert((float)m_frame_strings.size() <= 0.8f * s_frame_strings_capacity);


    return true;
}

void Tetris::UpdateResumeState(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);
        } else if (key == SDLK_ESCAPE) {
            m_game_status = game_paused;
        }
    }
    default:;
    }
}

void Tetris::UpdatePauseState(SDL_Event& event) {
    switch (event.type) {
    case SDL_EVENT_KEY_DOWN: {
        auto key = event.key.key;
        if (key == SDLK_ESCAPE) {
            m_game_status = game_resuming;
        }
    }
    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();
    }
    else {
        SDL_LogInfo(0, "Tetris: cannot open %s for reading", s_tetris_highscore_path);
    }
    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();

    if (m_game_status  == game_paused) {
        DrawDefaultGamePausedMenu();
    }
    else if (m_game_status == game_over) {
        DrawGameOverMenu();
    }
}

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")) {
        m_game_status = game_starting;
    }
    if (ImGui::Button("Exit")) {
        m_game_status = game_exit;
    }
    ImGui::End();
}

void
Tetris::DrawLineCounter()
{
    static std::u32string text = U"Lines: xxx";
    V3F32 pos = {0.5f, 2.6f, 4.0f};
    Color color = {0.9f, 0.9f, 0.9f, 1.0f};

    int line_count = std::min(m_line_counter, 999);

    text[9] = U'0' + char32_t(line_count % 10);
    line_count /= 10;
    text[8] = U'0' + char32_t(line_count % 10);
    line_count /= 10;
    text[7] = U'0' + char32_t(line_count % 10);
    line_count /= 10;


    g_renderer.PushText(text, m_font, pos, color);
    pos.x += 0.2f;
}

void
Tetris::DrawStatistics()
{
    V3F32 pos = {0.4f, 0.5f, s_text_z};


    static std::u32string title_text = U"Statistics";
    V3F32 title_pos = {pos.x + 0.02f, pos.y + 1.64f, pos.z};
    g_renderer.PushText(title_text, m_font, title_pos, s_text_color);


    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;


    float counters_x0 = pos.x + 0.4f;
    float counters_y0 = pos.y + 0.05f - yadvance * (float)Tetromino::id_count;
    V3F32 counters_pos = {counters_x0, counters_y0, s_text_z};

    std::u32string& t_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::t_piece]));
    std::u32string& j_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::j_piece]));
    std::u32string& z_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::z_piece]));
    std::u32string& o_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::o_piece]));
    std::u32string& s_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::s_piece]));
    std::u32string& l_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::l_piece]));
    std::u32string& i_count = m_frame_strings.emplace_back(int32_to_u32string(m_tetromino_counters[Tetromino::i_piece]));

    g_renderer.PushText(t_count, m_font, counters_pos, s_text_color);
    counters_pos.y += yadvance;
    g_renderer.PushText(j_count, m_font, counters_pos, s_text_color);
    counters_pos.y += yadvance;
    g_renderer.PushText(z_count, m_font, counters_pos, s_text_color);
    counters_pos.y += yadvance;
    g_renderer.PushText(o_count, m_font, counters_pos, s_text_color);
    counters_pos.y += yadvance;
    g_renderer.PushText(s_count, m_font, counters_pos, s_text_color);
    counters_pos.y += yadvance;
    g_renderer.PushText(l_count, m_font, counters_pos, s_text_color);
    counters_pos.y += yadvance;
    g_renderer.PushText(i_count, m_font, counters_pos, s_text_color);
}

void
Tetris::DrawScore()
{
    V3F32 pos = {3.0f, 2.6f, s_text_z};


    std::u32string& top_label = m_frame_strings.emplace_back(U"Top");
    std::u32string& top_value = m_frame_strings.emplace_back(int32_to_u32string(m_highscore));

    std::u32string& score_label = m_frame_strings.emplace_back(U"Score");
    std::u32string& score_value = m_frame_strings.emplace_back(int32_to_u32string(m_score));


    g_renderer.PushText(top_label, m_font, pos, s_text_color);
    pos.y -= 0.1f;
    g_renderer.PushText(top_value, m_font, pos, s_text_color);
    pos.y -= 0.2f;

    g_renderer.PushText(score_label, m_font, pos, s_text_color);
    pos.y -= 0.1f;
    g_renderer.PushText(score_value, m_font, pos, s_text_color);
}

void
Tetris::DrawNextTetromino()
{
    V3F32 pos = {3.0f, 1.4f, s_text_z};


    V3F32 label_pos = {pos.x, pos.y + 0.4f, pos.z};
    std::u32string& label_text = m_frame_strings.emplace_back(U"Next:");
    g_renderer.PushText(label_text, m_font, label_pos, s_text_color);


    V2F32 tetromino_pos = {pos.x, pos.y};
    Tetromino::Draw(m_next_tetromino_id, 0, tetromino_pos, 0.5f);
}

void
Tetris::DrawLevel()
{
    V3F32 pos = {3.0f, 1.1f};

    std::u32string& label = m_frame_strings.emplace_back(U"Level");
    g_renderer.PushText(label, m_font, pos, s_text_color);
    pos.y -= 0.1f;

    std::u32string& level = m_frame_strings.emplace_back(int32_to_u32string(m_level));
    g_renderer.PushText(level, m_font, pos, s_text_color);
}