split out helpers and datatypes into other files
This commit is contained in:
@@ -9,6 +9,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "datatypes.cpp"
|
||||
#include "helpers.cpp"
|
||||
|
||||
// this should be handled elsewhere, eventually, maybe, probably
|
||||
static void on_key(void *user, uint32_t keycode, uint32_t state) {
|
||||
(void)user;
|
||||
@@ -18,77 +21,18 @@ static void on_key(void *user, uint32_t keycode, uint32_t state) {
|
||||
}
|
||||
|
||||
namespace UwU {
|
||||
|
||||
// Color datatype returns individual channel values from rgba hexcode
|
||||
struct Color {
|
||||
Color(uint32_t hex) {
|
||||
this->a = hex & 0x000000ff;
|
||||
this->b = (hex & 0x0000ff00) >> 8;
|
||||
this->g = (hex & 0x00ff0000) >> 16;
|
||||
this->r = (hex & 0xff000000) >> 24;
|
||||
}
|
||||
Color() {}
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
uint8_t a;
|
||||
};
|
||||
|
||||
// Coord2 datatype for xy coordinates, from top-left of surface
|
||||
struct Coord2 {
|
||||
Coord2(uint32_t x, uint32_t y) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
Coord2() {}
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
};
|
||||
|
||||
// trapezoid datatype (horizontally constrained)
|
||||
struct Trapezoid {
|
||||
Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1) {
|
||||
this->y0 = y0;
|
||||
this->y1 = y1;
|
||||
this->xl0 = xl0;
|
||||
this->xl1 = xl1;
|
||||
this->xr0 = xr0;
|
||||
this->xr1 = xr1;
|
||||
}
|
||||
Trapezoid() {}
|
||||
uint16_t y0;
|
||||
uint16_t y1;
|
||||
uint16_t xl0;
|
||||
uint16_t xl1;
|
||||
uint16_t xr0;
|
||||
uint16_t xr1;
|
||||
};
|
||||
|
||||
class Draw {
|
||||
private:
|
||||
|
||||
uint8_t lerp8(uint8_t a, uint8_t b, uint8_t t) {
|
||||
uint16_t product = (a * (255 - t)) + (b * t);
|
||||
return product / 255;
|
||||
}
|
||||
|
||||
Color lerpColor(Color a, Color b, uint8_t t) {
|
||||
Color mixed = Color();
|
||||
mixed.r = this->lerp8(a.r, b.r, t);
|
||||
mixed.g = this->lerp8(a.g, b.g, t);
|
||||
mixed.b = this->lerp8(a.b, b.b, t);
|
||||
mixed.a = this->lerp8(a.a, b.a, t); // maybe the alpha channel should be handled differently?
|
||||
return mixed;
|
||||
}
|
||||
|
||||
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);
|
||||
b = Helpers::lerp8(b0, b, a);
|
||||
g = Helpers::lerp8(g0, g, a);
|
||||
r = Helpers::lerp8(r0, r, a);
|
||||
this->pixels[i + 0] = b; // B
|
||||
this->pixels[i + 1] = g; // G
|
||||
this->pixels[i + 2] = r; // R
|
||||
@@ -292,10 +236,10 @@ namespace UwU {
|
||||
Color Rmix = Color();
|
||||
Color Xmix = Color();
|
||||
for (int32_t y = y0; y <= y1; y++) {
|
||||
Lmix = this->lerpColor(colorUL, colorLL, (uint8_t)yGrad[y - y0]);
|
||||
Rmix = this->lerpColor(colorUR, colorLR, (uint8_t)yGrad[y - y0]);
|
||||
Lmix = Helpers::lerpColor(colorUL, colorLL, (uint8_t)yGrad[y - y0]);
|
||||
Rmix = Helpers::lerpColor(colorUR, colorLR, (uint8_t)yGrad[y - y0]);
|
||||
for (int32_t x = x0; x <= x1; x++) {
|
||||
Xmix = this->lerpColor(Lmix, Rmix, (uint8_t)xGrad[x - x0]);
|
||||
Xmix = Helpers::lerpColor(Lmix, Rmix, (uint8_t)xGrad[x - x0]);
|
||||
putPixel(x, y, Xmix.r, Xmix.g, Xmix.b, Xmix.a);
|
||||
}
|
||||
}
|
||||
@@ -344,36 +288,37 @@ namespace UwU {
|
||||
}
|
||||
|
||||
// Draws a horizontally bounded trapezoid
|
||||
// TODO: FIX
|
||||
void trapezoid (Trapezoid coords, Color color) {
|
||||
UwU::Trapezoid saneCoords = coords;
|
||||
if (coords.y0 > coords.y1) {
|
||||
saneCoords.y0 = coords.y1;
|
||||
saneCoords.y1 = coords.y0;
|
||||
saneCoords.xl0 = coords.xl1;
|
||||
saneCoords.xl1 = coords.xl0;
|
||||
saneCoords.xr0 = coords.xr1;
|
||||
saneCoords.xr1 = coords.xr0;
|
||||
}
|
||||
uint16_t tempVal;
|
||||
bool hflip0 = false;
|
||||
if (saneCoords.xl0 > saneCoords.xr0) {
|
||||
tempVal = saneCoords.xl0;
|
||||
saneCoords.xl0 = saneCoords.xr0;
|
||||
saneCoords.xr0 = tempVal;
|
||||
hflip0 = true;
|
||||
}
|
||||
if (saneCoords.xl1 > saneCoords.xr1) {
|
||||
if (!hflip0) {
|
||||
// invalid geometry; emit console warning and don't attempt to draw
|
||||
// possible options are: don't draw, lazy draw (incorrect geometry) -
|
||||
// or split into two (way more work)
|
||||
return;
|
||||
}
|
||||
tempVal = saneCoords.xl1;
|
||||
saneCoords.xl1 = saneCoords.xr1;
|
||||
saneCoords.xr1 = tempVal;
|
||||
}
|
||||
this->pTrapezoid(saneCoords, color);
|
||||
// UwU::Trapezoid saneCoords = coords;
|
||||
// if (coords.y0 > coords.y1) {
|
||||
// saneCoords.y0 = coords.y1;
|
||||
// saneCoords.y1 = coords.y0;
|
||||
// saneCoords.xl0 = coords.xl1;
|
||||
// saneCoords.xl1 = coords.xl0;
|
||||
// saneCoords.xr0 = coords.xr1;
|
||||
// saneCoords.xr1 = coords.xr0;
|
||||
// }
|
||||
// uint16_t tempVal;
|
||||
// bool hflip0 = false;
|
||||
// if (saneCoords.xl0 > saneCoords.xr0) {
|
||||
// tempVal = saneCoords.xl0;
|
||||
// saneCoords.xl0 = saneCoords.xr0;
|
||||
// saneCoords.xr0 = tempVal;
|
||||
// hflip0 = true;
|
||||
// }
|
||||
// if (saneCoords.xl1 > saneCoords.xr1) {
|
||||
// if (!hflip0) {
|
||||
// // invalid geometry; emit console warning and don't attempt to draw
|
||||
// // possible options are: don't draw, lazy draw (incorrect geometry) -
|
||||
// // or split into two (way more work)
|
||||
// return;
|
||||
// }
|
||||
// tempVal = saneCoords.xl1;
|
||||
// saneCoords.xl1 = saneCoords.xr1;
|
||||
// saneCoords.xr1 = tempVal;
|
||||
// }
|
||||
this->pTrapezoid(coords, color);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -396,7 +341,7 @@ int main(void) {
|
||||
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia);
|
||||
draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
|
||||
|
||||
draw.trapezoid(UwU::Trapezoid(500, 400, 360, 360, 600, 401), green);
|
||||
// draw.trapezoid(UwU::Trapezoid(400, 500, 360, 360, 400, 601), green);
|
||||
|
||||
|
||||
draw.point(UwU::Coord2(387, 43), green);
|
||||
|
||||
Reference in New Issue
Block a user