From 407dded717ee9309689f2ef20e3fa547ed29a85b Mon Sep 17 00:00:00 2001 From: oxmox Date: Wed, 22 Feb 2023 21:23:31 +0100 Subject: [PATCH] cleanup and revive the debug drawing code --- src/debug_draw.cc | 103 ++++++++++++++++++++++++++++++++ src/doompanning.cc | 146 +++++++-------------------------------------- 2 files changed, 126 insertions(+), 123 deletions(-) create mode 100644 src/debug_draw.cc diff --git a/src/debug_draw.cc b/src/debug_draw.cc new file mode 100644 index 0000000..2228c1f --- /dev/null +++ b/src/debug_draw.cc @@ -0,0 +1,103 @@ +inline s32 RoundFloatToInt(float value) +{ + return static_cast(value + 0.5); +} + +struct V4: public ImVec4 +{ + using ImVec4::ImVec4; + float &a = ImVec4::w; + float &r = ImVec4::x; + float &g = ImVec4::y; + float &b = ImVec4::z; +}; + +inline u32 V4ToARGB(V4 c) +{ + u32 result = ((RoundFloatToInt(c.a * 255.0) & 0xff) << 24 + | (RoundFloatToInt(c.r * 255.0) & 0xff) << 16 + | (RoundFloatToInt(c.g * 255.0) & 0xff) << 8 + | (RoundFloatToInt(c.b * 255.0) & 0xff) << 0); + return result; +} + +struct OffscreenBuffer +{ + s32 width; + s32 height; + u8 *pixels; + s32 pitch; + const int BytesPerPixel; +}; + +inline void PutPixelUnchecked(OffscreenBuffer *buffer, s32 x, s32 y, V4 color) +{ + u32 *pixel = reinterpret_cast(buffer->pixels + + y * buffer->pitch + + x * buffer->BytesPerPixel); + *pixel = V4ToARGB(color); +} + +inline void PutPixel(OffscreenBuffer *buffer, s32 x, s32 y, V4 color) +{ + if (x >= 0 && x < buffer->width && y >= 0 && y < buffer->height) + { + PutPixelUnchecked(buffer, x, y, color); + } +} + +void DrawRectangle(OffscreenBuffer *buffer, + float realMinX, float realMinY, + float realMaxX, float realMaxY, + V4 color) + +{ + s32 minX = RoundFloatToInt(realMinX); + s32 minY = RoundFloatToInt(realMinY); + s32 maxX = RoundFloatToInt(realMaxX); + s32 maxY = RoundFloatToInt(realMaxY); + + if (minX < 0) + minX = 0; + + if (minY < 0) + minY = 0; + + if (maxX > buffer->width) + maxX = buffer->width; + + if (maxY > buffer->height) + maxY = buffer->height; + + + u8 *row = buffer->pixels + minY * buffer->pitch; + + for (s32 y = minY; y < maxY; ++y) + { + u32 *dstPixel = reinterpret_cast(row + minX * buffer->BytesPerPixel); + + for (s32 x = minX; x < maxX; ++x) + { + float a = color.a; + float srcR = color.r * 255.0; + float srcG = color.g * 255.0; + float srcB = color.b * 255.0; + + float dstR = ((*dstPixel >> 16) & 0xff); + float dstG = ((*dstPixel >> 8) & 0xff); + float dstB = ((*dstPixel >> 0) & 0xff); + + dstR = (1 - a) * dstR + a * srcR; + dstG = (1 - a) * dstG + a * srcG; + dstB = (1 - a) * dstB + a * srcB; + + *dstPixel = (RoundFloatToInt(dstR) << 16 | + RoundFloatToInt(dstG) << 8 | + RoundFloatToInt(dstB) << 0); + + ++dstPixel; + } + + row += buffer->pitch; + } +} diff --git a/src/doompanning.cc b/src/doompanning.cc index ccd00b9..604355a 100644 --- a/src/doompanning.cc +++ b/src/doompanning.cc @@ -89,7 +89,6 @@ struct ControllerContext bool quit = false; ExampleAppLog appLog; int columns = 4; - std::array pixelBuffer; float scaleFactor = 1.0; s32 offsetX = 0; s32 offsetY = 0; @@ -214,6 +213,11 @@ void check_on_dooms(ControllerContext &ctx) // value to timeout dooms and erase them from ctx.dooms. } +#define DP_DO_DEBUG_DRAWING +#ifdef DP_DO_DEBUG_DRAWING +#include "debug_draw.cc" +#endif + void do_networking(ControllerContext &ctx) { // Set to true if we receive at least on DP_DS_Ready DoomState update. Then @@ -308,7 +312,24 @@ void do_networking(ControllerContext &ctx) auto &ds = *dit; ds.tLastActive = dp_now(); - const u8 *sourcePixels = msgDoomFrame->frame; + u8 *sourcePixels = msgDoomFrame->frame; + + #ifdef DP_DO_DEBUG_DRAWING + OffscreenBuffer buffer = + { + DoomScreenWidth, + DoomScreenHeight, + sourcePixels, + DoomScreenWidth * DoomBytesPerPixel, + DoomBytesPerPixel + }; + + DrawRectangle(&buffer, 0, 0, 10, 10, { 1, 0, 0, 1 }); + DrawRectangle(&buffer, 0, buffer.height-10, 10, buffer.height, { 0, 1, 0, 1 }); + DrawRectangle(&buffer, buffer.width-10, buffer.height-10, buffer.width, buffer.height, { 0, 0, 1, 1 }); + DrawRectangle(&buffer, buffer.width-10, 0, buffer.width, 10, { 0.840, 0.0168, 0.717, 1 }); + #endif + #if 0 // FIXME: buggy. black screen with tiny bar on top @@ -383,128 +404,8 @@ void final_cleanup(ControllerContext &ctx) } } -inline s32 RoundFloatToInt(float value) -{ - return static_cast(value + 0.5); -} - -struct V4: public ImVec4 -{ - using ImVec4::ImVec4; - float &a = ImVec4::w; - float &r = ImVec4::x; - float &g = ImVec4::y; - float &b = ImVec4::z; -}; - -inline u32 V4ToARGB(V4 c) -{ - u32 result = ((RoundFloatToInt(c.a * 255.0) & 0xff) << 24 - | (RoundFloatToInt(c.r * 255.0) & 0xff) << 16 - | (RoundFloatToInt(c.g * 255.0) & 0xff) << 8 - | (RoundFloatToInt(c.b * 255.0) & 0xff) << 0); - return result; -} - -struct OffscreenBuffer -{ - s32 width; - s32 height; - u8 *pixels; - s32 pitch; - const int BytesPerPixel; -}; - -inline void PutPixelUnchecked(OffscreenBuffer *buffer, s32 x, s32 y, V4 color) -{ - u32 *pixel = reinterpret_cast(buffer->pixels - + y * buffer->pitch - + x * buffer->BytesPerPixel); - *pixel = V4ToARGB(color); -} - -inline void PutPixel(OffscreenBuffer *buffer, s32 x, s32 y, V4 color) -{ - if (x >= 0 && x < buffer->width && y >= 0 && y < buffer->height) - { - PutPixelUnchecked(buffer, x, y, color); - } -} - -void DrawRectangle(OffscreenBuffer *buffer, - float realMinX, float realMinY, - float realMaxX, float realMaxY, - V4 color) - -{ - s32 minX = RoundFloatToInt(realMinX); - s32 minY = RoundFloatToInt(realMinY); - s32 maxX = RoundFloatToInt(realMaxX); - s32 maxY = RoundFloatToInt(realMaxY); - - if (minX < 0) - minX = 0; - - if (minY < 0) - minY = 0; - - if (maxX > buffer->width) - maxX = buffer->width; - - if (maxY > buffer->height) - maxY = buffer->height; - - - u8 *row = buffer->pixels + minY * buffer->pitch; - - for (s32 y = minY; y < maxY; ++y) - { - u32 *dstPixel = reinterpret_cast(row + minX * buffer->BytesPerPixel); - - for (s32 x = minX; x < maxX; ++x) - { - float a = color.a; - float srcR = color.r * 255.0; - float srcG = color.g * 255.0; - float srcB = color.b * 255.0; - - float dstR = ((*dstPixel >> 16) & 0xff); - float dstG = ((*dstPixel >> 8) & 0xff); - float dstB = ((*dstPixel >> 0) & 0xff); - - dstR = (1 - a) * dstR + a * srcR; - dstG = (1 - a) * dstG + a * srcG; - dstB = (1 - a) * dstB + a * srcB; - - *dstPixel = (RoundFloatToInt(dstR) << 16 | - RoundFloatToInt(dstG) << 8 | - RoundFloatToInt(dstB) << 0); - - ++dstPixel; - } - - row += buffer->pitch; - } -} - void render_dooms(ControllerContext &ctx) { - #if 0 - OffscreenBuffer buffer = - { - DoomScreenWidth, - DoomScreenHeight, - ctx.pixelBuffer.data(), - DoomScreenWidth * DoomBytesPerPixel, - DoomBytesPerPixel - }; - - DrawRectangle(&buffer, 0, 0, 10, 10, { 1, 0, 0, 1 }); - DrawRectangle(&buffer, 0, buffer.height-10, 10, buffer.height, { 0, 1, 0, 1 }); - DrawRectangle(&buffer, buffer.width-10, buffer.height-10, buffer.width, buffer.height, { 0, 0, 1, 1 }); - DrawRectangle(&buffer, buffer.width-10, 0, buffer.width, 10, { 0.840, 0.0168, 0.717, 1 }); - #endif - SDL_Rect destRect = { ctx.offsetX, ctx.offsetY, DoomScreenWidth, DoomScreenHeight }; destRect.w *= ctx.scaleFactor; @@ -798,7 +699,6 @@ int main(int argc, char *argv[]) ctx.sub = make_ctrl_sub(DoomUrlIpc); ctx.window = window; ctx.renderer = renderer; - ctx.pixelBuffer.fill(0u); log_add_callback(log_to_imgui, &ctx, LOG_DEBUG);