aboutsummaryrefslogtreecommitdiff
path: root/src/games/tetris/Board.cpp
blob: 9dff1357839d2c17775daaeedd04f89d52268c0f (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
#include <renderer/RenderGroup.hpp>
#include <games/tetris/Tetromino.hpp>
#include <games/tetris/Board.hpp>

Board::Board() {
    for (int y = 0; y < 2; y++) {
        m_Bitmap[y] = 0xffff; // 1111111111111111
    }
    for (int y = 2; y < 24; y++) {
        m_Bitmap[y] = 0xe007; // 1110000000000111
    }

    for (int y = 0; y < 22; y++) {
        for (int x = 0; x < 10; x++) {
            m_Idmap[y][x] = (uint8_t)TetrominoId::TETROMINO_ID_NONE;
        }
    }
}

int32_t Board::PlaceTetromino(Tetromino &tetromino) {
    BoardPos pos = tetromino.GetPos();
    TetrominoId id = tetromino.GetId();
    uint16_t tetromino_bitmap[4];
    tetromino.GetBitmap(tetromino_bitmap);


    // check if Tetromino cannot be placed (Game Over)
    if (tetromino.IsCollisionWithBoard()) {
        return -1;
    }


    // place in Board's Bitmap
    m_Bitmap[pos.y+0] |= tetromino_bitmap[0];
    m_Bitmap[pos.y+1] |= tetromino_bitmap[1];
    m_Bitmap[pos.y+2] |= tetromino_bitmap[2];
    m_Bitmap[pos.y+3] |= tetromino_bitmap[3];


    // place in Board's Idmap
    for (int32_t y = 0; y < 4; y++) {
        for (int32_t x = 0; x < 4; x++) {
            int32_t bitmap_x = 0x8000 >> (pos.x + x);
            if (tetromino_bitmap[y] & bitmap_x) {
                int32_t idmap_x = pos.x + x - 3;
                int32_t idmap_y = pos.y + y - 2;
                m_Idmap[idmap_y][idmap_x] = static_cast<uint8_t>(id);
            }
        }
    }

    int32_t rows_cleared = ClearRows(pos.y);
    return rows_cleared;
}

int32_t Board::ClearRows(int32_t y0) {
    int32_t rows_cleared = 0;
    int32_t y1 = y0 + 3;

    // ignore for y = {0,1}. Those bitmap rows are all 1's for collision testing
    if (y0 < 2) {
        y0 += 2 - y0;
    }

    for (int32_t y = y0; y <= y1; y++) {
        if (m_Bitmap[y] == 0xffff) {
            rows_cleared++;
        }
        else {
            m_Bitmap[y-rows_cleared] = m_Bitmap[y];
            std::copy(m_Idmap[y-2], m_Idmap[y-2] + 10, m_Idmap[y-2-rows_cleared]);
        }
    }
    for (int32_t y = y1+1; y < 24; y++) {
        m_Bitmap[y-rows_cleared] = m_Bitmap[y];
        std::copy(m_Idmap[y-2], m_Idmap[y-2] + 10, m_Idmap[y-2-rows_cleared]);
    }
    for (int32_t y = 24-rows_cleared; y < 24; y++) {
        m_Bitmap[y] = 0xe007;
        std::fill(m_Idmap[y-2], m_Idmap[y-2] + 10, (uint8_t)TetrominoId::TETROMINO_ID_NONE);
    }


    return rows_cleared;
}

void Board::Draw(int32_t level, RenderGroup& render_group) {
    float world_width = 4.0f;
    float world_height = 3.0f;
    float tetromino_size_with_border = world_height / 20.0f;
    float tetromino_size = 0.8f * tetromino_size_with_border;
    float tetromino_offset = 0.1f * tetromino_size_with_border;
    V2F32 board_world_pos = {
        (world_width - tetromino_size_with_border*10) / 2.0f,
        0.0f
    };


    // background
    V3F32 bg_world_pos = {
        board_world_pos.x,
        board_world_pos.y,
        0.0f
    };
    V2F32 bg_world_dim = {
        tetromino_size_with_border * 10,
        tetromino_size_with_border * 20
    };
    V3F32 bg_color = {0.0f, 0.0f, 0.0f};
    render_group.PushRectangle(bg_world_pos, bg_world_dim, bg_color);


    // tetromino parts
    for (size_t y = 0; y < 20; y++) {
        for (size_t x = 0; x < 10; x++) {
            uint8_t tetromino_id = m_Idmap[y][x];
            if (tetromino_id < (uint8_t)TetrominoId::TETROMINO_ID_COUNT) {
                V2F32 local_pos = {
                    (float)x * tetromino_size_with_border + tetromino_offset,
                    (float)y * tetromino_size_with_border + tetromino_offset
                };
                V2F32 local_dim = {tetromino_size, tetromino_size};


                V3F32 world_pos = {
                    board_world_pos.x + local_pos.x,
                    board_world_pos.y + local_pos.y,
                    1.0f
                };
                V2F32 world_dim = local_dim;


                V3F32 color = Tetromino::GetColor(static_cast<TetrominoId>(tetromino_id));
                render_group.PushRectangle(world_pos, world_dim, color);
            }
        }
    }
}