added line drawing
This commit is contained in:
@@ -44,91 +44,144 @@ namespace UwU {
|
||||
};
|
||||
|
||||
class Draw {
|
||||
// private:
|
||||
// lineLow()
|
||||
private:
|
||||
|
||||
// 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++) {
|
||||
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
|
||||
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++) {
|
||||
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
|
||||
if (d > 0) {
|
||||
x += xi;
|
||||
d += (2 * (dx - dy));
|
||||
} else {
|
||||
d += (2 * dx);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
public:
|
||||
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);
|
||||
struct waymini* wm;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t stride;
|
||||
uint8_t *pixels;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// 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(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->pixels = (uint8_t *)waymini_get_pixels(this->wm);
|
||||
if (!this->pixels) {
|
||||
waymini_destroy(this->wm);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
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
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
// 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
|
||||
}
|
||||
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 (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 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) {
|
||||
this->lineLow(x1, y1, x0, y0, color);
|
||||
} else {
|
||||
this->lineLow(x0, y0, x1, y1, color);
|
||||
}
|
||||
} else {
|
||||
if (y0 > y1) {
|
||||
this->lineHigh(x1, y1, x0, y0, color);
|
||||
} else {
|
||||
this->lineHigh(x0, y0, x1, y1, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -146,6 +199,14 @@ int main(void) {
|
||||
waymini_present(draw.wm);
|
||||
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia);
|
||||
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);
|
||||
|
||||
while (!waymini_should_close(draw.wm)) {
|
||||
if (waymini_dispatch(draw.wm) < 0) break;
|
||||
|
||||
Reference in New Issue
Block a user