adding error messages when using a corrupted window pointer, dropping SDL init responsability when running in sandbox

This commit is contained in:
2023-12-21 00:28:53 +01:00
parent 9f555316fc
commit ca13d77996
3 changed files with 29 additions and 2 deletions

View File

@@ -6,11 +6,12 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 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 "application.h"
#include <SDL2/SDL.h>
#include <renderer/images/texture.h> #include <renderer/images/texture.h>
#include <renderer/core/render_core.h> #include <renderer/core/render_core.h>
#include <array> #include <array>
@@ -20,8 +21,12 @@
namespace mlx::core namespace mlx::core
{ {
static bool __drop_sdl_responsability = false;
Application::Application() : _in(std::make_unique<Input>()) Application::Application() : _in(std::make_unique<Input>())
{ {
__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); SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free);
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) 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()); 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() Application::~Application()
{ {
if(__drop_sdl_responsability)
return;
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);
SDL_Quit(); SDL_Quit();
} }

View File

@@ -12,6 +12,18 @@
#include <core/application.h> #include <core/application.h>
#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<int*>(win) < 0 || *static_cast<std::size_t*>(win) > _graphics.size())\
{ \
core::error::report(e_kind::error, "invalid window ptr passed to '%s'", MLX_FUNC_SIG); \
return; \
} else {}\
namespace mlx::core namespace mlx::core
{ {
void Application::getMousePos(int* x, int* y) noexcept 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 void Application::mouseMove(void* win, int x, int y) noexcept
{ {
CHECK_WINDOW_PTR(win);
SDL_WarpMouseInWindow(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow(), x, y); SDL_WarpMouseInWindow(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow(), x, y);
SDL_PumpEvents(); SDL_PumpEvents();
SDL_FlushEvent(SDL_MOUSEMOTION); 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 void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
{ {
CHECK_WINDOW_PTR(win);
_in->onEvent(_graphics[*static_cast<int*>(win)]->getWindow()->getID(), event, funct_ptr, param); _in->onEvent(_graphics[*static_cast<int*>(win)]->getWindow()->getID(), event, funct_ptr, param);
} }
@@ -49,31 +63,37 @@ namespace mlx::core
void Application::clearGraphicsSupport(void* win) void Application::clearGraphicsSupport(void* win)
{ {
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)]->clearRenderData(); _graphics[*static_cast<int*>(win)]->clearRenderData();
} }
void Application::destroyGraphicsSupport(void* win) void Application::destroyGraphicsSupport(void* win)
{ {
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)].reset(); _graphics[*static_cast<int*>(win)].reset();
} }
void Application::pixelPut(void* win, int x, int y, uint32_t color) const noexcept void Application::pixelPut(void* win, int x, int y, uint32_t color) const noexcept
{ {
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)]->pixelPut(x, y, color); _graphics[*static_cast<int*>(win)]->pixelPut(x, y, color);
} }
void Application::stringPut(void* win, int x, int y, int color, char* str) void Application::stringPut(void* win, int x, int y, int color, char* str)
{ {
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)]->stringPut(x, y, color, str); _graphics[*static_cast<int*>(win)]->stringPut(x, y, color, str);
} }
void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale) void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale)
{ {
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)]->loadFont(filepath, scale); _graphics[*static_cast<int*>(win)]->loadFont(filepath, scale);
} }
void Application::texturePut(void* win, void* img, int x, int y) void Application::texturePut(void* win, void* img, int x, int y)
{ {
CHECK_WINDOW_PTR(win);
if(img == nullptr) if(img == nullptr)
{ {
core::error::report(e_kind::error, "wrong texture (NULL)"); core::error::report(e_kind::error, "wrong texture (NULL)");

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:53:12 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */