From 79b01a50076a492f88932a92aa31fc155e0de62a Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Thu, 21 Dec 2023 00:28:53 +0100 Subject: [PATCH] adding error messages when using a corrupted window pointer, dropping SDL init responsability when running in sandbox --- src/core/application.cpp | 9 ++++++++- src/core/application.inl | 20 ++++++++++++++++++++ src/platform/window.h | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/core/application.cpp b/src/core/application.cpp index 0a30f7b..5c77a04 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,11 +6,12 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2023/12/15 20:51:41 by maldavid ### ########.fr */ +/* Updated: 2023/12/21 00:17:54 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include "application.h" +#include #include #include #include @@ -20,8 +21,12 @@ 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()); @@ -68,6 +73,8 @@ namespace mlx::core Application::~Application() { + if(__drop_sdl_responsability) + return; SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); SDL_Quit(); } diff --git a/src/core/application.inl b/src/core/application.inl index d9c367a..edb529a 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -12,6 +12,18 @@ #include +#define CHECK_WINDOW_PTR(win) \ + if(win == nullptr) \ + { \ + core::error::report(e_kind::error, "invalid window ptr (NULL) passed to '%s'", MLX_FUNC_SIG); \ + return; \ + } \ + else if(*static_cast(win) < 0 || *static_cast(win) > _graphics.size())\ + { \ + core::error::report(e_kind::error, "invalid window ptr passed to '%s'", MLX_FUNC_SIG); \ + return; \ + } else {}\ + namespace mlx::core { void Application::getMousePos(int* x, int* y) noexcept @@ -22,6 +34,7 @@ namespace mlx::core void Application::mouseMove(void* win, int x, int y) noexcept { + CHECK_WINDOW_PTR(win); SDL_WarpMouseInWindow(_graphics[*static_cast(win)]->getWindow()->getNativeWindow(), x, y); SDL_PumpEvents(); SDL_FlushEvent(SDL_MOUSEMOTION); @@ -29,6 +42,7 @@ namespace mlx::core void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept { + CHECK_WINDOW_PTR(win); _in->onEvent(_graphics[*static_cast(win)]->getWindow()->getID(), event, funct_ptr, param); } @@ -49,31 +63,37 @@ namespace mlx::core void Application::clearGraphicsSupport(void* win) { + CHECK_WINDOW_PTR(win); _graphics[*static_cast(win)]->clearRenderData(); } void Application::destroyGraphicsSupport(void* win) { + CHECK_WINDOW_PTR(win); _graphics[*static_cast(win)].reset(); } void Application::pixelPut(void* win, int x, int y, uint32_t color) const noexcept { + CHECK_WINDOW_PTR(win); _graphics[*static_cast(win)]->pixelPut(x, y, color); } void Application::stringPut(void* win, int x, int y, int color, char* str) { + CHECK_WINDOW_PTR(win); _graphics[*static_cast(win)]->stringPut(x, y, color, str); } void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale) { + CHECK_WINDOW_PTR(win); _graphics[*static_cast(win)]->loadFont(filepath, scale); } void Application::texturePut(void* win, void* img, int x, int y) { + CHECK_WINDOW_PTR(win); if(img == nullptr) { core::error::report(e_kind::error, "wrong texture (NULL)"); diff --git a/src/platform/window.h b/src/platform/window.h index b362b90..876c14b 100644 --- a/src/platform/window.h +++ b/src/platform/window.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ -/* Updated: 2023/12/11 19:47:26 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/21 00:24:26 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */