summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2023-04-17 12:08:13 +0200
committerBad Diode <bd@badd10de.dev>2023-04-17 12:08:13 +0200
commit2c8000b2bd04d31e9de356ca60bafdf648dd420b (patch)
tree7d2380d93576a4bba0018a0b382aaf17277081bb
parent579f18c54565728c6efb50edee40ca27f86d2fcc (diff)
downloadgba-renderers-2c8000b2bd04d31e9de356ca60bafdf648dd420b.tar.gz
gba-renderers-2c8000b2bd04d31e9de356ca60bafdf648dd420b.zip
Minor fp math cleanup
-rw-r--r--src/gba/gba.h1
-rw-r--r--src/renderer_m0.c22
2 files changed, 12 insertions, 11 deletions
diff --git a/src/gba/gba.h b/src/gba/gba.h
index d607eee..99eb1f7 100644
--- a/src/gba/gba.h
+++ b/src/gba/gba.h
@@ -661,6 +661,7 @@ wait_vsync(void) {
661#define LEN(ARR) (sizeof(ARR) / sizeof((ARR)[0])) 661#define LEN(ARR) (sizeof(ARR) / sizeof((ARR)[0]))
662 662
663// Fixed-point arithmetic for (i.P) numbers. 663// Fixed-point arithmetic for (i.P) numbers.
664#define FP_NUM(A,P) ((A) << (P))
664#define FP_MUL(A,B,P) (((A) * (B)) >> (P)) 665#define FP_MUL(A,B,P) (((A) * (B)) >> (P))
665#define FP_DIV(A,B,P) (((A) << (P)) / (B)) 666#define FP_DIV(A,B,P) (((A) << (P)) / (B))
666#define FP_LERP(Y0,Y1,X,P) ((Y0) + FP_MUL((X), ((Y1) - (Y0)), P)) 667#define FP_LERP(Y0,Y1,X,P) ((Y0) + FP_MUL((X), ((Y1) - (Y0)), P))
diff --git a/src/renderer_m0.c b/src/renderer_m0.c
index 36ed01b..904c2a1 100644
--- a/src/renderer_m0.c
+++ b/src/renderer_m0.c
@@ -179,19 +179,19 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
179 draw_vline(x0, y0, y1, clr); 179 draw_vline(x0, y0, y1, clr);
180 } else { 180 } else {
181#if 1 181#if 1
182 int precision_bits = 6; 182 int fp_bit = 6;
183 int precision_one = (1 << precision_bits); 183 int fp_one = FP_NUM(1, fp_bit);
184 int precision_half = precision_one >> 1; 184 int fp_half = fp_one >> 1;
185 int dx = x0 > x1 ? x0 - x1 : x1 - x0; 185 int dx = x0 > x1 ? x0 - x1 : x1 - x0;
186 int dy = y0 > y1 ? y0 - y1 : y1 - y0; 186 int dy = y0 > y1 ? y0 - y1 : y1 - y0;
187 int frac_x = x0 > x1 ? ((x0 - x1) << precision_bits) : ((x1 - x0) << precision_bits); 187 int frac_x = x0 > x1 ? FP_NUM(x0 - x1, fp_bit) : FP_NUM(x1 - x0, fp_bit);
188 int frac_y = y0 > y1 ? ((y0 - y1) << precision_bits) : ((y1 - y0) << precision_bits); 188 int frac_y = y0 > y1 ? FP_NUM(y0 - y1, fp_bit) : FP_NUM(y1 - y0, fp_bit);
189 int x_step = x0 > x1 ? -1 : 1; 189 int x_step = x0 > x1 ? -1 : 1;
190 int y_step = y0 > y1 ? -1 : 1; 190 int y_step = y0 > y1 ? -1 : 1;
191 int dxf = (dx << precision_bits); 191 int dxf = (dx << fp_bit);
192 int dyf = (dy << precision_bits); 192 int dyf = (dy << fp_bit);
193 if (dx >= dy) { 193 if (dx >= dy) {
194 int distance = (frac_y - precision_one) * dx - (frac_x - precision_half) * dy; 194 int distance = (frac_y - fp_one) * dx - (frac_x - fp_half) * dy;
195 for (int i = 0; i <= dx; i++) { 195 for (int i = 0; i <= dx; i++) {
196 if (distance >= 0) { 196 if (distance >= 0) {
197 distance -= 2 * dxf; 197 distance -= 2 * dxf;
@@ -202,7 +202,7 @@ draw_line(size_t x0, size_t y0, size_t x1, size_t y1, u8 clr) {
202 x0 += x_step; 202 x0 += x_step;
203 } 203 }
204 } else { 204 } else {
205 int distance = (frac_x - precision_one) * dy - (frac_y - precision_half) * dx; 205 int distance = (frac_x - fp_one) * dy - (frac_y - fp_half) * dx;
206 for (int i = 0; i <= dy; i++) { 206 for (int i = 0; i <= dy; i++) {
207 draw_pixel(x0, y0, clr); 207 draw_pixel(x0, y0, clr);
208 if (distance >= 0) { 208 if (distance >= 0) {