From c775ca6133d93ed97359a6a50bd94a5563c740de Mon Sep 17 00:00:00 2001 From: fschildt Date: Wed, 1 Oct 2025 14:08:24 +0200 Subject: general refactoring, prepare breakout game --- src/basic/defs.hpp | 12 ----- src/basic/math.cpp | 94 ---------------------------------- src/basic/math.hpp | 68 ------------------------- src/common/Font.cpp | 4 +- src/common/Font.hpp | 4 +- src/common/defs.hpp | 14 ++++++ src/common/math.cpp | 95 +++++++++++++++++++++++++++++++++++ src/common/math.hpp | 61 ++++++++++++++++++++++ src/common/shapes.hpp | 16 ++++++ src/games/Game.cpp | 37 +++++++------- src/games/Game.hpp | 21 +++++--- src/games/breakout/Breakout.cpp | 66 ++++++++++++++++++++++++ src/games/breakout/Breakout.hpp | 38 ++++++++++++++ src/games/minesweeper/Minesweeper.cpp | 18 +++---- src/games/minesweeper/Minesweeper.hpp | 2 +- src/games/snake/Snake.cpp | 44 ++++++++-------- src/games/snake/Snake.hpp | 10 ++-- src/games/tetris/Board.cpp | 4 +- src/games/tetris/Board.hpp | 4 +- src/games/tetris/Tetris.cpp | 6 +-- src/games/tetris/Tetris.hpp | 1 + src/games/tetris/Tetromino.cpp | 32 ++++++------ src/games/tetris/Tetromino.hpp | 4 +- src/main.cpp | 20 ++++---- src/renderer/RSoftwareBackend.cpp | 55 ++++++++++---------- src/renderer/RSoftwareBackend.hpp | 6 +-- src/renderer/Renderer.cpp | 37 ++++++++------ src/renderer/Renderer.hpp | 37 ++++++++------ 28 files changed, 475 insertions(+), 335 deletions(-) delete mode 100644 src/basic/defs.hpp delete mode 100644 src/basic/math.cpp delete mode 100644 src/basic/math.hpp create mode 100644 src/common/defs.hpp create mode 100644 src/common/math.cpp create mode 100644 src/common/math.hpp create mode 100644 src/common/shapes.hpp create mode 100644 src/games/breakout/Breakout.cpp create mode 100644 src/games/breakout/Breakout.hpp (limited to 'src') diff --git a/src/basic/defs.hpp b/src/basic/defs.hpp deleted file mode 100644 index 858bdd7..0000000 --- a/src/basic/defs.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#include - -#define ARRAY_COUNT(x) (sizeof(x) / sizeof(x[0])) - -#define InvalidDefaultCase assert(0) - -#define KIBIBYTES(x) ((x)*1024) -#define MEBIBYTES(x) ((x)*KIBIBYTES(1024)) -#define GIBIBYTES(x) ((x)*MEBIBYTES(1024)) - diff --git a/src/basic/math.cpp b/src/basic/math.cpp deleted file mode 100644 index 81fcbbf..0000000 --- a/src/basic/math.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include - -/* V2ST */ - -bool -V2ST::operator==(V2ST& b) -{ - bool result = this->x == b.x && this->y == b.y; - return result; -} - - - -/* V2F32 */ - -V2F32 -V2F32::operator/(float scalar) -{ - V2F32 result {}; - result.x = this->x / scalar; - result.y = this->y / scalar; - return result; -} - -V2F32 -V2F32::operator*(float scalar) -{ - V2F32 result {}; - result.x = this->x * scalar; - result.y = this->y * scalar; - return result; -} - -V2F32 V2F32::operator+(V2F32 other) { - V2F32 result {}; - result.x = this->x + other.x; - result.y = this->y + other.y; - return result; -} - - -/* V3F32 */ - -V3F32 V3F32::operator/(float scalar) { - V3F32 result = {}; - result.x = this->x / scalar; - result.y = this->y / scalar; - result.z = this->z / scalar; - return result; -} - -V3F32 V3F32::operator*(float scalar) { - V3F32 result = {}; - result.x = this->x * scalar; - result.y = this->y * scalar; - result.z = this->z * scalar; - return result; -} - - -/* V4F32 */ - -V4F32 -V4F32::operator/(float scalar) -{ - V4F32 result {}; - result.x = this->x / scalar; - result.y = this->y / scalar; - result.z = this->z / scalar; - result.w = this->w / scalar; - return result; -} - -V4F32 -V4F32::operator*(float scalar) -{ - V4F32 result {}; - result.x = this->x * scalar; - result.y = this->y * scalar; - result.z = this->z * scalar; - result.w = this->z * scalar; - return result; -} - - -/* V2I32 */ - -bool -V2I32::operator==(V2I32 other) -{ - bool result = x == other.x && y == other.y; - return result; -} - diff --git a/src/basic/math.hpp b/src/basic/math.hpp deleted file mode 100644 index a3e4b64..0000000 --- a/src/basic/math.hpp +++ /dev/null @@ -1,68 +0,0 @@ -#pragma once - - -#include - -#include -#include - - -struct V2ST { - size_t x; - size_t y; - - bool operator==(V2ST &b); - bool operator==(const V2ST& other) const { - return x == other.x && y == other.y; - } -}; - -struct V2F32 { - float x; - float y; - - V2F32 operator/(float scalar); - V2F32 operator*(float scalar); - V2F32 operator+(V2F32 other); -}; - -struct V3F32 { - float x; - float y; - float z; - - V3F32 operator/(float scalar); - V3F32 operator*(float scalar); -}; - -struct V4F32 { - float x; - float y; - float z; - float w; - - V4F32 operator/(float scalar); - V4F32 operator*(float scalar); -}; - -struct V2I32 { - int32_t x; - int32_t y; - - bool operator==(V2I32 other); -}; - -struct Color { - float r; - float g; - float b; - float a; -}; - -struct RectF32 { - float x0; - float y0; - float x1; - float y1; -}; - diff --git a/src/common/Font.cpp b/src/common/Font.cpp index 31a6720..b5b177c 100644 --- a/src/common/Font.cpp +++ b/src/common/Font.cpp @@ -86,8 +86,8 @@ Font::LoadGlyph(Glyph& glyph, uint32_t codepoint) } delete[] bitmap_flipped; - glyph.bitmap.width = width; - glyph.bitmap.height = height; + glyph.bitmap.w = width; + glyph.bitmap.h = height; glyph.bitmap.pixels = std::unique_ptr(bitmap_correct); diff --git a/src/common/Font.hpp b/src/common/Font.hpp index a594add..91afe06 100644 --- a/src/common/Font.hpp +++ b/src/common/Font.hpp @@ -5,8 +5,8 @@ struct MonoBitmap { - int32_t width; - int32_t height; + int32_t w; + int32_t h; std::unique_ptr pixels; }; diff --git a/src/common/defs.hpp b/src/common/defs.hpp new file mode 100644 index 0000000..007d560 --- /dev/null +++ b/src/common/defs.hpp @@ -0,0 +1,14 @@ +#pragma once + +#include +#include + + +#define ARRAY_COUNT(x) (sizeof(x) / sizeof(x[0])) + +#define InvalidDefaultCase assert(0) + +#define KIBIBYTES(x) ((x)*1024) +#define MEBIBYTES(x) ((x)*KIBIBYTES(1024)) +#define GIBIBYTES(x) ((x)*MEBIBYTES(1024)) + diff --git a/src/common/math.cpp b/src/common/math.cpp new file mode 100644 index 0000000..ba54f3a --- /dev/null +++ b/src/common/math.cpp @@ -0,0 +1,95 @@ +#include + + +/* V2ST */ + +bool +V2ST::operator==(V2ST& b) +{ + bool result = this->x == b.x && this->y == b.y; + return result; +} + + + +/* V2F32 */ + +V2F32 +V2F32::operator/(float scalar) +{ + V2F32 result {}; + result.x = this->x / scalar; + result.y = this->y / scalar; + return result; +} + +V2F32 +V2F32::operator*(float scalar) +{ + V2F32 result {}; + result.x = this->x * scalar; + result.y = this->y * scalar; + return result; +} + +V2F32 V2F32::operator+(V2F32 other) { + V2F32 result {}; + result.x = this->x + other.x; + result.y = this->y + other.y; + return result; +} + + +/* V3F32 */ + +V3F32 V3F32::operator/(float scalar) { + V3F32 result = {}; + result.x = this->x / scalar; + result.y = this->y / scalar; + result.z = this->z / scalar; + return result; +} + +V3F32 V3F32::operator*(float scalar) { + V3F32 result = {}; + result.x = this->x * scalar; + result.y = this->y * scalar; + result.z = this->z * scalar; + return result; +} + + +/* V4F32 */ + +V4F32 +V4F32::operator/(float scalar) +{ + V4F32 result {}; + result.x = this->x / scalar; + result.y = this->y / scalar; + result.z = this->z / scalar; + result.w = this->w / scalar; + return result; +} + +V4F32 +V4F32::operator*(float scalar) +{ + V4F32 result {}; + result.x = this->x * scalar; + result.y = this->y * scalar; + result.z = this->z * scalar; + result.w = this->z * scalar; + return result; +} + + +/* V2I32 */ + +bool +V2I32::operator==(V2I32 other) +{ + bool result = x == other.x && y == other.y; + return result; +} + diff --git a/src/common/math.hpp b/src/common/math.hpp new file mode 100644 index 0000000..de856e8 --- /dev/null +++ b/src/common/math.hpp @@ -0,0 +1,61 @@ +#pragma once + + +#include + +#include +#include + + +struct V2ST { + size_t x; + size_t y; + + bool operator==(V2ST &b); + bool operator==(const V2ST& other) const { + return x == other.x && y == other.y; + } +}; + +struct V2F32 { + float x; + float y; + + V2F32 operator/(float scalar); + V2F32 operator*(float scalar); + V2F32 operator+(V2F32 other); +}; + +struct V3F32 { + float x; + float y; + float z; + + V3F32 operator/(float scalar); + V3F32 operator*(float scalar); +}; + +struct V4F32 { + float x; + float y; + float z; + float w; + + V4F32 operator/(float scalar); + V4F32 operator*(float scalar); +}; + +struct V2I32 { + int32_t x; + int32_t y; + + bool operator==(V2I32 other); +}; + +struct Color { + float r; + float g; + float b; + float a; +}; + diff --git a/src/common/shapes.hpp b/src/common/shapes.hpp new file mode 100644 index 0000000..b556b2b --- /dev/null +++ b/src/common/shapes.hpp @@ -0,0 +1,16 @@ +#pragma once + + +struct Rectangle { + float x0; + float y0; + float x1; + float y1; +}; + +struct Circle { + float x; + float y; + float r; +}; + diff --git a/src/games/Game.cpp b/src/games/Game.cpp index 5619c8a..7ed94eb 100644 --- a/src/games/Game.cpp +++ b/src/games/Game.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include #include @@ -12,30 +12,29 @@ std::unique_ptr Game::Select(GameType type) { switch (type) { - case NO_GAME: { - return nullptr; - } break; + case no_game: { + return nullptr; + } break; - case TETRIS: { - return std::make_unique(); - } break; + case tetris: { + return std::make_unique(); + } break; - case SNAKE: { - return std::make_unique(); - } break; + case snake: { + return std::make_unique(); + } break; - case MINESWEEPER: { - return std::make_unique(); - } break; + case minesweeper: { + return std::make_unique(); + } break; - InvalidDefaultCase; + case breakout: { + return std::make_unique(); + } break; + + InvalidDefaultCase; } return nullptr; } - -Game::~Game() -{ -} - diff --git a/src/games/Game.hpp b/src/games/Game.hpp index 2156b7e..1307e79 100644 --- a/src/games/Game.hpp +++ b/src/games/Game.hpp @@ -1,26 +1,31 @@ #pragma once -#include +#include + #include + #include #include + struct SDL_Window; + class Game { public: enum GameType { - NO_GAME, - TETRIS, - SNAKE, - MINESWEEPER + no_game, + tetris, + snake, + minesweeper, + breakout }; static std::unique_ptr Select(GameType type); - Game() = default; - virtual ~Game(); - virtual bool Update(std::vector &events) = 0; + Game() = default; + virtual ~Game() = default; + virtual bool Update(std::vector& events) = 0; }; diff --git a/src/games/breakout/Breakout.cpp b/src/games/breakout/Breakout.cpp new file mode 100644 index 0000000..65fafd6 --- /dev/null +++ b/src/games/breakout/Breakout.cpp @@ -0,0 +1,66 @@ +#include + +#include + + +bool +Breakout::Update(std::vector& events) +{ + for (auto& event : events) { + if (m_status == pause) { + ProcessEventDuringPause(event); + } + else { + ProcessEventDuringResume(event); + } + } + + if (m_status == pause) { + DrawPauseMenu(); + } + if (m_status == exit) { + return false; + } + + return true; +} + +void +Breakout::ProcessEventDuringResume(SDL_Event& event) +{ + switch (event.type) { + case SDL_EVENT_KEY_DOWN: { + if (event.key.key == SDLK_ESCAPE) { + m_status = pause; + } + } break; + default:; + } +} + +void +Breakout::ProcessEventDuringPause(SDL_Event &event) +{ + switch (event.type) { + case SDL_EVENT_KEY_DOWN: { + if (event.key.key == SDLK_ESCAPE) { + m_status = resume; + } + } break; + default:; + } +} + +void +Breakout::DrawPauseMenu() +{ + ImGui::Begin("BreakoutPause"); + if (ImGui::Button("Resume")) { + m_status = resume; + } + if (ImGui::Button("Exit")) { + m_status = exit; + } + ImGui::End(); +} + diff --git a/src/games/breakout/Breakout.hpp b/src/games/breakout/Breakout.hpp new file mode 100644 index 0000000..9f2a0ef --- /dev/null +++ b/src/games/breakout/Breakout.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include + + +struct Ball { + V3F32 pos; + float radius; +}; + + +class Breakout : public Game { + enum GameStatus { + resume, + pause, + exit + }; + +public: + Breakout() = default; + bool Update(std::vector &events) override; + +private: + void ProcessEventDuringPause(SDL_Event& event); + void ProcessEventDuringResume(SDL_Event& event); + + void Draw(); + void DrawPauseMenu(); + + +private: + GameStatus m_status; + + Circle m_circle; +}; + diff --git a/src/games/minesweeper/Minesweeper.cpp b/src/games/minesweeper/Minesweeper.cpp index 8e27eb3..1f40b33 100644 --- a/src/games/minesweeper/Minesweeper.cpp +++ b/src/games/minesweeper/Minesweeper.cpp @@ -156,6 +156,7 @@ Minesweeper::ProcessEventDuringPause(SDL_Event &event) m_run_state = resume; } } break; + default:; } } @@ -170,9 +171,9 @@ Minesweeper::ProcessEventDuringResume(SDL_Event &event) } } break; - case SDL_EVENT_MOUSE_BUTTON_DOWN: { - V2F32 click_screen_pos = {event.button.x, (float)g_renderer.m_screen_h-1 - event.button.y}; - V2F32 click_view_pos = ScreenPosToViewPos(click_screen_pos); + case SDL_EVENT_MOUSE_BUTTON_DOWN: { + V2F32 click_screen_pos = {event.button.x, (float)g_renderer.m_screen_h-1 - event.button.y}; + V2F32 click_view_pos = ScreenPosToViewPos(click_screen_pos); float x_adjusted = click_view_pos.x - m_grid_pos.x; float y_adjusted = click_view_pos.y - m_grid_pos.y; @@ -371,7 +372,7 @@ Minesweeper::DrawBoard() m_grid_pos.x + (float)x * m_cell_outer_size.x, m_grid_pos.y + (float)y * m_cell_outer_size.y, }; - RectF32 cell_rect = { + Rectangle cell_rect = { cell_pos.x, cell_pos.y, cell_pos.x + m_cell_inner_size.x, cell_pos.y + m_cell_inner_size.y }; @@ -390,7 +391,7 @@ Minesweeper::DrawBoard() cell_pos.y + flag_offset.y, 1.0f }; - RectF32 flag_rect = { + Rectangle flag_rect = { flag_pos.x, flag_pos.y, flag_pos.x + flag_size.x, @@ -406,7 +407,7 @@ Minesweeper::DrawBoard() cell_pos.y, 1.0f }; - RectF32 mine_rect = { + Rectangle mine_rect = { mine_pos.x, mine_pos.y, mine_pos.x + m_cell_inner_size.x, @@ -426,10 +427,9 @@ Minesweeper::DrawBoard() }; size_t mine_count_val = (size_t)m_adjacent_mine_counts[y*m_grid_width + x]; g_renderer.PushMonoBitmap( + m_digit_glyphs[mine_count_val].bitmap, mine_count_pos, - m_digit_glyphs[mine_count_val].bitmap.width, - m_digit_glyphs[mine_count_val].bitmap.height, - m_digit_glyphs[mine_count_val].bitmap.pixels.get()); + Color{0.0f, 0.0f, 0.0f, 0.0f}); } } } diff --git a/src/games/minesweeper/Minesweeper.hpp b/src/games/minesweeper/Minesweeper.hpp index b90f4a3..c0bae7a 100644 --- a/src/games/minesweeper/Minesweeper.hpp +++ b/src/games/minesweeper/Minesweeper.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include #include diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp index a5fd76c..4c6ca18 100644 --- a/src/games/snake/Snake.cpp +++ b/src/games/snake/Snake.cpp @@ -12,8 +12,8 @@ Snake::Snake() m_LastMillisecondsSinceT0 = SDL_GetTicks(); m_TilesPerSecond = 4.0f; - m_Direction = DIRECTION_RIGHT; - m_LastAdvancedDirection = DIRECTION_RIGHT; + m_Direction = right; + m_LastAdvancedDirection = right; m_MapWidth = 16; m_MapHeight = 16; @@ -84,16 +84,16 @@ void Snake::MaybeMoveSnake(float dt_in_seconds) { // find head_pos - if (m_Direction == DIRECTION_UP) { + if (m_Direction == up) { head_pos.y += 1; } - else if (m_Direction == DIRECTION_DOWN) { + else if (m_Direction == down) { head_pos.y -= 1; } - else if (m_Direction == DIRECTION_RIGHT) { + else if (m_Direction == right) { head_pos.x += 1; } - else if (m_Direction == DIRECTION_LEFT) { + else if (m_Direction == left) { head_pos.x -= 1; } if ((head_pos.x < 0 || head_pos.x >= m_MapWidth) || @@ -162,31 +162,31 @@ void Snake::ProcessEventDuringResume(SDL_Event &event) { switch (event.type) { case SDL_EVENT_KEY_DOWN: { if (event.key.key == SDLK_UP) { - if (m_LastAdvancedDirection == DIRECTION_RIGHT || - m_LastAdvancedDirection == DIRECTION_LEFT) + if (m_LastAdvancedDirection == right || + m_LastAdvancedDirection == left) { - m_Direction = DIRECTION_UP; + m_Direction = up; } } else if (event.key.key == SDLK_DOWN) { - if (m_LastAdvancedDirection == DIRECTION_RIGHT || - m_LastAdvancedDirection == DIRECTION_LEFT) + if (m_LastAdvancedDirection == right || + m_LastAdvancedDirection == left) { - m_Direction = DIRECTION_DOWN; + m_Direction = down; } } else if (event.key.key == SDLK_RIGHT) { - if (m_LastAdvancedDirection == DIRECTION_UP || - m_LastAdvancedDirection == DIRECTION_DOWN) + if (m_LastAdvancedDirection == up || + m_LastAdvancedDirection == down) { - m_Direction = DIRECTION_RIGHT; + m_Direction = right; } } else if (event.key.key == SDLK_LEFT) { - if (m_LastAdvancedDirection == DIRECTION_UP || - m_LastAdvancedDirection == DIRECTION_DOWN) + if (m_LastAdvancedDirection == up || + m_LastAdvancedDirection == down) { - m_Direction = DIRECTION_LEFT; + m_Direction = left; } } else if (event.key.key == SDLK_ESCAPE) { @@ -270,7 +270,7 @@ void Snake::Draw() { /* draw map background */ V3F32 map_world_pos = {map_x, map_y, 0.0f}; V2F32 map_world_dim = {map_view_width, map_view_height}; - RectF32 map_world_rect = { + Rectangle map_world_rect = { map_world_pos.x, map_world_pos.y, map_world_pos.x + map_world_dim.x, @@ -298,7 +298,7 @@ void Snake::Draw() { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, @@ -326,7 +326,7 @@ void Snake::Draw() { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, @@ -346,7 +346,7 @@ void Snake::Draw() { 1.0f }; V2F32 dim = {bodypart_size, bodypart_size}; - RectF32 rect = { + Rectangle rect = { pos.x, pos.y, pos.x + dim.x, diff --git a/src/games/snake/Snake.hpp b/src/games/snake/Snake.hpp index 1223cbe..51d98fb 100644 --- a/src/games/snake/Snake.hpp +++ b/src/games/snake/Snake.hpp @@ -1,7 +1,7 @@ #pragma once #include -#include +#include #include @@ -9,10 +9,10 @@ class Snake : public Game { public: enum Direction : int32_t { - DIRECTION_UP, - DIRECTION_DOWN, - DIRECTION_LEFT, - DIRECTION_RIGHT, + up, + down, + left, + right, }; diff --git a/src/games/tetris/Board.cpp b/src/games/tetris/Board.cpp index dd83cb1..1e57b2f 100644 --- a/src/games/tetris/Board.cpp +++ b/src/games/tetris/Board.cpp @@ -100,7 +100,7 @@ void Board::Draw(int32_t level) { tetromino_size_with_border * 10, tetromino_size_with_border * 20 }; - RectF32 bg_world_rect = { + Rectangle bg_world_rect = { bg_world_pos.x, bg_world_pos.y, bg_world_pos.x + bg_world_dim.x, @@ -128,7 +128,7 @@ void Board::Draw(int32_t level) { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, diff --git a/src/games/tetris/Board.hpp b/src/games/tetris/Board.hpp index f841e2f..2e2edb2 100644 --- a/src/games/tetris/Board.hpp +++ b/src/games/tetris/Board.hpp @@ -1,6 +1,7 @@ #pragma once -#include +#include + class Tetromino; @@ -9,6 +10,7 @@ struct BoardPos { int32_t y; }; + class Board { public: Board(); diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp index a8004f3..d26649f 100644 --- a/src/games/tetris/Tetris.cpp +++ b/src/games/tetris/Tetris.cpp @@ -67,9 +67,9 @@ bool Tetris::Update(std::vector &events) { for (auto &event : events) { using enum TetrisRunningState; switch (m_RunningState) { - case Resume: UpdateResumeState(event); break; - case Pause: UpdatePauseState(event); break; - default:; + case Resume: UpdateResumeState(event); break; + case Pause: UpdatePauseState(event); break; + default:; } } diff --git a/src/games/tetris/Tetris.hpp b/src/games/tetris/Tetris.hpp index 828f2e5..e517f1c 100644 --- a/src/games/tetris/Tetris.hpp +++ b/src/games/tetris/Tetris.hpp @@ -5,6 +5,7 @@ #include #include + enum class TetrisRunningState { Resume, Pause, diff --git a/src/games/tetris/Tetromino.cpp b/src/games/tetris/Tetromino.cpp index 94343c0..9179d6d 100644 --- a/src/games/tetris/Tetromino.cpp +++ b/src/games/tetris/Tetromino.cpp @@ -158,22 +158,24 @@ Color Tetromino::GetColor(TetrominoId id) { using enum TetrominoId; Color color; + switch (id) { - case i_piece: - case o_piece: - case t_piece: { - color = {0.8f, 0.8f, 0.8f, 1.0f}; - } break; - - case j_piece: - case s_piece: { - color = {0.8f, 0.2f, 0.2f, 1.0f}; - } break; - - default: { - color = {0.2f, 0.4f, 0.2f, 1.0f}; - } + case i_piece: + case o_piece: + case t_piece: { + color = {0.8f, 0.8f, 0.8f, 1.0f}; + } break; + + case j_piece: + case s_piece: { + color = {0.8f, 0.2f, 0.2f, 1.0f}; + } break; + + default: { + color = {0.2f, 0.4f, 0.2f, 1.0f}; } + } + return color; } @@ -202,7 +204,7 @@ void Tetromino::Draw(TetrominoId id, int32_t ori, V2F32 pos, float scale) { 1.0f }; V2F32 world_dim = local_dim; - RectF32 world_rect = { + Rectangle world_rect = { world_pos.x, world_pos.y, world_pos.x + world_dim.x, diff --git a/src/games/tetris/Tetromino.hpp b/src/games/tetris/Tetromino.hpp index ecc986b..88f71f5 100644 --- a/src/games/tetris/Tetromino.hpp +++ b/src/games/tetris/Tetromino.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include +#include +#include #include diff --git a/src/main.cpp b/src/main.cpp index 16dce53..7fc7af6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ -#include -#include +#include #include #include @@ -13,27 +12,30 @@ #include #include +#include #include #include -Game::GameType -DrawGameMenu() +Game::GameType DrawGameMenu() { - Game::GameType type = Game::NO_GAME; + Game::GameType type = Game::no_game; //ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove; ImGuiWindowFlags flags = 0; ImGui::Begin("Game Selection", nullptr, flags); if (ImGui::Button("Tetris")) { - type = Game::TETRIS; + type = Game::tetris; } if (ImGui::Button("Snake")) { - type = Game::SNAKE; + type = Game::snake; } if (ImGui::Button("Minesweeper")) { - type = Game::MINESWEEPER; + type = Game::minesweeper; + } + if (ImGui::Button("Breakout")) { + type = Game::breakout; } ImGui::End(); @@ -185,7 +187,7 @@ main(int argc, char **argv) } else { Game::GameType type = DrawGameMenu(); - if (type != Game::NO_GAME) { + if (type != Game::no_game) { game = Game::Select(type); } } diff --git a/src/renderer/RSoftwareBackend.cpp b/src/renderer/RSoftwareBackend.cpp index f0e2e5f..217354b 100644 --- a/src/renderer/RSoftwareBackend.cpp +++ b/src/renderer/RSoftwareBackend.cpp @@ -1,3 +1,4 @@ +#include "renderer/Renderer.hpp" #include #include @@ -55,8 +56,7 @@ RSoftwareBackend::Draw() REntity_Rectangle clear_rect = { REntityType_Rectangle, - 0, 0, - (float)(m_canvas.w-1), (float)(m_canvas.h-1), + {0, 0, (float)(m_canvas.w-1), (float)(m_canvas.h-1)}, 0.0f, m_renderer.m_clear_color }; @@ -71,16 +71,15 @@ RSoftwareBackend::Draw() REntity& entity = m_renderer.m_render_entities[sort_entry.entity_index]; switch (entity.type) { - case REntityType_Rectangle: { - DrawRectangle(entity.rect); - } break; + case REntityType_Rectangle: { + DrawRectangle(entity.rect); + } break; - case REntityType_MonoBitmap: { - Color color = {0.0f, 0.0f, 0.0f, 1.0f}; - DrawMonoBitmap(entity.bitmap, color); - } break; + case REntityType_MonoBitmap: { + DrawMonoBitmap(entity.bitmap); + } break; - default:; + default:; } } @@ -110,12 +109,12 @@ RSoftwareBackend::SortRenderEntities() } void -RSoftwareBackend::DrawRectangle(REntity_Rectangle& rect) +RSoftwareBackend::DrawRectangle(REntity_Rectangle& entity) { - int32_t xmin = m_renderer.WorldXToScreenX(rect.x0); - int32_t ymin = m_renderer.WorldYToScreenY(rect.y0); - int32_t xmax = m_renderer.WorldXToScreenX(rect.x1); - int32_t ymax = m_renderer.WorldYToScreenY(rect.y1); + int32_t xmin = m_renderer.WorldXToScreenX(entity.rect.x0); + int32_t ymin = m_renderer.WorldYToScreenY(entity.rect.y0); + int32_t xmax = m_renderer.WorldXToScreenX(entity.rect.x1); + int32_t ymax = m_renderer.WorldYToScreenY(entity.rect.y1); if (xmin < 0) { xmin = 0; @@ -136,9 +135,9 @@ RSoftwareBackend::DrawRectangle(REntity_Rectangle& rect) for (int32_t y = ymin; y <= ymax; ++y) { uint32_t *pixel = m_canvas.pixels + y * m_canvas.w + xmin; for (int32_t x = xmin; x <= xmax; ++x) { - uint32_t r = (uint32_t)(rect.color.r * 255.0f); - uint32_t g = (uint32_t)(rect.color.g * 255.0f); - uint32_t b = (uint32_t)(rect.color.b * 255.0f); + uint32_t r = (uint32_t)(entity.color.r * 255.0f); + uint32_t g = (uint32_t)(entity.color.g * 255.0f); + uint32_t b = (uint32_t)(entity.color.b * 255.0f); uint32_t val = r << rshift | g << gshift | b << bshift; *pixel++ = val; } @@ -147,12 +146,12 @@ RSoftwareBackend::DrawRectangle(REntity_Rectangle& rect) void -RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& mono_bitmap, Color color) +RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& entity) { - int32_t x0 = (int32_t)mono_bitmap.x; - int32_t y0 = (int32_t)mono_bitmap.y; - int32_t x1 = (int32_t)mono_bitmap.x + mono_bitmap.w - 1; - int32_t y1 = (int32_t)mono_bitmap.y + mono_bitmap.h - 1; + int32_t x0 = (int32_t)entity.pos.x; + int32_t y0 = (int32_t)entity.pos.y; + int32_t x1 = (int32_t)entity.pos.x + entity.bitmap.w - 1; + int32_t y1 = (int32_t)entity.pos.y + entity.bitmap.h - 1; int32_t cut_left = 0; int32_t cut_bot = 0; @@ -177,7 +176,7 @@ RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& mono_bitmap, Color color) uint32_t gshift = m_canvas.gshift; uint32_t bshift = m_canvas.bshift; - uint8_t* grayscale = (uint8_t*)mono_bitmap.data + (-cut_bot * mono_bitmap.w) + (-cut_left); + uint8_t* grayscale = (uint8_t*)entity.bitmap.pixels.get() + (-cut_bot * entity.bitmap.w) + (-cut_left); uint32_t* rgba = m_canvas.pixels + y0 * m_canvas.w + x0; for (int32_t y = y0; y <= y1; y++) { uint8_t *grayscale_pixels = grayscale; @@ -192,9 +191,9 @@ RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& mono_bitmap, Color color) float g0 = (rgba_result >> gshift) & 0xff; float b0 = (rgba_result >> bshift) & 0xff; - float r1 = r0 + (color.r - r0)*alpha; - float g1 = g0 + (color.g - g0)*alpha; - float b1 = b0 + (color.b - b0)*alpha; + float r1 = r0 + (entity.color.r - r0)*alpha; + float g1 = g0 + (entity.color.g - g0)*alpha; + float b1 = b0 + (entity.color.b - b0)*alpha; rgba_result = (uint32_t)r1 << rshift | (uint32_t)g1 << gshift | (uint32_t)b1 << bshift; *rgba_pixels = rgba_result; @@ -203,7 +202,7 @@ RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& mono_bitmap, Color color) rgba_pixels++; } - grayscale += mono_bitmap.w; + grayscale += entity.bitmap.w; rgba += m_canvas.w; } } diff --git a/src/renderer/RSoftwareBackend.hpp b/src/renderer/RSoftwareBackend.hpp index 5457c7e..53cca9b 100644 --- a/src/renderer/RSoftwareBackend.hpp +++ b/src/renderer/RSoftwareBackend.hpp @@ -13,7 +13,7 @@ public: uint32_t ashift; int32_t w; int32_t h; - uint32_t *pixels; + uint32_t* pixels; }; @@ -27,8 +27,8 @@ private: void ResizeCanvas(int32_t w, int32_t h); void SortRenderEntities(); - void DrawRectangle(REntity_Rectangle& rect); - void DrawMonoBitmap(REntity_MonoBitmap& mono_bitmap, Color color); + void DrawRectangle(REntity_Rectangle& entity); + void DrawMonoBitmap(REntity_MonoBitmap& entity); private: diff --git a/src/renderer/Renderer.cpp b/src/renderer/Renderer.cpp index 413b3cb..f10319a 100644 --- a/src/renderer/Renderer.cpp +++ b/src/renderer/Renderer.cpp @@ -70,33 +70,42 @@ Renderer::Clear(Color color) } void -Renderer::PushRectangle(RectF32 rect, float z, Color color) +Renderer::PushMonoBitmap(MonoBitmap& bitmap, V3F32 pos, Color color) +{ + m_render_entities.emplace_back(REntity{.bitmap{ + REntityType_MonoBitmap, + pos, + bitmap, + color + }}); + m_sort_entries.emplace_back(pos.z, m_render_entities.size()-1); +} + +void +Renderer::PushRectangle(Rectangle rect, float z, Color color) { m_render_entities.emplace_back(REntity{.rect{ REntityType_Rectangle, - rect.x0, rect.y0, - rect.x1, rect.y1, + rect, z, - color} - }); + color + }}); m_sort_entries.emplace_back(z, m_render_entities.size()-1); } void -Renderer::PushMonoBitmap(V3F32 pos, int w, int h, void *data) +Renderer::PushCircle(Circle circle, float z, Color color) { - m_render_entities.emplace_back(REntity{.bitmap{ - REntityType_MonoBitmap, - pos.x, pos.y, - w, h, - pos.z, - data + m_render_entities.emplace_back(REntity{.circle{ + REntityType_Circle, + circle, + z, + color }}); - m_sort_entries.emplace_back(pos.z, m_render_entities.size()-1); + m_sort_entries.emplace_back(z, m_render_entities.size()-1); } - /* temporary helper functions (from old RGroup api) */ float diff --git a/src/renderer/Renderer.hpp b/src/renderer/Renderer.hpp index 4fdf524..fc037d6 100644 --- a/src/renderer/Renderer.hpp +++ b/src/renderer/Renderer.hpp @@ -1,6 +1,8 @@ #pragma once -#include +#include +#include +#include #include #include @@ -19,32 +21,35 @@ extern Renderer g_renderer; enum REntityType : int32_t { REntityType_Rectangle, REntityType_MonoBitmap, + REntityType_Circle, +}; + +struct REntity_MonoBitmap { + REntityType type; + V3F32 pos; + MonoBitmap& bitmap; + Color color; }; struct REntity_Rectangle { REntityType type; - float x0; - float y0; - float x1; - float y1; + Rectangle rect; float z; Color color; }; -struct REntity_MonoBitmap { +struct REntity_Circle { REntityType type; - float x; - float y; - int32_t w; - int32_t h; + Circle circle; float z; - void *data; + Color color; }; union REntity { REntityType type; - REntity_Rectangle rect; REntity_MonoBitmap bitmap; + REntity_Rectangle rect; + REntity_Circle circle; }; struct RSortEntry { @@ -64,8 +69,9 @@ public: void Reset(); void Clear(Color color); - void PushRectangle(RectF32 rect, float z, Color color); - void PushMonoBitmap(V3F32 pos, int w, int h, void* bitmap); + void PushRectangle(Rectangle rect, float z, Color color); + void PushMonoBitmap(MonoBitmap& bitmap, V3F32 pos, Color color); + void PushCircle(Circle circle, float z, Color color); /* helper functions */ @@ -82,6 +88,7 @@ public: /* temporary helper functions (from old RGroup api) */ + float GetScale(); V2F32 ViewPosToScreenPos(V2F32 view_pos); V2F32 ViewSizeToScreenSize(V2F32 view_size); @@ -90,7 +97,6 @@ public: - // Todo: make this private public: int32_t m_screen_w; int32_t m_screen_h; @@ -102,7 +108,6 @@ public: std::vector m_render_entities; std::vector m_sort_entries; - std::unique_ptr m_backend; -- cgit v1.2.3