diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9903512 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "memory": "cpp" + } +} \ No newline at end of file diff --git a/YuppleMayham/include/gameplay/ai.h b/YuppleMayham/include/gameplay/ai.h index d25041b..e3247b1 100644 --- a/YuppleMayham/include/gameplay/ai.h +++ b/YuppleMayham/include/gameplay/ai.h @@ -41,6 +41,9 @@ private: sol::function idleFunc; sol::function patrolFunc; sol::function alertFunc; + + std::chrono::high_resolution_clock::time_point lastGCTime; + std::chrono::minutes GCTimeout; }; #endif \ No newline at end of file diff --git a/YuppleMayham/include/graphics/texture.h b/YuppleMayham/include/graphics/texture.h index 13b9107..b969d99 100644 --- a/YuppleMayham/include/graphics/texture.h +++ b/YuppleMayham/include/graphics/texture.h @@ -55,7 +55,7 @@ public: private: bool adjustCanvasSizes(std::vector& surfaces); - int numOfLayers; + size_t numOfLayers; unsigned ID = 0; std::vector textures; int canvasWidth = 0; diff --git a/YuppleMayham/include/utility/logger.h b/YuppleMayham/include/utility/logger.h index 58e0153..b2107c8 100644 --- a/YuppleMayham/include/utility/logger.h +++ b/YuppleMayham/include/utility/logger.h @@ -25,6 +25,9 @@ public: std::lock_guard lock(mutex); auto msg = formatString(message, args...); std::cout + << "[" + << logTag(level) + << "] " << "[" << std::format("{:%m-%d-%Y %X}", std::chrono::current_zone()->to_local(std::chrono::system_clock::now())) << "] " @@ -52,6 +55,22 @@ protected: return std::vformat(str, std::make_format_args(args...)); } + inline const std::string logTag(LogLevel lvl) const + { + switch (lvl) + { + case DEBUG: + return "DEBUG"; + case INFO: + return "INFO"; + case WARN: + return "WARN"; + case ERROR: + return "ERROR"; + } + return "INFO"; + }; + private: static Logger* instance; static std::mutex mutex; diff --git a/YuppleMayham/src/gameplay/ai.cpp b/YuppleMayham/src/gameplay/ai.cpp index 171375b..715c6a3 100644 --- a/YuppleMayham/src/gameplay/ai.cpp +++ b/YuppleMayham/src/gameplay/ai.cpp @@ -6,7 +6,8 @@ AI::AI(const std::shared_ptr& actor, const std::shared_ptr& raycaster) - : actor(actor), raycaster(raycaster), state(AIState::Idle) + : actor(actor), raycaster(raycaster), state(AIState::Idle), + lastGCTime(std::chrono::high_resolution_clock::now()), GCTimeout(3) {} void AI::attachBehaviourScript(const std::shared_ptr& behaviour) @@ -64,7 +65,12 @@ void AI::update() } break; } - //behaviour->lua.collect_gc(); + std::chrono::high_resolution_clock::time_point curTime = std::chrono::high_resolution_clock::now(); + if (curTime - lastGCTime >= GCTimeout) + { + behaviour->lua.collect_gc(); + lastGCTime = curTime; + } } catch (const std::exception& e) { std::cerr << "Error during AI update: " << e.what() << std::endl; diff --git a/YuppleMayham/src/gameplay/game.cpp b/YuppleMayham/src/gameplay/game.cpp index cdeb216..84769fc 100644 --- a/YuppleMayham/src/gameplay/game.cpp +++ b/YuppleMayham/src/gameplay/game.cpp @@ -21,18 +21,12 @@ bool Game::init() window = std::make_shared("Yupple Mayham", 800, 600); if (!window->Init()) - { - std::cout << "Failed to init GLWindow: \n" << SDL_GetError() << std::endl; - return false; - } + ERROR_LOG("Failed to init GLWindow: {}", SDL_GetError()); if (!gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress)) - { - std::cout << "Failed to load GLLoader" << std::endl; - return false; - } + ERROR_LOG("Failed to load GLLoader"); -#if _DEBUG 1 +#if _DEBUG LOG_LEVEL(DEBUG); #elif LOG_LEVEL(INFO); diff --git a/YuppleMayham/src/gameplay/weapons/weapon.cpp b/YuppleMayham/src/gameplay/weapons/weapon.cpp index cc5a165..0c00df6 100644 --- a/YuppleMayham/src/gameplay/weapons/weapon.cpp +++ b/YuppleMayham/src/gameplay/weapons/weapon.cpp @@ -10,6 +10,7 @@ #include "utility/events.h" #include "utility/resourcemanager.h" #include "utility/script.h" +#include "utility/logger.h" // TODO: Regular clean up, make this mess readable! @@ -132,7 +133,7 @@ void Weapon::attachScript(const std::shared_ptr& script) { weaponScript = script; weaponScript->lua["weapon"] = shared_from_this(); - std::cout << "weapon state bound" << std::endl; + LOG(DEBUG, "Weapon state bound"); } void Weapon::onHitCallback(std::shared_ptr target, std::shared_ptr bullet, const glm::vec2& normal) diff --git a/YuppleMayham/src/graphics/texture.cpp b/YuppleMayham/src/graphics/texture.cpp index 3250c8e..6af80b9 100644 --- a/YuppleMayham/src/graphics/texture.cpp +++ b/YuppleMayham/src/graphics/texture.cpp @@ -1,25 +1,24 @@ #include "graphics/texture.h" +#include "utility/logger.h" #include "util.h" #include -#include #include bool Texture::loadTexture(const char* imagePath) { SDL_Surface* buffer = IMG_Load(imagePath); if (!buffer) - { - std::cout << "Failed to load image file: " << imagePath << std::endl; - return false; - } + ERROR_LOG("Failed to load image file: {}", imagePath); //UTIL::flip_surface(buffer); glGenTextures(1, &ID); + /* GLenum error = glGetError(); if(error != GL_NO_ERROR) { std::cout << "OpenGL error: " << error << std::endl; } + */ glBindTexture(GL_TEXTURE_2D, ID); @@ -60,22 +59,12 @@ bool TextureArray::loadTextures(std::vector imagePaths) { surfaces[i] = IMG_Load(imagePaths[i]); if (!surfaces[i]) - { - std::cout << "Failed to load image file: " << imagePaths[i] << std::endl; - return false; - } + ERROR_LOG("Failed to load image file: {}", imagePaths[i]); } if (!adjustCanvasSizes(surfaces)) - { - std::cout << "ERROR: Failed to adjust canvas size of images!" << std::endl - << "Make sure to check that every tileset has square dimensions! (512x512, 756x756 ... etc)" << std::endl; - return false; - } + ERROR_LOG("Failed to adjust canvas size of images! \n Make sure to check that every tileset has square dimensions! (512x512, 756x756 ... etc)"); if (surfaces.empty()) - { - std::cout << "ERROR: No surfaces created" << std::endl; - return false; - } + ERROR_LOG("No surfaces created!"); numOfLayers = imagePaths.size(); glGenTextures(1, &ID); @@ -89,7 +78,7 @@ bool TextureArray::loadTextures(std::vector imagePaths) GL_RGBA, surfaces[0]->w, surfaces[0]->h, - numOfLayers, + (GLsizei)numOfLayers, 0, GL_RGBA, GL_UNSIGNED_BYTE, @@ -134,10 +123,8 @@ bool TextureArray::adjustCanvasSizes(std::vector& surfaces) for (auto& surface : surfaces) { if (surface->w != surface->h) - { - std::cout << "Image must be a square!" << std::endl; - return false; - } + ERROR_LOG("Image must be a square!"); + if (surface->w > maxWidth) maxWidth = surface->w; if (surface->h > maxHeight) maxHeight = surface->h; textures.push_back(new TextureData({ surface->w, surface->h }));