From edee7f1f66e7df2110069057289210c6bfbd272f Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Thu, 15 Apr 2021 16:00:56 +0200 Subject: Add rectangle drawing routines --- src/main.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 7f04f28..32c4170 100644 --- a/src/main.c +++ b/src/main.c @@ -71,9 +71,12 @@ rgb15(u32 red, u32 green, u32 blue ) { return (blue << 10) | (green << 5) | red; } -#define COLOR_RED rgb15(31, 0, 12) -#define COLOR_BLUE rgb15(2, 15, 30) -#define COLOR_CYAN rgb15(0, 30, 30) +#define COLOR_RED rgb15(31, 0, 12) +#define COLOR_BLUE rgb15(2, 15, 30) +#define COLOR_CYAN rgb15(0, 30, 30) +#define COLOR_GREY rgb15(10, 10, 10) +#define COLOR_BLACK rgb15(0, 0, 0) +#define COLOR_WHITE rgb15(28, 28, 28) // Using bd-font, an 8x8 bitmap font. static inline void @@ -165,6 +168,55 @@ draw_line(int x0, int y0, int x1, int y1, Color clr) { } } +static inline void +draw_rect(int x0, int y0, int x1, int y1, Color clr) { + if (x0 > x1) { + int tmp = x0; + x0 = x1; + x1 = tmp; + } + if (y0 > y1) { + int tmp = y0; + y0 = y1; + y1 = tmp; + } + int dx = x1 - x0; + int dy = y1 - y0; + for (size_t i = 0; i <= dx; ++i) { + int x = x0 + i; + FRAMEBUFFER[y0][x] = clr; + FRAMEBUFFER[y1][x] = clr; + } + for (size_t j = 0; j <= dy; ++j) { + int y = y0 + j; + FRAMEBUFFER[y][x0] = clr; + FRAMEBUFFER[y][x1] = clr; + } +} + +static inline void +draw_fill_rect(int x0, int y0, int x1, int y1, Color clr) { + if (x0 > x1) { + int tmp = x0; + x0 = x1; + x1 = tmp; + } + if (y0 > y1) { + int tmp = y0; + y0 = y1; + y1 = tmp; + } + int dx = x1 - x0; + int dy = y1 - y0; + for (size_t i = 0; i <= dx; ++i) { + for (size_t j = 0; j <= dy; ++j) { + int x = x0 + i; + int y = y0 + j; + FRAMEBUFFER[y][x] = clr; + } + } +} + static inline void wait_vsync() { while(DISP_VCOUNT >= 160); @@ -178,7 +230,23 @@ wait_vsync() { int main(void) { set_display_mode(DISP_MODE_3 | DISP_BG2); + // Rectangle drawing testing. + Color clr = COLOR_WHITE; + for (size_t j = 0; j < SCREEN_HEIGHT; j += 8) { + clr = clr == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE; + for (size_t i = 0; i < SCREEN_WIDTH; i += 8) { + clr = clr == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE; + draw_fill_rect(i, j, 0 + i + 7, j + 7, clr); + } + } + clr = COLOR_GREY; + for (size_t j = 8; j < SCREEN_HEIGHT - 8; j += 8) { + for (size_t i = 8; i < SCREEN_WIDTH - 8; i += 8) { + draw_rect(i, j, 0 + i + 8, j + 8, clr); + } + } + // Bresenham's testing put_text(8, 8, COLOR_RED, "Testing Bresenham's algorithm"); for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 5) { @@ -195,7 +263,6 @@ int main(void) { int y1 = SCREEN_HEIGHT - i; draw_line(x0, y0, x1, y1, COLOR_BLUE); } - for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 10) { int x0 = i; int y0 = 30; -- cgit v1.2.1