/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* application.cpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ /* Updated: 2023/12/24 08:56:33 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ #include "application.h" #include #include #include #include #include #include #include namespace mlx::core { static bool __drop_sdl_responsability = false; Application::Application() : _in(std::make_unique()) { __drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO); if(__drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init return; SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) error::report(e_kind::fatal_error, "SDL error : unable to init all subsystems : %s", SDL_GetError()); } void Application::run() noexcept { while(_in->is_running()) { _in->update(); if(_loop_hook) _loop_hook(_param); for(auto& gs : _graphics) gs->render(); } } void* Application::newTexture(int w, int h) { #ifdef DEBUG _textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_unamed_user_texture"); #else _textures.emplace_front().create(nullptr, w, h, VK_FORMAT_R8G8B8A8_UNORM, nullptr); #endif return &_textures.front(); } void* Application::newStbTexture(char* file, int* w, int* h) { _textures.emplace_front(stbTextureLoad(file, w, h)); return &_textures.front(); } void Application::destroyTexture(void* ptr) { vkDeviceWaitIdle(Render_Core::get().getDevice().get()); // TODO : synchronize with another method than stopping all the GPU porcess Texture* texture = static_cast(ptr); texture->destroy(); } Application::~Application() { if(__drop_sdl_responsability) return; SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); SDL_Quit(); } }