summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-04-14 21:48:47 +0200
committerBad Diode <bd@badd10de.dev>2023-04-15 18:29:26 +0200
commitdd009eb124cf929bee0233b7323b448319e25665 (patch)
tree056b6655fb02b96905bdb8186175083a72e90230
parentd63f3d01326e84bfd026690c0d03ed002b466332 (diff)
downloadgba-renderers-dd009eb124cf929bee0233b7323b448319e25665.tar.gz
gba-renderers-dd009eb124cf929bee0233b7323b448319e25665.zip
Add diagonal line drawing and tests
-rw-r--r--src/main.c13
-rw-r--r--src/renderer_m4.c24
2 files changed, 34 insertions, 3 deletions
diff --git a/src/main.c b/src/main.c
index 522ef70..96682f9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -36,6 +36,7 @@ WITH REGARD TO THIS SOFTWARE.
36 txt_printf("FLIP: %.9lu\n", flip_cycles);\ 36 txt_printf("FLIP: %.9lu\n", flip_cycles);\
37 txt_printf("CHR: %.9lu\n", test_chr_cycles);\ 37 txt_printf("CHR: %.9lu\n", test_chr_cycles);\
38 txt_printf("ICN: %.9lu\n", test_icn_cycles);\ 38 txt_printf("ICN: %.9lu\n", test_icn_cycles);\
39 txt_printf("LINE: %.9lu\n", test_lines_cycles);\
39 txt_render();\ 40 txt_render();\
40 txt_clear();\ 41 txt_clear();\
41 frame_counter++;\ 42 frame_counter++;\
@@ -47,6 +48,7 @@ WITH REGARD TO THIS SOFTWARE.
47 u32 test_clear_cycles = 0;\ 48 u32 test_clear_cycles = 0;\
48 u32 test_chr_cycles = 0;\ 49 u32 test_chr_cycles = 0;\
49 u32 test_icn_cycles = 0;\ 50 u32 test_icn_cycles = 0;\
51 u32 test_lines_cycles = 0;\
50 u32 flip_cycles = 0; 52 u32 flip_cycles = 0;
51 53
52void 54void
@@ -97,6 +99,16 @@ test_icn(void) {
97 } 99 }
98} 100}
99 101
102void
103test_lines(void) {
104 for (size_t i = 0; i < 20; i++) {
105 draw_line(0, i * 8, (30 * 8 - 1), ((20 - i) * 8 - 1), 5);
106 }
107 for (size_t i = 0; i < 30; i++) {
108 draw_line(i * 8, (20 * 8 - 1), ((30 - i) * 8 - 1), 0, 5);
109 }
110}
111
100int main(void) { 112int main(void) {
101 // Adjust system wait times. 113 // Adjust system wait times.
102 SYSTEM_WAIT = SYSTEM_WAIT_CARTRIDGE; 114 SYSTEM_WAIT = SYSTEM_WAIT_CARTRIDGE;
@@ -116,6 +128,7 @@ int main(void) {
116 bios_vblank_wait(); 128 bios_vblank_wait();
117 129
118 PROF(test_clear(), test_clear_cycles); 130 PROF(test_clear(), test_clear_cycles);
131 PROF(test_lines(), test_lines_cycles);
119 PROF(test_rect(), test_rect_cycles); 132 PROF(test_rect(), test_rect_cycles);
120 PROF(test_fill_rect(), test_fill_rect_cycles); 133 PROF(test_fill_rect(), test_fill_rect_cycles);
121 PROF(test_chr(), test_chr_cycles); 134 PROF(test_chr(), test_chr_cycles);
diff --git a/src/renderer_m4.c b/src/renderer_m4.c
index 21dc192..cbf3c3f 100644
--- a/src/renderer_m4.c
+++ b/src/renderer_m4.c
@@ -136,15 +136,33 @@ void
136draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) { 136draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
137 BOUNDCHECK_SCREEN(x0, y0); 137 BOUNDCHECK_SCREEN(x0, y0);
138 BOUNDCHECK_SCREEN(x1, y1); 138 BOUNDCHECK_SCREEN(x1, y1);
139 MAYBE_SWAP(x0, x1);
140 MAYBE_SWAP(y0, y1);
141 139
142 if (y0 == y1) { 140 if (y0 == y1) {
141 MAYBE_SWAP(x0, x1);
143 draw_hline(x0, x1, y0, clr); 142 draw_hline(x0, x1, y0, clr);
144 } else if (x0 == x1) { 143 } else if (x0 == x1) {
144 MAYBE_SWAP(y0, y1);
145 draw_vline(x0, y0, y1, clr); 145 draw_vline(x0, y0, y1, clr);
146 } else {
147 // Diagonal line.
148 int dx = x0 > x1 ? x0 - x1 : x1 - x0;
149 int dy = y0 > y1 ? y1 - y0 : y0 - y1;
150 int x_step = x0 < x1 ? 1 : -1;
151 int y_step = y0 < y1 ? 1 : -1;
152 int err = dx + dy;
153 while (!(x0 == x1 && y0 == y1)) {
154 draw_pixel(x0, y0, clr);
155 int diff = 2 * err;
156 if (diff >= dy) {
157 err += dy;
158 x0 += x_step;
159 }
160 if (diff <= dx) {
161 err += dx;
162 y0 += y_step;
163 }
164 }
146 } 165 }
147 // TODO: diagonal (bresengham)
148 screen_updated = true; 166 screen_updated = true;
149} 167}
150 168