Skip to content

Commit

Permalink
Merge pull request #203 from OpenBrickProtocolFoundation/fix_grdi_ope…
Browse files Browse the repository at this point in the history
…rations_in_u8

Fix grid operations in u8
  • Loading branch information
Totto16 authored Nov 6, 2024
2 parents ad9104b + cc15613 commit 3328fb7
Show file tree
Hide file tree
Showing 21 changed files with 163 additions and 166 deletions.
2 changes: 1 addition & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
}
]
},
"volume": 0.0,
"volume": 0.2,
"discord": false,
"api_url": "https://oopetris.totto.lt/api/"
}
2 changes: 1 addition & 1 deletion src/game/graphic_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void helper::graphics::render_mino(
const double original_scale,
const Mino::ScreenCordsFunction& to_screen_coords,
const shapes::UPoint& tile_size,
const Mino::GridPoint& offset
const grid::GridPoint& offset
) {
const auto alpha = get_transparency_value(transparency);
const auto alpha_factor = static_cast<double>(alpha) / 255.0;
Expand Down
2 changes: 1 addition & 1 deletion src/game/graphic_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace helper::graphics {
double original_scale,
const Mino::ScreenCordsFunction& to_screen_coords,
const shapes::UPoint& tile_size,
const Mino::GridPoint& offset = Mino::GridPoint::zero()
const grid::GridPoint& offset = grid::GridPoint::zero()
);


Expand Down
15 changes: 8 additions & 7 deletions src/game/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Grid::Grid(const ui::Layout& layout, bool is_top_level)
return static_cast<double>(m_tile_size) / static_cast<double>(grid::original_tile_size);
}

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

Expand All @@ -51,7 +51,7 @@ void Grid::draw_preview_background(const ServiceProvider& service_provider) cons
service_provider,
GridRect{
grid::preview_background_position,
grid::preview_background_position + grid::preview_extends - GridPoint{ 1, 1 },
grid::preview_background_position + grid::preview_extends - grid::GridPoint{ 1, 1 },
}
);
}
Expand All @@ -61,7 +61,7 @@ void Grid::draw_hold_background(const ServiceProvider& service_provider) const {
service_provider,
GridRect{
grid::hold_background_position,
grid::hold_background_position + grid::hold_background_extends - GridPoint{ 1, 1 },
grid::hold_background_position + grid::hold_background_extends - grid::GridPoint{ 1, 1 },
}
);
}
Expand All @@ -71,7 +71,7 @@ void Grid::draw_playing_field_background(const ServiceProvider& service_provider
service_provider,
GridRect{
grid::grid_position,
grid::grid_position + shapes::UPoint{ grid::width_in_tiles - 1, grid::height_in_tiles - 1 },
grid::grid_position + shapes::IPoint{ grid::width_in_tiles - 1, grid::height_in_tiles - 1 },
}
);
}
Expand All @@ -80,18 +80,19 @@ void Grid::draw_background(const ServiceProvider& service_provider, GridRect gri
const auto top_left = m_fill_rect.top_left + (grid_rect.top_left.cast<u32>() * m_tile_size);


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

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

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

for (u32 row = 0; row <= grid_rect.height(); ++row) {
for (i32 row = 0; row <= grid_rect.height(); ++row) {
const auto start = top_left + shapes::UPoint{ 0, row * m_tile_size };
const auto end = shapes::UPoint{ bottom_right.x, start.y };
service_provider.renderer().draw_line(start, end, grid_color);
Expand Down
8 changes: 3 additions & 5 deletions src/game/grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@

struct Grid final : public ui::Widget {
public:
using GridType = grid::GridType;
using GridPoint = grid::GridPoint;
using GridRect = shapes::AbstractRect<GridType>;

static constexpr Color background_color{ 12, 12, 12 };
static constexpr Color border_color{ 42, 42, 42 };
static constexpr Color grid_color{ 31, 31, 31 };

private:
using GridRect = shapes::AbstractRect<grid::GridType>;

shapes::URect m_fill_rect;
u32 m_tile_size;

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

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

OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] shapes::UPoint to_screen_coords(GridPoint grid_coords) const;
OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] shapes::UPoint to_screen_coords(grid::GridPoint grid_coords) const;

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

Expand Down
26 changes: 14 additions & 12 deletions src/game/simulated_tetrion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void SimulatedTetrion::spawn_next_tetromino(
const helper::TetrominoType type,
const SimulationStep simulation_step_index
) {
constexpr GridPoint spawn_position{ 3, 0 };
constexpr grid::GridPoint spawn_position{ 3, 0 };
m_active_tetromino = Tetromino{ spawn_position, type };
refresh_previews();
if (not is_active_tetromino_position_valid()) {
Expand All @@ -141,12 +141,12 @@ void SimulatedTetrion::spawn_next_tetromino(
auto current_pieces = m_active_tetromino.value().minos();

bool all_valid{ false };
u8 move_up = 0;
i8 move_up = 0;
while (not all_valid) {
all_valid = true;
for (auto& mino : current_pieces) {
if (mino.position().y != 0) {
mino.position() = mino.position() - GridPoint{ 0, 1 };
mino.position() = mino.position() - grid::GridPoint{ 0, 1 };
if (not is_valid_mino_position(mino.position())) {
all_valid = false;
}
Expand All @@ -159,7 +159,7 @@ void SimulatedTetrion::spawn_next_tetromino(
for (const Mino& mino : m_active_tetromino->minos()) {
auto position = mino.position();
if (mino.position().y >= move_up && move_up != 0) {
position -= GridPoint{ 0, move_up };
position -= grid::GridPoint{ 0, move_up };
m_mino_stack.set(position, mino.type());
}
}
Expand Down Expand Up @@ -289,10 +289,10 @@ void SimulatedTetrion::clear_fully_occupied_lines() {
const u32 lines_cleared_before = m_lines_cleared;
do { // NOLINT(cppcoreguidelines-avoid-do-while)
cleared = false;
for (u8 row = 0; row < grid::height_in_tiles; ++row) {
for (i8 row = 0; row < grid::height_in_tiles; ++row) {
bool fully_occupied = true;
for (u8 column = 0; column < grid::width_in_tiles; ++column) {
if (m_mino_stack.is_empty(GridPoint{ column, row })) {
for (i8 column = 0; column < grid::width_in_tiles; ++column) {
if (m_mino_stack.is_empty(grid::GridPoint{ column, row })) {
fully_occupied = false;
break;
}
Expand Down Expand Up @@ -355,16 +355,18 @@ bool SimulatedTetrion::is_active_tetromino_position_valid() const {
return is_tetromino_position_valid(m_active_tetromino.value());
}

bool SimulatedTetrion::is_valid_mino_position(GridPoint position) const {
return position.x < grid::width_in_tiles and position.y < grid::height_in_tiles and m_mino_stack.is_empty(position);
bool SimulatedTetrion::is_valid_mino_position(grid::GridPoint position) const {

return position.x >= 0 and position.x < grid::width_in_tiles and position.y >= 0
and position.y < grid::height_in_tiles and m_mino_stack.is_empty(position);
}

bool SimulatedTetrion::mino_can_move_down(GridPoint position) const {
bool SimulatedTetrion::mino_can_move_down(grid::GridPoint position) const {
if (position.y == (grid::height_in_tiles - 1)) {
return false;
}

return is_valid_mino_position(position + GridPoint{ 0, 1 });
return is_valid_mino_position(position + grid::GridPoint{ 0, 1 });
}


Expand All @@ -384,7 +386,7 @@ void SimulatedTetrion::refresh_previews() {
auto bag_index = usize{ 0 };
for (std::remove_cvref_t<decltype(num_preview_tetrominos)> i = 0; i < num_preview_tetrominos; ++i) {
m_preview_tetrominos.at(static_cast<usize>(i)) = Tetromino{
grid::preview_tetromino_position + shapes::UPoint{ 0, static_cast<u32>(grid::preview_padding * i) },
grid::preview_tetromino_position + shapes::IPoint{ 0, grid::preview_padding * i },
m_sequence_bags.at(bag_index)[sequence_index]
};
++sequence_index;
Expand Down
5 changes: 2 additions & 3 deletions src/game/simulated_tetrion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct SimulatedTetrion {
private:
using WallKickPoint = shapes::AbstractPoint<i8>;
using WallKickTable = std::array<std::array<WallKickPoint, 5>, 8>;
using GridPoint = Mino::GridPoint;

static constexpr SimulationStep lock_delay = 30;
static constexpr int num_lock_delays = 30;
Expand Down Expand Up @@ -151,8 +150,8 @@ struct SimulatedTetrion {
void clear_fully_occupied_lines();
void lock_active_tetromino(SimulationStep simulation_step_index);
[[nodiscard]] bool is_active_tetromino_position_valid() const;
[[nodiscard]] bool mino_can_move_down(GridPoint position) const;
[[nodiscard]] bool is_valid_mino_position(GridPoint position) const;
[[nodiscard]] bool mino_can_move_down(grid::GridPoint position) const;
[[nodiscard]] bool is_valid_mino_position(grid::GridPoint position) const;

void refresh_ghost_tetromino();
void refresh_previews();
Expand Down
2 changes: 1 addition & 1 deletion src/game/tetrion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void Tetrion::render(const ServiceProvider& service_provider) const {

const auto* grid = get_grid();
const double original_scale = grid->scale_to_original();
const ScreenCordsFunction to_screen_coords = [grid](const GridPoint& point) {
const ScreenCordsFunction to_screen_coords = [grid](const grid::GridPoint& point) {
return grid->to_screen_coords(point);
};
const shapes::UPoint& tile_size = grid->tile_size();
Expand Down
2 changes: 0 additions & 2 deletions src/game/tetrion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ namespace recorder {
struct Tetrion final : public ui::Widget, SimulatedTetrion {
private:
using ScreenCordsFunction = Mino::ScreenCordsFunction;
using GridPoint = Mino::GridPoint;

ui::TileLayout m_main_layout;


public:
OOPETRIS_GRAPHICS_EXPORTED Tetrion(
u8 tetrion_index,
Expand Down
6 changes: 3 additions & 3 deletions src/game/tetromino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void Tetromino::render(
const double original_scale,
const ScreenCordsFunction& to_screen_coords,
const shapes::UPoint& tile_size,
const GridPoint& offset
const grid::GridPoint& offset
) const {
for (const auto& mino : m_minos) {
helper::graphics::render_mino(
Expand Down Expand Up @@ -49,7 +49,7 @@ void Tetromino::move_right() {

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

Expand All @@ -67,7 +67,7 @@ Tetromino::Pattern Tetromino::get_pattern(helper::TetrominoType type, Rotation r
}


std::array<Mino, 4> Tetromino::create_minos(GridPoint position, Rotation rotation, helper::TetrominoType type) {
std::array<Mino, 4> Tetromino::create_minos(grid::GridPoint position, Rotation rotation, helper::TetrominoType type) {
return std::array<Mino, 4>{
Mino{ position + get_pattern(type, rotation).at(0), type },
Mino{ position + get_pattern(type, rotation).at(1), type },
Expand Down
11 changes: 5 additions & 6 deletions src/game/tetromino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@

struct Tetromino final {
private:
using GridPoint = Mino::GridPoint;
using ScreenCordsFunction = Mino::ScreenCordsFunction;

GridPoint m_position;
grid::GridPoint m_position;
Rotation m_rotation{ Rotation::North };
helper::TetrominoType m_type;
std::array<Mino, 4> m_minos;

public:
using TetrominoPoint = shapes::AbstractPoint<u8>;
using TetrominoPoint = shapes::AbstractPoint<i8>;
using Pattern = std::array<TetrominoPoint, 4>;

OOPETRIS_GRAPHICS_EXPORTED Tetromino(GridPoint position, helper::TetrominoType type)
OOPETRIS_GRAPHICS_EXPORTED Tetromino(grid::GridPoint position, helper::TetrominoType type)
: m_position{ position },
m_type{ type },
m_minos{ create_minos(position, m_rotation, type) } { }
Expand All @@ -37,7 +36,7 @@ struct Tetromino final {
double original_scale,
const ScreenCordsFunction& to_screen_coords,
const shapes::UPoint& tile_size,
const GridPoint& offset = GridPoint::zero()
const grid::GridPoint& offset = grid::GridPoint::zero()
) const;

OOPETRIS_GRAPHICS_EXPORTED void rotate_right();
Expand All @@ -56,7 +55,7 @@ struct Tetromino final {

static Pattern get_pattern(helper::TetrominoType type, Rotation rotation);

static std::array<Mino, 4> create_minos(GridPoint position, Rotation rotation, helper::TetrominoType type);
static std::array<Mino, 4> create_minos(grid::GridPoint position, Rotation rotation, helper::TetrominoType type);

using TetrominoPatterns = std::array<Pattern, 4>; // one pattern per rotation

Expand Down
4 changes: 3 additions & 1 deletion src/libs/core/game/grid_properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

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


namespace grid {

using GridType = u8;
using GridType = i8;
using GridPoint = shapes::AbstractPoint<GridType>;


constexpr GridType original_tile_size = 26;

constexpr GridType width_in_tiles = 10;
Expand Down
4 changes: 2 additions & 2 deletions src/libs/core/game/mino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
return m_type;
}

[[nodiscard]] const Mino::GridPoint& Mino::position() const {
[[nodiscard]] const grid::GridPoint& Mino::position() const {
return m_position;
}

[[nodiscard]] Mino::GridPoint& Mino::position() {
[[nodiscard]] grid::GridPoint& Mino::position() {
return m_position;
}

Expand Down
11 changes: 5 additions & 6 deletions src/libs/core/game/mino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@
#include <functional>
struct Mino final {
public:
using GridPoint = grid::GridPoint;
using ScreenCordsFunction = std::function<shapes::UPoint(const GridPoint&)>;
using ScreenCordsFunction = std::function<shapes::UPoint(const grid::GridPoint&)>;

private:
GridPoint m_position;
grid::GridPoint m_position;
helper::TetrominoType m_type;

public:
OOPETRIS_CORE_EXPORTED explicit constexpr Mino(GridPoint position, helper::TetrominoType type)
OOPETRIS_CORE_EXPORTED explicit constexpr Mino(grid::GridPoint position, helper::TetrominoType type)
: m_position{ position },
m_type{ type } { }

OOPETRIS_CORE_EXPORTED [[nodiscard]] helper::TetrominoType type() const;

OOPETRIS_CORE_EXPORTED [[nodiscard]] const GridPoint& position() const;
OOPETRIS_CORE_EXPORTED [[nodiscard]] const grid::GridPoint& position() const;

OOPETRIS_CORE_EXPORTED [[nodiscard]] GridPoint& position();
OOPETRIS_CORE_EXPORTED [[nodiscard]] grid::GridPoint& position();

OOPETRIS_CORE_EXPORTED [[nodiscard]] bool operator==(const Mino& other) const;

Expand Down
10 changes: 5 additions & 5 deletions src/libs/core/game/mino_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ void MinoStack::clear_row_and_let_sink(u8 row) {
}
}

[[nodiscard]] bool MinoStack::is_empty(GridPoint coordinates) const {
[[nodiscard]] bool MinoStack::is_empty(grid::GridPoint coordinates) const {
return not std::ranges::any_of(m_minos, [&coordinates](const Mino& mino) {
return mino.position() == coordinates;
});
}

void MinoStack::set(GridPoint coordinates, helper::TetrominoType type) {
void MinoStack::set(grid::GridPoint coordinates, helper::TetrominoType type) {
const Mino to_insert = Mino{ coordinates, type };
for (Mino& current : m_minos) {
if (current.position() == coordinates) {
Expand Down Expand Up @@ -67,10 +67,10 @@ void MinoStack::set(GridPoint coordinates, helper::TetrominoType type) {

std::ostream& operator<<(std::ostream& ostream, const MinoStack& mino_stack) {
ostream << "MinoStack(\n";
for (u8 y = 0; y < grid::height_in_tiles; ++y) {
for (u8 x = 0; x < grid::width_in_tiles; ++x) {
for (i8 y = 0; y < grid::height_in_tiles; ++y) {
for (i8 x = 0; x < grid::width_in_tiles; ++x) {
const auto find_iterator = std::ranges::find_if(mino_stack.minos(), [&](const auto& mino) {
return mino.position() == shapes::AbstractPoint<u8>{ x, y };
return mino.position() == shapes::AbstractPoint<i8>{ x, y };
});
const auto found = (find_iterator != mino_stack.minos().cend());
if (found) {
Expand Down
Loading

0 comments on commit 3328fb7

Please sign in to comment.