added alpha blending and putPixel helper function

This commit is contained in:
2026-07-04 05:02:37 -07:00
parent 1011f03467
commit a89a4dacb5
2 changed files with 27 additions and 24 deletions
BIN
View File
Binary file not shown.
+27 -24
View File
@@ -46,7 +46,26 @@ namespace UwU {
class Draw {
private:
// plot lines if m<= 1
uint8_t lerp8(uint8_t a, uint8_t b, uint8_t t) {
uint16_t product = (a * (255 - t)) + (b * t);
return product / 255;
}
void putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0xff) {
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
uint8_t b0 = this->pixels[i + 0];
uint8_t g0 = this->pixels[i + 1];
uint8_t r0 = this->pixels[i + 2];
b = lerp8(b0, b, a);
g = lerp8(g0, g, a);
r = lerp8(r0, r, a);
this->pixels[i + 0] = b; // B
this->pixels[i + 1] = g; // G
this->pixels[i + 2] = r; // R
this->pixels[i + 3] = 0xff; // A
}
// plot lines if m <= 1
void lineLow(int32_t x0, int32_t y0, int32_t x1, int32_t y1, Color color) {
int32_t dx = x1 - x0;
int32_t dy = y1 - y0;
@@ -58,11 +77,7 @@ namespace UwU {
int32_t d = (2 * dy) - dx;
int32_t y = y0;
for (int32_t x = x0; x < x1; x++) {
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
this->pixels[i + 0] = color.b; // B
this->pixels[i + 1] = color.g; // G
this->pixels[i + 2] = color.r; // R
this->pixels[i + 3] = 0xFF; // A
putPixel(x, y, color.r, color.g, color.b, color.a);
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
@@ -73,7 +88,7 @@ namespace UwU {
return;
}
// plot lines if m<= 1
// plot lines if m >1
void lineHigh(int32_t x0, int32_t y0, int32_t x1, int32_t y1, Color color) {
int32_t dx = x1 - x0;
int32_t dy = y1 - y0;
@@ -85,11 +100,7 @@ namespace UwU {
int32_t d = (2 * dx) - dy;
int32_t x = x0;
for (int32_t y = y0; y < y1; y++) {
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
this->pixels[i + 0] = color.b; // B
this->pixels[i + 1] = color.g; // G
this->pixels[i + 2] = color.r; // R
this->pixels[i + 3] = 0xFF; // A
putPixel(x, y, color.r, color.g, color.b, color.a);
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
@@ -128,11 +139,7 @@ namespace UwU {
if (point0.x > this->width || point0.y > this->height || point1.x > this->width || point1.y > this->height) {return;};
for (uint32_t y = point0.y; y < point1.y; y++) {
for (uint32_t x = point0.x; x < point1.x; x++) {
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
this->pixels[i + 0] = color.b; // B
this->pixels[i + 1] = color.g; // G
this->pixels[i + 2] = color.r; // R
this->pixels[i + 3] = 0xFF; // A
putPixel(x, y, color.r, color.g, color.b, color.a);
}
}
return;
@@ -154,11 +161,7 @@ namespace UwU {
// Draw a single point
void point (Coord2 point, Color color) {
if (point.x > this->width || point.y > this->height) {return;};
size_t i = (size_t)point.y * waymini_get_stride(this->wm) + (size_t)point.x * 4;
this->pixels[i + 0] = color.b; // B
this->pixels[i + 1] = color.g; // G
this->pixels[i + 2] = color.r; // R
this->pixels[i + 3] = 0xFF; // A
putPixel(point.x, point.y, color.r, color.g, color.b, color.a);
}
// Uses Bresenham's line algorithm to draw line of any slope
@@ -187,8 +190,8 @@ namespace UwU {
int main(void) {
uint32_t w = 800, h = 480;
UwU::Color fuchsia = UwU::Color(0xb00b69ff);
UwU::Color green = UwU::Color(0x00ff00ff);
UwU::Color fuchsia = UwU::Color(0xb00b6980);
UwU::Color green = UwU::Color(0x00ff0060);
printf("r: %u, g:%u, b:%u, a:%u\n", fuchsia.r, fuchsia.g, fuchsia.b, fuchsia.a);
// struct waymini *wm = waymini_create(w, h, on_key, NULL);