added alpha blending and putPixel helper function
This commit is contained in:
@@ -46,7 +46,26 @@ namespace UwU {
|
|||||||
class Draw {
|
class Draw {
|
||||||
private:
|
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) {
|
void lineLow(int32_t x0, int32_t y0, int32_t x1, int32_t y1, Color color) {
|
||||||
int32_t dx = x1 - x0;
|
int32_t dx = x1 - x0;
|
||||||
int32_t dy = y1 - y0;
|
int32_t dy = y1 - y0;
|
||||||
@@ -58,11 +77,7 @@ namespace UwU {
|
|||||||
int32_t d = (2 * dy) - dx;
|
int32_t d = (2 * dy) - dx;
|
||||||
int32_t y = y0;
|
int32_t y = y0;
|
||||||
for (int32_t x = x0; x < x1; x++) {
|
for (int32_t x = x0; x < x1; x++) {
|
||||||
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
|
putPixel(x, y, color.r, color.g, color.b, color.a);
|
||||||
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
|
|
||||||
if (d > 0) {
|
if (d > 0) {
|
||||||
y += yi;
|
y += yi;
|
||||||
d += (2 * (dy - dx));
|
d += (2 * (dy - dx));
|
||||||
@@ -73,7 +88,7 @@ namespace UwU {
|
|||||||
return;
|
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) {
|
void lineHigh(int32_t x0, int32_t y0, int32_t x1, int32_t y1, Color color) {
|
||||||
int32_t dx = x1 - x0;
|
int32_t dx = x1 - x0;
|
||||||
int32_t dy = y1 - y0;
|
int32_t dy = y1 - y0;
|
||||||
@@ -85,11 +100,7 @@ namespace UwU {
|
|||||||
int32_t d = (2 * dx) - dy;
|
int32_t d = (2 * dx) - dy;
|
||||||
int32_t x = x0;
|
int32_t x = x0;
|
||||||
for (int32_t y = y0; y < y1; y++) {
|
for (int32_t y = y0; y < y1; y++) {
|
||||||
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
|
putPixel(x, y, color.r, color.g, color.b, color.a);
|
||||||
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
|
|
||||||
if (d > 0) {
|
if (d > 0) {
|
||||||
x += xi;
|
x += xi;
|
||||||
d += (2 * (dx - dy));
|
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;};
|
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 y = point0.y; y < point1.y; y++) {
|
||||||
for (uint32_t x = point0.x; x < point1.x; x++) {
|
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;
|
putPixel(x, y, color.r, color.g, color.b, color.a);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -154,11 +161,7 @@ namespace UwU {
|
|||||||
// Draw a single point
|
// Draw a single point
|
||||||
void point (Coord2 point, Color color) {
|
void point (Coord2 point, Color color) {
|
||||||
if (point.x > this->width || point.y > this->height) {return;};
|
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;
|
putPixel(point.x, point.y, color.r, color.g, color.b, color.a);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uses Bresenham's line algorithm to draw line of any slope
|
// Uses Bresenham's line algorithm to draw line of any slope
|
||||||
@@ -187,8 +190,8 @@ namespace UwU {
|
|||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
uint32_t w = 800, h = 480;
|
uint32_t w = 800, h = 480;
|
||||||
UwU::Color fuchsia = UwU::Color(0xb00b69ff);
|
UwU::Color fuchsia = UwU::Color(0xb00b6980);
|
||||||
UwU::Color green = UwU::Color(0x00ff00ff);
|
UwU::Color green = UwU::Color(0x00ff0060);
|
||||||
printf("r: %u, g:%u, b:%u, a:%u\n", fuchsia.r, fuchsia.g, fuchsia.b, fuchsia.a);
|
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);
|
// struct waymini *wm = waymini_create(w, h, on_key, NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user