Browse Source

trying to make FP working. Not doing a great job up so far ;)

Manoel Trapier 1 year ago
parent
commit
d53d677e93
2 changed files with 50 additions and 43 deletions
  1. 3 3
      source/include/fpmath.h
  2. 47 40
      source/main.c

+ 3 - 3
source/include/fpmath.h

@@ -18,14 +18,14 @@
 /*** Setting for the Fixed Point numbers. ***/
 typedef int32_t fp_num_t; /**< The base type used for the FP number */
 /* These defines should not be used outside of this header file */
-#define FP_DECIMAL_BITS (8) /**< Number of bit used for the decimal part */
+#define FP_DECIMAL_BITS (16) /**< Number of bit used for the decimal part */
 #define FP_DIVISOR (1 << FP_DECIMAL_BITS)
 
 #define FP_GET_INT(_fp) ((_fp) / FP_DIVISOR)
 #define FP_SET_INT(_val) (fp_num_t)((_val) * FP_DIVISOR)
 
-//#define FP_GET_FRAC(_val) (_val)
-//#define FP_SET_FLT(_val) FP_SET_INT(_val) | 1
+#define FP_GET_FRAC(_val) (fp_num_t)(_val - FP_GET_INT(_val))
+#define FP_SET_FLT(_val) (fp_num_t)(floor((_val) * FP_DIVISOR))
 
 #define FP_MUL(_a, _b) (fp_num_t)(((int64_t)(_a) * (int64_t)(_b)) / FP_DIVISOR)
 #define FP_DIV(_a, _b) (fp_num_t)(((int64_t)(_a) * FP_DIVISOR) / (_b))

+ 47 - 40
source/main.c

@@ -23,19 +23,19 @@ int previousFrameTime = 0;
 double deltaTime = 0;
 
 static double updateTime, renderTime, waitTime;
-static int windowHeight;
-static int windowWidth;
+static int32_t windowHeight;
+static int32_t windowWidth;
 static texture_t heightMap;
 static texture_t colourMap;
-static fp_num_t *yBuffer;
+static int32_t *yBuffer;
 
-static fp_num_t playerHeight = FP_SET_INT(70);
+static int32_t playerHeight = 70;
 static fp_num_t playerX = FP_SET_INT(444);
 static fp_num_t playerY = FP_SET_INT(299);
-static fp_num_t playerRot = FP_SET_INT(0);
-static fp_num_t cameraHorizon = FP_SET_INT(55);
-static fp_num_t scaleFactor = FP_SET_INT(90);
-static fp_num_t renderDistance = FP_SET_INT(300);
+static int32_t playerRot = 0;
+static int32_t cameraHorizon = 55;
+static int32_t scaleFactor = 90;
+static int32_t renderDistance = 300;
 
 uint32_t getMicroSecTime()
 {
@@ -47,8 +47,8 @@ uint32_t getMicroSecTime()
 void processInput()
 {
     SDL_Event event;
-    double direction = 1;
-    double dx, dy;
+    double direction = FP_GET_INT(1);
+    fp_num_t dx, dy;
 
     while(SDL_PollEvent(&event))
     {
@@ -63,31 +63,36 @@ void processInput()
             {
             case SDLK_ESCAPE: isRunning = false; break;
 
-            case SDLK_LEFT: playerRot = playerRot + 90 * deltaTime; break;
-            case SDLK_RIGHT: playerRot = playerRot - 90 * deltaTime; break;
+            case SDLK_LEFT: playerRot = playerRot + 1; break;
+            case SDLK_RIGHT: playerRot = playerRot - 1; break;
 
             case SDLK_DOWN:
-                direction = -1;
+                direction = FP_GET_INT(-1);
                 /* fall-trough */
 
             case SDLK_UP:
-                dx = cos(playerRot) * 75 * deltaTime;
-                dy = sin(playerRot) * 75 * deltaTime;
-                playerX += (dx * direction);
-                playerY += (dy * direction);
+                dx = FP_MUL(FP_MUL(fpcos(playerRot), 75), FP_SET_FLT(deltaTime));
+                dy = FP_MUL(FP_MUL(fpsin(playerRot), 75), FP_SET_FLT(deltaTime));
+                playerX += FP_MUL(dx, direction);
+                playerY += FP_MUL(dy, direction);
                 break;
 
-            case SDLK_PAGEUP: playerHeight = playerHeight + 50 * deltaTime; break;
-            case SDLK_PAGEDOWN: playerHeight = playerHeight - 50 * deltaTime; break;
+            case SDLK_KP_4: playerX -= FP_SET_INT(1); break;
+            case SDLK_KP_6: playerX += FP_SET_INT(1); break;
+            case SDLK_KP_8: playerY += FP_SET_INT(1); break;
+            case SDLK_KP_2: playerY -= FP_SET_INT(1); break;
 
-            case SDLK_HOME: cameraHorizon = cameraHorizon + 50 * deltaTime; break;
-            case SDLK_END: cameraHorizon = cameraHorizon - 50 * deltaTime; break;
+            case SDLK_PAGEUP: playerHeight = playerHeight + 5; break;
+            case SDLK_PAGEDOWN: playerHeight = playerHeight - 5; break;
 
-            case SDLK_KP_MULTIPLY: scaleFactor = scaleFactor + 30 * deltaTime; break;
-            case SDLK_KP_DIVIDE: scaleFactor = scaleFactor - 30 * deltaTime; break;
+            case SDLK_HOME: cameraHorizon = cameraHorizon + 5; break;
+            case SDLK_END: cameraHorizon = cameraHorizon - 5; break;
 
-            case SDLK_KP_PLUS: renderDistance = renderDistance + 30 * deltaTime; break;
-            case SDLK_KP_MINUS: renderDistance = renderDistance - 30 * deltaTime; break;
+            case SDLK_KP_MULTIPLY: scaleFactor = scaleFactor + 3; break;
+            case SDLK_KP_DIVIDE: scaleFactor = scaleFactor - 3; break;
+
+            case SDLK_KP_PLUS: renderDistance = renderDistance + 3; break;
+            case SDLK_KP_MINUS: renderDistance = renderDistance - 3; break;
             }
             break;
 
@@ -102,7 +107,7 @@ void setup()
     loadTextureDateFromPng(&heightMap, "assets/D1.png");
     loadTextureDateFromPng(&colourMap, "assets/C1W.png");
 
-    yBuffer = (fp_num_t *)calloc(windowWidth, sizeof(fp_num_t));
+    yBuffer = (int32_t *)calloc(windowWidth, sizeof(int32_t));
 
     isRunning = true;
 }
@@ -127,7 +132,7 @@ void update()
     updateTime = (updateTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
 }
 
-void renderVoxels(tex2_t *p, fp_num_t angle, fp_num_t height, fp_num_t horizon, fp_num_t scaleHeight, fp_num_t renderDistance)
+void renderVoxels(tex2_t *p, int32_t angle, int32_t height, int32_t horizon, int32_t scaleHeight, int32_t renderDistance)
 {
     fp_num_t angleSin = fpsin(angle);
     fp_num_t angleCos = fpcos(angle);
@@ -139,7 +144,7 @@ void renderVoxels(tex2_t *p, fp_num_t angle, fp_num_t height, fp_num_t horizon,
         yBuffer[i] = FP_SET_INT(windowHeight);
     }
 
-    while(z < renderDistance)
+    while( FP_GET_INT(z) < renderDistance)
     {
         tex2_t pleft;
         pleft.u = -FP_MUL(angleCos, z) - FP_MUL(angleSin, z) + p->u;
@@ -155,15 +160,16 @@ void renderVoxels(tex2_t *p, fp_num_t angle, fp_num_t height, fp_num_t horizon,
 
         for(i = 0; i < windowWidth; i++)
         {
-            fp_num_t voxelHeight = height - FP_SET_INT(getTextureColour(&heightMap, &pleft));
-            voxelHeight = FP_DIV(voxelHeight, z);
-            voxelHeight = FP_MUL(voxelHeight, scaleHeight);
+            int32_t voxelHeight2 = getTextureColour(&heightMap, &pleft);
+            voxelHeight2 = height - voxelHeight2;
+            fp_num_t temp = FP_MUL(FP_DIV(FP_SET_INT(1), z), FP_SET_INT(scaleHeight));
+            voxelHeight2 = FP_GET_INT(FP_MUL(temp, FP_SET_INT(voxelHeight2)));
+            int32_t heightOnScreen = voxelHeight2 + horizon;
 
-            fp_num_t heightOnScreen = voxelHeight + horizon;
-            colour_t voxelColour = getTextureColour(&colourMap, &pleft);
-            if (heightOnScreen < yBuffer[i])
+            if (heightOnScreen > 0 && heightOnScreen < yBuffer[i])
             {
-                drawVLine(i, FP_GET_INT(yBuffer[i]), FP_GET_INT(heightOnScreen), voxelColour);
+                colour_t voxelColour = getTextureColour(&colourMap, &pleft);
+                drawVLine(i, yBuffer[i], heightOnScreen, voxelColour);
                 yBuffer[i] = heightOnScreen;
             }
             pleft.u += dx;
@@ -171,10 +177,11 @@ void renderVoxels(tex2_t *p, fp_num_t angle, fp_num_t height, fp_num_t horizon,
         }
 
         z += dz;
+
 #ifdef USE_FLOATING_POINT
         dz += 0.01;
 #else
-        dz += (fp_num_t)(0.05 * FP_DIVISOR);
+        dz += 1; // (fp_num_t)(0.1 * FP_DIVISOR);
 #endif
     }
 }
@@ -202,10 +209,10 @@ void render()
 #else
     drawText(5, 53, 0x11FF22, "P: (%d;%d) | PH: %d | CH: %d | SF: %d | RD: %d", FP_GET_INT(playerX),
              FP_GET_INT(playerY),
-             FP_GET_INT(playerHeight),
-             FP_GET_INT(cameraHorizon),
-             FP_GET_INT(scaleFactor),
-             FP_GET_INT(renderDistance));
+             playerHeight,
+             cameraHorizon,
+             scaleFactor,
+             renderDistance);
 #endif
 
     displayWindowRender();