summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-04-17 08:32:44 +0200
committerBad Diode <bd@badd10de.dev>2023-04-17 08:32:44 +0200
commitaa04adf0d2068fb1ef96ac64edc07a92fedf6025 (patch)
tree84af1f2fe2da736a03d8c9b49609992b91d91554
parent7bd95db63bfd7e16247ce774c124806e0cf7e1fb (diff)
downloadgba-renderers-aa04adf0d2068fb1ef96ac64edc07a92fedf6025.tar.gz
gba-renderers-aa04adf0d2068fb1ef96ac64edc07a92fedf6025.zip
Add back line drawing with bresenham
-rw-r--r--src/renderer_m0.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/renderer_m0.c b/src/renderer_m0.c
index 18d1c5d..2cb7675 100644
--- a/src/renderer_m0.c
+++ b/src/renderer_m0.c
@@ -176,8 +176,26 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
176 MAYBE_SWAP(y0, y1); 176 MAYBE_SWAP(y0, y1);
177 draw_vline(x0, y0, y1, clr); 177 draw_vline(x0, y0, y1, clr);
178 } else { 178 } else {
179 // TODO 179#if 1
180 // // Diagonal line. 180 // Diagonal line.
181 int dx = x0 > x1 ? x0 - x1 : x1 - x0;
182 int dy = y0 > y1 ? y1 - y0 : y0 - y1;
183 int x_step = x0 < x1 ? 1 : -1;
184 int y_step = y0 < y1 ? 1 : -1;
185 int err = dx + dy;
186 while (!(x0 == x1 && y0 == y1)) {
187 draw_pixel(x0, y0, clr);
188 int diff = 2 * err;
189 if (diff >= dy) {
190 err += dy;
191 x0 += x_step;
192 }
193 if (diff <= dx) {
194 err += dx;
195 y0 += y_step;
196 }
197 }
198#else
181 // int dx = x0 > x1 ? x0 - x1 : x1 - x0; 199 // int dx = x0 > x1 ? x0 - x1 : x1 - x0;
182 // int dy = y0 > y1 ? y0 - y1 : y1 - y0; 200 // int dy = y0 > y1 ? y0 - y1 : y1 - y0;
183 // int x_step = x0 > x1 ? -1 : 1; 201 // int x_step = x0 > x1 ? -1 : 1;
@@ -214,6 +232,7 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
214 // addr += y_step; 232 // addr += y_step;
215 // } 233 // }
216 // } 234 // }
235#endif
217 } 236 }
218 237
219 // // Find row positions for the given x/y coordinates. 238 // // Find row positions for the given x/y coordinates.
@@ -322,6 +341,12 @@ draw_rect(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
322 draw_vline(x0, y0, y1, clr); 341 draw_vline(x0, y0, y1, clr);
323 draw_vline(x1, y0, y1, clr); 342 draw_vline(x1, y0, y1, clr);
324#else 343#else
344 // NOTE: This is the specialized version trying to update 2 pixels at
345 // a time. As of writing this it is slightly slower than using
346 // 4 draw_hline/vline functions. The horizontal drawing part seems
347 // marginally faster but the vertical one is slower. Potentially the
348 // vertical part could be optimized or just use 2 draw_vline instead. The
349 // perf improvements seem marginal enough that it doesn't seem worth it.
325 BOUNDCHECK_SCREEN(x0, y0); 350 BOUNDCHECK_SCREEN(x0, y0);
326 BOUNDCHECK_SCREEN(x1, y1); 351 BOUNDCHECK_SCREEN(x1, y1);
327 MAYBE_SWAP(x0, x1); 352 MAYBE_SWAP(x0, x1);