Skip to content

Commit 3328fb7

Browse files
authored
Merge pull request #203 from OpenBrickProtocolFoundation/fix_grdi_operations_in_u8
Fix grid operations in u8
2 parents ad9104b + cc15613 commit 3328fb7

21 files changed

+163
-166
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::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 Mino::GridPoint& offset = Mino::GridPoint::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(GridPoint 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 - GridPoint{ 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 - GridPoint{ 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 + (GridPoint(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: 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::GridType>;
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::GridPoint grid_coords) const;
3432

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

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 GridPoint 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() - GridPoint{ 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 -= GridPoint{ 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(GridPoint{ 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(GridPoint 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(GridPoint 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 + GridPoint{ 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 & 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::GridPoint position) const;
154+
[[nodiscard]] bool is_valid_mino_position(grid::GridPoint 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::GridPoint& 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: 3 additions & 3 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::GridPoint& 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,7 +67,7 @@ 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>{
7272
Mino{ position + get_pattern(type, rotation).at(0), type },
7373
Mino{ position + get_pattern(type, rotation).at(1), type },

0 commit comments

Comments
 (0)