cleanup and revive the debug drawing code
This commit is contained in:
parent
abb563440d
commit
407dded717
2 changed files with 126 additions and 123 deletions
103
src/debug_draw.cc
Normal file
103
src/debug_draw.cc
Normal file
|
@ -0,0 +1,103 @@
|
|||
inline s32 RoundFloatToInt(float value)
|
||||
{
|
||||
return static_cast<s32>(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<u32 *>(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<u32 *>(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;
|
||||
}
|
||||
}
|
|
@ -89,7 +89,6 @@ struct ControllerContext
|
|||
bool quit = false;
|
||||
ExampleAppLog appLog;
|
||||
int columns = 4;
|
||||
std::array<u8, DoomScreenWidth * DoomScreenHeight * DoomBytesPerPixel> 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<s32>(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<u32 *>(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<u32 *>(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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue