From b32f0a216bc076a3df8a3f1b1452b73f9fe19b7b Mon Sep 17 00:00:00 2001 From: Annika Date: Sat, 4 Jul 2026 02:38:48 -0700 Subject: [PATCH] added Color and Coord2 structs --- example.c | 54 ---------------- test | Bin 36928 -> 36928 bytes test.cpp | 180 ++++++++++++++++++++++++++++++++---------------------- 3 files changed, 106 insertions(+), 128 deletions(-) delete mode 100644 example.c diff --git a/example.c b/example.c deleted file mode 100644 index 358cd8d..0000000 --- a/example.c +++ /dev/null @@ -1,54 +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 - -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); -} - -int main(void) { - uint32_t w = 800, h = 480; - - struct waymini *wm = waymini_create(w, h, on_key, NULL); - if (!wm) return 1; - - printf("surface: %ux%u stride=%u\n", - waymini_get_width(wm), waymini_get_height(wm), waymini_get_stride(wm)); - - uint8_t *pixels = (uint8_t *)waymini_get_pixels(wm); - if (!pixels) { - waymini_destroy(wm); - return 1; - } - - /* Fill a simple gradient. Each row is stride bytes. */ - for (uint32_t y = 0; y < h; y++) { - for (uint32_t x = 0; x < w; x++) { - size_t i = (size_t)y * waymini_get_stride(wm) + (size_t)x * 4; - pixels[i + 0] = (uint8_t)(y * 255 / h); /* B */ - pixels[i + 1] = 0x00; /* G */ - pixels[i + 2] = (uint8_t)(x * 255 / w); /* R */ - pixels[i + 3] = 0xFF; /* A (ignored for XRGB) */ - } - } - - waymini_present(wm); - pixels[200001] = 255; - - while (!waymini_should_close(wm)) { - if (waymini_dispatch(wm) < 0) break; - } - - waymini_destroy(wm); - return 0; -} diff --git a/test b/test index 7acbb7d5d8eb83c9985587a149aaf38986f47c42..90078994392fc14db76c64505a8860a232c7c515 100755 GIT binary patch delta 6176 zcmZ`-4RjRM6`r>tkSLq%vPm`@ezGYGEMhZRLd+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;gzf9hRrs_YCI!IW}V?EMV&!WGtc%KVRtA9;O;r42QSYiAc!Qo^P z=wCy6Iq@%YBu4NCD%Qjk{6XGa!=uxkH3vz1pnPpDA&r6V7oN9}tuWs&gzuZ7neBrn z|12^Cx=_~F(00_Pgp2taLxf@+^|iH|aMarvhyT>4SbCf0#b^QKfv7-YgLTmV0X8_j AK>z>% diff --git a/test.cpp b/test.cpp index edcb6f3..75dede3 100644 --- a/test.cpp +++ b/test.cpp @@ -9,7 +9,7 @@ #include #include - +// 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 "; @@ -18,93 +18,125 @@ static void on_key(void *user, uint32_t keycode, uint32_t state) { } 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; + } + 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; + } + uint32_t x; + uint32_t y; + }; + class Draw { + // private: + // lineLow() + public: - struct waymini* wm; - uint32_t width; - uint32_t height; - uint32_t stride; - uint8_t *pixels; + 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); - 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->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); + 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++) { + size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4; + this->pixels[i + 0] = color.b; // B + this->pixels[i + 1] = color.g; // G + this->pixels[i + 2] = color.r; // R + this->pixels[i + 3] = 0xFF; // A } } - - // Draw a rectangle (duh) - void rectangle(uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint8_t r, uint8_t g, uint8_t b) { - if (x0 > this->width || y0 > this->height || x1 > this->width || y1 > this->height) {return;}; - for (uint32_t y = y0; y < y1; y++) { - for (uint32_t x = x0; x < x1; x++) { - size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4; - this->pixels[i + 0] = b; // B - this->pixels[i + 1] = g; // G - this->pixels[i + 2] = r; // R - this->pixels[i + 3] = 0xFF; // 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 - } + 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 } } + } - void point (uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b) { - if (x > this->width || y > this->height) {return;}; - size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4; - this->pixels[i + 0] = b; // B - this->pixels[i + 1] = g; // G - this->pixels[i + 2] = r; // 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;}; + size_t i = (size_t)point.y * waymini_get_stride(this->wm) + (size_t)point.x * 4; + this->pixels[i + 0] = color.b; // B + this->pixels[i + 1] = color.g; // G + this->pixels[i + 2] = color.r; // R + this->pixels[i + 3] = 0xFF; // 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 dx = (int32_t)x1 - (int32_t)x0; + int32_t dy = (int32_t)y1 - (int32_t)y1; + int32_t yi = 1; + if (dy < 0) { + yi = -1; + dy = -dy; } + int32_t d = (2 * dy) - dx; + int32_t y = y0 + for (uint32_t x = x0; x < x1; x++) { - // // Uses Bresenham's line algorithm to draw line of any slope - // void line (uint32_t x0, uint32_t y0, uint32_t x1, uint32_t y1, uint8_t r, uint8_t g, uint8_t b) { - // if (x0 > this->width || y0 > this->height || x1 > this->width || y1 > this->height) {return;}; - // int32_t dx = (int32_t)x1 - (int32_t)x0; - // int32_t dy = (int32_t)y1 - (int32_t)y1; - // int32_t yi = 1; - // if (dy < 0) { - // yi = -1; - // dy = -dy; - // } - // int32_t d = (2 * dy) - dx; - // int32_t y = y0 - // for (uint32_t x = x0; x < x1; x++) { - - // } - // size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4; - // this->pixels[i + 0] = b; // B - // this->pixels[i + 1] = g; // G - // this->pixels[i + 2] = r; // R - // this->pixels[i + 3] = 0xFF; // A - // } + } + size_t i = (size_t)y * waymini_get_stride(this->wm) + (size_t)x * 4; + this->pixels[i + 0] = b; // B + this->pixels[i + 1] = g; // G + this->pixels[i + 2] = r; // R + this->pixels[i + 3] = 0xFF; // A + } }; } int main(void) { uint32_t w = 800, h = 480; + UwU::Color fuchsia = UwU::Color(0xb00b69ff); + UwU::Color green = UwU::Color(0x00ff00ff); + 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); @@ -112,8 +144,8 @@ int main(void) { draw.rainbow(); waymini_present(draw.wm); - draw.rectangle(69, 69, 420, 420, 0xB0, 0x0B, 0x69); - draw.point(387, 43, 0x00, 0xff, 0x00); + draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia); + draw.point(UwU::Coord2(387, 43), green); while (!waymini_should_close(draw.wm)) { if (waymini_dispatch(draw.wm) < 0) break;