Draw bitmaps clip are clipped to screen space

This commit is contained in:
Doyle Thai 2017-05-14 19:18:41 +10:00
parent 2bb16ebe68
commit d384a303d3
2 changed files with 60 additions and 9 deletions

View File

@ -529,19 +529,30 @@ FILE_SCOPE void BitmapFontCreate(const PlatformAPI api,
FILE_SCOPE void DrawBitmap(PlatformRenderBuffer *const renderBuffer, FILE_SCOPE void DrawBitmap(PlatformRenderBuffer *const renderBuffer,
DRBitmap *const bitmap, i32 x, i32 y) DRBitmap *const bitmap, i32 x, i32 y)
{ {
if (!bitmap || !bitmap->memory) return; if (!bitmap || !bitmap->memory || !renderBuffer) return;
DqnRect viewport = DqnRect_4i(0, 0, renderBuffer->width, renderBuffer->height);
DqnRect bitmapRect = DqnRect_4i(x, y, x + bitmap->dim.w, y + bitmap->dim.h);
bitmapRect = DqnRect_ClipRect(bitmapRect, viewport);
if (bitmapRect.max.x < 0 || bitmapRect.max.y < 0) return;
i32 startX = (x > 0) ? 0 : DQN_ABS(x);
i32 startY = (y > 0) ? 0 : DQN_ABS(y);
i32 endX, endY;
DqnRect_GetSize2i(bitmapRect, &endX, &endY);
const i32 pitch = bitmap->dim.w * bitmap->bytesPerPixel; const i32 pitch = bitmap->dim.w * bitmap->bytesPerPixel;
for (i32 bitmapY = 0; bitmapY < bitmap->dim.w; bitmapY++) for (i32 bitmapY = startY; bitmapY < endY; bitmapY++)
{ {
u8 *const srcRow = bitmap->memory + (bitmapY * pitch); u8 *const srcRow = bitmap->memory + (bitmapY * pitch);
i32 bufferY = y + bitmapY; i32 bufferY = (i32)bitmapRect.min.y + bitmapY;
for (i32 bitmapX = 0; bitmapX < bitmap->dim.w; bitmapX++) for (i32 bitmapX = startX; bitmapX < endX; bitmapX++)
{ {
u32 *pixelPtr = (u32 *)srcRow; u32 *pixelPtr = (u32 *)srcRow;
u32 pixel = pixelPtr[bitmapX]; u32 pixel = pixelPtr[bitmapX];
i32 bufferX = x + bitmapX; i32 bufferX = (i32)bitmapRect.min.x + bitmapX;
DqnV4 color = {}; DqnV4 color = {};
color.a = (f32)(pixel >> 24); color.a = (f32)(pixel >> 24);
@ -658,7 +669,6 @@ void DebugUpdate(PlatformRenderBuffer *const renderBuffer,
debug->totalSetPixels += debug->setPixelsPerFrame; debug->totalSetPixels += debug->setPixelsPerFrame;
debug->totalSetPixels = DQN_MAX(0, debug->totalSetPixels); debug->totalSetPixels = DQN_MAX(0, debug->totalSetPixels);
debug->setPixelsPerFrame = 0;
// totalSetPixels // totalSetPixels
{ {
@ -683,6 +693,8 @@ void DebugUpdate(PlatformRenderBuffer *const renderBuffer,
DebugDisplayMemBuffer(renderBuffer, "TransBuffer", DebugDisplayMemBuffer(renderBuffer, "TransBuffer",
&memory->transientBuffer, &debugP, font); &memory->transientBuffer, &debugP, font);
} }
debug->setPixelsPerFrame = 0;
} }
extern "C" void DR_Update(PlatformRenderBuffer *const renderBuffer, extern "C" void DR_Update(PlatformRenderBuffer *const renderBuffer,
@ -745,8 +757,7 @@ extern "C" void DR_Update(PlatformRenderBuffer *const renderBuffer,
DqnV2 fontP = DqnV2_2i(200, 180); DqnV2 fontP = DqnV2_2i(200, 180);
DrawText(renderBuffer, state->font, fontP, "hello world!"); DrawText(renderBuffer, state->font, fontP, "hello world!");
DrawBitmap(renderBuffer, &state->bitmap, 700, 400); DrawBitmap(renderBuffer, &state->bitmap, 700, 400);
DebugUpdate(renderBuffer, input, memory, state->font); DebugUpdate(renderBuffer, input, memory, state->font);
} }

View File

@ -589,10 +589,14 @@ typedef struct DqnRect
DqnV2 max; DqnV2 max;
} DqnRect; } DqnRect;
DQN_FILE_SCOPE DqnRect DqnRect_4f (f32 minX, f32 minY, f32 maxX, f32 maxY);
DQN_FILE_SCOPE DqnRect DqnRect_4i (i32 minX, i32 minY, i32 maxX, i32 maxY);
DQN_FILE_SCOPE DqnRect DqnRect_Init (DqnV2 origin, DqnV2 size); DQN_FILE_SCOPE DqnRect DqnRect_Init (DqnV2 origin, DqnV2 size);
DQN_FILE_SCOPE void DqnRect_GetSize2f(DqnRect rect, f32 *width, f32 *height); DQN_FILE_SCOPE void DqnRect_GetSize2f(DqnRect rect, f32 *width, f32 *height);
DQN_FILE_SCOPE DqnV2 DqnRect_GetSizev2(DqnRect rect); DQN_FILE_SCOPE void DqnRect_GetSize2i(DqnRect rect, i32 *width, i32 *height);
DQN_FILE_SCOPE DqnV2 DqnRect_GetSizeV2(DqnRect rect);
DQN_FILE_SCOPE DqnV2 DqnRect_GetCenter(DqnRect rect); DQN_FILE_SCOPE DqnV2 DqnRect_GetCenter(DqnRect rect);
DQN_FILE_SCOPE DqnRect DqnRect_ClipRect (DqnRect rect, DqnRect clip);
DQN_FILE_SCOPE DqnRect DqnRect_Move (DqnRect rect, DqnV2 shift); DQN_FILE_SCOPE DqnRect DqnRect_Move (DqnRect rect, DqnV2 shift);
DQN_FILE_SCOPE bool DqnRect_ContainsP(DqnRect rect, DqnV2 p); DQN_FILE_SCOPE bool DqnRect_ContainsP(DqnRect rect, DqnV2 p);
@ -2229,6 +2233,24 @@ DQN_FILE_SCOPE DqnV4 DqnMat4_MulV4(DqnMat4 a, DqnV4 b)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Rect // Rect
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
DQN_FILE_SCOPE DqnRect DqnRect_4f(f32 minX, f32 minY, f32 maxX, f32 maxY)
{
DqnRect result = {};
result.min = DqnV2_2f(minX, minY);
result.max = DqnV2_2f(maxX, maxY);
return result;
}
DQN_FILE_SCOPE DqnRect DqnRect_4i(i32 minX, i32 minY, i32 maxX, i32 maxY)
{
DqnRect result = {};
result.min = DqnV2_2i(minX, minY);
result.max = DqnV2_2i(maxX, maxY);
return result;
}
DQN_FILE_SCOPE DqnRect DqnRect_Init(DqnV2 origin, DqnV2 size) DQN_FILE_SCOPE DqnRect DqnRect_Init(DqnV2 origin, DqnV2 size)
{ {
DqnRect result = {}; DqnRect result = {};
@ -2244,6 +2266,12 @@ DQN_FILE_SCOPE void DqnRect_GetSize2f(DqnRect rect, f32 *width, f32 *height)
*height = DQN_ABS(rect.max.y - rect.min.y); *height = DQN_ABS(rect.max.y - rect.min.y);
} }
DQN_FILE_SCOPE void DqnRect_GetSize2i(DqnRect rect, i32 *width, i32 *height)
{
*width = (i32)DQN_ABS(rect.max.x - rect.min.x);
*height = (i32)DQN_ABS(rect.max.y - rect.min.y);
}
DQN_FILE_SCOPE DqnV2 DqnRect_GetSizeV2(DqnRect rect) DQN_FILE_SCOPE DqnV2 DqnRect_GetSizeV2(DqnRect rect)
{ {
f32 width = DQN_ABS(rect.max.x - rect.min.x); f32 width = DQN_ABS(rect.max.x - rect.min.x);
@ -2261,6 +2289,18 @@ DQN_FILE_SCOPE DqnV2 DqnRect_GetCentre(DqnRect rect)
return result; return result;
} }
DQN_FILE_SCOPE DqnRect DqnRect_ClipRect(DqnRect rect, DqnRect clip)
{
DqnRect result = {};
DqnV2 clipSize = DqnRect_GetSizeV2(clip);
result.max.x = DQN_MIN(rect.max.x, clipSize.w);
result.max.y = DQN_MIN(rect.max.y, clipSize.h);
result.min.x = DQN_MAX(clip.min.x, rect.min.x);
result.min.y = DQN_MAX(clip.min.y, rect.min.y);
return result;
}
DQN_FILE_SCOPE DqnRect DqnRect_Move(DqnRect rect, DqnV2 shift) DQN_FILE_SCOPE DqnRect DqnRect_Move(DqnRect rect, DqnV2 shift)
{ {
DqnRect result = {0}; DqnRect result = {0};