Files
UwUGL/draw.cpp
T

153 lines
4.3 KiB
C++
Raw Normal View History

2026-07-07 03:49:24 -07:00
#pragma once
2026-07-07 03:25:10 -07:00
#include "waymini.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "datatypes.cpp"
#include "helpers.cpp"
#include "triangle.cpp"
#include "buffer.cpp"
#include "surface.cpp"
#include "trapezoid.cpp"
#include "line.cpp"
#include "rectangle.cpp"
2026-07-07 07:25:49 -07:00
#include "point.cpp"
2026-07-07 03:25:10 -07:00
namespace UwU {
class Draw {
public:
Draw(uint16_t width, uint16_t height, waymini_key_cb onKey)
: surface(width, height, onKey) {}
class Surface surface;
2026-07-07 03:25:10 -07:00
bool shouldClose() {
return surface.shouldClose();
}
void updateSize() {
surface.updateSize();
return;
}
uint16_t getWidth() {
return surface.getWidth();
}
uint16_t getHeight() {
return surface.getHeight();
}
uint16_t getStride() {
return surface.getStride();
}
void present() {
surface.present();
return;
}
void destroy() {
surface.destroy();
return;
}
// Draw a rectangle (duh)
void rectangle(Coord2 point0, Coord2 point1, Color color) {
Rectangle rectangle = Rectangle(point0.x, point0.y, point1.x, point1.y);
rectangle.draw(surface.buffer, color);
2026-07-07 03:25:10 -07:00
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
Helpers::bresenham(0, 0, w - 1, 255, xGrad);
Helpers::bresenham(0, 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 = 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 = Helpers::lerpColor(Lmix, Rmix, (uint8_t)xGrad[x - x0]);
// putPixel(x, y, Xmix.r, Xmix.g, Xmix.b, Xmix.a);
surface.buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), Xmix);
}
}
return;
}
// Fill a simple gradient. Each row is stride bytes.
void rainbow() {
for (uint32_t y = 0; y < getHeight(); y++) {
for (uint32_t x = 0; x < getWidth(); x++) {
surface.buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), Color(
(uint8_t)(y * 255 / getHeight()),
0x00,
(uint8_t)(x * 255 / getWidth()),
0xff));
// size_t i = (size_t)y * getStride() + (size_t)x * 4;
// pixels[i + 0] = (uint8_t)(y * 255 / getHeight()); // B
// pixels[i + 1] = 0x00; // G
// pixels[i + 2] = (uint8_t)(x * 255 / getWidth()); // R
// pixels[i + 3] = 0xFF; // A
}
}
}
// Draw a single point
2026-07-07 07:25:49 -07:00
void point (Point point, Color color) {
point.draw(surface.buffer, color);
2026-07-07 03:25:10 -07:00
}
void line (Line line, Color color) {
line.draw(surface.buffer, color);
return;
}
void trapezoid (Trapezoid coords, Color color) {
coords.draw(surface.buffer, color);
2026-07-07 03:25:10 -07:00
return;
}
// Draw a triangle
void triangle (uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, Color color) {
Triangle triangle = Triangle(x0, y0, x1, y1, x2, y2);
triangle.draw(surface.buffer, color);
return;
}
};
}