bit of code cleanup

This commit is contained in:
2026-07-08 14:05:42 -07:00
parent 04afd9eee0
commit b7032c3b8e
8 changed files with 31 additions and 163 deletions
+1 -5
View File
@@ -4,11 +4,7 @@
#include "datatypes.cpp"
#include "helpers.cpp"
#include "shaders/shader.cpp"
// #include "shaders/colorshader.cpp"
// #include "shaders/gradientshader.cpp"
// #include "shaders/hgradientshader.cpp"
// #include "shaders/vgradientshader.cpp"
#include "shader.cpp"
namespace UwU {
// Buffer object - can be a wayland surface or a virtual buffer
BIN
View File
Binary file not shown.
+25 -37
View File
@@ -16,13 +16,7 @@
#include "primatives/rectangle.cpp"
#include "primatives/trapezoid.cpp"
#include "primatives/triangle.cpp"
// #include "draw.cpp"
#include "shaders/shader.cpp"
// #include "shaders/colorshader.cpp"
// #include "shaders/gradientshader.cpp"
// #include "shaders/hgradientshader.cpp"
// #include "shaders/vgradientshader.cpp"
// #include "mapper.cpp"
#include "shader.cpp"
#include <time.h>
@@ -38,13 +32,8 @@ static void onKey(void *user, uint32_t keycode, uint32_t state) {
int main(void) {
uint16_t w = 800, h = 480;
// UwU::GradientShader fuchsia = UwU::GradientShader(UwU::Color(0xb00b69ff), UwU::Color(0xb00b6940));
// UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff));
UwU::Shader fuchsia = UwU::Shader(UwU::Direction::H, 69, 420, UwU::Color(0xb00b69ff), UwU::Color(0x00000000));
UwU::Shader warmup = UwU::Shader(UwU::Color(0xb00b69ff));
// int32_t rectScale = (255 * 65536) / 351;
// rectScale = 41233; //-23806
// UwU::Mapper rectMapper = UwU::Mapper(UwU::Vec2(0, 0), UwU::Vec2(0, 0), UwU::Matrix4(0, 0, 0, 0));
UwU::Color red = UwU::Color(0xff000080);
UwU::Color green = UwU::Color(0x00ff00ff);
UwU::Color blue = UwU::Color(0x0000ff80);
@@ -54,34 +43,33 @@ int main(void) {
// UwU::Buffer buffer = UwU::Buffer(surface.getWidth(), surface.getHeight());
UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420);
// draw.rainbow();
// draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
// Benchmarking
struct timespec start, end;
const int WARMUP = 100;
const int BATCH = 1000;
const int SAMPLES = 10;
const int WARMUP = 100;
const int BATCH = 1000;
const int SAMPLES = 10;
for (int i = 0; i < WARMUP; i++) {
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
}
long times[SAMPLES];
for (int s = 0; s < SAMPLES; s++) {
clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < BATCH; i++) rectangle.draw(surface.buffer, warmup);
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
clock_gettime(CLOCK_MONOTONIC, &end);
times[s] = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
}
long timeAvg = 0;
for (int i = 0; i < SAMPLES; i++) {
timeAvg += times[i];
}
timeAvg /= SAMPLES;
timeAvg /= BATCH;
printf("Elapsed: %ld ns\n", timeAvg);
for (int i = 0; i < WARMUP; i++) {
rectangle.draw(surface.buffer, fuchsia);
}
long times[SAMPLES];
for (int s = 0; s < SAMPLES; s++) {
clock_gettime(CLOCK_MONOTONIC, &start);
for (int i = 0; i < BATCH; i++) {
rectangle.draw(surface.buffer, fuchsia);
}
clock_gettime(CLOCK_MONOTONIC, &end);
times[s] = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
}
long timeAvg = 0;
for (int i = 0; i < SAMPLES; i++) {
timeAvg += times[i];
}
timeAvg /= SAMPLES;
timeAvg /= BATCH;
printf("Elapsed: %ld ns\n", timeAvg);
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
trapezoid.draw(surface.buffer, red);
+2 -19
View File
@@ -5,12 +5,7 @@
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "../shaders/shader.cpp"
// #include "../shaders/colorshader.cpp"
// #include "../shaders/gradientshader.cpp"
// #include "../shaders/hgradientshader.cpp"
// #include "../shaders/vgradientshader.cpp"
// #include "../mapper.cpp"
#include "../shader.cpp"
namespace UwU {
// rectangle
@@ -32,22 +27,10 @@ namespace UwU {
void draw (Buffer buffer, Shader shader) {
if (shader.shaderType == COLOR) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
Color c = shader.c0;
uint8_t r = c.r;
uint8_t g = c.g;
uint8_t b = c.b;
size_t stride = buffer.stride;
size_t i = (size_t)y0 * buffer.stride + (size_t)x0 * 4;
for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) {
// buffer.drawPixel(Coord2(x, y), shader.c0);
buffer.pixels[i + 0] = b;
buffer.pixels[i + 1] = g;
buffer.pixels[i + 2] = r;
buffer.pixels[i + 3] = 255;
i += 4;
buffer.drawPixel(Coord2(x, y), shader.c0);
}
i += stride - ((x1 - x0) * 4);
}
return;
+3 -3
View File
@@ -2,9 +2,9 @@
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "datatypes.cpp"
#include "helpers.cpp"
#include "buffer.cpp"
namespace UwU {
// shader prototype
-29
View File
@@ -1,29 +0,0 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "shader.cpp"
namespace UwU {
class ColorShader {
private:
public:
ColorShader(Color color) {
this->shaderType = COLORSHADER;
this->shaderColor = color;
}
ShaderType shaderType;
Color shaderColor;
Color color(uint16_t u, uint16_t v) {
(void)u;
(void)v;
return shaderColor;
}
};
}
-35
View File
@@ -1,35 +0,0 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "shader.cpp"
namespace UwU {
class HGradientShader{
public:
HGradientShader(uint16_t x0, Color color0, uint16_t x1, Color color1) {
this->shaderType = HGRADIENTSHADER;
this->x0 = x0;
this->shaderColor0 = color0;
this->scale = 0xffffffff / (1 + (uint32_t)x1 - (uint32_t)x0);
this->shaderColor1 = color1;
}
ShaderType shaderType;
uint16_t x0;
Color shaderColor0;
uint32_t scale;
Color shaderColor1;
Color hGradient(uint16_t u, uint16_t v) {
(void)v;
uint32_t t = (((uint32_t)u - (uint32_t)x0) * scale) / 0x01000000;
// if (v == 42) {
// printf("(%u, %u)\n", u, t);
// }
return Helpers::lerpColor(shaderColor0, shaderColor1, (uint8_t)t);
}
};
}
-35
View File
@@ -1,35 +0,0 @@
#pragma once
#include <stdint.h>
#include "../datatypes.cpp"
#include "../helpers.cpp"
#include "../buffer.cpp"
#include "shader.cpp"
namespace UwU {
class VGradientShader {
public:
VGradientShader(uint16_t y0, Color color0, uint16_t y1, Color color1) {
this->shaderType = VGRADIENTSHADER;
this->y0 = y0;
this->shaderColor0 = color0;
this->scale = 0xffffffff / (1 + (uint32_t)y1 - (uint32_t)y0);
this->shaderColor1 = color1;
}
ShaderType shaderType;
uint16_t y0;
Color shaderColor0;
uint32_t scale;
Color shaderColor1;
Color vGradient(uint16_t u, uint16_t v) {
(void)u;
uint32_t t = (((uint32_t)v - (uint32_t)y0) * scale) / 0x01000000;
// if (v == 42) {
// printf("(%u, %u)\n", u, t);
// }
return Helpers::lerpColor(shaderColor0, shaderColor1, (uint8_t)t);
}
};
}