Luna/libui/src/Canvas.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

2023-08-14 10:34:27 +00:00
/**
* @file Canvas.cpp
* @author apio (cloudapio.eu)
* @brief Drawable surfaces.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
2023-08-03 10:19:45 +00:00
#include <ui/Canvas.h>
namespace ui
{
Canvas Canvas::create(u8* ptr, int width, int height)
{
return Canvas { .width = width, .height = height, .stride = width, .ptr = ptr };
}
2023-08-03 15:38:49 +00:00
Canvas Canvas::subcanvas(Rect rect)
2023-08-03 10:19:45 +00:00
{
2023-08-03 16:32:58 +00:00
if (rect.pos.x < 0) rect.pos.x = 0;
if (rect.pos.y < 0) rect.pos.y = 0;
2023-08-03 15:38:49 +00:00
if (rect.pos.x + rect.width > width) rect.width = width - rect.pos.x;
if (rect.pos.y + rect.height > height) rect.height = height - rect.pos.y;
2023-08-03 10:19:45 +00:00
2023-08-03 15:38:49 +00:00
u8* p = ptr + rect.pos.x * sizeof(Color) + (rect.pos.y * sizeof(Color) * stride);
2023-08-03 10:19:45 +00:00
2023-08-03 15:38:49 +00:00
return Canvas { .width = rect.width, .height = rect.height, .stride = stride, .ptr = p };
2023-08-03 10:19:45 +00:00
}
void Canvas::fill(Color color)
{
u8* p = ptr;
for (int i = 0; i < height; i++)
{
u32* colorp = (u32*)p;
for (int j = 0; j < width; j++)
{
*colorp = color.raw;
colorp++;
}
p += stride * sizeof(Color);
}
}
void Canvas::fill(u32* pixels, int _stride)
{
u8* p = ptr;
for (int i = 0; i < height; i++)
{
u32* colorp = (u32*)p;
for (int j = 0; j < width; j++)
{
u32 pix = pixels[j];
if (Color::from_u32(pix).alpha() == 0xff) *colorp = pix;
colorp++;
}
pixels += _stride;
p += stride * sizeof(Color);
}
}
2023-08-03 10:19:45 +00:00
}