Browse Source

Building version doing little.

Manoel Trapier 1 year ago
parent
commit
c3568539f9
5 changed files with 171 additions and 135 deletions
  1. BIN
      assets/pico8.ttf
  2. 9 128
      source/display.c
  3. 24 0
      source/include/colour.h
  4. 0 6
      source/include/display.h
  5. 138 1
      source/main.c

BIN
assets/pico8.ttf


+ 9 - 128
source/display.c

@@ -12,9 +12,9 @@
 #include <SDL.h>
 #include <SDL_ttf.h>
 
-#include <3dengine.h>
 #include <log.h>
 #include <display.h>
+#include <colour.h>
 
 /***********************************************************************************************************************
  * Variables
@@ -34,6 +34,14 @@ static TTF_Font *font = NULL;
 #define MAX(_a, _b) ((_a) < (_b)) ? (_b) : (_a)
 #define MIN(_a, _b) ((_a) > (_b)) ? (_b) : (_a)
 
+static inline void intSwap(int *a, int *b)
+{
+    int tmp = *a;
+    *a = *b;
+    *b = tmp;
+}
+
+
 bool initialiseWindow(int32_t width, int32_t height, bool fullScreen)
 {
     bool ret = false;
@@ -270,18 +278,6 @@ void clearFrameBuffer(colour_t colour)
     }
 }
 
-void clearZBuffer()
-{
-    int32_t x;
-    if (doZBuffer)
-    {
-        for (x = 0 ; x < windowWidth * windowHeight ; x++)
-        {
-                zBuffer[x] = 1.0;
-        }
-    }
-}
-
 void drawGrid(int spacing, colour_t colour)
 {
     int32_t x, y;
@@ -340,118 +336,3 @@ void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour)
         }
     }
 }
-
-static vec3_t barycentricWeights(vec2_t a, vec2_t b, vec2_t c, vec2_t p)
-{
-    vec2_t ab = vec2SubVectors(b, a);
-    vec2_t bc = vec2SubVectors(c, b);
-    vec2_t ac = vec2SubVectors(c, a);
-    vec2_t ap = vec2SubVectors(p, a);
-    vec2_t bp = vec2SubVectors(p, b);
-    vec3_t ret;
-
-    double areaTriangleABC = (ab.x * ac.y) - (ab.y * ac.x);
-
-    double alpha = ((bc.x * bp.y) - (bp.x * bc.y)) / areaTriangleABC;
-    double beta = ((ap.x * ac.y) - (ac.x * ap.y)) / areaTriangleABC;
-    double gamma = 1 - alpha - beta;
-
-    ret.x = alpha;
-    ret.y = beta;
-    ret.z = gamma;
-
-    return ret;
-}
-
-void drawZPixel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, colour_t colour)
-{
-    vec2_t pointP = { x, y };
-    vec2_t a2 = vec2FromVec4(a);
-    vec2_t b2 = vec2FromVec4(b);
-    vec2_t c2 = vec2FromVec4(c);
-
-    if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
-    {
-        return;
-    }
-
-    vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
-
-    double alpha = weights.x;
-    double beta = weights.y;
-    double gamma = weights.z;
-    double interpolatedReciprocalW;
-
-    interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
-
-    /* Inverse to get logical W invrementing going further to us */
-    interpolatedReciprocalW = 1 - interpolatedReciprocalW;
-    if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
-    {
-        drawPixel(x, y, colour);
-
-        /* Update Z Buffer */
-        zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
-    }
-}
-
-void drawTexel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, tex2_t ta, tex2_t tb, tex2_t tc, texture_t *texture)
-{
-    vec2_t pointP = { x, y };
-    vec2_t a2 = vec2FromVec4(a);
-    vec2_t b2 = vec2FromVec4(b);
-    vec2_t c2 = vec2FromVec4(c);
-
-    if ((x < 0) || (y < 0) || (x > windowWidth) ||(y > windowHeight))
-    {
-        return;
-    }
-
-    vec3_t weights = barycentricWeights(a2, b2, c2, pointP);
-
-    double alpha = weights.x;
-    double beta = weights.y;
-    double gamma = weights.z;
-    double interpolatedU, interpolatedV, interpolatedReciprocalW;
-    int32_t texX, texY;
-
-    interpolatedReciprocalW = (1 / a.w) * alpha + (1 / b.w) * beta + (1 / c.w) * gamma;
-
-    if (doPerspectiveCorrection)
-    {
-        interpolatedU = (ta.u / a.w) * alpha + (tb.u / b.w) * beta + (tc.u / c.w) * gamma;
-        interpolatedV = ((1 - ta.v) / a.w) * alpha + ((1 - tb.v) / b.w) * beta + ((1 - tc.v) / c.w) * gamma;
-
-
-        interpolatedU /= interpolatedReciprocalW;
-        interpolatedV /= interpolatedReciprocalW;
-    }
-    else
-    {
-        interpolatedU = ta.u * alpha + tb.u * beta + tc.u * gamma;
-        interpolatedV = (1 - ta.v) * alpha + (1 - tb.v) * beta + (1 - tc.v) * gamma;
-    }
-
-    texX = abs((int32_t)(interpolatedU * texture->width));
-    texY = abs((int32_t)(interpolatedV * texture->height));
-
-    texX = texX % texture->width;
-    texY = texY % texture->height;
-
-    if (doZBuffer)
-    {
-        /* Inverse to get logical W invrementing going further to us */
-        interpolatedReciprocalW = 1 - interpolatedReciprocalW;
-        if (interpolatedReciprocalW < zBuffer[(windowWidth * y) + x])
-        {
-            drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
-
-            /* Update Z Buffer */
-            zBuffer[(windowWidth * y) + x] = interpolatedReciprocalW;
-        }
-    }
-    else
-    {
-        drawPixel(x, y, texture->data[(texY * texture->width) + texX]);
-    }
-}

+ 24 - 0
source/include/colour.h

@@ -0,0 +1,24 @@
+/*
+ * 3D Engine 
+ * colour.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 15/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_COLOUR_H
+#define THREEDENGINE_SOURCE_INCLUDE_COLOUR_H
+
+/***********************************************************************************************************************
+ * Data types
+ **********************************************************************************************************************/
+typedef uint32_t colour_t;
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+#define MAKE_RGB(_r, _g, _b) ((0xFF000000) | ((_b & 0xFF) << 16) | ((_g & 0xFF) << 8) | ((_r & 0xFF)))
+#define MAKE_ARGB(_a, _r, _g, _b) (((_a & 0xFF) << 24) | ((_g & 0xFF) << 16) | ((_g & 0xFF) << 8) | ((_r & 0xFF)))
+
+#endif /* THREEDENGINE_SOURCE_INCLUDE_COLOUR_H */

+ 0 - 6
source/include/display.h

@@ -12,8 +12,6 @@
 #include <stdbool.h>
 #include <SDL.h>
 
-#include <vector.h>
-#include <texture.h>
 #include <colour.h>
 
 /***********************************************************************************************************************
@@ -42,7 +40,6 @@ void displayWindowClear();
 void displayWindowRender();
 
 /* --- Drawing functions --- */
-void clearZBuffer();
 void clearFrameBuffer(colour_t colour);
 
 void drawPixel(int32_t x, int32_t y, colour_t colour);
@@ -54,8 +51,5 @@ void drawHLine(int32_t x0, int32_t y, int32_t x1, colour_t colour);
 void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, colour_t colour);
 void drawText(int32_t x, int32_t y, colour_t colour, const char *format, ...);
 
-/* Other */
-void drawZPixel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, colour_t colour);
-void drawTexel(int32_t x, int32_t y, vec4_t a, vec4_t b, vec4_t c, tex2_t ta, tex2_t tb, tex2_t tc, texture_t *texture);
 
 #endif /* THREEDENGINE_SOURCE_INCLUDE_DISPLAY_H */

+ 138 - 1
source/main.c

@@ -6,9 +6,146 @@
  * Created by Manoël Trapier on 28/09/2022.
  */
 
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <sys/time.h>
 
-int main(int argc, char *arvg[])
+#include <SDL.h>
+
+#include <log.h>
+#include <display.h>
+
+bool isRunning = false;
+int previousFrameTime = 0;
+double deltaTime = 0;
+
+static double updateTime, renderTime, waitTime;
+static int windowHeight;
+static int windowWidth;
+
+uint32_t getMicroSecTime()
+{
+    struct timeval curTime;
+    gettimeofday(&curTime, NULL);
+    return ((curTime.tv_sec) * 1000 * 1000) + (curTime.tv_usec);
+}
+
+void processInput()
+{
+    SDL_Event event;
+    while(SDL_PollEvent(&event))
+    {
+        switch (event.type)
+        {
+        case SDL_QUIT:
+            isRunning = false;
+            break;
+
+        case SDL_KEYDOWN:
+            switch (event.key.keysym.sym)
+            {
+            case SDLK_ESCAPE: isRunning = false; break;
+
+            case SDLK_LEFT: break;
+            case SDLK_RIGHT: break;
+            case SDLK_UP:  break;
+            case SDLK_DOWN: break;
+
+            case SDLK_PAGEUP: break;
+            case SDLK_PAGEDOWN: break;
+            }
+            break;
+
+        default:
+            break;
+        }
+    }
+}
+
+void setup()
 {
 
+    isRunning = true;
+}
+
+void update()
+{
+    int i;
+    uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
+    uint32_t ticks = getMicroSecTime();
+
+    if (timeToWait <= FRAME_TARGET_TIME)
+    {
+        SDL_Delay(timeToWait);
+    }
+    deltaTime = (SDL_GetTicks() - previousFrameTime) / 1000.0;
+    previousFrameTime = SDL_GetTicks();
+
+    waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
+    ticks = getMicroSecTime();
+
+    /* [...] */
+
+    updateTime = (updateTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
+}
+
+void render()
+{
+    uint32_t ticks = getMicroSecTime();
+
+
+    renderFrameBuffer();
+
+    renderTime = (renderTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
+
+    drawText(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime);
+    drawText(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime);
+    drawText(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime);
+    drawText(5, 41, 0x11FF22, "FPS: %.1f | dT: %.3f", 1000 / (waitTime + updateTime + renderTime), deltaTime);
+
+    displayWindowRender();
+}
+
+int main(int argc, char *argv[])
+{
+    int param_i, i;
+    bool fullScreen = false;
+    MAX_DEBUG_LEVEL = TLOG_DEBUG;
+
+    Log(TLOG_ALWAYS, NULL, "Starting Voxel Engine (version %s)!", VERSION);
+
+    windowWidth = 320;
+    windowHeight = 240;
+
+    for (param_i = 1 ; (param_i < argc) && (argv[param_i][0] == '-') ; param_i++)
+    {
+        switch (argv[param_i][1])
+        {
+        default: exit(-1); /* Option not recognized */
+        case 'f': fullScreen = true; break;
+        case 'w': windowWidth = atoi(argv[++param_i]); break;
+        case 'h': windowHeight = atoi(argv[++param_i]); break;
+#ifdef DYNA_LOG_LEVEL
+        case 'l': MAX_DEBUG_LEVEL = atoi(argv[++param_i]); break;
+#endif
+        case '-': goto no_more_params; /* Could use break, but this is more clear */
+        }
+    }
+    no_more_params:
+
+    isRunning = initialiseWindow(windowWidth, windowHeight, fullScreen);
+
+    setup();
+
+    while (isRunning)
+    {
+        processInput();
+        update();
+        render();
+    }
+
+    destroyWindow();
+
     return 0;
 }