Skip to content

Commit cc15613

Browse files
committed
fix: handle everything regarding minos in i8, since that is way easier
1 parent 05d4b3a commit cc15613

File tree

18 files changed

+167
-170
lines changed

18 files changed

+167
-170
lines changed

src/game/graphic_helpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void helper::graphics::render_mino(
3535
const double original_scale,
3636
const Mino::ScreenCordsFunction& to_screen_coords,
3737
const shapes::UPoint& tile_size,
38-
const grid::GridUPoint& offset
38+
const grid::GridPoint& offset
3939
) {
4040
const auto alpha = get_transparency_value(transparency);
4141
const auto alpha_factor = static_cast<double>(alpha) / 255.0;

src/game/graphic_helpers.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace helper::graphics {
2828
double original_scale,
2929
const Mino::ScreenCordsFunction& to_screen_coords,
3030
const shapes::UPoint& tile_size,
31-
const grid::GridUPoint& offset = grid::GridUPoint::zero()
31+
const grid::GridPoint& offset = grid::GridPoint::zero()
3232
);
3333

3434

src/game/grid.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Grid::Grid(const ui::Layout& layout, bool is_top_level)
3131
return static_cast<double>(m_tile_size) / static_cast<double>(grid::original_tile_size);
3232
}
3333

34-
[[nodiscard]] shapes::UPoint Grid::to_screen_coords(grid::GridUPoint grid_coords) const {
34+
[[nodiscard]] shapes::UPoint Grid::to_screen_coords(grid::GridPoint grid_coords) const {
3535
return m_fill_rect.top_left + (grid_coords.cast<u32>() * m_tile_size);
3636
}
3737

@@ -51,7 +51,7 @@ void Grid::draw_preview_background(const ServiceProvider& service_provider) cons
5151
service_provider,
5252
GridRect{
5353
grid::preview_background_position,
54-
grid::preview_background_position + grid::preview_extends - grid::GridUPoint{ 1, 1 },
54+
grid::preview_background_position + grid::preview_extends - grid::GridPoint{ 1, 1 },
5555
}
5656
);
5757
}
@@ -61,7 +61,7 @@ void Grid::draw_hold_background(const ServiceProvider& service_provider) const {
6161
service_provider,
6262
GridRect{
6363
grid::hold_background_position,
64-
grid::hold_background_position + grid::hold_background_extends - grid::GridUPoint{ 1, 1 },
64+
grid::hold_background_position + grid::hold_background_extends - grid::GridPoint{ 1, 1 },
6565
}
6666
);
6767
}
@@ -71,7 +71,7 @@ void Grid::draw_playing_field_background(const ServiceProvider& service_provider
7171
service_provider,
7272
GridRect{
7373
grid::grid_position,
74-
grid::grid_position + shapes::UPoint{ grid::width_in_tiles - 1, grid::height_in_tiles - 1 },
74+
grid::grid_position + shapes::IPoint{ grid::width_in_tiles - 1, grid::height_in_tiles - 1 },
7575
}
7676
);
7777
}
@@ -80,18 +80,19 @@ void Grid::draw_background(const ServiceProvider& service_provider, GridRect gri
8080
const auto top_left = m_fill_rect.top_left + (grid_rect.top_left.cast<u32>() * m_tile_size);
8181

8282

83-
const auto bottom_right = top_left + (grid::GridUPoint(grid_rect.width(), grid_rect.height()).cast<u32>() * m_tile_size);
83+
const auto bottom_right =
84+
top_left + (grid::GridPoint(grid_rect.width(), grid_rect.height()).cast<u32>() * m_tile_size);
8485

8586
service_provider.renderer().draw_rect_filled(shapes::URect{ top_left, bottom_right }, background_color);
8687

87-
for (u32 column = 0; column <= grid_rect.width(); ++column) {
88+
for (i32 column = 0; column <= grid_rect.width(); ++column) {
8889
const auto start = top_left + shapes::UPoint{ column * m_tile_size, 0 };
8990
const auto end = shapes::UPoint{ start.x, start.y + (grid_rect.height() * m_tile_size) };
9091
service_provider.renderer().draw_line(start, end, grid_color);
9192
service_provider.renderer().draw_line(start - shapes::UPoint{ 1, 0 }, end - shapes::UPoint{ 1, 0 }, grid_color);
9293
}
9394

94-
for (u32 row = 0; row <= grid_rect.height(); ++row) {
95+
for (i32 row = 0; row <= grid_rect.height(); ++row) {
9596
const auto start = top_left + shapes::UPoint{ 0, row * m_tile_size };
9697
const auto end = shapes::UPoint{ bottom_right.x, start.y };
9798
service_provider.renderer().draw_line(start, end, grid_color);

src/game/grid.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Grid final : public ui::Widget {
1616
static constexpr Color grid_color{ 31, 31, 31 };
1717

1818
private:
19-
using GridRect = shapes::AbstractRect<grid::GridUType>;
19+
using GridRect = shapes::AbstractRect<grid::GridType>;
2020

2121
shapes::URect m_fill_rect;
2222
u32 m_tile_size;
@@ -28,7 +28,7 @@ struct Grid final : public ui::Widget {
2828

2929
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] double scale_to_original() const;
3030

31-
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] shapes::UPoint to_screen_coords(grid::GridUPoint grid_coords) const;
31+
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] shapes::UPoint to_screen_coords(grid::GridPoint grid_coords) const;
3232

3333
OOPETRIS_GRAPHICS_EXPORTED void render(const ServiceProvider& service_provider) const override;
3434

src/game/simulated_tetrion.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void SimulatedTetrion::spawn_next_tetromino(
132132
const helper::TetrominoType type,
133133
const SimulationStep simulation_step_index
134134
) {
135-
constexpr grid::GridUPoint spawn_position{ 3, 0 };
135+
constexpr grid::GridPoint spawn_position{ 3, 0 };
136136
m_active_tetromino = Tetromino{ spawn_position, type };
137137
refresh_previews();
138138
if (not is_active_tetromino_position_valid()) {
@@ -141,12 +141,12 @@ void SimulatedTetrion::spawn_next_tetromino(
141141
auto current_pieces = m_active_tetromino.value().minos();
142142

143143
bool all_valid{ false };
144-
u8 move_up = 0;
144+
i8 move_up = 0;
145145
while (not all_valid) {
146146
all_valid = true;
147147
for (auto& mino : current_pieces) {
148148
if (mino.position().y != 0) {
149-
mino.position() = mino.position() - grid::GridUPoint{ 0, 1 };
149+
mino.position() = mino.position() - grid::GridPoint{ 0, 1 };
150150
if (not is_valid_mino_position(mino.position())) {
151151
all_valid = false;
152152
}
@@ -159,7 +159,7 @@ void SimulatedTetrion::spawn_next_tetromino(
159159
for (const Mino& mino : m_active_tetromino->minos()) {
160160
auto position = mino.position();
161161
if (mino.position().y >= move_up && move_up != 0) {
162-
position -= grid::GridUPoint{ 0, move_up };
162+
position -= grid::GridPoint{ 0, move_up };
163163
m_mino_stack.set(position, mino.type());
164164
}
165165
}
@@ -289,10 +289,10 @@ void SimulatedTetrion::clear_fully_occupied_lines() {
289289
const u32 lines_cleared_before = m_lines_cleared;
290290
do { // NOLINT(cppcoreguidelines-avoid-do-while)
291291
cleared = false;
292-
for (u8 row = 0; row < grid::height_in_tiles; ++row) {
292+
for (i8 row = 0; row < grid::height_in_tiles; ++row) {
293293
bool fully_occupied = true;
294-
for (u8 column = 0; column < grid::width_in_tiles; ++column) {
295-
if (m_mino_stack.is_empty(grid::GridUPoint{ column, row })) {
294+
for (i8 column = 0; column < grid::width_in_tiles; ++column) {
295+
if (m_mino_stack.is_empty(grid::GridPoint{ column, row })) {
296296
fully_occupied = false;
297297
break;
298298
}
@@ -355,16 +355,18 @@ bool SimulatedTetrion::is_active_tetromino_position_valid() const {
355355
return is_tetromino_position_valid(m_active_tetromino.value());
356356
}
357357

358-
bool SimulatedTetrion::is_valid_mino_position(grid::GridUPoint position) const {
359-
return position.x < grid::width_in_tiles and position.y < grid::height_in_tiles and m_mino_stack.is_empty(position);
358+
bool SimulatedTetrion::is_valid_mino_position(grid::GridPoint position) const {
359+
360+
return position.x >= 0 and position.x < grid::width_in_tiles and position.y >= 0
361+
and position.y < grid::height_in_tiles and m_mino_stack.is_empty(position);
360362
}
361363

362-
bool SimulatedTetrion::mino_can_move_down(grid::GridUPoint position) const {
364+
bool SimulatedTetrion::mino_can_move_down(grid::GridPoint position) const {
363365
if (position.y == (grid::height_in_tiles - 1)) {
364366
return false;
365367
}
366368

367-
return is_valid_mino_position(position + grid::GridUPoint{ 0, 1 });
369+
return is_valid_mino_position(position + grid::GridPoint{ 0, 1 });
368370
}
369371

370372

@@ -384,7 +386,7 @@ void SimulatedTetrion::refresh_previews() {
384386
auto bag_index = usize{ 0 };
385387
for (std::remove_cvref_t<decltype(num_preview_tetrominos)> i = 0; i < num_preview_tetrominos; ++i) {
386388
m_preview_tetrominos.at(static_cast<usize>(i)) = Tetromino{
387-
grid::preview_tetromino_position + shapes::UPoint{ 0, static_cast<u32>(grid::preview_padding * i) },
389+
grid::preview_tetromino_position + shapes::IPoint{ 0, grid::preview_padding * i },
388390
m_sequence_bags.at(bag_index)[sequence_index]
389391
};
390392
++sequence_index;

src/game/simulated_tetrion.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ struct SimulatedTetrion {
150150
void clear_fully_occupied_lines();
151151
void lock_active_tetromino(SimulationStep simulation_step_index);
152152
[[nodiscard]] bool is_active_tetromino_position_valid() const;
153-
[[nodiscard]] bool mino_can_move_down(grid::GridUPoint position) const;
154-
[[nodiscard]] bool is_valid_mino_position(grid::GridUPoint position) const;
153+
[[nodiscard]] bool mino_can_move_down(grid::GridPoint position) const;
154+
[[nodiscard]] bool is_valid_mino_position(grid::GridPoint position) const;
155155

156156
void refresh_ghost_tetromino();
157157
void refresh_previews();

src/game/tetrion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void Tetrion::render(const ServiceProvider& service_provider) const {
7272

7373
const auto* grid = get_grid();
7474
const double original_scale = grid->scale_to_original();
75-
const ScreenCordsFunction to_screen_coords = [grid](const grid::GridUPoint& point) {
75+
const ScreenCordsFunction to_screen_coords = [grid](const grid::GridPoint& point) {
7676
return grid->to_screen_coords(point);
7777
};
7878
const shapes::UPoint& tile_size = grid->tile_size();

src/game/tetromino.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void Tetromino::render(
1616
const double original_scale,
1717
const ScreenCordsFunction& to_screen_coords,
1818
const shapes::UPoint& tile_size,
19-
const grid::GridUPoint& offset
19+
const grid::GridPoint& offset
2020
) const {
2121
for (const auto& mino : m_minos) {
2222
helper::graphics::render_mino(
@@ -69,9 +69,9 @@ Tetromino::Pattern Tetromino::get_pattern(helper::TetrominoType type, Rotation r
6969

7070
std::array<Mino, 4> Tetromino::create_minos(grid::GridPoint position, Rotation rotation, helper::TetrominoType type) {
7171
return std::array<Mino, 4>{
72-
Mino{ (position + get_pattern(type, rotation).at(0)).cast<u8>(), type },
73-
Mino{ (position + get_pattern(type, rotation).at(1)).cast<u8>(), type },
74-
Mino{ (position + get_pattern(type, rotation).at(2)).cast<u8>(), type },
75-
Mino{ (position + get_pattern(type, rotation).at(3)).cast<u8>(), type },
72+
Mino{ position + get_pattern(type, rotation).at(0), type },
73+
Mino{ position + get_pattern(type, rotation).at(1), type },
74+
Mino{ position + get_pattern(type, rotation).at(2), type },
75+
Mino{ position + get_pattern(type, rotation).at(3), type },
7676
};
7777
}

src/game/tetromino.hpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ struct Tetromino final {
2727
m_type{ type },
2828
m_minos{ create_minos(position, m_rotation, type) } { }
2929

30-
OOPETRIS_GRAPHICS_EXPORTED Tetromino(grid::GridUPoint position, helper::TetrominoType type)
31-
: m_position{ position.cast<i8>() },
32-
m_type{ type },
33-
m_minos{ create_minos(m_position, m_rotation, type) } { }
34-
3530
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] helper::TetrominoType type() const;
3631
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] Rotation rotation() const;
3732

@@ -41,7 +36,7 @@ struct Tetromino final {
4136
double original_scale,
4237
const ScreenCordsFunction& to_screen_coords,
4338
const shapes::UPoint& tile_size,
44-
const grid::GridUPoint& offset = grid::GridUPoint::zero()
39+
const grid::GridPoint& offset = grid::GridPoint::zero()
4540
) const;
4641

4742
OOPETRIS_GRAPHICS_EXPORTED void rotate_right();

src/libs/core/game/grid_properties.hpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,26 @@
44

55
#include "../helper/point.hpp"
66

7+
78
namespace grid {
89

910
using GridType = i8;
1011
using GridPoint = shapes::AbstractPoint<GridType>;
1112

12-
using GridUType = u8;
13-
using GridUPoint = shapes::AbstractPoint<GridUType>;
1413

15-
constexpr GridUType original_tile_size = 26;
14+
constexpr GridType original_tile_size = 26;
1615

17-
constexpr GridUType width_in_tiles = 10;
18-
constexpr GridUType height_in_tiles = 20;
16+
constexpr GridType width_in_tiles = 10;
17+
constexpr GridType height_in_tiles = 20;
1918

20-
constexpr GridUPoint preview_extends{ 4, (6 * 3) + 1 };
21-
constexpr GridUPoint hold_background_position{ 0, 0 };
22-
constexpr GridUPoint grid_position = hold_background_position + GridUPoint{ preview_extends.x + 1, 0 };
23-
constexpr GridUPoint preview_background_position{ grid_position.x + width_in_tiles + 1, 0 };
24-
constexpr GridUPoint preview_tetromino_position = preview_background_position + GridUPoint{ 0, 1 };
25-
constexpr GridUPoint hold_tetromino_position = hold_background_position + GridUPoint{ 0, 1 };
26-
constexpr GridUPoint hold_background_extends = GridUPoint{ 4, 4 };
27-
constexpr GridUType preview_padding = 3;
19+
constexpr GridPoint preview_extends{ 4, (6 * 3) + 1 };
20+
constexpr GridPoint hold_background_position{ 0, 0 };
21+
constexpr GridPoint grid_position = hold_background_position + GridPoint{ preview_extends.x + 1, 0 };
22+
constexpr GridPoint preview_background_position{ grid_position.x + width_in_tiles + 1, 0 };
23+
constexpr GridPoint preview_tetromino_position = preview_background_position + GridPoint{ 0, 1 };
24+
constexpr GridPoint hold_tetromino_position = hold_background_position + GridPoint{ 0, 1 };
25+
constexpr GridPoint hold_background_extends = GridPoint{ 4, 4 };
26+
constexpr GridType preview_padding = 3;
2827

2928

3029
} // namespace grid

0 commit comments

Comments
 (0)