Skip to content

Commit

Permalink
adding error messages when using a corrupted window pointer, dropping…
Browse files Browse the repository at this point in the history
… SDL init responsability when running in sandbox
  • Loading branch information
Kbz-8 committed Dec 20, 2023
1 parent 6fb8932 commit 79b01a5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/core/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <SDL2/SDL.h>
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
#include <array>
Expand All @@ -20,8 +21,12 @@

namespace mlx::core
{
static bool __drop_sdl_responsability = false;
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);
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());
Expand Down Expand Up @@ -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();
}
Expand Down
20 changes: 20 additions & 0 deletions src/core/application.inl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

#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
{
void Application::getMousePos(int* x, int* y) noexcept
Expand All @@ -22,13 +34,15 @@ namespace mlx::core

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_PumpEvents();
SDL_FlushEvent(SDL_MOUSEMOTION);
}

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);
}

Expand All @@ -49,31 +63,37 @@ namespace mlx::core

void Application::clearGraphicsSupport(void* win)
{
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)]->clearRenderData();
}

void Application::destroyGraphicsSupport(void* win)
{
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(win)].reset();
}

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);
}

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);
}

void Application::loadFont(void* win, const std::filesystem::path& filepath, float scale)
{
CHECK_WINDOW_PTR(win);
_graphics[*static_cast<int*>(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)");
Expand Down
2 changes: 1 addition & 1 deletion src/platform/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down

0 comments on commit 79b01a5

Please sign in to comment.