From 1fce028eeae4cb60f3fd463aa782944ec75ce4bd Mon Sep 17 00:00:00 2001 From: Annika Date: Tue, 7 Jul 2026 07:46:27 -0700 Subject: [PATCH] stubbed out shaders --- TODO | 8 +-- buffer.cpp | 17 +++++ draw.cpp | 153 --------------------------------------------- example | Bin 36984 -> 36984 bytes example.cpp | 16 +---- rectangle.cpp | 5 +- shaders/shader.cpp | 19 ++++++ surface.cpp | 1 + 8 files changed, 47 insertions(+), 172 deletions(-) delete mode 100644 draw.cpp create mode 100644 shaders/shader.cpp diff --git a/TODO b/TODO index 3bf67f4..137a585 100644 --- a/TODO +++ b/TODO @@ -1,4 +1,4 @@ -write a monolithic header file -refactor example code using composition -remove draw class entirely -create shader classes with polymorphism \ No newline at end of file +[ ] write a monolithic header file +[x] refactor example code using composition +[ ] remove draw class entirely +[ ] create shader classes with polymorphism \ No newline at end of file diff --git a/buffer.cpp b/buffer.cpp index a6babec..419d0eb 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -4,6 +4,7 @@ #include "datatypes.cpp" #include "helpers.cpp" +#include "shaders/shader.cpp" namespace UwU { // Buffer object - can be a wayland surface or a virtual buffer @@ -42,5 +43,21 @@ namespace UwU { pixels[i + 3] = 0xff; // A return; } + + void drawPixelShader(Coord2 coord, Shader shader) { + size_t i = (size_t)coord.y * stride + (size_t)coord.x * 4; + uint8_t b0 = pixels[i + 0]; + uint8_t g0 = pixels[i + 1]; + uint8_t r0 = pixels[i + 2]; + Color newColor = shader.color(); + uint8_t b = Helpers::lerp8(b0, newColor.b, newColor.a); + uint8_t g = Helpers::lerp8(g0, newColor.g, newColor.a); + uint8_t r = Helpers::lerp8(r0, newColor.r, newColor.a); + pixels[i + 0] = b; // B + pixels[i + 1] = g; // G + pixels[i + 2] = r; // R + pixels[i + 3] = 0xff; // A + return; + } }; } \ No newline at end of file diff --git a/draw.cpp b/draw.cpp deleted file mode 100644 index 47ee8e1..0000000 --- a/draw.cpp +++ /dev/null @@ -1,153 +0,0 @@ -#pragma once - -#include "waymini.h" - -#include -#include -#include -#include - -#include "datatypes.cpp" -#include "helpers.cpp" -#include "triangle.cpp" -#include "buffer.cpp" -#include "surface.cpp" -#include "trapezoid.cpp" -#include "line.cpp" -#include "rectangle.cpp" -#include "point.cpp" - -namespace UwU { - - class Draw { - public: - - Draw(uint16_t width, uint16_t height, waymini_key_cb onKey) - : surface(width, height, onKey) {} - - class Surface surface; - - 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); - 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 - void point (Point point, Color color) { - point.draw(surface.buffer, color); - } - - void line (Line line, Color color) { - line.draw(surface.buffer, color); - return; - } - - void trapezoid (Trapezoid coords, Color color) { - coords.draw(surface.buffer, color); - 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; - } - }; - } \ No newline at end of file diff --git a/example b/example index fb375837e2ebdad6395e2c536b41a0f6fbe14109..b09f1281ba5522c4a51ae14fa4dc1a3b3d3017f4 100755 GIT binary patch delta 3501 zcmZ`+eNa@_6~A{EaF@WoU10a^0|eI57O3IL@F9$BWFsq&7A6?QN@FWVY1r5{0&rJk;V9*aolAz+cEzcwcr1??*hW?{x6XD=~r zl}`Jh8M1{EiEc|6b?Vf!uvd_&8BPfz&4%-WELj-yWgSz3C1kD!m$hD6^E6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BaaKvXj}em`?>;Yjbc(^n!+ekgbnv@bzkKiSXP)8*^xFiu8c% zqvth&ylkz-dl^2!#4PZUaz}cK`pTmwKNxgxOU3m zKidp{h^@63k}D|w8_jzXi6-a-2lO|VtXl>bQQFoWSSKBqQrdK_jIYI+hB_o@4ev4s zZQ=oj53!N&=Mq~_YH%*1!Y^J1Dy6xXl=%BfDSQUct#<}o8u2qIWxZZzISe(CE;<>` z^-<(#{ZrN{@=mPYf9mCwSp3@PZ&pYaOgBq%RT1kx8&v#u(g_;{ACQ&`GV8Bw`C$}s R`Oma|fd6~>_y06A^?%6|KtKQh diff --git a/example.cpp b/example.cpp index fc4255c..6e39cc0 100644 --- a/example.cpp +++ b/example.cpp @@ -16,7 +16,8 @@ #include "line.cpp" #include "rectangle.cpp" #include "point.cpp" -#include "draw.cpp" +// #include "draw.cpp" +#include "shaders/shader.cpp" static void onKey(void *user, uint32_t keycode, uint32_t state) { if (keycode == 1) { @@ -30,38 +31,27 @@ static void onKey(void *user, uint32_t keycode, uint32_t state) { int main(void) { uint16_t w = 800, h = 480; - UwU::Color fuchsia = UwU::Color(0xb00b6980); + UwU::Shader fuchsia = UwU::Shader(); UwU::Color red = UwU::Color(0xff000080); UwU::Color green = UwU::Color(0x00ff00ff); UwU::Color blue = UwU::Color(0x0000ff80); 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, onKey); UwU::Surface surface = UwU::Surface(w, h, onKey); // draw.rainbow(); - // draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); rectangle.draw(surface.buffer, fuchsia); // draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red); - // draw.trapezoid(UwU::Trapezoid(300, 400, 360, 380, 600, 480), red); UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480); trapezoid.draw(surface.buffer, red); - // draw.triangle(280, 100, 240, 290, 320, 300, blue); UwU::Triangle triangle = UwU::Triangle(280, 100, 240, 290, 320, 300); triangle.draw(surface.buffer, blue); - // myTrap = UwU:Trapezoid(300, 400, 360, 380, 600, 480); - // myTrap.draw() - - - // draw.point(UwU::Point(387, 43), green); UwU::Point point = UwU::Point(387, 43); point.draw(surface.buffer, green); diff --git a/rectangle.cpp b/rectangle.cpp index 39ed8f4..7ee9e83 100644 --- a/rectangle.cpp +++ b/rectangle.cpp @@ -5,6 +5,7 @@ #include "datatypes.cpp" #include "helpers.cpp" #include "buffer.cpp" +#include "shaders/shader.cpp" namespace UwU { // rectangle @@ -23,11 +24,11 @@ namespace UwU { uint16_t y1; // Draw a rectangle - void draw (Buffer buffer, Color color) { + void draw (Buffer buffer, Shader shader) { if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; for (uint16_t y = y0; y < y1; y++) { for (uint16_t x = x0; x < x1; x++) { - buffer.drawPixel(Coord2(x, y), color); + buffer.drawPixelShader(Coord2(x, y), shader); } } return; diff --git a/shaders/shader.cpp b/shaders/shader.cpp new file mode 100644 index 0000000..a9e6cf2 --- /dev/null +++ b/shaders/shader.cpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +#include "../datatypes.cpp" +#include "../helpers.cpp" +#include "../buffer.cpp" + +namespace UwU { + // shader prototype + class Shader { + public: + Shader() {} + + Color color() { + return Color(0xb00b69ff); + } + }; +} \ No newline at end of file diff --git a/surface.cpp b/surface.cpp index d11d318..02b504b 100644 --- a/surface.cpp +++ b/surface.cpp @@ -12,6 +12,7 @@ namespace UwU { class Surface { + // should probably be a child of buffer public: Surface(uint16_t width, uint16_t height, waymini_key_cb onKey) { this->wm = waymini_create((uint32_t)width, (uint32_t)height, onKey, NULL);