diff --git a/core/Material.cpp b/core/Material.cpp index 4f3a589..b037263 100644 --- a/core/Material.cpp +++ b/core/Material.cpp @@ -32,8 +32,7 @@ Texture Texture::FromFile(const char *filename) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tex.width, tex.height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { - std::cout << "Error loading texture file: " << filename << std::endl; - exit(1); + Util::Die("Error loading texture file: ", filename); } stbi_image_free(data); diff --git a/core/Material.h b/core/Material.h index 96c727e..dfec8ab 100644 --- a/core/Material.h +++ b/core/Material.h @@ -1,13 +1,11 @@ #ifndef MATERIAL_H #define MATERIAL_H -#include -#include - #include #include "stb_image.h" #include "types.h" +#include "Util.h" struct Color { cfloat r, g, b; diff --git a/core/Mesh.cpp b/core/Mesh.cpp index 26d8706..8891d01 100644 --- a/core/Mesh.cpp +++ b/core/Mesh.cpp @@ -52,16 +52,33 @@ Mesh* Mesh::FromFile(const char *filename) { ); if (!scene) { - std::cerr << importer.GetErrorString() << std::endl; - exit(1); + Util::Die(importer.GetErrorString()); } aiNode *root = scene->mRootNode; aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[0]]; - return Util::aiMesh2Mesh(mesh_to_import); + return aiMesh2Mesh(mesh_to_import); } +Mesh *Mesh::aiMesh2Mesh(aiMesh *aimesh){ + Mesh *mymesh = new Mesh(); + for (int i = 0; i < aimesh->mNumVertices; i++) { + aiVector3D aiPosition = aimesh->mVertices[i]; + aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords + Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z, aiUV.x, aiUV.y); + mymesh->vertices.push_back(vertex); + } + for (int i = 0; i < aimesh->mNumFaces; i++) { + // We're importing triangulated meshes, so each face is three indices + unsigned int *face = aimesh->mFaces[i].mIndices; + Index index(face[0], face[1], face[2]); + mymesh->indices.push_back(index); + } + return mymesh; +} + + void Mesh::Draw() { glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, indices.size() * 3, GL_UNSIGNED_INT, 0); diff --git a/core/Mesh.h b/core/Mesh.h index ae57ad7..4353926 100644 --- a/core/Mesh.h +++ b/core/Mesh.h @@ -2,8 +2,6 @@ #define MESH_H #include -#include -#include // Thirdpart includes #include @@ -31,6 +29,7 @@ public: virtual void SetupMesh(); private: Id VAO, VBO, EBO; + static Mesh *aiMesh2Mesh(aiMesh *mesh); }; typedef std::list MeshList; diff --git a/core/Scripting/Lua.cpp b/core/Scripting/Lua.cpp index 93bc944..dcd58a7 100644 --- a/core/Scripting/Lua.cpp +++ b/core/Scripting/Lua.cpp @@ -25,8 +25,7 @@ void Lua::Initialize() { Error(); } } else if (err == LUA_ERRFILE) { - std::cerr << "Could not find main.lua." << std::endl; - exit(1); + Util::Die("Could not find main.lua."); } else { // Syntax error Error(); @@ -38,8 +37,7 @@ void Lua::Initialize() { input->keyHandlers.push_back(LuaKeyHandler); input->mousePositionHandlers.push_back(LuaMousePositionHandler); #else // LUA_SCRIPTING - std::cerr << "Lua is selected as scripting language, but this binary was built without Lua support." << std::endl; - exit(1); + Util::Die("Lua is selected as scripting language, but this binary was built without Lua support."); #endif // LUA_SCRIPTING } @@ -60,9 +58,9 @@ void Lua::Close() { void Lua::Error() { #ifdef LUA_SCRIPTING const char *error = lua_tolstring(L, -1, 0); - std::cerr << error << std::endl; + Util::Die(error); #endif // LUA_SCRIPTING - exit(1); + Util::Die("Whut?"); } void Lua::LuaKeyHandler(Window *window, int key, int code, int action, int mods) { diff --git a/core/Scripting/Lua.h b/core/Scripting/Lua.h index de75192..60968ab 100644 --- a/core/Scripting/Lua.h +++ b/core/Scripting/Lua.h @@ -1,10 +1,8 @@ #ifndef LUA_H #define LUA_H -#include -#include - #include "Input.h" +#include "Util.h" #ifdef LUA_SCRIPTING // Lua includes diff --git a/core/Util.cpp b/core/Util.cpp index 3f3f741..bbb817e 100644 --- a/core/Util.cpp +++ b/core/Util.cpp @@ -1,18 +1,16 @@ #include "Util.h" -Mesh *Util::aiMesh2Mesh(aiMesh *aimesh){ - Mesh *mymesh = new Mesh(); - for (int i = 0; i < aimesh->mNumVertices; i++) { - aiVector3D aiPosition = aimesh->mVertices[i]; - aiVector3D aiUV = aimesh->mTextureCoords[0][i]; // TODO get ALL texture coords - Vertex vertex(aiPosition.x, aiPosition.y, aiPosition.z, aiUV.x, aiUV.y); - mymesh->vertices.push_back(vertex); - } - for (int i = 0; i < aimesh->mNumFaces; i++) { - // We're importing triangulated meshes, so each face is three indices - unsigned int *face = aimesh->mFaces[i].mIndices; - Index index(face[0], face[1], face[2]); - mymesh->indices.push_back(index); - } - return mymesh; +void Util::Die(const char * msg) { + std::cerr << msg << std::endl; + exit(1); +} + +void Util::Die(const char * msg, const char * more) { + std::cerr << msg << more << std::endl; + exit(1); +} + +void Util::Die(std::string msg) { + std::cerr << msg << std::endl; + exit(1); } diff --git a/core/Util.h b/core/Util.h index 20e6fe5..64581a9 100644 --- a/core/Util.h +++ b/core/Util.h @@ -1,15 +1,14 @@ #ifndef UTIL_H #define UTIL_H -// Thirdparty includes -#include - -// Gahhhhhh mutual inclusion! -#include "Mesh.h" -class Mesh; +#include +#include +#include namespace Util { - Mesh *aiMesh2Mesh(aiMesh *mesh); + void Die(const char *msg); + void Die(const char * msg, const char * more); + void Die(std::string msg); } #endif /* UTIL_H */