diff --git a/Makefile b/Makefile index 459ef17..de58a3d 100644 --- a/Makefile +++ b/Makefile @@ -12,13 +12,13 @@ LIB_OBJ := $(LIB_SRC:.c=.o) .PHONY: all clean -all: libwaymini.a test waymini +all: libwaymini.a draw waymini libwaymini.a: $(LIB_OBJ) ar rcs $@ $^ -test: test.cpp libwaymini.a - g++ $(CFLAGS) -o $@ test.cpp -L. -lwaymini $(LDFLAGS) +draw: draw.cpp libwaymini.a + g++ $(CFLAGS) -o $@ draw.cpp -L. -lwaymini $(LDFLAGS) waymini: waymini_standalone.c waymini.c xdg-shell-client-protocol.c $(CC) $(CFLAGS) -o $@ waymini_standalone.c waymini.c xdg-shell-client-protocol.c $(LDFLAGS) diff --git a/datatypes.cpp b/datatypes.cpp index 14fdf67..ac9dd32 100644 --- a/datatypes.cpp +++ b/datatypes.cpp @@ -12,6 +12,12 @@ namespace UwU { this->g = (hex & 0x00ff0000) >> 16; this->r = (hex & 0xff000000) >> 24; } + Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { + this->r = r; + this->g = g; + this->b = b; + this->a = a; + } Color() {} uint8_t r; uint8_t g; @@ -21,13 +27,13 @@ namespace UwU { // Coord2 datatype for xy coordinates, from top-left of surface struct Coord2 { - Coord2(uint32_t x, uint32_t y) { + Coord2(uint16_t x, uint16_t y) { this->x = x; this->y = y; } Coord2() {} - uint32_t x; - uint32_t y; + uint16_t x; + uint16_t y; }; // trapezoid datatype (horizontally constrained) @@ -48,22 +54,4 @@ namespace UwU { uint16_t xr0; uint16_t xr1; }; - - struct Triangle { - Triangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { - this->x0 = x0; - this->y0 = y0; - this->x1 = x1; - this->y1 = y1; - this->x2 = x2; - this->y2 = y2; - } - Triangle() {} - uint16_t x0; - uint16_t y0; - uint16_t x1; - uint16_t y1; - uint16_t x2; - uint16_t y2; - }; } \ No newline at end of file diff --git a/datatypes.h b/datatypes.h index fd19294..a99eee0 100644 --- a/datatypes.h +++ b/datatypes.h @@ -28,4 +28,13 @@ namespace UwU { uint16_t xr0; uint16_t xr1; }; + + struct Buffer { + Buffer(uint8_t* pixels, uint16_t stride, uint16_t height, uint16_t width); + Buffer(); + uint8_t* pixels; + uint16_t stride; + uint16_t height; + uint16_t width; + }; } \ No newline at end of file diff --git a/test b/test deleted file mode 100755 index d71b263..0000000 Binary files a/test and /dev/null differ diff --git a/test.cpp b/test.cpp deleted file mode 100644 index 3cea2b6..0000000 --- a/test.cpp +++ /dev/null @@ -1,441 +0,0 @@ -/* Example program that links against the waymini library. - * No external dependencies beyond waymini itself and the C standard library. - */ - -#include "waymini.h" - -#include -#include -#include -#include - -#include "datatypes.cpp" -#include "helpers.cpp" -// #include "triangle.cpp" - -// this should be handled elsewhere, eventually, maybe, probably -static void on_key(void *user, uint32_t keycode, uint32_t state) { - if (keycode == 1) { - exit(1); - } - (void)user; - const char *label = state ? "down" : "up "; - printf("key %s: %u\n", label, keycode); - fflush(stdout); -} - -namespace UwU { - - class Draw { - private: - - uint8_t *pixels; - - 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(wm) + (size_t)x * 4; - uint8_t b0 = pixels[i + 0]; - uint8_t g0 = pixels[i + 1]; - uint8_t r0 = pixels[i + 2]; - b = Helpers::lerp8(b0, b, a); - g = Helpers::lerp8(g0, g, a); - r = Helpers::lerp8(r0, r, a); - pixels[i + 0] = b; // B - pixels[i + 1] = g; // G - pixels[i + 2] = r; // R - pixels[i + 3] = 0xff; // A - } - - // 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: - - - 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", waymini_get_width(this->wm), waymini_get_height(this->wm), waymini_get_stride(this->wm)); - - this->pixels = (uint8_t *)waymini_get_pixels(this->wm); - if (!this->pixels) { - waymini_destroy(this->wm); - exit(1); - } - } - - struct waymini* wm; - - uint32_t getWidth() { - return waymini_get_width(wm); - } - - uint32_t getHeight() { - return waymini_get_height(wm); - } - - uint32_t getStride() { - return waymini_get_stride(wm); - } - - void present() { - waymini_present(wm); - return; - } - - void destroy() { - waymini_destroy(wm); - return; - } - - // Draw a rectangle (duh) - void rectangle(Coord2 point0, Coord2 point1, Color color) { - if (point0.x > getWidth() || point0.y > getHeight() || point1.x > getWidth() || point1.y > getHeight()) {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 - 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); - } - } - 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++) { - 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 (Coord2 point, Color color) { - if (point.x > getWidth() || point.y > getHeight()) {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 > getWidth() || point0.y > getHeight() || point1.x > getWidth() || point1.y > getHeight()) {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) { - lineLow(x1, y1, x0, y0, color); - } else { - lineLow(x0, y0, x1, y1, color); - } - } else { - if (y0 > y1) { - lineHigh(x1, y1, x0, y0, color); - } else { - lineHigh(x0, y0, x1, y1, color); - } - } - } - - // Draw a horizontally bounded trapezoid - // coords.y1 >= coords.y0 - // todo move to private and add public checks - void trapezoid (Trapezoid coords, Color color) { - printf("(%u, %u, %u, %u, %u, %u)", coords.y0, coords.y1, coords.xl0, coords.xl1, coords.xr0, coords.xr1); - uint16_t height = 1 + coords.y1 - coords.y0; - int16_t LeftBound[height]; - int16_t RightBound[height]; - - Helpers::bresenham(0, coords.xl0, coords.y1 - coords.y0, coords.xl1, LeftBound); - Helpers::bresenham(0, coords.xr0, coords.y1 - coords.y0, coords.xr1, RightBound); - - printf("leftbound\n"); - for (uint16_t i = 0; i < height; i++) { - printf("(%u, %u)\n", i, LeftBound[i]); - } - printf("rightbound\n"); - for (uint16_t i = 0; i < height; i++) { - printf("(%u, %u)\n", i, RightBound[i]); - } - - for (uint16_t i = 0; i < height; i++) { - for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) { - // printf("(%u, %u)", x, i + coords.y0); - putPixel((uint32_t)x, (uint32_t)i + coords.y0, color.r, color.g, color.b, color.a); - } - } - return; - } - - // Draw a triangle - void triangle (Triangle coords, Color color) { - Coord2 top = Coord2(); - Coord2 mid = Coord2(); - Coord2 btm = Coord2(); - // cases: flat top, flat bottom, left point, right point - if (coords.y0 <= coords.y1 && coords.y0 <= coords.y2) { - top.x = coords.x0; - top.y = coords.y0; - if (coords.y1 < coords.y2) { - mid.x = coords.x1; - mid.y = coords.y1; - btm.x = coords.x2; - btm.y = coords.y2; - } else { - mid.x = coords.x2; - mid.y = coords.y2; - btm.x = coords.x1; - btm.y = coords.y1; - } - } else if (coords.y1 <= coords.y0 && coords.y1 <= coords.y2) { - top.x = coords.x1; - top.y = coords.y1; - if (coords.y0 < coords.y2) { - mid.x = coords.x0; - mid.y = coords.y0; - btm.x = coords.x2; - btm.y = coords.y2; - } else { - mid.x = coords.x2; - mid.y = coords.y2; - btm.x = coords.x0; - btm.y = coords.y0; - } - } else { - top.x = coords.x2; - top.y = coords.y2; - if (coords.y0 < coords.y1) { - mid.x = coords.x0; - mid.y = coords.y0; - btm.x = coords.x1; - btm.y = coords.y1; - } else { - mid.x = coords.x1; - mid.y = coords.y1; - btm.x = coords.x0; - btm.y = coords.y0; - } - } - printf("top: (%u, %u)\n", top.x, top.y); - printf("mid: (%u, %u)\n", mid.x, mid.y); - printf("btm: (%u, %u)\n", btm.x, btm.y); - - uint16_t height = 1 + btm.y - top.y; - int16_t longBound[height]; - int16_t splitBound[height]; - // int16_t rightBound[height]; - Helpers::bresenham(0, top.x, height - 1, btm.x, longBound); - printf("longBound\n"); - if ((int16_t)mid.x < longBound[mid.y - top.y]) { - // left-handed triangle - printf("left-handed\n"); - // rightBound = longBound; - Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); - printf("splitTop\n"); - Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); - printf("splitBtm\n"); - for (uint16_t i = 0; i < height; i++) { - printf("(%u, %u)\n", i, splitBound[i]); - } - for (uint32_t i = 0; i < height; i++) { - for (uint32_t x = (uint32_t)splitBound[i]; x <= (uint32_t)longBound[i]; x++) { - putPixel(x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a); - } - } - } else { - // right-handed triangle - printf("right-handed\n"); - // leftBound = longBound; - Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); - printf("splitTop\n"); - Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); - printf("splitBtm\n"); - for (uint32_t i = 0; i < height; i++) { - for (uint32_t x = (uint32_t)longBound[i]; x <= (uint32_t)splitBound[i]; x++) { - putPixel(x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a); - } - } - } - - - - // // handle case where top or bottom is flat - // if (mid.y == top.y) { - // // flat top - // if (mid.x < top.x) { - // trapezoid(Trapezoid(top.y, btm.y, mid.x, btm.x, top.x, btm.x), color); - // return; - // } else { - // trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, mid.x, btm.x), color); - // return; - // } - // } else if (mid.y == btm.y) { - // // flat bottom - // if (mid.x < btm.x) { - // trapezoid(Trapezoid(top.y, btm.y, top.x, mid.x, top.x, btm.x), color); - // return; - // } else { - // trapezoid(Trapezoid(top.y, btm.y, top.x, btm.x, top.x, mid.x), color); - // return; - // } - // } - - // // calculate midpoint of straight line - // int32_t dy = (int32_t)btm.y - (int32_t)top.y; - // if (dy == 0) { - // return; // shouldn't be possible, should be detected earlier - // } - // int32_t dx = (int32_t)btm.x - (int32_t)top.x; - // dx *= (mid.y - top.y); - // dx /= dy; - // dx += top.x; - // uint16_t oppx = (uint16_t)dx; - - // // split triangle into two trapezoids - // if (mid.x < oppx) { - // trapezoid(Trapezoid(top.y, mid.y, top.x, mid.x, top.x, oppx), color); - // trapezoid(Trapezoid(mid.y, btm.y, mid.x, btm.x, oppx, btm.x), color); - // return; - // } else { - // trapezoid(Trapezoid(top.y, mid.y, top.x, oppx, top.x, mid.x), color); - // trapezoid(Trapezoid(mid.y, btm.y, oppx, btm.x, mid.x, btm.x), color); - // return; - // } - 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, 380, 600, 400), green); - draw.triangle(UwU::Triangle(280, 100, 240, 290, 320, 300), 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); - draw.present(); - - while (!waymini_should_close(draw.wm)) { - if (waymini_dispatch(draw.wm) < 0) break; - } - - draw.destroy(); - return 0; -} diff --git a/triangle.cpp b/triangle.cpp index 8885334..fb7cf1e 100644 --- a/triangle.cpp +++ b/triangle.cpp @@ -4,9 +4,13 @@ #include "datatypes.cpp" #include "helpers.cpp" +#include "buffer.cpp" namespace UwU { class Triangle { + private: + + public: Triangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { this->x0 = x0; @@ -23,5 +27,99 @@ namespace UwU { uint16_t y1; uint16_t x2; uint16_t y2; + + // Draw a triangle + void draw (Buffer buffer, Color color) { + Coord2 top = Coord2(); + Coord2 mid = Coord2(); + Coord2 btm = Coord2(); + // cases: left-handed, right-handed + if (y0 <= y1 && y0 <= y2) { + top.x = x0; + top.y = y0; + if (y1 < y2) { + mid.x = x1; + mid.y = y1; + btm.x = x2; + btm.y = y2; + } else { + mid.x = x2; + mid.y = y2; + btm.x = x1; + btm.y = y1; + } + } else if (y1 <= y0 && y1 <= y2) { + top.x = x1; + top.y = y1; + if (y0 < y2) { + mid.x = x0; + mid.y = y0; + btm.x = x2; + btm.y = y2; + } else { + mid.x = x2; + mid.y = y2; + btm.x = x0; + btm.y = y0; + } + } else { + top.x = x2; + top.y = y2; + if (y0 < y1) { + mid.y = y0; + mid.x = x0; + btm.x = x1; + btm.y = y1; + } else { + mid.x = x1; + mid.y = y1; + btm.x = x0; + btm.y = y0; + } + } + // printf("top: (%u, %u)\n", top.x, top.y); + // printf("mid: (%u, %u)\n", mid.x, mid.y); + // printf("btm: (%u, %u)\n", btm.x, btm.y); + + uint16_t height = 1 + btm.y - top.y; + int16_t longBound[height]; + int16_t splitBound[height]; + // int16_t rightBound[height]; + Helpers::bresenham(0, top.x, height - 1, btm.x, longBound); + // printf("longBound\n"); + if ((int16_t)mid.x < longBound[mid.y - top.y]) { + // left-handed triangle + // printf("left-handed\n"); + // rightBound = longBound; + Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); + // printf("splitTop\n"); + Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); + // printf("splitBtm\n"); + // for (uint16_t i = 0; i < height; i++) { + // printf("(%u, %u)\n", i, splitBound[i]); + // } + for (uint16_t i = 0; i < height; i++) { + for (uint16_t x = (uint16_t)splitBound[i]; x <= (uint16_t)longBound[i]; x++) { + // Helpers::putPixel(buffer, x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a); + buffer.drawPixel(Coord2(x, i + (uint16_t)top.y), color); + } + } + } else { + // right-handed triangle + // printf("right-handed\n"); + // leftBound = longBound; + Helpers::bresenham(0, top.x, mid.y - top.y, mid.x, splitBound); + // printf("splitTop\n"); + Helpers::bresenham(mid.y - top.y, mid.x, height - 1, btm.x, splitBound); + // printf("splitBtm\n"); + for (uint16_t i = 0; i < height; i++) { + for (uint16_t x = (uint16_t)longBound[i]; x <= (uint16_t)splitBound[i]; x++) { + // Helpers::putPixel(buffer, x, i + (uint32_t)top.y, color.r, color.g, color.b, color.a); + buffer.drawPixel(Coord2(x, i + (uint16_t)top.y), color); + } + } + } + return; + } }; } \ No newline at end of file diff --git a/waymini b/waymini index 811fb3e..351bb7a 100755 Binary files a/waymini and b/waymini differ