Files
UwUGL/test.cpp
T
2026-07-05 06:41:26 -07:00

387 lines
12 KiB
C++

/* Example program that links against the waymini library.
* No external dependencies beyond waymini itself and the C standard library.
*/
#include "waymini.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// this should be handled elsewhere, eventually, maybe, probably
static void on_key(void *user, uint32_t keycode, uint32_t state) {
(void)user;
const char *label = state ? "down" : "up ";
printf("key %s: %u\n", label, keycode);
fflush(stdout);
}
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);
this->pixels[i + 0] = b; // B
this->pixels[i + 1] = g; // G
this->pixels[i + 2] = r; // R
this->pixels[i + 3] = 0xff; // A
}
void bresenhamLow(int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
int16_t dx = int16_t(x1);
int16_t dy = y1 - y0;
int16_t yi = 1;
if (dy < 0) {
yi = -1;
dy = -dy;
}
int16_t d = (2 * dy) - dx;
int16_t y = y0;
for (uint16_t x = 0; x <= x1; x++) {
LUT[x] = y;
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
}
return;
}
void bresenhamHigh(uint16_t x0, int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
int16_t dx = (int16_t)x1 - (int16_t)x0;
int16_t dy = y1 - y0;
int16_t xi = 1;
if (dx < 0) {
xi = -1;
dx = -dx;
}
int16_t d = (2 * dx) - dy;
int16_t x = (int16_t)x0;
for (int16_t y = y0; y <= y1; y++) {
LUT[x] = y;
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
}
return;
}
void bresenham(int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
int16_t dy = y1 - y0;
if (dy == 0) {
// catch hline condition
for (uint16_t i = 0; i <= x1; i++) {
LUT[i] = y0;
}
return;
}
if (abs(y1 - y0) <= x1) {
bresenhamLow(y0, x1, y1, LUT);
} else {
if (y0 > y1) {
bresenhamHigh(x1, y1, 0, y0, LUT);
} else {
bresenhamHigh(0, y0, x1, y1, LUT);
}
}
return;
}
// 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;
int32_t yi = 1;
if (dy < 0) {
yi = -1;
dy = -dy;
}
int32_t d = (2 * dy) - dx;
int32_t y = y0;
for (int32_t x = x0; x < x1; x++) {
putPixel(x, y, color.r, color.g, color.b, color.a);
if (d > 0) {
y += yi;
d += (2 * (dy - dx));
} else {
d += (2 * dy);
}
}
return;
}
// 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;
int32_t xi = 1;
if (dx < 0) {
xi = -1;
dx = -dx;
}
int32_t d = (2 * dx) - dy;
int32_t x = x0;
for (int32_t y = y0; y < y1; y++) {
putPixel(x, y, color.r, color.g, color.b, color.a);
if (d > 0) {
x += xi;
d += (2 * (dx - dy));
} else {
d += (2 * dx);
}
}
return;
}
public:
struct waymini* wm;
uint32_t width;
uint32_t height;
uint32_t stride;
uint8_t *pixels;
Draw(uint32_t width, uint32_t height, waymini_key_cb on_key) {
this->wm = waymini_create(width, height, on_key, NULL);
if (!this->wm) exit(1);
this->width = waymini_get_width(this->wm);
this->height = waymini_get_height(this->wm);
this->stride = waymini_get_stride(this->wm);
printf("surface: %ux%u stride=%u\n", this->width, this->height, this->stride);
this->pixels = (uint8_t *)waymini_get_pixels(this->wm);
if (!this->pixels) {
waymini_destroy(this->wm);
exit(1);
}
}
// Draw a rectangle (duh)
void rectangle(Coord2 point0, Coord2 point1, Color color) {
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++) {
putPixel(x, y, color.r, color.g, color.b, color.a);
}
}
return;
}
// Draws a rectangle with a 4-point color gradient
void rectangleGradient(Coord2 point0, Coord2 point1, Color colorUL, Color colorUR, Color colorLL, Color colorLR) {
// convert to int32_t and ensure x1>x0 && y1>y0
int32_t x0, y0, x1, y1;
if (point0.x > point1.x) {
x0 = (int32_t)point1.x;
x1 = (int32_t)point0.x;
} else {
x0 = (int32_t)point0.x;
x1 = (int32_t)point1.x;
}
if (point0.y > point1.y) {
y0 = (int32_t)point1.y;
y1 = (int32_t)point0.y;
} else {
y0 = (int32_t)point0.y;
y1 = (int32_t)point1.y;
}
// setup gradient coefficient arrays
int32_t w = x1 - x0 + 1;
int32_t h = y1 - y0 + 1;
int16_t xGrad[w];
int16_t yGrad[h];
// calculate gradient coefficients
this->bresenham(0, w - 1, 255, xGrad);
this->bresenham(0, h - 1, 255, yGrad);
// for each point calculate, alpha blend and print color
Color Lmix = Color();
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]);
for (int32_t x = x0; x <= x1; x++) {
Xmix = this->lerpColor(Lmix, Rmix, (uint8_t)xGrad[x - x0]);
putPixel(x, y, Xmix.r, Xmix.g, Xmix.b, Xmix.a);
}
}
return;
}
// Fill a simple gradient. Each row is stride bytes.
void rainbow() {
for (uint32_t y = 0; y < this->height; y++) {
for (uint32_t x = 0; x < this->width; x++) {
size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4;
this->pixels[i + 0] = (uint8_t)(y * 255 / this->height); // B
this->pixels[i + 1] = 0x00; // G
this->pixels[i + 2] = (uint8_t)(x * 255 / this->width); // R
this->pixels[i + 3] = 0xFF; // A
}
}
}
// Draw a single point
void point (Coord2 point, Color color) {
if (point.x > this->width || point.y > this->height) {return;};
putPixel(point.x, point.y, color.r, color.g, color.b, color.a);
}
// Uses Bresenham's line algorithm to draw line of any slope
void line (Coord2 point0, Coord2 point1, Color color) {
if (point0.x > this->width || point0.y > this->height || point1.x > this->width || point1.y > this->height) {return;};
int32_t x0 = (int32_t)point0.x;
int32_t y0 = (int32_t)point0.y;
int32_t x1 = (int32_t)point1.x;
int32_t y1 = (int32_t)point1.y;
if (abs(y1 - y0) < abs(x1 - x0)) {
if (x0 > x1) {
this->lineLow(x1, y1, x0, y0, color);
} else {
this->lineLow(x0, y0, x1, y1, color);
}
} else {
if (y0 > y1) {
this->lineHigh(x1, y1, x0, y0, color);
} else {
this->lineHigh(x0, y0, x1, y1, color);
}
}
}
// Draw a horizontally bounded trapezoid
// coords.y1 >= coords.y0
void trapezoid (Trapezoid coords, Color color) {
uint16_t height = 1 + coords.y1 - coords.y0;
int16_t LeftBound[height];
int16_t RightBound[height];
this->bresenham(coords.xl0, coords.y1 - coords.y0, coords.xl1, LeftBound);
this->bresenham(coords.xr0, coords.y1 - coords.y0, coords.xr1, RightBound);
for (uint16_t i = 0; i <= height; i++) {
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
this->putPixel((uint32_t)x, (uint32_t)i + coords.y0, color.r, color.g, color.b, color.a);
}
}
return;
}
};
}
int main(void) {
uint32_t w = 800, h = 480;
UwU::Color fuchsia = UwU::Color(0xb00b6940);
UwU::Color red = UwU::Color(0xff0000ff);
UwU::Color green = UwU::Color(0x00ff00ff);
UwU::Color blue = UwU::Color(0x0000ffff);
UwU::Color alpha = UwU::Color(0x00000000);
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);
UwU::Draw draw = UwU::Draw(w, h, on_key);
draw.rainbow();
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(300, 400, 360, 360, 600, 401), green);
draw.point(UwU::Coord2(387, 43), green);
draw.line(UwU::Coord2(300, 100), UwU::Coord2(250, 469), green);
draw.line(UwU::Coord2(250, 100), UwU::Coord2(300, 469), green);
draw.line(UwU::Coord2(300, 100), UwU::Coord2(469, 250), green);
draw.line(UwU::Coord2(469, 100), UwU::Coord2(300, 250), green);
draw.line(UwU::Coord2(100, 300), UwU::Coord2(250, 469), green);
draw.line(UwU::Coord2(250, 300), UwU::Coord2(100, 469), green);
draw.line(UwU::Coord2(100, 300), UwU::Coord2(469, 250), green);
draw.line(UwU::Coord2(469, 300), UwU::Coord2(100, 250), green);
waymini_present(draw.wm);
while (!waymini_should_close(draw.wm)) {
if (waymini_dispatch(draw.wm) < 0) break;
}
waymini_destroy(draw.wm);
return 0;
}