diff --git a/datatypes.cpp b/datatypes.cpp index ac9dd32..7ff0edd 100644 --- a/datatypes.cpp +++ b/datatypes.cpp @@ -36,22 +36,22 @@ namespace UwU { uint16_t y; }; - // trapezoid datatype (horizontally constrained) - struct Trapezoid { - Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1) { - this->y0 = y0; - this->y1 = y1; - this->xl0 = xl0; - this->xl1 = xl1; - this->xr0 = xr0; - this->xr1 = xr1; - } - Trapezoid() {} - uint16_t y0; - uint16_t y1; - uint16_t xl0; - uint16_t xl1; - uint16_t xr0; - uint16_t xr1; - }; + // // trapezoid datatype (horizontally constrained) + // struct Trapezoid { + // Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1) { + // this->y0 = y0; + // this->y1 = y1; + // this->xl0 = xl0; + // this->xl1 = xl1; + // this->xr0 = xr0; + // this->xr1 = xr1; + // } + // Trapezoid() {} + // uint16_t y0; + // uint16_t y1; + // uint16_t xl0; + // uint16_t xl1; + // uint16_t xr0; + // uint16_t xr1; + // }; } \ No newline at end of file diff --git a/draw b/draw deleted file mode 100755 index 0e3e945..0000000 Binary files a/draw and /dev/null differ diff --git a/draw.cpp b/draw.cpp index bcfc3d4..091fb75 100644 --- a/draw.cpp +++ b/draw.cpp @@ -1,7 +1,3 @@ -/* Example program that links against the waymini library. - * No external dependencies beyond waymini itself and the C standard library. - */ - #pragma once #include "waymini.h" @@ -16,90 +12,19 @@ #include "triangle.cpp" #include "buffer.cpp" #include "surface.cpp" - -// this should be handled elsewhere, eventually, maybe, probably -// static void onKey(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); -// } +#include "trapezoid.cpp" +#include "line.cpp" +#include "rectangle.cpp" namespace UwU { class Draw { - private: - class Surface surface; - // 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 * surface.getStride() + (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); - surface.buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color); - 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); - surface.buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color); - if (d > 0) { - x += xi; - d += (2 * (dx - dy)); - } else { - d += (2 * dx); - } - } - return; - } - public: - Draw(uint16_t width, uint16_t height, waymini_key_cb onKey) : surface(width, height, onKey) {} + + class Surface surface; bool shouldClose() { return surface.shouldClose(); @@ -134,13 +59,8 @@ namespace UwU { // 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); - surface.buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color); - } - } + Rectangle rectangle = Rectangle(point0.x, point0.y, point1.x, point1.y); + rectangle.draw(surface.buffer, color); return; } @@ -214,55 +134,65 @@ namespace UwU { surface.buffer.drawPixel(point, color); } - // 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); - } - } + // // 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); + // surface.buffer.drawPixel(Coord2(x, i + coords.y0), color); + // } + // } + // return; + // } + + void line (Line line, Color color) { + line.draw(surface.buffer, color); + return; } - // 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); - surface.buffer.drawPixel(Coord2(x, i + coords.y0), color); - } - } + coords.draw(surface.buffer, color); return; } @@ -273,43 +203,4 @@ namespace UwU { 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, onKey); - -// 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, 480), green); -// draw.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 (!draw.shouldClose()) {} - -// draw.destroy(); -// return 0; -// } + } \ No newline at end of file diff --git a/example b/example index 69e09e8..c4f91fa 100755 Binary files a/example and b/example differ diff --git a/example.cpp b/example.cpp index 806a9fc..b97fdc0 100644 --- a/example.cpp +++ b/example.cpp @@ -1,6 +1,6 @@ // #pragma once -// #include "waymini.h" +#include "waymini.h" #include #include @@ -9,9 +9,11 @@ #include "datatypes.cpp" #include "draw.cpp" -// #include "triangle.cpp" -// #include "buffer.cpp" -// #include "surface.cpp" +#include "trapezoid.cpp" +#include "triangle.cpp" +#include "buffer.cpp" +#include "surface.cpp" +#include "line.cpp" static void onKey(void *user, uint32_t keycode, uint32_t state) { if (keycode == 1) { @@ -44,16 +46,19 @@ int main(void) { draw.trapezoid(UwU::Trapezoid(300, 400, 360, 380, 600, 480), green); draw.triangle(280, 100, 240, 290, 320, 300, green); + // myTrap = UwU:Trapezoid(300, 400, 360, 380, 600, 480); + // myTrap.draw() + 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.line(UwU::Line(300, 100, 250, 469), green); + draw.line(UwU::Line(250, 100, 300, 469), green); + draw.line(UwU::Line(300, 100, 469, 250), green); + draw.line(UwU::Line(469, 100, 300, 250), green); + draw.line(UwU::Line(100, 300, 250, 469), green); + draw.line(UwU::Line(250, 300, 100, 469), green); + draw.line(UwU::Line(100, 300, 469, 250), green); + draw.line(UwU::Line(469, 300, 100, 250), green); draw.present(); while (!draw.shouldClose()) {} diff --git a/line.cpp b/line.cpp new file mode 100644 index 0000000..5c889cf --- /dev/null +++ b/line.cpp @@ -0,0 +1,95 @@ +#pragma once + +#include + +#include "datatypes.cpp" +#include "helpers.cpp" +#include "buffer.cpp" + +namespace UwU { + // line + class Line { + private: + // plot lines if m <= 1 + void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Color color) { + int16_t dx = ix1 - ix0; + int16_t dy = iy1 - iy0; + int16_t yi = 1; + if (dy < 0) { + yi = -1; + dy = -dy; + } + int16_t d = (2 * dy) - dx; + int16_t y = iy0; + for (int16_t x = ix0; x < ix1; x++) { + buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color); + if (d > 0) { + y += yi; + d += (2 * (dy - dx)); + } else { + d += (2 * dy); + } + } + return; + } + + // plot lines if m >1 + void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, Color color) { + int16_t dx = ix1 - ix0; + int16_t dy = iy1 - iy0; + int16_t xi = 1; + if (dx < 0) { + xi = -1; + dx = -dx; + } + int16_t d = (2 * dx) - dy; + int16_t x = ix0; + for (int16_t y = iy0; y < iy1; y++) { + buffer.drawPixel(Coord2((uint16_t)x, (uint16_t)y), color); + if (d > 0) { + x += xi; + d += (2 * (dx - dy)); + } else { + d += (2 * dx); + } + } + return; + } + + public: + Line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { + this->x0 = x0; + this->y0 = y0; + this->x1 = x1; + this->y1 = y1; + } + Line() {} + uint16_t x0; + uint16_t y0; + uint16_t x1; + uint16_t y1; + + // Uses Bresenham's line algorithm to draw line of any slope + void draw (Buffer buffer, Color color) { + if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; + int16_t ix0 = x0; + int16_t iy0 = y0; + int16_t ix1 = x1; + int16_t iy1 = y1; + if (abs(iy1 - iy0) < abs(ix1 - ix0)) { + if (ix0 > ix1) { + lineLow(ix1, iy1, ix0, iy0, buffer, color); + } else { + lineLow(ix0, iy0, ix1, iy1, buffer, color); + } + } else { + if (iy0 > iy1) { + lineHigh(ix1, iy1, ix0, iy0, buffer, color); + } else { + lineHigh(ix0, iy0, ix1, iy1, buffer, color); + } + } + return; + } + }; +} \ No newline at end of file diff --git a/rectangle.cpp b/rectangle.cpp new file mode 100644 index 0000000..39ed8f4 --- /dev/null +++ b/rectangle.cpp @@ -0,0 +1,36 @@ +#pragma once + +#include + +#include "datatypes.cpp" +#include "helpers.cpp" +#include "buffer.cpp" + +namespace UwU { + // rectangle + class Rectangle { + public: + Rectangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { + this->x0 = x0; + this->y0 = y0; + this->x1 = x1; + this->y1 = y1; + } + Rectangle() {} + uint16_t x0; + uint16_t y0; + uint16_t x1; + uint16_t y1; + + // Draw a rectangle + void draw (Buffer buffer, Color color) { + 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); + } + } + return; + } + }; +} \ No newline at end of file diff --git a/trapezoid.cpp b/trapezoid.cpp new file mode 100644 index 0000000..60d5972 --- /dev/null +++ b/trapezoid.cpp @@ -0,0 +1,59 @@ +#pragma once + +#include + +#include "datatypes.cpp" +#include "helpers.cpp" +#include "buffer.cpp" + +namespace UwU { + // trapezoid (horizontally constrained) + class Trapezoid { + public: + Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1) { + this->y0 = y0; + this->y1 = y1; + this->xl0 = xl0; + this->xl1 = xl1; + this->xr0 = xr0; + this->xr1 = xr1; + } + Trapezoid() {} + uint16_t y0; + uint16_t y1; + uint16_t xl0; + uint16_t xl1; + uint16_t xr0; + uint16_t xr1; + + // Draw a horizontally bounded trapezoid + // y1 >= y0 + // todo move to private and add public checks + void draw (Buffer buffer, 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 + y1 - y0; + int16_t LeftBound[height]; + int16_t RightBound[height]; + + Helpers::bresenham(0, xl0, y1 - y0, xl1, LeftBound); + Helpers::bresenham(0, xr0, y1 - y0, 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); + buffer.drawPixel(Coord2(x, i + y0), color); + } + } + return; + } + }; +} \ No newline at end of file