aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfschildt <florian.schildt@protonmail.com>2025-11-24 18:18:24 +0100
committerfschildt <florian.schildt@protonmail.com>2025-11-24 18:19:45 +0100
commitfeb4bc8cbc66ba928319ddb2cc1bf48010487863 (patch)
tree2011affc8ecb16d295fa86a8ef5b8e6f715a9bc3
parent935f1971bee684c6c9baded2564a550351cc5582 (diff)
renderer: improve resizing
-rw-r--r--src/games/minesweeper/Minesweeper.cpp8
-rw-r--r--src/games/snake/Snake.cpp6
-rw-r--r--src/games/tetris/Tetris.cpp2
-rw-r--r--src/main.cpp13
-rw-r--r--src/renderer/RSoftwareBackend.cpp66
-rw-r--r--src/renderer/RSoftwareBackend.hpp4
-rw-r--r--src/renderer/Renderer.cpp69
-rw-r--r--src/renderer/Renderer.hpp18
8 files changed, 64 insertions, 122 deletions
diff --git a/src/games/minesweeper/Minesweeper.cpp b/src/games/minesweeper/Minesweeper.cpp
index 5a37a04..ae019c4 100644
--- a/src/games/minesweeper/Minesweeper.cpp
+++ b/src/games/minesweeper/Minesweeper.cpp
@@ -134,7 +134,7 @@ bool
Minesweeper::Update(std::vector<SDL_Event>& events)
{
g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.Clear({0.3f, 0.2f, 0.3f});
+ g_renderer.SetClearColor({0.3f, 0.2f, 0.3f});
for (SDL_Event &event : events) {
if (m_game_status == game_exit) {
@@ -220,7 +220,7 @@ Minesweeper::ProcessEventDuringResume(SDL_Event &event)
if (event.button.button == left_click) {
if (IsCovered(x, y)) {
if (IsMine(x, y)) {
- m_is_covered_bitmap[y] &= ~(1 << x);
+ m_is_covered_bitmap[y] &= (uint32_t)~(1 << x);
UncoverMines();
m_game_status = game_over;
}
@@ -253,7 +253,7 @@ Minesweeper::Uncover(int32_t x, int32_t y)
if (y >= m_grid_height) return;
if (!IsCovered(x, y)) return;
- m_is_covered_bitmap[y] &= ~(1 << x);
+ m_is_covered_bitmap[y] &= (uint32_t)~(1 << x);
m_cells_uncovered += 1;
if (IsFlagged(x, y)) {
@@ -281,7 +281,7 @@ Minesweeper::UncoverMines()
for (int32_t y{0}; y < m_grid_height; ++y) {
for (int32_t x{0}; x < m_grid_width; ++x) {
if (IsMine(x, y) && IsCovered(x, y)) {
- m_is_covered_bitmap[y] &= ~(1 << x);
+ m_is_covered_bitmap[y] &= (uint32_t)~(1 << x);
}
}
}
diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp
index 542122e..2149730 100644
--- a/src/games/snake/Snake.cpp
+++ b/src/games/snake/Snake.cpp
@@ -51,7 +51,7 @@ Snake::Update(std::vector<SDL_Event> &events)
{
Color clear_color = {0.3f, 0.3f, 0.3f, 1.0f};
g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.Clear(clear_color);
+ g_renderer.SetClearColor(clear_color);
if (m_game_status == game_starting) {
@@ -126,7 +126,7 @@ Snake::MaybeMoveSnake(float dt)
uint64_t head_bit = 1 << head_pos.x;
uint64_t body_bits = m_body_bitmap[head_pos.y];
if (head_pos.y == tail_pos.y) {
- body_bits &= ~(1 << tail_pos.x);
+ body_bits &= (uint32_t)~(1 << tail_pos.x);
}
if (head_bit & body_bits) {
m_game_status = game_over;
@@ -151,7 +151,7 @@ Snake::MaybeMoveSnake(float dt)
else {
// advance tail
V2I32 next_tail_pos = m_body_positions[m_tail];
- m_body_bitmap[next_tail_pos.y] &= ~(1 << next_tail_pos.x);
+ m_body_bitmap[next_tail_pos.y] &= (uint32_t)~(1 << next_tail_pos.x);
m_tail += 1;
if (m_tail >= max_positions) {
diff --git a/src/games/tetris/Tetris.cpp b/src/games/tetris/Tetris.cpp
index b59d1e9..d5c735d 100644
--- a/src/games/tetris/Tetris.cpp
+++ b/src/games/tetris/Tetris.cpp
@@ -60,7 +60,7 @@ Tetris::Update(std::vector<SDL_Event>& events)
Color clear_color = {0.2f, 0.2f, 0.2f, 1.0f};
g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.Clear(clear_color);
+ g_renderer.SetClearColor(clear_color);
float dt = ProcessDt();
diff --git a/src/main.cpp b/src/main.cpp
index 8129c68..c830443 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -21,10 +21,12 @@ Game::GameType
DrawGameMenu()
{
Game::GameType type = Game::no_game;
-
- //ImGuiWindowFlags flags = ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
ImGuiWindowFlags flags = 0;
+ ImVec2 pos = {100, 200};
+ ImGui::SetNextWindowPos(pos);
+
+
ImGui::Begin("Game Selection", nullptr, flags);
if (ImGui::Button("Tetris")) {
type = Game::tetris;
@@ -38,11 +40,6 @@ DrawGameMenu()
ImGui::End();
- Color clear_color = {0.4f, 0.4f, 0.4f, 1.0f};
- g_renderer.SetCameraSize(4.0f, 3.0f);
- g_renderer.Clear(clear_color);
-
-
return type;
}
@@ -175,6 +172,8 @@ main(int argc, char** argv)
int w, h;
SDL_GetWindowSize(window, &w, &h);
g_renderer.SetScreenSize(w, h);
+ g_renderer.SetClearColor({0.3f, 0.3f, 0.3f});
+ g_renderer.SetCameraSize(4.0f, 3.0f);
if (game) {
diff --git a/src/renderer/RSoftwareBackend.cpp b/src/renderer/RSoftwareBackend.cpp
index d05cee7..587ac74 100644
--- a/src/renderer/RSoftwareBackend.cpp
+++ b/src/renderer/RSoftwareBackend.cpp
@@ -1,4 +1,3 @@
-#include "imgui.h"
#include <renderer/RSoftwareBackend.hpp>
#include <SDL3/SDL_video.h>
@@ -25,33 +24,19 @@ RSoftwareBackend::RSoftwareBackend(Renderer& renderer)
glGenTextures(1, &m_gltexture_id);
glBindTexture(GL_TEXTURE_2D, m_gltexture_id);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-}
-
-void
-RSoftwareBackend::ResizeCanvas(int32_t w, int32_t h)
-{
- size_t realloc_size = (size_t)(w * h) * sizeof(m_canvas.pixels[0]);
- void *realloc_data = realloc(m_canvas.pixels, realloc_size);
- if (!realloc_data) {
- printf("could not resize offscreen buffer\n");
- return;
- }
-
- m_canvas.w = w;
- m_canvas.h = h;
- m_canvas.pixels = (uint32_t*)realloc_data;
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
void
RSoftwareBackend::Draw()
{
- ResizeCanvas(m_renderer.m_screen_w, m_renderer.m_screen_h);
- SortRenderEntities();
+ Resize(m_renderer.m_screen_w, m_renderer.m_screen_h);
+ DrawClear();
+ SortRenderEntities();
for (RSortEntry sort_entry : m_renderer.m_sort_entries) {
REntity& entity = m_renderer.m_render_entities[sort_entry.entity_index];
@@ -76,7 +61,6 @@ RSoftwareBackend::Draw()
}
- glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_gltexture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_canvas.w, m_canvas.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_canvas.pixels);
@@ -86,8 +70,23 @@ RSoftwareBackend::Draw()
glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f);
glEnd();
+}
+
+void
+RSoftwareBackend::Resize(int32_t w, int32_t h)
+{
+ size_t realloc_size = (size_t)(w * h) * sizeof(m_canvas.pixels[0]);
+ void *realloc_data = realloc(m_canvas.pixels, realloc_size);
+ if (!realloc_data) {
+ printf("could not resize offscreen buffer\n");
+ return;
+ }
- glBindTexture(GL_TEXTURE_2D, 0);
+ m_canvas.w = w;
+ m_canvas.h = h;
+ m_canvas.pixels = (uint32_t*)realloc_data;
+
+ glViewport(0, 0, w, h);
}
void
@@ -101,6 +100,25 @@ RSoftwareBackend::SortRenderEntities()
}
void
+RSoftwareBackend::DrawClear()
+{
+ Color color = m_renderer.m_clear_color;
+ uint32_t rshift = m_canvas.rshift;
+ uint32_t gshift = m_canvas.gshift;
+ uint32_t bshift = m_canvas.bshift;
+
+ for (int32_t y = 0; y < m_canvas.h; y++) {
+ for (int32_t x = 0; x < m_canvas.w; x++) {
+ uint32_t r = (uint32_t)(color.r * 255.0f);
+ uint32_t g = (uint32_t)(color.g * 255.0f);
+ uint32_t b = (uint32_t)(color.b * 255.0f);
+ uint32_t val = r << rshift | g << gshift | b << bshift;
+ m_canvas.pixels[y*m_canvas.w + x] = val;
+ }
+ }
+}
+
+void
RSoftwareBackend::DrawRectangle(REntity_Rectangle& entity)
{
int32_t xmin = m_renderer.WorldXToScreenX(entity.rect.x0);
diff --git a/src/renderer/RSoftwareBackend.hpp b/src/renderer/RSoftwareBackend.hpp
index 3e5e17d..9217061 100644
--- a/src/renderer/RSoftwareBackend.hpp
+++ b/src/renderer/RSoftwareBackend.hpp
@@ -28,13 +28,13 @@ public:
RSoftwareBackend(Renderer& renderer);
void Draw();
- void Clear(Color color);
private:
- void ResizeCanvas(int32_t w, int32_t h);
+ void Resize(int32_t w, int32_t h);
void SortRenderEntities();
+ void DrawClear();
void DrawRectangle(REntity_Rectangle& entity);
void DrawAlphaBitmap(REntity_AlphaBitmap& entity);
void DrawText(REntity_Text& entity);
diff --git a/src/renderer/Renderer.cpp b/src/renderer/Renderer.cpp
index 956c488..4b6cb2a 100644
--- a/src/renderer/Renderer.cpp
+++ b/src/renderer/Renderer.cpp
@@ -62,15 +62,9 @@ Renderer::WorldYToScreenY(float world_y)
}
void
-Renderer::Clear(Color color)
+Renderer::SetClearColor(Color color)
{
- Rectangle rect = {
- 0.0f,
- 0.0f,
- m_camera_w,
- m_camera_h
- };
- PushRectangle(rect, color, -1.0f);
+ m_clear_color = color;
}
void
@@ -121,62 +115,3 @@ Renderer::PushText(std::u32string& text, Font& font, V3F32 pos, Color color)
}
-/* temporary helper functions (from old RGroup api) */
-
-float
-Renderer::GetScale()
-{
- float screen_width = static_cast<float>(m_screen_w);
- float screen_height = static_cast<float>(m_screen_h);
- float xunits = screen_width / m_camera_w;
- float yunits = screen_height / m_camera_h;
- float scale = std::min(xunits, yunits);
- return scale;
-}
-
-V2F32
-Renderer::ViewPosToScreenPos(V2F32 view_pos)
-{
- float scale = GetScale();
- float screen_width = static_cast<float>(m_screen_w);
- float screen_height = static_cast<float>(m_screen_h);
- float viewport_width = m_camera_w * scale;
- float viewport_height = m_camera_h * scale;
- float viewport_x0 = (screen_width - viewport_width) / 2;
- float viewport_y0 = (screen_height - viewport_height) / 2;
-
- V2F32 result;
- result.x = viewport_x0 + view_pos.x * scale;
- result.y = screen_height - (viewport_y0 + view_pos.y * scale);
-
- return result;
-}
-
-V2F32
-Renderer::ViewSizeToScreenSize(V2F32 view_size)
-{
- float scale = GetScale();
-
- V2F32 result;
- result.x = view_size.x * scale;
- result.y = view_size.y * scale;
-
- return result;
-}
-
-ImVec2
-Renderer::ViewPosToScreenPosImGui(V2F32 view_pos)
-{
- V2F32 screen_pos = ViewPosToScreenPos(view_pos);
- ImVec2 result = {screen_pos.x, screen_pos.y};
- return result;
-}
-
-ImVec2
-Renderer::ViewSizeToScreenSizeImGui(V2F32 view_size)
-{
- V2F32 screen_size = ViewSizeToScreenSize(view_size);
- ImVec2 result = {screen_size.x, screen_size.y};
- return result;
-}
-
diff --git a/src/renderer/Renderer.hpp b/src/renderer/Renderer.hpp
index 515e1da..47837ee 100644
--- a/src/renderer/Renderer.hpp
+++ b/src/renderer/Renderer.hpp
@@ -78,7 +78,10 @@ public:
void Draw();
void Reset();
- void Clear(Color color);
+ void SetClearColor(Color color);
+ void SetScreenSize(int32_t w, int32_t h);
+ void SetCameraSize(float w, float h);
+
void PushAlphaBitmap(AlphaBitmap& bitmap, V3F32 pos, Color color);
void PushRectangle(Rectangle rect, Color color, float z);
void PushCircle(Circle circle, Color color, float z);
@@ -87,26 +90,13 @@ public:
/* helper functions */
- void SetScreenSize(int32_t w, int32_t h);
- void SetCameraSize(float w, float h);
-
int32_t WorldXToScreenX(float x);
int32_t WorldYToScreenY(float y);
-
int32_t WorldWidthToScreenWidth(float w);
int32_t WorldHeightToScreenHeight(float h);
- /* temporary helper functions (from old RGroup api) */
-
- float GetScale();
- V2F32 ViewPosToScreenPos(V2F32 view_pos);
- V2F32 ViewSizeToScreenSize(V2F32 view_size);
- ImVec2 ViewPosToScreenPosImGui(V2F32 view_pos);
- ImVec2 ViewSizeToScreenSizeImGui(V2F32 view_size);
-
-
public:
SDL_Window* m_window;