rewrote example.cpp to use composition
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
write a monolithic header file
|
||||
refactor example code using composition
|
||||
remove draw class entirely
|
||||
create shader classes with polymorphism
|
||||
-40
@@ -1,40 +0,0 @@
|
||||
#include <stdint.h>
|
||||
|
||||
namespace UwU {
|
||||
|
||||
struct Color {
|
||||
Color(uint32_t hex);
|
||||
Color();
|
||||
uint8_t r;
|
||||
uint8_t g;
|
||||
uint8_t b;
|
||||
uint8_t a;
|
||||
};
|
||||
|
||||
struct Coord2 {
|
||||
Coord2(uint32_t x, uint32_t y);
|
||||
Coord2();
|
||||
uint32_t x;
|
||||
uint32_t y;
|
||||
};
|
||||
|
||||
struct Trapezoid {
|
||||
Trapezoid(uint16_t y0, uint16_t y1, uint16_t xl0, uint16_t xl1, uint16_t xr0, uint16_t xr1);
|
||||
Trapezoid();
|
||||
uint16_t y0;
|
||||
uint16_t y1;
|
||||
uint16_t xl0;
|
||||
uint16_t xl1;
|
||||
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;
|
||||
};
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "trapezoid.cpp"
|
||||
#include "line.cpp"
|
||||
#include "rectangle.cpp"
|
||||
#include "point.cpp"
|
||||
|
||||
namespace UwU {
|
||||
|
||||
@@ -128,64 +129,10 @@ namespace UwU {
|
||||
}
|
||||
|
||||
// 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);
|
||||
surface.buffer.drawPixel(point, color);
|
||||
void point (Point point, Color color) {
|
||||
point.draw(surface.buffer, 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;
|
||||
|
||||
+45
-24
@@ -8,12 +8,15 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "datatypes.cpp"
|
||||
#include "draw.cpp"
|
||||
#include "trapezoid.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"
|
||||
#include "draw.cpp"
|
||||
|
||||
static void onKey(void *user, uint32_t keycode, uint32_t state) {
|
||||
if (keycode == 1) {
|
||||
@@ -26,43 +29,61 @@ static void onKey(void *user, uint32_t keycode, uint32_t state) {
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
uint32_t w = 800, h = 480;
|
||||
UwU::Color fuchsia = UwU::Color(0xb00b6940);
|
||||
UwU::Color red = UwU::Color(0xff0000ff);
|
||||
uint16_t w = 800, h = 480;
|
||||
UwU::Color fuchsia = UwU::Color(0xb00b6980);
|
||||
UwU::Color red = UwU::Color(0xff000080);
|
||||
UwU::Color green = UwU::Color(0x00ff00ff);
|
||||
UwU::Color blue = UwU::Color(0x0000ffff);
|
||||
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::Draw draw = UwU::Draw(w, h, onKey);
|
||||
UwU::Surface surface = UwU::Surface(w, h, onKey);
|
||||
|
||||
draw.rainbow();
|
||||
// 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.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.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.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::Coord2(387, 43), 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();
|
||||
// draw.point(UwU::Point(387, 43), green);
|
||||
UwU::Point point = UwU::Point(387, 43);
|
||||
point.draw(surface.buffer, green);
|
||||
|
||||
while (!draw.shouldClose()) {}
|
||||
UwU::Line lines[8] {
|
||||
UwU::Line(300, 100, 250, 469),
|
||||
UwU::Line(250, 100, 300, 469),
|
||||
UwU::Line(300, 100, 469, 250),
|
||||
UwU::Line(469, 100, 300, 250),
|
||||
UwU::Line(100, 300, 250, 469),
|
||||
UwU::Line(250, 300, 100, 469),
|
||||
UwU::Line(100, 300, 469, 250),
|
||||
UwU::Line(469, 300, 100, 250)
|
||||
};
|
||||
|
||||
draw.destroy();
|
||||
for (uint16_t i = 0; i < 8; i++) {
|
||||
lines[i].draw(surface.buffer, green);
|
||||
}
|
||||
|
||||
surface.present();
|
||||
|
||||
while (!surface.shouldClose()) {}
|
||||
|
||||
surface.destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "datatypes.cpp"
|
||||
#include "helpers.cpp"
|
||||
#include "buffer.cpp"
|
||||
|
||||
namespace UwU {
|
||||
// point
|
||||
class Point {
|
||||
public:
|
||||
Point(uint16_t x, uint16_t y) {
|
||||
this->x = x;
|
||||
this->y = y;
|
||||
}
|
||||
Point() {}
|
||||
uint16_t x;
|
||||
uint16_t y;
|
||||
|
||||
// Draw a rectangle
|
||||
void draw (Buffer buffer, Color color) {
|
||||
if (x > buffer.width || y > buffer.height) {return;};
|
||||
buffer.drawPixel(Coord2(x, y), color);
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user