Preventing some segfaults (#37)

![nerd](https://github.com/seekrs/MacroLibX/assets/105780726/74152e81-029a-4f35-afda-11e6411646bf)
This commit is contained in:
kbz_8
2024-01-12 01:51:51 +01:00
committed by GitHub

View File

@@ -47,6 +47,11 @@ extern "C"
void* mlx_new_window(void* mlx, int w, int h, const char* title) void* mlx_new_window(void* mlx, int w, int h, const char* title)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0)
{
mlx::core::error::report(e_kind::fatal_error, "invalid window size (%d x %d)", w, h);
return NULL; // not nullptr for the C compatibility
}
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title); return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
} }
@@ -73,12 +78,14 @@ extern "C"
int mlx_mouse_show() int mlx_mouse_show()
{ {
return SDL_ShowCursor(SDL_ENABLE); SDL_ShowCursor(SDL_ENABLE);
return 0;
} }
int mlx_mouse_hide() int mlx_mouse_hide()
{ {
return SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
return 0;
} }
int mlx_mouse_move(void* mlx, void* win, int x, int y) int mlx_mouse_move(void* mlx, void* win, int x, int y)
@@ -105,6 +112,8 @@ extern "C"
void* mlx_new_image(void* mlx, int width, int height) void* mlx_new_image(void* mlx, int width, int height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (width <= 0 || height <= 0)
mlx::core::error::report(e_kind::fatal_error, "invalid image size (%d x %d)", width, height);
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height); return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
} }
@@ -142,6 +151,8 @@ extern "C"
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height) void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "PNG loader : filename is NULL");
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".png") if(file.extension() != ".png")
{ {
@@ -154,6 +165,8 @@ extern "C"
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height) void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "JPG loader : filename is NULL");
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg") if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{ {
@@ -166,6 +179,8 @@ extern "C"
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height) void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "BMP loader : filename is NULL");
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib") if(file.extension() != ".bmp" && file.extension() != ".dib")
{ {
@@ -202,6 +217,11 @@ extern "C"
void mlx_set_font(void* mlx, void* win, char* filepath) void mlx_set_font(void* mlx, void* win, char* filepath)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
{
mlx::core::error::report(e_kind::error, "Font loader : filepath is NULL");
return ;
}
std::filesystem::path file(filepath); std::filesystem::path file(filepath);
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte") if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
{ {
@@ -214,6 +234,11 @@ extern "C"
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale) void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
{
mlx::core::error::report(e_kind::error, "Font loader : filepath is NULL");
return ;
}
std::filesystem::path file(filepath); std::filesystem::path file(filepath);
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte") if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
{ {