Skip to content

Commit 05d4b3a

Browse files
committed
fix: fix mess with i8 and u8 grid coordinates
the minos and the grids coordinates are unsigned, but single tetrions can have a negative position
1 parent ad9104b commit 05d4b3a

21 files changed

+170
-170
lines changed

settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
}
7777
]
7878
},
79-
"volume": 0.0,
79+
"volume": 0.2,
8080
"discord": false,
8181
"api_url": "https://oopetris.totto.lt/api/"
8282
}

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 Mino::GridPoint& offset
38+
const grid::GridUPoint& 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 Mino::GridPoint& offset = Mino::GridPoint::zero()
31+
const grid::GridUPoint& offset = grid::GridUPoint::zero()
3232
);
3333

3434

src/game/grid.cpp

Lines changed: 4 additions & 4 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(GridPoint grid_coords) const {
34+
[[nodiscard]] shapes::UPoint Grid::to_screen_coords(grid::GridUPoint 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 - GridPoint{ 1, 1 },
54+
grid::preview_background_position + grid::preview_extends - grid::GridUPoint{ 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 - GridPoint{ 1, 1 },
64+
grid::hold_background_position + grid::hold_background_extends - grid::GridUPoint{ 1, 1 },
6565
}
6666
);
6767
}
@@ -80,7 +80,7 @@ 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 + (GridPoint(grid_rect.width(), grid_rect.height()).cast<u32>() * m_tile_size);
83+
const auto bottom_right = top_left + (grid::GridUPoint(grid_rect.width(), grid_rect.height()).cast<u32>() * m_tile_size);
8484

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

src/game/grid.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
struct Grid final : public ui::Widget {
1313
public:
14-
using GridType = grid::GridType;
15-
using GridPoint = grid::GridPoint;
16-
using GridRect = shapes::AbstractRect<GridType>;
17-
1814
static constexpr Color background_color{ 12, 12, 12 };
1915
static constexpr Color border_color{ 42, 42, 42 };
2016
static constexpr Color grid_color{ 31, 31, 31 };
2117

2218
private:
19+
using GridRect = shapes::AbstractRect<grid::GridUType>;
20+
2321
shapes::URect m_fill_rect;
2422
u32 m_tile_size;
2523

@@ -30,7 +28,7 @@ struct Grid final : public ui::Widget {
3028

3129
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] double scale_to_original() const;
3230

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

3533
OOPETRIS_GRAPHICS_EXPORTED void render(const ServiceProvider& service_provider) const override;
3634

src/game/simulated_tetrion.cpp

Lines changed: 7 additions & 7 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 GridPoint spawn_position{ 3, 0 };
135+
constexpr grid::GridUPoint spawn_position{ 3, 0 };
136136
m_active_tetromino = Tetromino{ spawn_position, type };
137137
refresh_previews();
138138
if (not is_active_tetromino_position_valid()) {
@@ -146,7 +146,7 @@ void SimulatedTetrion::spawn_next_tetromino(
146146
all_valid = true;
147147
for (auto& mino : current_pieces) {
148148
if (mino.position().y != 0) {
149-
mino.position() = mino.position() - GridPoint{ 0, 1 };
149+
mino.position() = mino.position() - grid::GridUPoint{ 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 -= GridPoint{ 0, move_up };
162+
position -= grid::GridUPoint{ 0, move_up };
163163
m_mino_stack.set(position, mino.type());
164164
}
165165
}
@@ -292,7 +292,7 @@ void SimulatedTetrion::clear_fully_occupied_lines() {
292292
for (u8 row = 0; row < grid::height_in_tiles; ++row) {
293293
bool fully_occupied = true;
294294
for (u8 column = 0; column < grid::width_in_tiles; ++column) {
295-
if (m_mino_stack.is_empty(GridPoint{ column, row })) {
295+
if (m_mino_stack.is_empty(grid::GridUPoint{ column, row })) {
296296
fully_occupied = false;
297297
break;
298298
}
@@ -355,16 +355,16 @@ 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(GridPoint position) const {
358+
bool SimulatedTetrion::is_valid_mino_position(grid::GridUPoint position) const {
359359
return position.x < grid::width_in_tiles and position.y < grid::height_in_tiles and m_mino_stack.is_empty(position);
360360
}
361361

362-
bool SimulatedTetrion::mino_can_move_down(GridPoint position) const {
362+
bool SimulatedTetrion::mino_can_move_down(grid::GridUPoint position) const {
363363
if (position.y == (grid::height_in_tiles - 1)) {
364364
return false;
365365
}
366366

367-
return is_valid_mino_position(position + GridPoint{ 0, 1 });
367+
return is_valid_mino_position(position + grid::GridUPoint{ 0, 1 });
368368
}
369369

370370

src/game/simulated_tetrion.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ struct SimulatedTetrion {
3232
private:
3333
using WallKickPoint = shapes::AbstractPoint<i8>;
3434
using WallKickTable = std::array<std::array<WallKickPoint, 5>, 8>;
35-
using GridPoint = Mino::GridPoint;
3635

3736
static constexpr SimulationStep lock_delay = 30;
3837
static constexpr int num_lock_delays = 30;
@@ -151,8 +150,8 @@ struct SimulatedTetrion {
151150
void clear_fully_occupied_lines();
152151
void lock_active_tetromino(SimulationStep simulation_step_index);
153152
[[nodiscard]] bool is_active_tetromino_position_valid() const;
154-
[[nodiscard]] bool mino_can_move_down(GridPoint position) const;
155-
[[nodiscard]] bool is_valid_mino_position(GridPoint position) const;
153+
[[nodiscard]] bool mino_can_move_down(grid::GridUPoint position) const;
154+
[[nodiscard]] bool is_valid_mino_position(grid::GridUPoint position) const;
156155

157156
void refresh_ghost_tetromino();
158157
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 GridPoint& point) {
75+
const ScreenCordsFunction to_screen_coords = [grid](const grid::GridUPoint& point) {
7676
return grid->to_screen_coords(point);
7777
};
7878
const shapes::UPoint& tile_size = grid->tile_size();

src/game/tetrion.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ namespace recorder {
2121
struct Tetrion final : public ui::Widget, SimulatedTetrion {
2222
private:
2323
using ScreenCordsFunction = Mino::ScreenCordsFunction;
24-
using GridPoint = Mino::GridPoint;
2524

2625
ui::TileLayout m_main_layout;
2726

28-
2927
public:
3028
OOPETRIS_GRAPHICS_EXPORTED Tetrion(
3129
u8 tetrion_index,

src/game/tetromino.cpp

Lines changed: 7 additions & 7 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 GridPoint& offset
19+
const grid::GridUPoint& offset
2020
) const {
2121
for (const auto& mino : m_minos) {
2222
helper::graphics::render_mino(
@@ -49,7 +49,7 @@ void Tetromino::move_right() {
4949

5050
void Tetromino::move(const shapes::AbstractPoint<i8> offset) {
5151
// this looks weird but silently asserts, that the final point is not negative
52-
m_position = (m_position.cast<i8>() + offset).cast<u8>();
52+
m_position = m_position.cast<i8>() + offset;
5353
refresh_minos();
5454
}
5555

@@ -67,11 +67,11 @@ Tetromino::Pattern Tetromino::get_pattern(helper::TetrominoType type, Rotation r
6767
}
6868

6969

70-
std::array<Mino, 4> Tetromino::create_minos(GridPoint position, Rotation rotation, helper::TetrominoType type) {
70+
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), 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 },
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 },
7676
};
7777
}

0 commit comments

Comments
 (0)