From abb22cda9a82a323fd8f1d077adefd6970a1abaa Mon Sep 17 00:00:00 2001 From: fschildt Date: Mon, 13 Oct 2025 13:59:54 +0200 Subject: minesweeper: draw colored mine counters --- src/renderer/RSoftwareBackend.cpp | 47 +++++++++++++++++++++------------------ src/renderer/RSoftwareBackend.hpp | 2 +- src/renderer/Renderer.cpp | 4 ++-- src/renderer/Renderer.hpp | 10 ++++----- 4 files changed, 33 insertions(+), 30 deletions(-) (limited to 'src/renderer') diff --git a/src/renderer/RSoftwareBackend.cpp b/src/renderer/RSoftwareBackend.cpp index 217354b..7bcdd8f 100644 --- a/src/renderer/RSoftwareBackend.cpp +++ b/src/renderer/RSoftwareBackend.cpp @@ -75,8 +75,8 @@ RSoftwareBackend::Draw() DrawRectangle(entity.rect); } break; - case REntityType_MonoBitmap: { - DrawMonoBitmap(entity.bitmap); + case REntityType_AlphaBitmap: { + DrawAlphaBitmap(entity.bitmap); } break; default:; @@ -146,7 +146,7 @@ RSoftwareBackend::DrawRectangle(REntity_Rectangle& entity) void -RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& entity) +RSoftwareBackend::DrawAlphaBitmap(REntity_AlphaBitmap& entity) { int32_t x0 = (int32_t)entity.pos.x; int32_t y0 = (int32_t)entity.pos.y; @@ -176,34 +176,37 @@ RSoftwareBackend::DrawMonoBitmap(REntity_MonoBitmap& entity) uint32_t gshift = m_canvas.gshift; uint32_t bshift = m_canvas.bshift; - 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; + 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++) { - uint8_t *grayscale_pixels = grayscale; - uint32_t *rgba_pixels = rgba; + uint8_t* alpha = alpha_row; + uint32_t* rgba = rgba_row; for (int32_t x = x0; x <= x1; x++) { - float alpha = *grayscale_pixels / 255.0f; + if (*alpha == 0) { + alpha++; + rgba++; + continue; + } - // Todo: we do not want to blend with existing color! + float alphaf = *alpha / 255.0f; - uint32_t rgba_result = *rgba_pixels; - float r0 = (rgba_result >> rshift) & 0xff; - float g0 = (rgba_result >> gshift) & 0xff; - float b0 = (rgba_result >> bshift) & 0xff; + float r1 = entity.color.r * alphaf; + float g1 = entity.color.g * alphaf; + float b1 = entity.color.b * alphaf; - float r1 = r0 + (entity.color.r - r0)*alpha; - float g1 = g0 + (entity.color.g - g0)*alpha; - float b1 = b0 + (entity.color.b - b0)*alpha; + 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; - rgba_result = (uint32_t)r1 << rshift | (uint32_t)g1 << gshift | (uint32_t)b1 << bshift; - *rgba_pixels = rgba_result; + uint32_t rgba_result = r2 | g2 | b2; + *rgba = rgba_result; - grayscale_pixels++; - rgba_pixels++; + alpha++; + rgba++; } - grayscale += entity.bitmap.w; - rgba += m_canvas.w; + alpha_row += entity.bitmap.w; + rgba_row += m_canvas.w; } } diff --git a/src/renderer/RSoftwareBackend.hpp b/src/renderer/RSoftwareBackend.hpp index 53cca9b..89d6a52 100644 --- a/src/renderer/RSoftwareBackend.hpp +++ b/src/renderer/RSoftwareBackend.hpp @@ -28,7 +28,7 @@ private: void SortRenderEntities(); void DrawRectangle(REntity_Rectangle& entity); - void DrawMonoBitmap(REntity_MonoBitmap& entity); + void DrawAlphaBitmap(REntity_AlphaBitmap& entity); private: diff --git a/src/renderer/Renderer.cpp b/src/renderer/Renderer.cpp index f10319a..4e449ba 100644 --- a/src/renderer/Renderer.cpp +++ b/src/renderer/Renderer.cpp @@ -70,10 +70,10 @@ Renderer::Clear(Color color) } void -Renderer::PushMonoBitmap(MonoBitmap& bitmap, V3F32 pos, Color color) +Renderer::PushAlphaBitmap(AlphaBitmap& bitmap, V3F32 pos, Color color) { m_render_entities.emplace_back(REntity{.bitmap{ - REntityType_MonoBitmap, + REntityType_AlphaBitmap, pos, bitmap, color diff --git a/src/renderer/Renderer.hpp b/src/renderer/Renderer.hpp index fc037d6..b825681 100644 --- a/src/renderer/Renderer.hpp +++ b/src/renderer/Renderer.hpp @@ -20,14 +20,14 @@ extern Renderer g_renderer; enum REntityType : int32_t { REntityType_Rectangle, - REntityType_MonoBitmap, + REntityType_AlphaBitmap, REntityType_Circle, }; -struct REntity_MonoBitmap { +struct REntity_AlphaBitmap { REntityType type; V3F32 pos; - MonoBitmap& bitmap; + AlphaBitmap& bitmap; Color color; }; @@ -47,7 +47,7 @@ struct REntity_Circle { union REntity { REntityType type; - REntity_MonoBitmap bitmap; + REntity_AlphaBitmap bitmap; REntity_Rectangle rect; REntity_Circle circle; }; @@ -70,7 +70,7 @@ public: void Clear(Color color); void PushRectangle(Rectangle rect, float z, Color color); - void PushMonoBitmap(MonoBitmap& bitmap, V3F32 pos, Color color); + void PushAlphaBitmap(AlphaBitmap& bitmap, V3F32 pos, Color color); void PushCircle(Circle circle, float z, Color color); -- cgit v1.2.3