Godzil 3 lat temu
rodzic
commit
fee03ef464
3 zmienionych plików z 129 dodań i 4 usunięć
  1. 85 0
      source/array.c
  2. 30 0
      source/include/array.h
  3. 14 4
      source/main.c

+ 85 - 0
source/array.c

@@ -0,0 +1,85 @@
+/*
+ * 3D Engine 
+ * array.c: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 04/03/2021.
+ */
+
+#include <stdlib.h>
+#include <stddef.h>
+
+#include <array.h>
+
+struct array_t
+{
+    uint32_t allocatedSize;
+    uint32_t usedSize;
+    char data;
+};
+#define TO_ARRAY_T(_array) (struct array_t *) ((void*)(_array) - offsetof(struct array_t, data))
+
+void *arrayCheckSize(void *array, uint32_t count, size_t typeSize)
+{
+    struct array_t *base = TO_ARRAY_T(array);
+    uint32_t arrayLength;
+    if (array == NULL)
+    {
+        /* Need to create the array */
+        base = (struct array_t*)malloc(typeSize * count + sizeof(struct array_t));
+        base->allocatedSize = count;
+        base->usedSize = count;
+        arrayLength = count;
+        return &(base->data);
+    }
+    else if ((base->usedSize + count) <= base->allocatedSize)
+    {
+        /* Have enough space inside the array */
+        base->usedSize += count;
+        return array;
+    }
+    else
+    {
+        /* The array is not big enough */
+        uint32_t newSize = base->allocatedSize;
+        while(newSize < (base->usedSize + count))
+        {
+            newSize *= 2;
+        }
+
+        base = (struct array_t*)realloc(base, newSize * typeSize + sizeof(struct array_t));
+        base->allocatedSize = newSize;
+        base->usedSize += count;
+
+        return &(base->data);
+    }
+}
+
+uint32_t arrayGetSize(void *array)
+{
+    struct array_t *base = TO_ARRAY_T(array);
+    if ((array != NULL) && (base != NULL))
+    {
+        return base->usedSize;
+    }
+    return 0;
+}
+
+void arrayEmpty(void *array)
+{
+    struct array_t *base = TO_ARRAY_T(array);
+    if ((array != NULL) && (base != NULL))
+    {
+        base->usedSize = 0;
+    }
+}
+
+void arrayFree(void *array)
+{
+    struct array_t *base = TO_ARRAY_T(array);
+    if ((array != NULL) && (base != NULL))
+    {
+        free(base);
+    }
+}

+ 30 - 0
source/include/array.h

@@ -0,0 +1,30 @@
+/*
+ * 3D Engine 
+ * array.h: 
+ * Based on pikuma.com 3D software renderer in C
+ * Copyright (c) 2021 986-Studio. All rights reserved.
+ *
+ * Created by Manoël Trapier on 04/03/2021.
+ */
+
+#ifndef THREEDENGINE_SOURCE_INCLUDE_ARRAY_H
+#define THREEDENGINE_SOURCE_INCLUDE_ARRAY_H
+
+#include <stdint.h>
+
+/***********************************************************************************************************************
+ * Prototypes
+ **********************************************************************************************************************/
+#define arrayAdd(_array, _data) do { \
+        (_array) = arrayCheckSize((_array), 1, sizeof(_data)); \
+        uint32_t lenght = arrayGetSize((_array));  \
+        (_array)[lenght - 1] = (_data); \
+} while(0)
+
+void *arrayCheckSize(void *array, uint32_t count, size_t typeSize);
+uint32_t arrayGetSize(void *array);
+void arrayEmpty(void *array);
+void arrayFree(void *array);
+
+
+#endif /* THREEDENGINE_SOURCE_INCLUDE_ARRAY_H */

+ 14 - 4
source/main.c

@@ -15,6 +15,7 @@
 
 #include <log.h>
 
+#include <array.h>
 #include <display.h>
 #include <vector.h>
 #include <mesh.h>
@@ -35,7 +36,7 @@ vec3_t cubeRotation =
     .z = 0,
 };
 
-triangle_t projectedTriangles[N_MESH_FACES];
+triangle_t *projectedTriangles = NULL;
 
 double fovFactor = 640;
 
@@ -118,8 +119,11 @@ void update()
     cubeRotation.y += 0.01;
     cubeRotation.z += 0.01;
 
+    arrayEmpty(projectedTriangles);
+
     for(i = 0; i < N_MESH_FACES; i++)
     {
+        triangle_t currentTriangle;
         vec3_t vertices[3] =
         {
             meshVertices[meshFaces[i].a - 1],
@@ -142,8 +146,12 @@ void update()
             projected.x += (windowWidth  / 2);
             projected.y += (windowHeight / 2);
 
-            projectedTriangles[i].points[j] = projected;
+
+
+            currentTriangle.points[j] = projected;
         }
+
+        arrayAdd(projectedTriangles, currentTriangle);
     }
 }
 
@@ -152,9 +160,9 @@ void render()
     int i;
     SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
     SDL_RenderClear(renderer);
+    int triangleCount = arrayGetSize(projectedTriangles);
 
-
-    for(i = 0; i < N_MESH_FACES; i++)
+    for(i = 0; i < triangleCount; i++)
     {
         drawTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
                      projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
@@ -204,6 +212,8 @@ no_more_params:
         render();
     }
 
+    arrayFree(projectedTriangles);
+
     destroy_window();
 
     return 0;