diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-11-24 18:18:24 +0100 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-11-24 18:19:45 +0100 |
| commit | feb4bc8cbc66ba928319ddb2cc1bf48010487863 (patch) | |
| tree | 2011affc8ecb16d295fa86a8ef5b8e6f715a9bc3 /src/renderer | |
| parent | 935f1971bee684c6c9baded2564a550351cc5582 (diff) | |
renderer: improve resizing
Diffstat (limited to 'src/renderer')
| -rw-r--r-- | src/renderer/RSoftwareBackend.cpp | 66 | ||||
| -rw-r--r-- | src/renderer/RSoftwareBackend.hpp | 4 | ||||
| -rw-r--r-- | src/renderer/Renderer.cpp | 69 | ||||
| -rw-r--r-- | src/renderer/Renderer.hpp | 18 |
4 files changed, 50 insertions, 107 deletions
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; |
