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 7acbb7d..9007899 100755 Binary files a/test and b/test differ 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;