diff options
author | Bad Diode <bd@badd10de.dev> | 2021-04-15 16:00:56 +0200 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-04-15 16:00:56 +0200 |
commit | edee7f1f66e7df2110069057289210c6bfbd272f (patch) | |
tree | e6b62cf1126704376489ca2e78db83043be6d609 | |
parent | 07cc344c87306309316d30cb3fce1f6c1f15c0be (diff) | |
download | gba-experiments-edee7f1f66e7df2110069057289210c6bfbd272f.tar.gz gba-experiments-edee7f1f66e7df2110069057289210c6bfbd272f.zip |
Add rectangle drawing routines
-rw-r--r-- | src/main.c | 75 |
1 files changed, 71 insertions, 4 deletions
@@ -71,9 +71,12 @@ rgb15(u32 red, u32 green, u32 blue ) { | |||
71 | return (blue << 10) | (green << 5) | red; | 71 | return (blue << 10) | (green << 5) | red; |
72 | } | 72 | } |
73 | 73 | ||
74 | #define COLOR_RED rgb15(31, 0, 12) | 74 | #define COLOR_RED rgb15(31, 0, 12) |
75 | #define COLOR_BLUE rgb15(2, 15, 30) | 75 | #define COLOR_BLUE rgb15(2, 15, 30) |
76 | #define COLOR_CYAN rgb15(0, 30, 30) | 76 | #define COLOR_CYAN rgb15(0, 30, 30) |
77 | #define COLOR_GREY rgb15(10, 10, 10) | ||
78 | #define COLOR_BLACK rgb15(0, 0, 0) | ||
79 | #define COLOR_WHITE rgb15(28, 28, 28) | ||
77 | 80 | ||
78 | // Using bd-font, an 8x8 bitmap font. | 81 | // Using bd-font, an 8x8 bitmap font. |
79 | static inline void | 82 | static inline void |
@@ -166,6 +169,55 @@ draw_line(int x0, int y0, int x1, int y1, Color clr) { | |||
166 | } | 169 | } |
167 | 170 | ||
168 | static inline void | 171 | static inline void |
172 | draw_rect(int x0, int y0, int x1, int y1, Color clr) { | ||
173 | if (x0 > x1) { | ||
174 | int tmp = x0; | ||
175 | x0 = x1; | ||
176 | x1 = tmp; | ||
177 | } | ||
178 | if (y0 > y1) { | ||
179 | int tmp = y0; | ||
180 | y0 = y1; | ||
181 | y1 = tmp; | ||
182 | } | ||
183 | int dx = x1 - x0; | ||
184 | int dy = y1 - y0; | ||
185 | for (size_t i = 0; i <= dx; ++i) { | ||
186 | int x = x0 + i; | ||
187 | FRAMEBUFFER[y0][x] = clr; | ||
188 | FRAMEBUFFER[y1][x] = clr; | ||
189 | } | ||
190 | for (size_t j = 0; j <= dy; ++j) { | ||
191 | int y = y0 + j; | ||
192 | FRAMEBUFFER[y][x0] = clr; | ||
193 | FRAMEBUFFER[y][x1] = clr; | ||
194 | } | ||
195 | } | ||
196 | |||
197 | static inline void | ||
198 | draw_fill_rect(int x0, int y0, int x1, int y1, Color clr) { | ||
199 | if (x0 > x1) { | ||
200 | int tmp = x0; | ||
201 | x0 = x1; | ||
202 | x1 = tmp; | ||
203 | } | ||
204 | if (y0 > y1) { | ||
205 | int tmp = y0; | ||
206 | y0 = y1; | ||
207 | y1 = tmp; | ||
208 | } | ||
209 | int dx = x1 - x0; | ||
210 | int dy = y1 - y0; | ||
211 | for (size_t i = 0; i <= dx; ++i) { | ||
212 | for (size_t j = 0; j <= dy; ++j) { | ||
213 | int x = x0 + i; | ||
214 | int y = y0 + j; | ||
215 | FRAMEBUFFER[y][x] = clr; | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | |||
220 | static inline void | ||
169 | wait_vsync() { | 221 | wait_vsync() { |
170 | while(DISP_VCOUNT >= 160); | 222 | while(DISP_VCOUNT >= 160); |
171 | while(DISP_VCOUNT < 160); | 223 | while(DISP_VCOUNT < 160); |
@@ -178,7 +230,23 @@ wait_vsync() { | |||
178 | int main(void) { | 230 | int main(void) { |
179 | set_display_mode(DISP_MODE_3 | DISP_BG2); | 231 | set_display_mode(DISP_MODE_3 | DISP_BG2); |
180 | 232 | ||
233 | // Rectangle drawing testing. | ||
234 | Color clr = COLOR_WHITE; | ||
235 | for (size_t j = 0; j < SCREEN_HEIGHT; j += 8) { | ||
236 | clr = clr == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE; | ||
237 | for (size_t i = 0; i < SCREEN_WIDTH; i += 8) { | ||
238 | clr = clr == COLOR_WHITE ? COLOR_BLACK : COLOR_WHITE; | ||
239 | draw_fill_rect(i, j, 0 + i + 7, j + 7, clr); | ||
240 | } | ||
241 | } | ||
242 | clr = COLOR_GREY; | ||
243 | for (size_t j = 8; j < SCREEN_HEIGHT - 8; j += 8) { | ||
244 | for (size_t i = 8; i < SCREEN_WIDTH - 8; i += 8) { | ||
245 | draw_rect(i, j, 0 + i + 8, j + 8, clr); | ||
246 | } | ||
247 | } | ||
181 | 248 | ||
249 | // Bresenham's testing | ||
182 | put_text(8, 8, COLOR_RED, "Testing Bresenham's algorithm"); | 250 | put_text(8, 8, COLOR_RED, "Testing Bresenham's algorithm"); |
183 | 251 | ||
184 | for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 5) { | 252 | for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 5) { |
@@ -195,7 +263,6 @@ int main(void) { | |||
195 | int y1 = SCREEN_HEIGHT - i; | 263 | int y1 = SCREEN_HEIGHT - i; |
196 | draw_line(x0, y0, x1, y1, COLOR_BLUE); | 264 | draw_line(x0, y0, x1, y1, COLOR_BLUE); |
197 | } | 265 | } |
198 | |||
199 | for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 10) { | 266 | for (size_t i = 70; i <= SCREEN_WIDTH - 70; i += 10) { |
200 | int x0 = i; | 267 | int x0 = i; |
201 | int y0 = 30; | 268 | int y0 = 30; |