Skip to content

Commit b7424e7

Browse files
authored
Merge pull request #18 from 420verfl0w/errors-everywhere
Errors everywhere
2 parents 68d2c63 + 0b4b32b commit b7424e7

File tree

15 files changed

+178
-51
lines changed

15 files changed

+178
-51
lines changed

includes/mlx_profile.h

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */
9-
/* Updated: 2023/12/11 20:35:57 by kbz_8 ### ########.fr */
9+
/* Updated: 2023/12/16 20:20:35 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -50,19 +50,49 @@
5050
#elif defined(unix) || defined(__unix__) || defined(__unix)
5151
#define MLX_PLAT_UNIX
5252
#else
53-
#error "Unknown environment!"
53+
#error "Unknown environment (not Windows, not Linux, not MacOS, not Unix)"
5454
#endif
5555

56-
#ifdef MLX_COMPILER_MSVC
57-
#ifdef MLX_BUILD
58-
#define MLX_API __declspec(dllexport)
56+
#ifdef MLX_PLAT_WINDOWS
57+
#ifdef MLX_COMPILER_MSVC
58+
#ifdef MLX_BUILD
59+
#define MLX_API __declspec(dllexport)
60+
#else
61+
#define MLX_API __declspec(dllimport)
62+
#endif
63+
#elif defined(MLX_COMPILER_GCC)
64+
#ifdef MLX_BUILD
65+
#define MLX_API __attribute__((dllexport))
66+
#else
67+
#define MLX_API __attribute__((dllimport))
68+
#endif
5969
#else
60-
#define MLX_API __declspec(dllimport)
70+
#define MLX_API
6171
#endif
72+
#elif defined(MLX_COMPILER_GCC)
73+
#define MLX_API __attribute__((visibility("default")))
6274
#else
6375
#define MLX_API
6476
#endif
6577

78+
#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)
79+
#define MLX_FUNC_SIG __PRETTY_FUNCTION__
80+
#elif defined(__DMC__) && (__DMC__ >= 0x810)
81+
#define MLX_FUNC_SIG __PRETTY_FUNCTION__
82+
#elif (defined(__FUNCSIG__) || (_MSC_VER))
83+
#define MLX_FUNC_SIG __FUNCSIG__
84+
#elif (defined(__INTEL_COMPILER) && (__INTEL_COMPILER >= 600)) || (defined(__IBMCPP__) && (__IBMCPP__ >= 500))
85+
#define MLX_FUNC_SIG __FUNCTION__
86+
#elif defined(__BORLANDC__) && (__BORLANDC__ >= 0x550)
87+
#define MLX_FUNC_SIG __FUNC__
88+
#elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901)
89+
#define MLX_FUNC_SIG __func__
90+
#elif defined(__cplusplus) && (__cplusplus >= 201103)
91+
#define MLX_FUNC_SIG __func__
92+
#else
93+
#define MLX_FUNC_SIG "Unknown function"
94+
#endif
95+
6696
// Checking common assumptions
6797
#ifdef __cplusplus
6898
#include <climits>

src/core/application.inl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,32 @@ namespace mlx::core
8888

8989
int Application::getTexturePixel(void* img, int x, int y)
9090
{
91+
if(img == nullptr)
92+
{
93+
core::error::report(e_kind::error, "wrong texture (NULL)");
94+
return 0;
95+
}
9196
Texture* texture = static_cast<Texture*>(img);
97+
if(!texture->isInit())
98+
{
99+
core::error::report(e_kind::error, "trying to get a pixel from texture that has been destroyed");
100+
return 0;
101+
}
92102
return texture->getPixel(x, y);
93103
}
94104

95105
void Application::setTexturePixel(void* img, int x, int y, uint32_t color)
96106
{
107+
if(img == nullptr)
108+
{
109+
core::error::report(e_kind::error, "wrong texture (NULL)");
110+
return;
111+
}
97112
Texture* texture = static_cast<Texture*>(img);
98-
texture->setPixel(x, y, color);
113+
if(!texture->isInit())
114+
core::error::report(e_kind::error, "trying to set a pixel on texture that has been destroyed");
115+
else
116+
texture->setPixel(x, y, color);
99117
}
100118

101119
void Application::loopHook(int (*f)(void*), void* param)

src/core/bridge.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
9-
/* Updated: 2023/12/14 17:47:17 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/16 20:20:41 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -17,13 +17,20 @@
1717
#include <filesystem>
1818
#include <mlx.h>
1919
#include <core/memory.h>
20+
#include <mlx_profile.h>
21+
22+
static void* __mlx_ptr = nullptr;
23+
24+
#define MLX_CHECK_APPLICATION_POINTER(ptr) \
25+
if(ptr != __mlx_ptr || ptr == NULL) \
26+
mlx::core::error::report(e_kind::fatal_error, "invalid mlx pointer passed to '%s'", MLX_FUNC_SIG); \
27+
else {} // just to avoid issues with possible if-else statements outside this macro
2028

2129
extern "C"
2230
{
2331
void* mlx_init()
2432
{
25-
static bool init = false;
26-
if(init)
33+
if(__mlx_ptr != nullptr)
2734
{
2835
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
2936
return NULL; // not nullptr for the C compatibility
@@ -33,29 +40,33 @@ extern "C"
3340
mlx::Render_Core::get().init();
3441
if(app == nullptr)
3542
mlx::core::error::report(e_kind::fatal_error, "Tout a pété");
36-
init = true;
37-
return static_cast<void*>(app);
43+
__mlx_ptr = static_cast<void*>(app);
44+
return __mlx_ptr;
3845
}
3946

4047
void* mlx_new_window(void* mlx, int w, int h, const char* title)
4148
{
49+
MLX_CHECK_APPLICATION_POINTER(mlx);
4250
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
4351
}
4452

4553
int mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
4654
{
55+
MLX_CHECK_APPLICATION_POINTER(mlx);
4756
static_cast<mlx::core::Application*>(mlx)->loopHook(f, param);
4857
return 0;
4958
}
5059

5160
int mlx_loop(void* mlx)
5261
{
62+
MLX_CHECK_APPLICATION_POINTER(mlx);
5363
static_cast<mlx::core::Application*>(mlx)->run();
5464
return 0;
5565
}
5666

5767
int mlx_loop_end(void* mlx)
5868
{
69+
MLX_CHECK_APPLICATION_POINTER(mlx);
5970
static_cast<mlx::core::Application*>(mlx)->loopEnd();
6071
return 0;
6172
}
@@ -72,34 +83,40 @@ extern "C"
7283

7384
int mlx_mouse_move(void* mlx, void* win, int x, int y)
7485
{
86+
MLX_CHECK_APPLICATION_POINTER(mlx);
7587
static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y);
7688
return 0;
7789
}
7890

7991
int mlx_mouse_get_pos(void* mlx, int* x, int* y)
8092
{
93+
MLX_CHECK_APPLICATION_POINTER(mlx);
8194
static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y);
8295
return 0;
8396
}
8497

8598
int mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
8699
{
100+
MLX_CHECK_APPLICATION_POINTER(mlx);
87101
static_cast<mlx::core::Application*>(mlx)->onEvent(win, static_cast<int>(event), funct_ptr, param);
88102
return 0;
89103
}
90104

91105
void* mlx_new_image(void* mlx, int width, int height)
92106
{
107+
MLX_CHECK_APPLICATION_POINTER(mlx);
93108
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
94109
}
95110

96111
int mlx_get_image_pixel(void* mlx, void* img, int x, int y)
97112
{
113+
MLX_CHECK_APPLICATION_POINTER(mlx);
98114
return static_cast<mlx::core::Application*>(mlx)->getTexturePixel(img, x, y);
99115
}
100116

101117
void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color)
102118
{
119+
MLX_CHECK_APPLICATION_POINTER(mlx);
103120
unsigned char color_bits[4];
104121
color_bits[0] = (color & 0x00FF0000) >> 16;
105122
color_bits[1] = (color & 0x0000FF00) >> 8;
@@ -110,18 +127,21 @@ extern "C"
110127

111128
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
112129
{
130+
MLX_CHECK_APPLICATION_POINTER(mlx);
113131
static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y);
114132
return 0;
115133
}
116134

117135
int mlx_destroy_image(void* mlx, void* img)
118136
{
137+
MLX_CHECK_APPLICATION_POINTER(mlx);
119138
static_cast<mlx::core::Application*>(mlx)->destroyTexture(img);
120139
return 0;
121140
}
122141

123142
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
124143
{
144+
MLX_CHECK_APPLICATION_POINTER(mlx);
125145
std::filesystem::path file(filename);
126146
if(file.extension() != ".png")
127147
{
@@ -133,6 +153,7 @@ extern "C"
133153

134154
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
135155
{
156+
MLX_CHECK_APPLICATION_POINTER(mlx);
136157
std::filesystem::path file(filename);
137158
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
138159
{
@@ -144,6 +165,7 @@ extern "C"
144165

145166
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
146167
{
168+
MLX_CHECK_APPLICATION_POINTER(mlx);
147169
std::filesystem::path file(filename);
148170
if(file.extension() != ".bmp" && file.extension() != ".dib")
149171
{
@@ -155,6 +177,7 @@ extern "C"
155177

156178
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
157179
{
180+
MLX_CHECK_APPLICATION_POINTER(mlx);
158181
unsigned char color_bits[4];
159182
color_bits[0] = (color & 0x00FF0000) >> 16;
160183
color_bits[1] = (color & 0x0000FF00) >> 8;
@@ -166,6 +189,7 @@ extern "C"
166189

167190
int mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str)
168191
{
192+
MLX_CHECK_APPLICATION_POINTER(mlx);
169193
unsigned char color_bits[4];
170194
color_bits[0] = (color & 0x00FF0000) >> 16;
171195
color_bits[1] = (color & 0x0000FF00) >> 8;
@@ -177,6 +201,7 @@ extern "C"
177201

178202
void mlx_set_font(void* mlx, void* win, char* filepath)
179203
{
204+
MLX_CHECK_APPLICATION_POINTER(mlx);
180205
std::filesystem::path file(filepath);
181206
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
182207
{
@@ -188,6 +213,7 @@ extern "C"
188213

189214
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
190215
{
216+
MLX_CHECK_APPLICATION_POINTER(mlx);
191217
std::filesystem::path file(filepath);
192218
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
193219
{
@@ -199,25 +225,30 @@ extern "C"
199225

200226
int mlx_clear_window(void* mlx, void* win)
201227
{
228+
MLX_CHECK_APPLICATION_POINTER(mlx);
202229
static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win);
203230
return 0;
204231
}
205232

206233
int mlx_destroy_window(void* mlx, void* win)
207234
{
235+
MLX_CHECK_APPLICATION_POINTER(mlx);
208236
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win);
209237
return 0;
210238
}
211239

212240
int mlx_destroy_display(void* mlx)
213241
{
242+
MLX_CHECK_APPLICATION_POINTER(mlx);
214243
delete static_cast<mlx::core::Application*>(mlx);
215244
mlx::Render_Core::get().destroy();
245+
__mlx_ptr = nullptr;
216246
return 0;
217247
}
218248

219249
int mlx_get_screens_size(void* mlx, int* w, int* h)
220250
{
251+
MLX_CHECK_APPLICATION_POINTER(mlx);
221252
static_cast<mlx::core::Application*>(mlx)->getScreenSize(w, h);
222253
return 0;
223254
}

src/renderer/buffers/vk_buffer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/08 18:55:57 by maldavid #+# #+# */
9-
/* Updated: 2023/12/15 21:48:02 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/16 17:10:17 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -98,7 +98,6 @@ namespace mlx
9898

9999
// TODO, use global cmd buffer pool to manage resources
100100
CmdBuffer& cmd = Render_Core::get().getSingleTimeCmdBuffer();
101-
cmd.reset();
102101
cmd.beginRecord();
103102

104103
VkBufferCopy copyRegion{};

src/renderer/command/single_time_cmd_manager.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/12/15 19:57:49 by maldavid #+# #+# */
9-
/* Updated: 2023/12/15 20:21:54 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/16 18:46:26 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -22,26 +22,32 @@ namespace mlx
2222
{
2323
_pool.init();
2424
for(int i = 0; i < MIN_POOL_SIZE; i++)
25-
_buffers.emplace_back().init(&_pool);
26-
}
27-
28-
void SingleTimeCmdManager::destroy() noexcept
29-
{
30-
std::for_each(_buffers.begin(), _buffers.end(), [](CmdBuffer& buf)
3125
{
32-
buf.destroy();
33-
});
34-
_pool.destroy();
26+
_buffers.emplace_back();
27+
_buffers.back().init(&_pool);
28+
}
3529
}
3630

3731
CmdBuffer& SingleTimeCmdManager::getCmdBuffer() noexcept
3832
{
3933
for(CmdBuffer& buf : _buffers)
4034
{
4135
if(buf.isReadyToBeUsed())
36+
{
37+
buf.reset();
4238
return buf;
39+
}
4340
}
4441
_buffers.emplace_back().init(&_pool);
4542
return _buffers.back();
4643
}
44+
45+
void SingleTimeCmdManager::destroy() noexcept
46+
{
47+
std::for_each(_buffers.begin(), _buffers.end(), [](CmdBuffer& buf)
48+
{
49+
buf.destroy();
50+
});
51+
_pool.destroy();
52+
}
4753
}

src/renderer/command/single_time_cmd_manager.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/12/15 18:25:57 by maldavid #+# #+# */
9-
/* Updated: 2023/12/15 19:59:40 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/16 18:09:56 by maldavid ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -35,6 +35,8 @@ namespace mlx
3535

3636
inline static constexpr const uint8_t MIN_POOL_SIZE = 8;
3737

38+
private:
39+
3840
private:
3941
std::vector<CmdBuffer> _buffers;
4042
CmdPool _pool;

0 commit comments

Comments
 (0)