]> git.lizzy.rs Git - shadowclad.git/commitdiff
Create basic String struct
authoroutfrost <kotlet.bahn@gmail.com>
Thu, 11 Jun 2020 23:12:02 +0000 (01:12 +0200)
committeroutfrost <kotlet.bahn@gmail.com>
Thu, 11 Jun 2020 23:12:02 +0000 (01:12 +0200)
Makefile
src/engine/asset.c
src/engine/string.c [new file with mode: 0644]
src/engine/string.h [new file with mode: 0644]

index 9a9e0e595674f5ad5a89e1e93e55a1a4b49055f5..42225a5a91e9b15e93d2ede59c6f2cf1763f9793 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,6 +20,7 @@ sources ::= main.c \
             engine/performance.c \
             engine/render.c \
             engine/scene.c \
+            engine/string.c \
             engine/tga.c \
             engine/ui.c \
             game/input.c \
index a26a7e4794da3cbbe60b2bae023592c0b7a87f25..b5744aaaf32bb99b0ca4850ea3b261c90de570c6 100644 (file)
@@ -7,6 +7,7 @@
 #include <assimp/scene.h>
 
 #include "logger.h"
+#include "string.h"
 #include "tga.h"
 
 #define IMPORT_DEBUG_ 1
@@ -21,46 +22,39 @@ static const char* replaceFileExtension(const struct aiString path, const char*
 static void printMetadata(const struct aiMetadata* meta) {
        if (meta) {
                for (size_t i = 0; i < meta->mNumProperties; ++i) {
-                       char* key = memcpy(malloc((meta->mKeys[i].length + 1) * sizeof(char)),
-                                          meta->mKeys[i].data,
-                                          meta->mKeys[i].length * sizeof(char));
-                       key[meta->mKeys[i].length] = '\0';
+                       String key = stringFromAiString(meta->mKeys[i]);
                        const struct aiMetadataEntry value = meta->mValues[i];
                        switch (value.mType) {
                                case AI_BOOL:
-                                       logDebug("\"%s\": (bool) %d", key, *((int*) value.mData));
+                                       logDebug("\"%s\": (bool) %d", key.cstr, *((int*) value.mData));
                                        break;
                                case AI_INT32:
-                                       logDebug("\"%s\": (int32) %d", key, *((int32_t*) value.mData));
+                                       logDebug("\"%s\": (int32) %d", key.cstr, *((int32_t*) value.mData));
                                        break;
                                case AI_UINT64:
-                                       logDebug("\"%s\": (uint64) %llu", key, *((uint64_t*) value.mData));
+                                       logDebug("\"%s\": (uint64) %llu", key.cstr, *((uint64_t*) value.mData));
                                        break;
                                case AI_FLOAT:
-                                       logDebug("\"%s\": (float) %f", key, *((float*) value.mData));
+                                       logDebug("\"%s\": (float) %f", key.cstr, *((float*) value.mData));
                                        break;
                                case AI_DOUBLE:
-                                       logDebug("\"%s\": (double) %f", key, *((double*) value.mData));
+                                       logDebug("\"%s\": (double) %f", key.cstr, *((double*) value.mData));
                                        break;
                                case AI_AISTRING: {
-                                       struct aiString aistr = *((struct aiString*) value.mData);
-                                       char* str = memcpy(malloc((aistr.length + 1) * sizeof(char)),
-                                                          aistr.data,
-                                                          aistr.length * sizeof(char));
-                                       str[aistr.length] = '\0';
-                                       logDebug("\"%s\": (string) %s", key, str);
-                                       free(str);
+                                       String str = stringFromAiString(*((struct aiString*) value.mData));
+                                       logDebug("\"%s\": (string) %s", key.cstr, str.cstr);
+                                       dropString(str);
                                        break; }
                                case AI_AIVECTOR3D: {
                                        struct aiVector3D vec = *((struct aiVector3D*) value.mData);
-                                       logDebug("\"%s\": (vector3d) { %f, %f, %f }", key, vec.x, vec.y, vec.z);
+                                       logDebug("\"%s\": (vector3d) { %f, %f, %f }", key.cstr, vec.x, vec.y, vec.z);
                                        break; }
                                case AI_META_MAX:
                                default:
-                                       logDebug("\"%s\": (unrecognized type)", key);
+                                       logDebug("\"%s\": (unrecognized type)", key.cstr);
                                        break;
                        }
-                       free(key);
+                       dropString(key);
                }
        }
 }
@@ -70,17 +64,15 @@ void printAiNodeMetadata(const struct aiNode* node) {
                return;
        }
 
-       struct aiString aistr = node->mName;
-       char* name = memcpy(malloc((aistr.length + 1) * sizeof(char)),
-                           aistr.data,
-                           aistr.length * sizeof(char));
-       name[aistr.length] = '\0';
-       logDebug("Metadata from node \"%s\": %p", name, node->mMetaData);
+       String name = stringFromAiString(node->mName);
+       logDebug("Metadata from node \"%s\": %p", name.cstr, node->mMetaData);
        printMetadata(node->mMetaData);
 
        for (size_t i = 0; i < node->mNumChildren; ++i) {
                printAiNodeMetadata(node->mChildren[i]);
        }
+
+       dropString(name);
 }
 #endif // IMPORT_DEBUG_
 
@@ -169,17 +161,15 @@ const Solid* importSolid(const char* path) {
                aiGetMaterialProperty(scene->mMaterials[matIndex],
                                      AI_MATKEY_SHADING_MODEL,
                                      &prop);
-               // print mKey
-               struct aiString aistr = prop->mKey;
-               char* key = memcpy(malloc((aistr.length + 1) * sizeof(char)),
-                                  aistr.data,
-                                  aistr.length * sizeof(char));
-               key[aistr.length] = '\0';
+
+               String key = stringFromAiString(prop->mKey);
 
                logDebug("Material property \"%s\": Shading model: (length %u) %d",
-                        key,
+                        key.cstr,
                         prop->mDataLength,
                         *((int32_t*) prop->mData));
+
+               dropString(key);
 #endif // IMPORT_DEBUG_
                
                glBindTexture(GL_TEXTURE_2D, material.textureId);
diff --git a/src/engine/string.c b/src/engine/string.c
new file mode 100644 (file)
index 0000000..05c93cc
--- /dev/null
@@ -0,0 +1,17 @@
+#include "string.h"
+
+#include <stdlib.h>
+
+String stringFromAiString(const struct aiString aistr) {
+       char* cstr = memcpy(malloc((aistr.length + 1) * sizeof(char)),
+                               aistr.data,
+                               aistr.length * sizeof(char));
+       cstr[aistr.length] = '\0';
+       return (String) { .length = aistr.length, .cstr = cstr };
+}
+
+void dropString(String str) {
+       free(str.cstr);
+       str.length = 0u;
+       str.cstr = NULL;
+}
diff --git a/src/engine/string.h b/src/engine/string.h
new file mode 100644 (file)
index 0000000..73e600b
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef STRING_H_
+#define STRING_H_
+
+#include <assimp/types.h>
+
+typedef struct String String;
+
+struct String {
+       size_t length;
+       char* cstr;
+};
+
+String stringFromAiString(const struct aiString aistr);
+void dropString(String str);
+
+#endif // STRING_H_