Added more robust logging. Put garbage collection on 3 minute interval

This commit is contained in:
Ethan Adams 2025-01-28 20:02:40 -05:00
parent b11a99b0d4
commit 169cc6d617
8 changed files with 51 additions and 36 deletions

5
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,5 @@
{
"files.associations": {
"memory": "cpp"
}
}

View file

@ -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

View file

@ -55,7 +55,7 @@ public:
private:
bool adjustCanvasSizes(std::vector<SDL_Surface*>& surfaces);
int numOfLayers;
size_t numOfLayers;
unsigned ID = 0;
std::vector<TextureData*> textures;
int canvasWidth = 0;

View file

@ -25,6 +25,9 @@ public:
std::lock_guard<std::mutex> 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;

View file

@ -6,7 +6,8 @@
AI::AI(const std::shared_ptr<GameActor>& actor, const std::shared_ptr<Raycaster>& 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<AIScript>& 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;

View file

@ -21,18 +21,12 @@ bool Game::init()
window = std::make_shared<GLWindow>("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);

View file

@ -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<WeaponScript>& 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<GameActor> target, std::shared_ptr<PhysicsComponent> bullet, const glm::vec2& normal)

View file

@ -1,25 +1,24 @@
#include "graphics/texture.h"
#include "utility/logger.h"
#include "util.h"
#include <SDL_image.h>
#include <iostream>
#include <glad/glad.h>
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<const char*> 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<const char*> 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<SDL_Surface*>& 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 }));