]> git.lizzy.rs Git - shadowclad.git/commitdiff
Convert data counts and indices to size_t
authoroutfrost <kotlet.bahn@gmail.com>
Thu, 30 Apr 2020 00:36:41 +0000 (02:36 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Thu, 30 Apr 2020 00:36:41 +0000 (02:36 +0200)
src/engine/asset.c
src/engine/asset.h
src/engine/render.c
src/game/level.c
src/game/level.h

index fafe81cc2aefb0fd846b21ce6728f870054d878c..75e96f46d9c53885fcce1d569e993971bb50ed26 100644 (file)
@@ -3,6 +3,7 @@
 #include <assimp/postprocess.h>
 
 #include "asset.h"
+#include "assimp_types.h"
 #include "logger.h"
 #include "tga.h"
 
@@ -20,6 +21,8 @@ const Asset3D* importAsset(const char* path) {
        
        const unsigned int numMeshes = scene->mNumMeshes;
        const unsigned int numMaterials = scene->mNumMaterials;
+
+       // TODO Consider assets with some arrays empty, and prevent zero mallocs
        
        Asset3D* asset = malloc(sizeof(Asset3D));
        asset->numMeshes = numMeshes;
@@ -64,8 +67,7 @@ const Asset3D* importAsset(const char* path) {
                        const unsigned int numIndices = aiFace.mNumIndices;
                        
                        Face face = { .numIndices = numIndices,
-                                     .indices = malloc(numIndices
-                                                       * sizeof(unsigned int)) };
+                                     .indices = malloc(numIndices * sizeof(size_t)) };
                        
                        for (unsigned int i = 0; i < numIndices; ++i) {
                                face.indices[i] = aiFace.mIndices[i];
@@ -92,11 +94,11 @@ const Asset3D* importAsset(const char* path) {
                                         0,
                                         &originalTexturePath,
                                         NULL, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
-                       const char* textureFile = replaceFileExtension(originalTexturePath, ".tga");
-                       const size_t textureFileLength = strlen(textureFile);
-                       char* texturePath = malloc(strlen("assets/") + textureFileLength + 1);
+                       const char* textureFilename = replaceFileExtension(originalTexturePath, ".tga");
+                       const size_t textureFilenameLength = strlen(textureFilename);
+                       char* texturePath = malloc(strlen("assets/") + textureFilenameLength + 1);
                        strcpy(texturePath, "assets/");
-                       strncat(texturePath, textureFile, textureFileLength);
+                       strncat(texturePath, textureFilename, textureFilenameLength);
                        
                        TgaImage* textureImage = readTga(texturePath);
                        if (textureImage == NULL) {
@@ -130,7 +132,7 @@ static const AiScene* importScene(const char* path) {
        if (scene == NULL) {
                logError("Failed to import asset from %s", path);
        }
-       else if ((scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) == AI_SCENE_FLAGS_INCOMPLETE) {
+       else if (scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE) {
                logError("Incomplete scene imported from %s", path);
                aiReleaseImport(scene);
                scene = NULL;
@@ -144,7 +146,8 @@ static Vector3D convertAiVector3D(AiVector3D vect) {
                            .z = vect.z };
 }
 
-/** BUGS
+/**
+ * BUGS
  * The following function will not work properly with texture
  * file names (excluding directory part) beginning with '.'
  */
index 9ca1d80f7cdb3a52d456da58d7f5b8b95130ad58..8a68ca07deb4a490ba02df3c43ffe1513233549c 100644 (file)
@@ -1,9 +1,9 @@
 #ifndef ASSET_H_
 #define ASSET_H_
 
-#include <GL/gl.h>
+#include <stddef.h>
 
-#include "assimp_types.h"
+#include <GL/gl.h>
 
 #include "geometry.h"
 
@@ -13,25 +13,25 @@ typedef struct Face Face;
 typedef struct Material Material;
 
 struct Asset3D {
-       unsigned int numMeshes;
+       size_t numMeshes;
        Mesh* meshes;
-       unsigned int numMaterials;
+       size_t numMaterials;
        Material* materials;
 };
 
 struct Mesh {
-       unsigned int numVertices;
+       size_t numVertices;
        Vector3D* vertices;
        Vector3D* normals;
        Vector3D* textureCoords;
-       unsigned int numFaces;
+       size_t numFaces;
        Face* faces;
-       unsigned int materialIndex;
+       size_t materialIndex;
 };
 
 struct Face {
-       unsigned int numIndices;
-       unsigned int* indices;
+       size_t numIndices;
+       size_t* indices;
 };
 
 struct Material {
index ef72f6431c5ba1e4e757c1da8ce4515e71dacf47..47ec89ca5da10ed3844d8e1e34b264e8ab340c79 100644 (file)
@@ -107,10 +107,10 @@ static void drawAxes() {
 
 static void renderBlockGrid(const BlockGrid grid) {
        glMatrixMode(GL_MODELVIEW);
-       for (int z = 0; z < grid.depth; ++z) {
+       for (size_t z = 0; z < grid.depth; ++z) {
                glLoadIdentity();
                glTranslatef(0.0f, 0.0f, z * BLOCKGRID_CELL_SIZE);
-               for (int x = 0; x < grid.width; ++x) {
+               for (size_t x = 0; x < grid.width; ++x) {
                        drawAsset3D(getBlockFromGrid(grid, x, z)->asset3D);
                        glTranslatef(BLOCKGRID_CELL_SIZE, 0.0f, 0.0f);
                }
@@ -154,7 +154,7 @@ static void drawAsset3D(const Asset3D* asset3D) {
                        glBegin(faceMode);
                        
                        for (size_t i = 0; i < face.numIndices; ++i) {
-                               unsigned int vertIndex = face.indices[i];
+                               size_t vertIndex = face.indices[i];
                                if (hasNormals) {
                                        if (hasTextureCoords) {
                                                Vector3D coords = mesh.textureCoords[vertIndex];
index 21275dd797cbd40e8e7a664fed17c5b748398cdf..048db470f7e8cc9413c663ec80ac028cfc750aaa 100644 (file)
@@ -48,10 +48,10 @@ void buildLevelFromImage(TgaImage* image) {
                                               * sizeof(Block*)) };
        playerSpawnPos = (Vector3D) DEFAULT_PLAYER_SPAWN_POS;
        
-       for (int row = 0; row < newGrid.depth; ++row) {
-               for (int x = 0; x < newGrid.width; ++x) {
+       for (size_t row = 0; row < newGrid.depth; ++row) {
+               for (size_t x = 0; x < newGrid.width; ++x) {
                        // Flip the image vertically due to (0, 0) being bottom left
-                       int z = newGrid.depth - row - 1;
+                       size_t z = newGrid.depth - row - 1;
                        
                        uint32_t pixelColorARGB = ((uint32_t*) image->bytes)[(row * newGrid.width) + x];
                        Block* block;
index 2f2d8db4466ab02b1dc698c119fd256069bd5a8c..89d7e2e0daeb96d85c3c9b286cf1454b9595661e 100644 (file)
@@ -19,8 +19,8 @@ typedef struct {
 } Block;
 
 typedef struct {
-       int width;
-       int depth;
+       size_t width;
+       size_t depth;
        Block** blocks;
 } BlockGrid;
 
@@ -32,11 +32,11 @@ Vector3D playerSpawnPos;
 void initLevel();
 void buildLevelFromImage(TgaImage* image);
 
-static inline Block* getBlockFromGrid(BlockGrid grid, int x, int z) {
+static inline Block* getBlockFromGrid(BlockGrid grid, size_t x, size_t z) {
        return grid.blocks[(z * grid.width) + x];
 }
 
-static inline void setBlockInGrid(BlockGrid grid, int x, int z, Block* block) {
+static inline void setBlockInGrid(BlockGrid grid, size_t x, size_t z, Block* block) {
        grid.blocks[(z * grid.width) + x] = block;
 }