6
6
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
7
7
/* +#+#+#+#+#+ +#+ */
8
8
/* 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 */
10
10
/* */
11
11
/* ************************************************************************** */
12
12
17
17
#include < filesystem>
18
18
#include < mlx.h>
19
19
#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
20
28
21
29
extern " C"
22
30
{
23
31
void * mlx_init ()
24
32
{
25
- static bool init = false ;
26
- if (init)
33
+ if (__mlx_ptr != nullptr )
27
34
{
28
35
mlx::core::error::report (e_kind::error, " MLX cannot be initialized multiple times" );
29
36
return NULL ; // not nullptr for the C compatibility
@@ -33,29 +40,33 @@ extern "C"
33
40
mlx::Render_Core::get ().init ();
34
41
if (app == nullptr )
35
42
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 ;
38
45
}
39
46
40
47
void * mlx_new_window (void * mlx, int w, int h, const char * title)
41
48
{
49
+ MLX_CHECK_APPLICATION_POINTER (mlx);
42
50
return static_cast <mlx::core::Application*>(mlx)->newGraphicsSuport (w, h, title);
43
51
}
44
52
45
53
int mlx_loop_hook (void * mlx, int (*f)(void *), void* param)
46
54
{
55
+ MLX_CHECK_APPLICATION_POINTER (mlx);
47
56
static_cast <mlx::core::Application*>(mlx)->loopHook (f, param);
48
57
return 0 ;
49
58
}
50
59
51
60
int mlx_loop (void * mlx)
52
61
{
62
+ MLX_CHECK_APPLICATION_POINTER (mlx);
53
63
static_cast <mlx::core::Application*>(mlx)->run ();
54
64
return 0 ;
55
65
}
56
66
57
67
int mlx_loop_end (void * mlx)
58
68
{
69
+ MLX_CHECK_APPLICATION_POINTER (mlx);
59
70
static_cast <mlx::core::Application*>(mlx)->loopEnd ();
60
71
return 0 ;
61
72
}
@@ -72,34 +83,40 @@ extern "C"
72
83
73
84
int mlx_mouse_move (void * mlx, void * win, int x, int y)
74
85
{
86
+ MLX_CHECK_APPLICATION_POINTER (mlx);
75
87
static_cast <mlx::core::Application*>(mlx)->mouseMove (win, x, y);
76
88
return 0 ;
77
89
}
78
90
79
91
int mlx_mouse_get_pos (void * mlx, int * x, int * y)
80
92
{
93
+ MLX_CHECK_APPLICATION_POINTER (mlx);
81
94
static_cast <mlx::core::Application*>(mlx)->getMousePos (x, y);
82
95
return 0 ;
83
96
}
84
97
85
98
int mlx_on_event (void * mlx, void * win, mlx_event_type event, int (*funct_ptr)(int , void *), void* param)
86
99
{
100
+ MLX_CHECK_APPLICATION_POINTER (mlx);
87
101
static_cast <mlx::core::Application*>(mlx)->onEvent (win, static_cast <int >(event), funct_ptr, param);
88
102
return 0 ;
89
103
}
90
104
91
105
void * mlx_new_image (void * mlx, int width, int height)
92
106
{
107
+ MLX_CHECK_APPLICATION_POINTER (mlx);
93
108
return static_cast <mlx::core::Application*>(mlx)->newTexture (width, height);
94
109
}
95
110
96
111
int mlx_get_image_pixel (void * mlx, void * img, int x, int y)
97
112
{
113
+ MLX_CHECK_APPLICATION_POINTER (mlx);
98
114
return static_cast <mlx::core::Application*>(mlx)->getTexturePixel (img, x, y);
99
115
}
100
116
101
117
void mlx_set_image_pixel (void * mlx, void * img, int x, int y, int color)
102
118
{
119
+ MLX_CHECK_APPLICATION_POINTER (mlx);
103
120
unsigned char color_bits[4 ];
104
121
color_bits[0 ] = (color & 0x00FF0000 ) >> 16 ;
105
122
color_bits[1 ] = (color & 0x0000FF00 ) >> 8 ;
@@ -110,18 +127,21 @@ extern "C"
110
127
111
128
int mlx_put_image_to_window (void * mlx, void * win, void * img, int x, int y)
112
129
{
130
+ MLX_CHECK_APPLICATION_POINTER (mlx);
113
131
static_cast <mlx::core::Application*>(mlx)->texturePut (win, img, x, y);
114
132
return 0 ;
115
133
}
116
134
117
135
int mlx_destroy_image (void * mlx, void * img)
118
136
{
137
+ MLX_CHECK_APPLICATION_POINTER (mlx);
119
138
static_cast <mlx::core::Application*>(mlx)->destroyTexture (img);
120
139
return 0 ;
121
140
}
122
141
123
142
void * mlx_png_file_to_image (void * mlx, char * filename, int * width, int * height)
124
143
{
144
+ MLX_CHECK_APPLICATION_POINTER (mlx);
125
145
std::filesystem::path file (filename);
126
146
if (file.extension () != " .png" )
127
147
{
@@ -133,6 +153,7 @@ extern "C"
133
153
134
154
void * mlx_jpg_file_to_image (void * mlx, char * filename, int * width, int * height)
135
155
{
156
+ MLX_CHECK_APPLICATION_POINTER (mlx);
136
157
std::filesystem::path file (filename);
137
158
if (file.extension () != " .jpg" && file.extension () != " .jpeg" )
138
159
{
@@ -144,6 +165,7 @@ extern "C"
144
165
145
166
void * mlx_bmp_file_to_image (void * mlx, char * filename, int * width, int * height)
146
167
{
168
+ MLX_CHECK_APPLICATION_POINTER (mlx);
147
169
std::filesystem::path file (filename);
148
170
if (file.extension () != " .bmp" && file.extension () != " .dib" )
149
171
{
@@ -155,6 +177,7 @@ extern "C"
155
177
156
178
int mlx_pixel_put (void * mlx, void * win, int x, int y, int color)
157
179
{
180
+ MLX_CHECK_APPLICATION_POINTER (mlx);
158
181
unsigned char color_bits[4 ];
159
182
color_bits[0 ] = (color & 0x00FF0000 ) >> 16 ;
160
183
color_bits[1 ] = (color & 0x0000FF00 ) >> 8 ;
@@ -166,6 +189,7 @@ extern "C"
166
189
167
190
int mlx_string_put (void * mlx, void * win, int x, int y, int color, char * str)
168
191
{
192
+ MLX_CHECK_APPLICATION_POINTER (mlx);
169
193
unsigned char color_bits[4 ];
170
194
color_bits[0 ] = (color & 0x00FF0000 ) >> 16 ;
171
195
color_bits[1 ] = (color & 0x0000FF00 ) >> 8 ;
@@ -177,6 +201,7 @@ extern "C"
177
201
178
202
void mlx_set_font (void * mlx, void * win, char * filepath)
179
203
{
204
+ MLX_CHECK_APPLICATION_POINTER (mlx);
180
205
std::filesystem::path file (filepath);
181
206
if (std::strcmp (filepath, " default" ) != 0 && file.extension () != " .ttf" && file.extension () != " .tte" )
182
207
{
@@ -188,6 +213,7 @@ extern "C"
188
213
189
214
void mlx_set_font_scale (void * mlx, void * win, char * filepath, float scale)
190
215
{
216
+ MLX_CHECK_APPLICATION_POINTER (mlx);
191
217
std::filesystem::path file (filepath);
192
218
if (std::strcmp (filepath, " default" ) != 0 && file.extension () != " .ttf" && file.extension () != " .tte" )
193
219
{
@@ -199,25 +225,30 @@ extern "C"
199
225
200
226
int mlx_clear_window (void * mlx, void * win)
201
227
{
228
+ MLX_CHECK_APPLICATION_POINTER (mlx);
202
229
static_cast <mlx::core::Application*>(mlx)->clearGraphicsSupport (win);
203
230
return 0 ;
204
231
}
205
232
206
233
int mlx_destroy_window (void * mlx, void * win)
207
234
{
235
+ MLX_CHECK_APPLICATION_POINTER (mlx);
208
236
static_cast <mlx::core::Application*>(mlx)->destroyGraphicsSupport (win);
209
237
return 0 ;
210
238
}
211
239
212
240
int mlx_destroy_display (void * mlx)
213
241
{
242
+ MLX_CHECK_APPLICATION_POINTER (mlx);
214
243
delete static_cast <mlx::core::Application*>(mlx);
215
244
mlx::Render_Core::get ().destroy ();
245
+ __mlx_ptr = nullptr ;
216
246
return 0 ;
217
247
}
218
248
219
249
int mlx_get_screens_size (void * mlx, int * w, int * h)
220
250
{
251
+ MLX_CHECK_APPLICATION_POINTER (mlx);
221
252
static_cast <mlx::core::Application*>(mlx)->getScreenSize (w, h);
222
253
return 0 ;
223
254
}
0 commit comments