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/renderer/RSoftwareBackend.cpp | 55 +++++++++++++++++++-------------------- src/renderer/RSoftwareBackend.hpp | 6 ++--- src/renderer/Renderer.cpp | 37 ++++++++++++++++---------- src/renderer/Renderer.hpp | 37 ++++++++++++++------------ 4 files changed, 74 insertions(+), 61 deletions(-) (limited to 'src/renderer') 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