diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-10-16 15:33:06 +0200 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-10-16 15:33:06 +0200 |
| commit | a873df7a66dc1831cee4eae2d998abed88246268 (patch) | |
| tree | c19cd079ce106e1431d64c34babf4ef59cf71723 /src/renderer/RSoftwareBackend.cpp | |
| parent | 9f2845b12135c32dde91e58afc1193d54333ec9f (diff) | |
renderer: introduce text rendering
Diffstat (limited to 'src/renderer/RSoftwareBackend.cpp')
| -rw-r--r-- | src/renderer/RSoftwareBackend.cpp | 123 |
1 files changed, 95 insertions, 28 deletions
diff --git a/src/renderer/RSoftwareBackend.cpp b/src/renderer/RSoftwareBackend.cpp index 4d78ec7..d05cee7 100644 --- a/src/renderer/RSoftwareBackend.cpp +++ b/src/renderer/RSoftwareBackend.cpp @@ -1,3 +1,4 @@ +#include "imgui.h" #include <renderer/RSoftwareBackend.hpp> #include <SDL3/SDL_video.h> @@ -8,9 +9,8 @@ #include <cstdio> -RSoftwareBackend::RSoftwareBackend(SDL_Window* window, Renderer& renderer) - : m_window {window} - , m_renderer {renderer} +RSoftwareBackend::RSoftwareBackend(Renderer& renderer) + : m_renderer {renderer} { m_canvas.rshift = 0; m_canvas.gshift = 8; @@ -53,21 +53,7 @@ RSoftwareBackend::Draw() SortRenderEntities(); - REntity_Rectangle clear_rect = { - REntityType_Rectangle, - {0, 0, (float)(m_canvas.w-1), (float)(m_canvas.h-1)}, - 0.0f, - m_renderer.m_clear_color - }; - DrawRectangle(clear_rect); - - - float z = -1; for (RSortEntry sort_entry : m_renderer.m_sort_entries) { - if (sort_entry.z >= z) { - z = sort_entry.z; - } - REntity& entity = m_renderer.m_render_entities[sort_entry.entity_index]; switch (entity.type) { case REntityType_Rectangle: { @@ -78,6 +64,13 @@ RSoftwareBackend::Draw() DrawAlphaBitmap(entity.bitmap); } break; + case REntityType_Text: { + DrawText(entity.text); + }; break; + + case REntityType_Circle: { + }; break; + default:; } } @@ -147,10 +140,10 @@ RSoftwareBackend::DrawRectangle(REntity_Rectangle& entity) void RSoftwareBackend::DrawAlphaBitmap(REntity_AlphaBitmap& entity) { - 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 x0 = m_renderer.WorldXToScreenX(entity.pos.x); + int32_t y0 = m_renderer.WorldYToScreenY(entity.pos.y); + int32_t x1 = x0 + entity.bitmap.w - 1; + int32_t y1 = y0 + entity.bitmap.h - 1; int32_t cut_left = 0; int32_t cut_bot = 0; @@ -171,10 +164,6 @@ RSoftwareBackend::DrawAlphaBitmap(REntity_AlphaBitmap& entity) } - uint32_t rshift = m_canvas.rshift; - uint32_t gshift = m_canvas.gshift; - uint32_t bshift = m_canvas.bshift; - uint8_t* alpha_row = (uint8_t*)entity.bitmap.pixels.get() + (-cut_bot * entity.bitmap.w) + (-cut_left); uint32_t* rgba_row = m_canvas.pixels + y0 * m_canvas.w + x0; for (int32_t y = y0; y <= y1; y++) { @@ -193,9 +182,9 @@ RSoftwareBackend::DrawAlphaBitmap(REntity_AlphaBitmap& entity) float g1 = entity.color.g * alphaf; float b1 = entity.color.b * alphaf; - uint32_t r2 = uint32_t(r1 * 255.0f) << rshift; - uint32_t g2 = uint32_t(g1 * 255.0f) << gshift; - uint32_t b2 = uint32_t(b1 * 255.0f) << bshift; + uint32_t r2 = uint32_t(r1 * 255.0f) << m_canvas.rshift; + uint32_t g2 = uint32_t(g1 * 255.0f) << m_canvas.gshift; + uint32_t b2 = uint32_t(b1 * 255.0f) << m_canvas.bshift; uint32_t rgba_result = r2 | g2 | b2; *rgba = rgba_result; @@ -209,3 +198,81 @@ RSoftwareBackend::DrawAlphaBitmap(REntity_AlphaBitmap& entity) } } +void +RSoftwareBackend::DrawText(REntity_Text& entity) +{ + int32_t xscreen = m_renderer.WorldXToScreenX(entity.pos.x); + int32_t yscreen = m_renderer.WorldYToScreenY(entity.pos.y); + for (char32_t ch : entity.text) { + Glyph& glyph = entity.font.GetGlyph(ch); + + int32_t x = xscreen + glyph.xoff; + int32_t y = yscreen + glyph.yoff; + DrawTextGlyph(glyph, entity.color, x, y); + + xscreen += glyph.xadvance; + } +} + +void +RSoftwareBackend::DrawTextGlyph(Glyph& glyph, Color color, int32_t xscreen, int32_t yscreen) +{ + int32_t x0 = xscreen; + int32_t y0 = yscreen; + int32_t x1 = x0 + glyph.bitmap.w - 1; + int32_t y1 = y0 + glyph.bitmap.h - 1; + + int32_t cut_left = 0; + int32_t cut_bot = 0; + + if (x0 < 0) { + cut_left = x0; + x0 = 0; + } + if (y0 < 0) { + cut_bot = y0; + y0 = 0; + } + if (x1 >= m_canvas.w) { + x1 = m_canvas.w - 1; + } + if (y1 >= m_canvas.h) { + y1 = m_canvas.h - 1; + } + + + uint8_t* alpha_row = (uint8_t*)glyph.bitmap.pixels.get() + (-cut_bot * glyph.bitmap.w) + (-cut_left); + uint32_t* rgba_row = m_canvas.pixels + y0 * m_canvas.w + x0; + + for (int32_t y = y0; y <= y1; y++) { + uint8_t* alpha = alpha_row; + uint32_t* rgba = rgba_row; + for (int32_t x = x0; x <= x1; x++) { + if (*alpha == 0) { + alpha++; + rgba++; + continue; + } + + float alphaf = *alpha / 255.0f; + + float r1 = color.r * alphaf; + float g1 = color.g * alphaf; + float b1 = color.b * alphaf; + + uint32_t r2 = uint32_t(r1 * 255.0f) << m_canvas.rshift; + uint32_t g2 = uint32_t(g1 * 255.0f) << m_canvas.gshift; + uint32_t b2 = uint32_t(b1 * 255.0f) << m_canvas.bshift; + + uint32_t rgba_result = r2 | g2 | b2; + *rgba = rgba_result; + + alpha++; + rgba++; + } + + alpha_row += glyph.bitmap.w; + rgba_row += m_canvas.w; + } +} + |
