adding error checks

This commit is contained in:
2023-12-16 20:17:49 +01:00
parent 73c8e331af
commit 72cc2eb9ab
6 changed files with 110 additions and 22 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */ /* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */
/* Updated: 2023/12/11 20:35:57 by kbz_8 ### ########.fr */ /* Updated: 2023/12/16 20:11:18 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -50,19 +50,55 @@
#elif defined(unix) || defined(__unix__) || defined(__unix) #elif defined(unix) || defined(__unix__) || defined(__unix)
#define MLX_PLAT_UNIX #define MLX_PLAT_UNIX
#else #else
#error "Unknown environment!" #error "Unknown environment (not Windows, not Linux, not MacOS, not Unix)"
#endif #endif
#ifdef MLX_COMPILER_MSVC #ifdef MLX_PLAT_WINDOWS
#ifdef MLX_BUILD #ifdef MLX_COMPILER_MSVC
#define MLX_API __declspec(dllexport) #ifdef MLX_BUILD
#define MLX_API __declspec(dllexport)
#else
#define MLX_API __declspec(dllimport)
#endif
#elif defined(MLX_COMPILER_GCC)
#ifdef MLX_BUILD
#define MLX_API __attribute__((dllexport))
#else
#define MLX_API __attribute__((dllimport))
#endif
#else #else
#define MLX_API __declspec(dllimport) #define MLX_API
#endif #endif
#elif defined(MLX_COMPILER_GCC)
#define MLX_API __attribute__((visibility("default")))
#else #else
#define MLX_API #define MLX_API
#endif #endif
#if defined(MLX_COMPILER_GCC) && !defined(MLX_PLAT_WINDOWS)
#define MLX_VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
#else
#define MLX_VISIBILITY_HIDDEN
#endif
#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)
#define MLX_FUNC_SIG __PRETTY_FUNCTION__
#elif defined(__DMC__) && (__DMC__ >= 0x810)
#define MLX_FUNC_SIG __PRETTY_FUNCTION__
#elif (defined(__FUNCSIG__) || (_MSC_VER))
#define MLX_FUNC_SIG __FUNCSIG__
#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || (defined(__IBMCPP__) && (__IBMCPP__ >= 500))
#define MLX_FUNC_SIG __FUNCTION__
#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
#define MLX_FUNC_SIG __FUNC__
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
#define MLX_FUNC_SIG __func__
#elif defined(__cplusplus) && (__cplusplus >= 201103)
#define MLX_FUNC_SIG __func__
#else
#define MLX_FUNC_SIG "Unknown function"
#endif
// Checking common assumptions // Checking common assumptions
#ifdef __cplusplus #ifdef __cplusplus
#include <climits> #include <climits>

View File

@@ -88,14 +88,32 @@ namespace mlx::core
int Application::getTexturePixel(void* img, int x, int y) int Application::getTexturePixel(void* img, int x, int y)
{ {
if(img == nullptr)
{
core::error::report(e_kind::error, "wrong texture (NULL)");
return 0;
}
Texture* texture = static_cast<Texture*>(img); Texture* texture = static_cast<Texture*>(img);
if(!texture->isInit())
{
core::error::report(e_kind::error, "trying to get a pixel from texture that has been destroyed");
return 0;
}
return texture->getPixel(x, y); return texture->getPixel(x, y);
} }
void Application::setTexturePixel(void* img, int x, int y, uint32_t color) void Application::setTexturePixel(void* img, int x, int y, uint32_t color)
{ {
if(img == nullptr)
{
core::error::report(e_kind::error, "wrong texture (NULL)");
return;
}
Texture* texture = static_cast<Texture*>(img); Texture* texture = static_cast<Texture*>(img);
texture->setPixel(x, y, color); if(!texture->isInit())
core::error::report(e_kind::error, "trying to set a pixel on texture that has been destroyed");
else
texture->setPixel(x, y, color);
} }
void Application::loopHook(int (*f)(void*), void* param) void Application::loopHook(int (*f)(void*), void* param)

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 17:35:20 by maldavid #+# #+# */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/12/14 17:47:17 by maldavid ### ########.fr */ /* Updated: 2023/12/16 20:17:23 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -17,13 +17,20 @@
#include <filesystem> #include <filesystem>
#include <mlx.h> #include <mlx.h>
#include <core/memory.h> #include <core/memory.h>
#include <mlx_profile.h>
static void* __mlx_ptr MLX_VISIBILITY_HIDDEN = nullptr;
#define MLX_CHECK_APPLICATION_POINTER(ptr) \
if(ptr != __mlx_ptr || ptr == NULL) \
mlx::core::error::report(e_kind::fatal_error, "invalid mlx pointer passed to '%s'", MLX_FUNC_SIG); \
else {} // just to avoid issues with possible if-else statements outside this macro
extern "C" extern "C"
{ {
void* mlx_init() void* mlx_init()
{ {
static bool init = false; if(__mlx_ptr != nullptr)
if(init)
{ {
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times"); mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
return NULL; // not nullptr for the C compatibility return NULL; // not nullptr for the C compatibility
@@ -33,29 +40,33 @@ extern "C"
mlx::Render_Core::get().init(); mlx::Render_Core::get().init();
if(app == nullptr) if(app == nullptr)
mlx::core::error::report(e_kind::fatal_error, "Tout a pété"); mlx::core::error::report(e_kind::fatal_error, "Tout a pété");
init = true; __mlx_ptr = static_cast<void*>(app);
return static_cast<void*>(app); return __mlx_ptr;
} }
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);
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title); return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
} }
int mlx_loop_hook(void* mlx, int (*f)(void*), void* param) int mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->loopHook(f, param); static_cast<mlx::core::Application*>(mlx)->loopHook(f, param);
return 0; return 0;
} }
int mlx_loop(void* mlx) int mlx_loop(void* mlx)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->run(); static_cast<mlx::core::Application*>(mlx)->run();
return 0; return 0;
} }
int mlx_loop_end(void* mlx) int mlx_loop_end(void* mlx)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->loopEnd(); static_cast<mlx::core::Application*>(mlx)->loopEnd();
return 0; return 0;
} }
@@ -72,34 +83,40 @@ extern "C"
int mlx_mouse_move(void* mlx, void* win, int x, int y) int mlx_mouse_move(void* mlx, void* win, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y); static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y);
return 0; return 0;
} }
int mlx_mouse_get_pos(void* mlx, int* x, int* y) int mlx_mouse_get_pos(void* mlx, int* x, int* y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y); static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y);
return 0; return 0;
} }
int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param) int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->onEvent(win, static_cast<int>(event), funct_ptr, param); static_cast<mlx::core::Application*>(mlx)->onEvent(win, static_cast<int>(event), funct_ptr, param);
return 0; return 0;
} }
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);
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height); return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
} }
int mlx_get_image_pixel(void* mlx, void* img, int x, int y) int mlx_get_image_pixel(void* mlx, void* img, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
return static_cast<mlx::core::Application*>(mlx)->getTexturePixel(img, x, y); return static_cast<mlx::core::Application*>(mlx)->getTexturePixel(img, x, y);
} }
void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color) void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4]; unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16; color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8; color_bits[1] = (color & 0x0000FF00) >> 8;
@@ -110,18 +127,21 @@ extern "C"
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y) int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y); static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y);
return 0; return 0;
} }
int mlx_destroy_image(void* mlx, void* img) int mlx_destroy_image(void* mlx, void* img)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->destroyTexture(img); static_cast<mlx::core::Application*>(mlx)->destroyTexture(img);
return 0; return 0;
} }
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);
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".png") if(file.extension() != ".png")
{ {
@@ -133,6 +153,7 @@ 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);
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg") if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{ {
@@ -144,6 +165,7 @@ 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);
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib") if(file.extension() != ".bmp" && file.extension() != ".dib")
{ {
@@ -155,6 +177,7 @@ extern "C"
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color) int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4]; unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16; color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8; color_bits[1] = (color & 0x0000FF00) >> 8;
@@ -166,6 +189,7 @@ extern "C"
int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str) int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4]; unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16; color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8; color_bits[1] = (color & 0x0000FF00) >> 8;
@@ -177,6 +201,7 @@ 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);
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")
{ {
@@ -188,6 +213,7 @@ 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);
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")
{ {
@@ -199,25 +225,30 @@ extern "C"
int mlx_clear_window(void* mlx, void* win) int mlx_clear_window(void* mlx, void* win)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win); static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win);
return 0; return 0;
} }
int mlx_destroy_window(void* mlx, void* win) int mlx_destroy_window(void* mlx, void* win)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win); static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win);
return 0; return 0;
} }
int mlx_destroy_display(void* mlx) int mlx_destroy_display(void* mlx)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
delete static_cast<mlx::core::Application*>(mlx); delete static_cast<mlx::core::Application*>(mlx);
mlx::Render_Core::get().destroy(); mlx::Render_Core::get().destroy();
__mlx_ptr = nullptr;
return 0; return 0;
} }
int mlx_get_screens_size(void* mlx, int* w, int* h) int mlx_get_screens_size(void* mlx, int* w, int* h)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->getScreenSize(w, h); static_cast<mlx::core::Application*>(mlx)->getScreenSize(w, h);
return 0; return 0;
} }

View File

@@ -6,7 +6,7 @@
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */ /* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */
/* Updated: 2023/12/16 14:47:53 by maldavid ### ########.fr */ /* Updated: 2023/12/16 19:14:15 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -97,7 +97,7 @@ namespace mlx
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new buffer"); core::error::report(e_kind::message, "Graphics Allocator : created new buffer");
#endif #endif
_active_allocations++; _active_buffers_allocations++;
return allocation; return allocation;
} }
@@ -108,7 +108,7 @@ namespace mlx
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : destroyed buffer"); core::error::report(e_kind::message, "Graphics Allocator : destroyed buffer");
#endif #endif
_active_allocations--; _active_buffers_allocations--;
} }
VmaAllocation GPUallocator::createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, const char* name) noexcept VmaAllocation GPUallocator::createImage(const VkImageCreateInfo* iminfo, const VmaAllocationCreateInfo* vinfo, VkImage& image, const char* name) noexcept
@@ -121,7 +121,7 @@ namespace mlx
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new image"); core::error::report(e_kind::message, "Graphics Allocator : created new image");
#endif #endif
_active_allocations++; _active_images_allocations++;
return allocation; return allocation;
} }
@@ -132,7 +132,7 @@ namespace mlx
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : destroyed image"); core::error::report(e_kind::message, "Graphics Allocator : destroyed image");
#endif #endif
_active_allocations--; _active_images_allocations--;
} }
void GPUallocator::mapMemory(VmaAllocation allocation, void** data) noexcept void GPUallocator::mapMemory(VmaAllocation allocation, void** data) noexcept
@@ -172,8 +172,10 @@ namespace mlx
void GPUallocator::destroy() noexcept void GPUallocator::destroy() noexcept
{ {
if(_active_allocations != 0) if(_active_images_allocations != 0)
core::error::report(e_kind::error, "Graphics allocator : some allocations were not freed before destroying the display (%d active allocations)", _active_allocations); core::error::report(e_kind::error, "Graphics allocator : some user-dependant allocations were not freed before destroying the display (%d active allocations)", _active_images_allocations);
else if(_active_buffers_allocations != 0)
core::error::report(e_kind::error, "Graphics allocator : some MLX-dependant allocations were not freed before destroying the display (%d active allocations), please report, this should not happen", _active_buffers_allocations);
vmaDestroyAllocator(_allocator); vmaDestroyAllocator(_allocator);
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */ /* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */
/* Updated: 2023/12/16 14:43:31 by maldavid ### ########.fr */ /* Updated: 2023/12/16 18:53:51 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -44,7 +44,8 @@ namespace mlx
private: private:
VmaAllocator _allocator; VmaAllocator _allocator;
uint32_t _active_allocations = 0; uint32_t _active_buffers_allocations = 0;
uint32_t _active_images_allocations = 0;
}; };
} }

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 17:55:21 by maldavid #+# #+# */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2023/12/15 21:08:07 by maldavid ### ########.fr */ /* Updated: 2023/12/16 19:14:56 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */