JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
* float.h (fwrap, fclip): new file.
authorJoshua Grams <josh@qualdan.com>
Mon, 5 Mar 2007 12:33:24 +0000 (12:33 +0000)
committerJoshua Grams <josh@qualdan.com>
Mon, 5 Mar 2007 12:33:24 +0000 (12:33 +0000)
Makefile
dust.c
float.h [new file with mode: 0644]
main.c

index 3260367..92401ad 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -67,11 +67,11 @@ font.o: font.h
 
 args.o: args.h
 
-dust.o: globals.h dust.h mt.h
+dust.o: globals.h dust.h float.h mt.h
 
 file.o: file.h common.h
 
-main.o: args.h common.h dust.h file.h globals.h mt.h rocks.h score.h sprite.h sound.h
+main.o: args.h common.h dust.h file.h float.h globals.h mt.h rocks.h score.h sprite.h sound.h
 
 mt.o: mt.h
 
diff --git a/dust.c b/dust.c
index 96a5805..4eeeb8b 100644 (file)
--- a/dust.c
+++ b/dust.c
@@ -4,6 +4,7 @@
 #include "config.h"
 #include "globals.h"
 #include "dust.h"
+#include "float.h"
 #include "mt.h"
 
 struct dust_mote {
@@ -13,9 +14,6 @@ struct dust_mote {
 
 struct dust_mote motes[N_DUST_MOTES];
 
-// lower bound to ensure that we don't lose precision when wrapping.
-static float zero;
-
 void
 init_dust(void)
 {
@@ -27,8 +25,6 @@ init_dust(void)
                b = (MAX_DUST_DEPTH - motes[i].z) * 255.0 / MAX_DUST_DEPTH;
                motes[i].color = SDL_MapRGB(surf_screen->format, b, b, b);
        }
-
-       zero = pow(-10, ceil(log10(XSIZE)) - FLT_DIG);
 }
 
 void
@@ -40,12 +36,10 @@ move_dust(float ticks)
        
        for(i=0; i<N_DUST_MOTES; i++) {
                motes[i].x -= xscroll / (1.3 + motes[i].z);
-               if(motes[i].x >= XSIZE) motes[i].x -= XSIZE;
-               else if(motes[i].x < zero) motes[i].x += XSIZE;
+               motes[i].x = fwrap(motes[i].x, XSIZE);
 
                motes[i].y -= yscroll / (1.3 + motes[i].z);
-               if(motes[i].y >= YSIZE) motes[i].y -= YSIZE;
-               else if(motes[i].y < zero) motes[i].y += YSIZE;
+               motes[i].y = fwrap(motes[i].y, YSIZE);
        }
 }
 
diff --git a/float.h b/float.h
new file mode 100644 (file)
index 0000000..58f6616
--- /dev/null
+++ b/float.h
@@ -0,0 +1,23 @@
+#ifndef VOR_FLOAT_H
+#define VOR_FLOAT_H
+
+#include <math.h>
+
+static inline int
+fclip(float f, float max)
+{
+       return f < 0 || (float)f >= (float)max;
+}
+
+static inline float
+fwrap(float f, float max)
+{
+       if((float)f >= (float)max) f = (float)f - (float)max;
+       else if(f < 0) {
+               f += max;
+               if((float)f >= (float)max) f = nextafterf(f, 0);
+       }
+       return f;
+}
+
+#endif // VOR_FLOAT_H
diff --git a/main.c b/main.c
index 72f3cfd..88d207a 100644 (file)
--- a/main.c
+++ b/main.c
@@ -34,6 +34,7 @@
 #include "config.h"
 #include "dust.h"
 #include "file.h"
+#include "float.h"
 #include "globals.h"
 #include "mt.h"
 #include "rocks.h"
@@ -192,7 +193,7 @@ move_bang_dots(float ticks)
                // move and clip
                bdot[i].x += (bdot[i].dx - screendx)*ticks;
                bdot[i].y += (bdot[i].dy - screendy)*ticks;
-               if(bdot[i].x < 0 || bdot[i].x >= (XSIZE - 0.000001) || bdot[i].y < 0 || bdot[i].y >= (YSIZE - 0.000001)) {
+               if(fclip(bdot[i].x, XSIZE) || fclip(bdot[i].y, YSIZE)) {
                        bdot[i].active = 0;
                        continue;
                }
@@ -306,7 +307,7 @@ move_engine_dots(float ticks) {
                edot[i].x += (edot[i].dx - screendx)*ticks;
                edot[i].y += (edot[i].dy - screendy)*ticks;
                edot[i].life -= t_frame*3;
-               if(edot[i].life < 0 || edot[i].x<0 || edot[i].x >= (XSIZE - 0.000001) || edot[i].y < 0 || edot[i].y >= (YSIZE - 0.000001)) {
+               if(edot[i].life < 0 || fclip(edot[i].x, XSIZE) || fclip(edot[i].y, YSIZE)) {
                        edot[i].active = 0;
                        continue;
                }