MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
sdl.cpp
Go to the documentation of this file.
1
6#include "mx_sdl.h"
7#include <cstdint>
8#include <mxvm/icode.hpp>
9#include <mxvm/instruct.hpp>
10
11extern "C" void mxvm_sdl_init(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
12 int64_t result = init();
13 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
14 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
15 program->vars["%rax"].var_value.int_value = result;
16}
17
18// SDL_Quit
19extern "C" void mxvm_sdl_quit(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
20 quit();
21}
22
23// SDL_CreateWindow
24extern "C" void mxvm_sdl_create_window(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
25 if (operand.size() != 6) {
26 throw mx::Exception("sdl_create_window requires 6 arguments (title, x, y, w, h, flags).");
27 }
28
29 std::string title;
30 if (program->isVariable(operand[0].op)) {
31 mxvm::Variable &var = program->getVariable(operand[0].op);
33 title = var.var_value.str_value;
34 } else if (var.type == mxvm::VarType::VAR_POINTER) {
35 title = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
36 }
37 } else {
38 title = operand[0].op;
39 }
40
41 int64_t x = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
42 int64_t y = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
43 int64_t w = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
44 int64_t h = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
45 int64_t flags = program->isVariable(operand[5].op) ? program->getVariable(operand[5].op).var_value.int_value : operand[5].op_value;
46
47 int64_t result = create_window(title.c_str(), x, y, w, h, flags);
48 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
49 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
50 program->vars["%rax"].var_value.int_value = result;
51}
52
53// SDL_DestroyWindow
54extern "C" void mxvm_sdl_destroy_window(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
55 if (operand.size() != 1) {
56 throw mx::Exception("sdl_destroy_window requires 1 argument (window_id).");
57 }
58
59 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
60
61 destroy_window(window_id);
62}
63
64// SDL_CreateRenderer
65extern "C" void mxvm_sdl_create_renderer(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
66 if (operand.size() != 3) {
67 throw mx::Exception("sdl_create_renderer requires 3 arguments (window_id, index, flags).");
68 }
69
70 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
71 int64_t index = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
72 int64_t flags = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
73
74 int64_t result = create_renderer(window_id, index, flags);
75 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
76 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
77 program->vars["%rax"].var_value.int_value = result;
78}
79
80// SDL_DestroyRenderer
81extern "C" void mxvm_sdl_destroy_renderer(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
82 if (operand.size() != 1) {
83 throw mx::Exception("sdl_destroy_renderer requires 1 argument (renderer_id).");
84 }
85
86 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
87
88 destroy_renderer(renderer_id);
89}
90
91// Window management functions
92extern "C" void mxvm_sdl_set_window_title(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
93 if (operand.size() != 2) {
94 throw mx::Exception("sdl_set_window_title requires 2 arguments (window_id, title).");
95 }
96
97 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
98
99 std::string title;
100 if (program->isVariable(operand[1].op)) {
101 mxvm::Variable &var = program->getVariable(operand[1].op);
102 if (var.type == mxvm::VarType::VAR_STRING) {
103 title = var.var_value.str_value;
104 } else if (var.type == mxvm::VarType::VAR_POINTER) {
105 title = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
106 }
107 } else {
108 title = operand[1].op;
109 }
110
111 set_window_title(window_id, title.c_str());
112}
113
114extern "C" void mxvm_sdl_set_window_position(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
115 if (operand.size() != 3) {
116 throw mx::Exception("sdl_set_window_position requires 3 arguments (window_id, x, y).");
117 }
118
119 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
120 int64_t x = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
121 int64_t y = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
122
123 set_window_position(window_id, x, y);
124}
125
126extern "C" void mxvm_sdl_get_window_size(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
127 if (operand.size() != 3) {
128 throw mx::Exception("sdl_get_window_size requires 3 arguments (window_id, w_var, h_var).");
129 }
130
131 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
132
133 int64_t w, h;
134 get_window_size(window_id, &w, &h);
135
136 // Store width in the second operand variable
137 if (program->isVariable(operand[1].op)) {
138 mxvm::Variable &w_var = program->getVariable(operand[1].op);
141 w_var.var_value.int_value = w;
142 }
143
144 // Store height in the third operand variable
145 if (program->isVariable(operand[2].op)) {
146 mxvm::Variable &h_var = program->getVariable(operand[2].op);
149 h_var.var_value.int_value = h;
150 }
151}
152
153extern "C" void mxvm_sdl_set_window_fullscreen(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
154 if (operand.size() != 2) {
155 throw mx::Exception("sdl_set_window_fullscreen requires 2 arguments (window_id, fullscreen).");
156 }
157
158 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
159 int64_t fullscreen = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
160
161 set_window_fullscreen(window_id, fullscreen);
162}
163
164extern "C" void mxvm_sdl_get_renderer_output_size(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
165 if (operand.size() != 3) {
166 throw mx::Exception("sdl_get_renderer_output_size requires 3 arguments (renderer_id, w_var, h_var).");
167 }
168
169 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
170
171 int64_t w, h;
172 get_renderer_output_size(renderer_id, &w, &h);
173
174 // Store width in the second operand variable
175 if (program->isVariable(operand[1].op)) {
176 mxvm::Variable &w_var = program->getVariable(operand[1].op);
179 w_var.var_value.int_value = w;
180 }
181
182 // Store height in the third operand variable
183 if (program->isVariable(operand[2].op)) {
184 mxvm::Variable &h_var = program->getVariable(operand[2].op);
187 h_var.var_value.int_value = h;
188 }
189}
190
191// Mouse functions
192extern "C" void mxvm_sdl_get_mouse_buttons(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
193 int64_t result = get_mouse_buttons();
194 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
195 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
196 program->vars["%rax"].var_value.int_value = result;
197}
198
199extern "C" void mxvm_sdl_get_relative_mouse_x(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
200 int64_t result = get_relative_mouse_x();
201 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
202 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
203 program->vars["%rax"].var_value.int_value = result;
204}
205
206extern "C" void mxvm_sdl_get_relative_mouse_y(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
207 int64_t result = get_relative_mouse_y();
208 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
209 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
210 program->vars["%rax"].var_value.int_value = result;
211}
212
213extern "C" void mxvm_sdl_get_relative_mouse_buttons(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
214 int64_t result = get_relative_mouse_buttons();
215 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
216 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
217 program->vars["%rax"].var_value.int_value = result;
218}
219
220extern "C" void mxvm_sdl_get_mouse_state(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
221 if (operand.size() != 2) {
222 throw mx::Exception("sdl_get_mouse_state requires 2 arguments (x_var, y_var).");
223 }
224
225 int64_t x, y;
226 int64_t result = get_mouse_state(&x, &y);
227
228 // Store x in the first operand variable
229 if (program->isVariable(operand[0].op)) {
230 mxvm::Variable &x_var = program->getVariable(operand[0].op);
233 x_var.var_value.int_value = x;
234 }
235
236 // Store y in the second operand variable
237 if (program->isVariable(operand[1].op)) {
238 mxvm::Variable &y_var = program->getVariable(operand[1].op);
241 y_var.var_value.int_value = y;
242 }
243
244 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
245 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
246 program->vars["%rax"].var_value.int_value = result;
247}
248
249extern "C" void mxvm_sdl_get_relative_mouse_state(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
250 if (operand.size() != 2) {
251 throw mx::Exception("sdl_get_relative_mouse_state requires 2 arguments (x_var, y_var).");
252 }
253
254 int64_t x, y;
255 int64_t result = get_relative_mouse_state(&x, &y);
256
257 // Store x in the first operand variable
258 if (program->isVariable(operand[0].op)) {
259 mxvm::Variable &x_var = program->getVariable(operand[0].op);
262 x_var.var_value.int_value = x;
263 }
264
265 // Store y in the second operand variable
266 if (program->isVariable(operand[1].op)) {
267 mxvm::Variable &y_var = program->getVariable(operand[1].op);
270 y_var.var_value.int_value = y;
271 }
272
273 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
274 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
275 program->vars["%rax"].var_value.int_value = result;
276}
277
278// Keyboard functions
279extern "C" void mxvm_sdl_get_keyboard_state(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
280 if (operand.size() != 1) {
281 throw mx::Exception("sdl_get_keyboard_state requires 1 argument (numkeys_var).");
282 }
283
284 int64_t numkeys;
285 int64_t result = get_keyboard_state(&numkeys);
286
287 // Store numkeys in the operand variable
288 if (program->isVariable(operand[0].op)) {
289 mxvm::Variable &nk_var = program->getVariable(operand[0].op);
292 nk_var.var_value.int_value = numkeys;
293 }
294
295 program->vars["%rax"].type = mxvm::VarType::VAR_POINTER;
296 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_POINTER;
297 program->vars["%rax"].var_value.ptr_value = (void *)result;
298}
299
300extern "C" void mxvm_sdl_is_key_pressed(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
301 if (operand.size() != 1) {
302 throw mx::Exception("sdl_is_key_pressed requires 1 argument (scancode).");
303 }
304
305 int64_t scancode = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
306
307 int64_t result = is_key_pressed(scancode);
308 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
309 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
310 program->vars["%rax"].var_value.int_value = result;
311}
312
313extern "C" void mxvm_sdl_get_num_keys(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
314 int64_t result = get_num_keys();
315 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
316 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
317 program->vars["%rax"].var_value.int_value = result;
318}
319
320// Clipboard functions
321extern "C" void mxvm_sdl_set_clipboard_text(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
322 if (operand.size() != 1) {
323 throw mx::Exception("sdl_set_clipboard_text requires 1 argument (text).");
324 }
325
326 std::string text;
327 if (program->isVariable(operand[0].op)) {
328 mxvm::Variable &var = program->getVariable(operand[0].op);
329 if (var.type == mxvm::VarType::VAR_STRING) {
330 text = var.var_value.str_value;
331 } else if (var.type == mxvm::VarType::VAR_POINTER) {
332 text = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
333 }
334 } else {
335 text = operand[0].op;
336 }
337
338 set_clipboard_text(text.c_str());
339}
340
341extern "C" void mxvm_sdl_get_clipboard_text(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
342 const char *result = get_clipboard_text();
343 // Store result as a string pointer in %rax
344 program->vars["%rax"].type = mxvm::VarType::VAR_POINTER;
345 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_POINTER;
346 program->vars["%rax"].var_value.ptr_value = (void *)result;
347}
348
349// Cursor functions
350extern "C" void mxvm_sdl_show_cursor(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
351 if (operand.size() != 1) {
352 throw mx::Exception("sdl_show_cursor requires 1 argument (show).");
353 }
354
355 int64_t show = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
356
357 show_cursor(show);
358}
359
360// Surface functions
361extern "C" void mxvm_sdl_create_rgb_surface(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
362 if (operand.size() != 3) {
363 throw mx::Exception("sdl_create_rgb_surface requires 3 arguments (width, height, depth).");
364 }
365
366 int64_t width = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
367 int64_t height = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
368 int64_t depth = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
369
370 int64_t result = create_rgb_surface(width, height, depth);
371 program->vars["%rax"].type = mxvm::VarType::VAR_POINTER;
372 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_POINTER;
373 program->vars["%rax"].var_value.ptr_value = (void *)result;
374}
375
376extern "C" void mxvm_sdl_free_surface(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
377 if (operand.size() != 1) {
378 throw mx::Exception("sdl_free_surface requires 1 argument (surface_ptr).");
379 }
380
381 int64_t surf_ptr = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
382
383 free_surface(surf_ptr);
384}
385
386extern "C" void mxvm_sdl_blit_surface(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
387 if (operand.size() != 4) {
388 throw mx::Exception("sdl_blit_surface requires 4 arguments (src_ptr, dst_ptr, x, y).");
389 }
390
391 int64_t src_ptr = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
392 int64_t dst_ptr = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
393 int64_t x = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
394 int64_t y = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
395
396 int64_t result = blit_surface(src_ptr, dst_ptr, x, y);
397 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
398 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
399 program->vars["%rax"].var_value.int_value = result;
400}
401
402// Additional functions
403extern "C" void mxvm_sdl_free_wav(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
404 if (operand.size() != 1) {
405 throw mx::Exception("sdl_free_wav requires 1 argument (audio_buf).");
406 }
407
408 int64_t audio_buf = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
409
410 free_wav(audio_buf);
411}
412
413// SDL_PollEvent
414extern "C" void mxvm_sdl_poll_event(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
415 int64_t result = poll_event();
416 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
417 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
418 program->vars["%rax"].var_value.int_value = result;
419}
420
421// SDL_GetEventType
422extern "C" void mxvm_sdl_get_event_type(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
423 int64_t result = get_event_type();
424 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
425 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
426 program->vars["%rax"].var_value.int_value = result;
427}
428
429// SDL_GetKeyCode
430extern "C" void mxvm_sdl_get_key_code(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
431 int64_t result = get_key_code();
432 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
433 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
434 program->vars["%rax"].var_value.int_value = result;
435}
436
437// SDL_GetMouseX
438extern "C" void mxvm_sdl_get_mouse_x(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
439 int64_t result = get_mouse_x();
440 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
441 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
442 program->vars["%rax"].var_value.int_value = result;
443}
444
445// SDL_GetMouseY
446extern "C" void mxvm_sdl_get_mouse_y(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
447 int64_t result = get_mouse_y();
448 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
449 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
450 program->vars["%rax"].var_value.int_value = result;
451}
452
453// SDL_GetMouseButton
454extern "C" void mxvm_sdl_get_mouse_button(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
455 int64_t result = get_mouse_button();
456 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
457 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
458 program->vars["%rax"].var_value.int_value = result;
459}
460
461// SDL_SetDrawColor
462extern "C" void mxvm_sdl_set_draw_color(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
463 if (operand.size() != 5) {
464 throw mx::Exception("sdl_set_draw_color requires 5 arguments (renderer_id, r, g, b, a).");
465 }
466
467 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
468 int64_t r = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
469 int64_t g = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
470 int64_t b = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
471 int64_t a = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
472
473 set_draw_color(renderer_id, r, g, b, a);
474}
475
476// SDL_Clear
477extern "C" void mxvm_sdl_clear(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
478 if (operand.size() != 1) {
479 throw mx::Exception("sdl_clear requires 1 argument (renderer_id).");
480 }
481
482 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
483
484 clear(renderer_id);
485}
486
487// SDL_Present
488extern "C" void mxvm_sdl_present(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
489 if (operand.size() != 1) {
490 throw mx::Exception("sdl_present requires 1 argument (renderer_id).");
491 }
492
493 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
494
495 present(renderer_id);
496}
497
498// SDL_DrawPoint
499extern "C" void mxvm_sdl_draw_point(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
500 if (operand.size() != 3) {
501 throw mx::Exception("sdl_draw_point requires 3 arguments (renderer_id, x, y).");
502 }
503
504 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
505 int64_t x = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
506 int64_t y = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
507
508 draw_point(renderer_id, x, y);
509}
510
511// SDL_DrawLine
512extern "C" void mxvm_sdl_draw_line(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
513 if (operand.size() != 5) {
514 throw mx::Exception("sdl_draw_line requires 5 arguments (renderer_id, x1, y1, x2, y2).");
515 }
516
517 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
518 int64_t x1 = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
519 int64_t y1 = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
520 int64_t x2 = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
521 int64_t y2 = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
522
523 draw_line(renderer_id, x1, y1, x2, y2);
524}
525
526// SDL_DrawRect
527extern "C" void mxvm_sdl_draw_rect(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
528 if (operand.size() != 5) {
529 throw mx::Exception("sdl_draw_rect requires 5 arguments (renderer_id, x, y, w, h).");
530 }
531
532 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
533 int64_t x = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
534 int64_t y = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
535 int64_t w = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
536 int64_t h = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
537
538 draw_rect(renderer_id, x, y, w, h);
539}
540
541// SDL_FillRect
542extern "C" void mxvm_sdl_fill_rect(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
543 if (operand.size() != 5) {
544 throw mx::Exception("sdl_fill_rect requires 5 arguments (renderer_id, x, y, w, h).");
545 }
546
547 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
548 int64_t x = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
549 int64_t y = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
550 int64_t w = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
551 int64_t h = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
552
553 fill_rect(renderer_id, x, y, w, h);
554}
555
556// SDL_CreateTexture
557extern "C" void mxvm_sdl_create_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
558 if (operand.size() != 5) {
559 throw mx::Exception("sdl_create_texture requires 5 arguments (renderer_id, format, access, w, h).");
560 }
561
562 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
563 int64_t format = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
564 int64_t access = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
565 int64_t w = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
566 int64_t h = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
567
568 int64_t result = create_texture(renderer_id, format, access, w, h);
569 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
570 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
571 program->vars["%rax"].var_value.int_value = result;
572}
573
574// SDL_DestroyTexture
575extern "C" void mxvm_sdl_destroy_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
576 if (operand.size() != 1) {
577 throw mx::Exception("sdl_destroy_texture requires 1 argument (texture_id).");
578 }
579
580 int64_t texture_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
581
582 destroy_texture(texture_id);
583}
584
585// SDL_LoadTexture
586extern "C" void mxvm_sdl_load_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
587 if (operand.size() != 2) {
588 throw mx::Exception("sdl_load_texture requires 2 arguments (renderer_id, file_path).");
589 }
590
591 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
592
593 std::string file_path;
594 if (program->isVariable(operand[1].op)) {
595 mxvm::Variable &var = program->getVariable(operand[1].op);
596 if (var.type == mxvm::VarType::VAR_STRING) {
597 file_path = var.var_value.str_value;
598 } else if (var.type == mxvm::VarType::VAR_POINTER) {
599 file_path = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
600 }
601 } else {
602 file_path = operand[1].op;
603 }
604
605 int64_t result = load_texture(renderer_id, file_path.c_str());
606 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
607 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
608 program->vars["%rax"].var_value.int_value = result;
609}
610
611// SDL_LoadTextureColorKey
612extern "C" void mxvm_sdl_load_texture_color_key(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
613 if (operand.size() != 2) {
614 throw mx::Exception("sdl_load_texture_color_key requires 2 arguments (renderer_id, file_path).");
615 }
616
617 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
618
619 std::string file_path;
620 if (program->isVariable(operand[1].op)) {
621 mxvm::Variable &var = program->getVariable(operand[1].op);
622 if (var.type == mxvm::VarType::VAR_STRING) {
623 file_path = var.var_value.str_value;
624 } else if (var.type == mxvm::VarType::VAR_POINTER) {
625 file_path = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
626 }
627 } else {
628 file_path = operand[1].op;
629 }
630
631 int64_t result = load_texture_color_key(renderer_id, file_path.c_str());
632 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
633 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
634 program->vars["%rax"].var_value.int_value = result;
635}
636
647extern "C" void mxvm_sdl_load_texture_color_key_rgb(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
648 if (operand.size() != 5) {
649 throw mx::Exception("sdl_load_texture_color_key_rgb requires 5 arguments (renderer_id, file_path, r, g, b).");
650 }
651
652 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
653
654 std::string file_path;
655 if (program->isVariable(operand[1].op)) {
656 mxvm::Variable &var = program->getVariable(operand[1].op);
657 if (var.type == mxvm::VarType::VAR_STRING) {
658 file_path = var.var_value.str_value;
659 } else if (var.type == mxvm::VarType::VAR_POINTER) {
660 file_path = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
661 }
662 } else {
663 file_path = operand[1].op;
664 }
665
666 int64_t r = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
667 int64_t g = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
668 int64_t b = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
669
670 int64_t result = load_texture_color_key_rgb(renderer_id, file_path.c_str(), r, g, b);
671 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
672 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
673 program->vars["%rax"].var_value.int_value = result;
674}
675
676// SDL_RenderTexture
677extern "C" void mxvm_sdl_render_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
678 if (operand.size() != 10) {
679 throw mx::Exception("sdl_render_texture requires 10 arguments (renderer_id, texture_id, src_x, src_y, src_w, src_h, dst_x, dst_y, dst_w, dst_h).");
680 }
681
682 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
683 int64_t texture_id = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
684 int64_t src_x = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
685 int64_t src_y = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
686 int64_t src_w = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
687 int64_t src_h = program->isVariable(operand[5].op) ? program->getVariable(operand[5].op).var_value.int_value : operand[5].op_value;
688 int64_t dst_x = program->isVariable(operand[6].op) ? program->getVariable(operand[6].op).var_value.int_value : operand[6].op_value;
689 int64_t dst_y = program->isVariable(operand[7].op) ? program->getVariable(operand[7].op).var_value.int_value : operand[7].op_value;
690 int64_t dst_w = program->isVariable(operand[8].op) ? program->getVariable(operand[8].op).var_value.int_value : operand[8].op_value;
691 int64_t dst_h = program->isVariable(operand[9].op) ? program->getVariable(operand[9].op).var_value.int_value : operand[9].op_value;
692
693 render_texture(renderer_id, texture_id, src_x, src_y, src_w, src_h, dst_x, dst_y, dst_w, dst_h);
694}
695
696// Timing functions
697extern "C" void mxvm_sdl_get_ticks(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
698 int64_t result = get_ticks();
699 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
700 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
701 program->vars["%rax"].var_value.int_value = result;
702}
703
704extern "C" void mxvm_sdl_delay(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
705 if (operand.size() != 1) {
706 throw mx::Exception("sdl_delay requires 1 argument (ms).");
707 }
708
709 int64_t ms = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
710
711 delay(ms);
712}
713
714// Audio functions
715extern "C" void mxvm_sdl_open_audio(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
716 if (operand.size() != 4) {
717 throw mx::Exception("sdl_open_audio requires 4 arguments (freq, format, channels, samples).");
718 }
719
720 int64_t frequency = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
721 int64_t format = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
722 int64_t channels = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
723 int64_t samples = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
724
725 int64_t result = open_audio(frequency, format, channels, samples);
726 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
727 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
728 program->vars["%rax"].var_value.int_value = result;
729}
730
731extern "C" void mxvm_sdl_close_audio(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
732 close_audio();
733}
734
735extern "C" void mxvm_sdl_pause_audio(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
736 if (operand.size() != 1) {
737 throw mx::Exception("sdl_pause_audio requires 1 argument (pause_on).");
738 }
739
740 int64_t pause_on = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
741
742 pause_audio(pause_on);
743}
744
745extern "C" void mxvm_sdl_load_wav(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
746 if (operand.size() != 4) {
747 throw mx::Exception("sdl_load_wav requires 4 arguments (file_path, audio_buf_var, audio_len_var, audio_spec_var).");
748 }
749
750 std::string file_path;
751 if (program->isVariable(operand[0].op)) {
752 mxvm::Variable &var = program->getVariable(operand[0].op);
753 if (var.type == mxvm::VarType::VAR_STRING) {
754 file_path = var.var_value.str_value;
755 } else if (var.type == mxvm::VarType::VAR_POINTER) {
756 file_path = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
757 }
758 } else {
759 file_path = operand[0].op;
760 }
761
762 int64_t audio_buf, audio_len, audio_spec;
763 int64_t result = load_wav(file_path.c_str(), &audio_buf, &audio_len, &audio_spec);
764
765 // Store results in variables
766 if (program->isVariable(operand[1].op)) {
767 mxvm::Variable &buf_var = program->getVariable(operand[1].op);
770 buf_var.var_value.int_value = audio_buf;
771 }
772
773 if (program->isVariable(operand[2].op)) {
774 mxvm::Variable &len_var = program->getVariable(operand[2].op);
777 len_var.var_value.int_value = audio_len;
778 }
779
780 if (program->isVariable(operand[3].op)) {
781 mxvm::Variable &spec_var = program->getVariable(operand[3].op);
784 spec_var.var_value.int_value = audio_spec;
785 }
786
787 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
788 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
789 program->vars["%rax"].var_value.int_value = result;
790}
791
792extern "C" void mxvm_sdl_queue_audio(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
793 if (operand.size() != 2) {
794 throw mx::Exception("sdl_queue_audio requires 2 arguments (data, len).");
795 }
796
797 void *data = nullptr;
798 if (program->isVariable(operand[0].op)) {
799 mxvm::Variable &var = program->getVariable(operand[0].op);
800 if (var.type == mxvm::VarType::VAR_POINTER) {
801 data = var.var_value.ptr_value;
802 }
803 } else {
804 data = reinterpret_cast<void *>(static_cast<uintptr_t>(operand[0].op_value));
805 }
806
807 int64_t len = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
808
809 int64_t result = queue_audio(data, len);
810 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
811 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
812 program->vars["%rax"].var_value.int_value = result;
813}
814
815extern "C" void mxvm_sdl_get_queued_audio_size(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
816 int64_t result = get_queued_audio_size();
817 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
818 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
819 program->vars["%rax"].var_value.int_value = result;
820}
821
822extern "C" void mxvm_sdl_clear_queued_audio(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
824}
825
826// Texture functions
827extern "C" void mxvm_sdl_update_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
828 if (operand.size() != 3) {
829 throw mx::Exception("sdl_update_texture requires 3 arguments (texture_id, pixels, pitch).");
830 }
831
832 int64_t texture_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
833
834 void *pixels = nullptr;
835 if (program->isVariable(operand[1].op)) {
836 mxvm::Variable &var = program->getVariable(operand[1].op);
837 if (var.type == mxvm::VarType::VAR_POINTER) {
838 pixels = var.var_value.ptr_value;
839 }
840 } else {
841 pixels = reinterpret_cast<void *>(static_cast<uintptr_t>(operand[1].op_value));
842 }
843
844 int64_t pitch = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
845
846 int64_t result = update_texture(texture_id, pixels, pitch);
847 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
848 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
849 program->vars["%rax"].var_value.int_value = result;
850}
851
852extern "C" void mxvm_sdl_lock_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
853 if (operand.size() != 3) {
854 throw mx::Exception("sdl_lock_texture requires 3 arguments (texture_id, pixels_var, pitch_var).");
855 }
856
857 int64_t texture_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
858
859 void *pixels;
860 int64_t pitch;
861 int64_t result = lock_texture(texture_id, &pixels, &pitch);
862
863 // Store results in variables
864 if (program->isVariable(operand[1].op)) {
865 mxvm::Variable &pixels_var = program->getVariable(operand[1].op);
866 pixels_var.type = mxvm::VarType::VAR_POINTER;
868 pixels_var.var_value.ptr_value = pixels;
869 }
870
871 if (program->isVariable(operand[2].op)) {
872 mxvm::Variable &pitch_var = program->getVariable(operand[2].op);
875 pitch_var.var_value.int_value = pitch;
876 }
877
878 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
879 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
880 program->vars["%rax"].var_value.int_value = result;
881}
882
883extern "C" void mxvm_sdl_unlock_texture(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
884 if (operand.size() != 1) {
885 throw mx::Exception("sdl_unlock_texture requires 1 argument (texture_id).");
886 }
887
888 int64_t texture_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
889
890 unlock_texture(texture_id);
891}
892
893extern "C" void mxvm_sdl_init_text(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
894 int64_t result = init_text();
895 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
896 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
897 program->vars["%rax"].var_value.int_value = result;
898}
899
900extern "C" void mxvm_sdl_quit_text(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
901 quit_text();
902}
903
904extern "C" void mxvm_sdl_load_font(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
905 if (operand.size() != 2) {
906 throw mx::Exception("sdl_load_font requires 2 arguments (file, ptsize).");
907 }
908 std::string file;
909 if (program->isVariable(operand[0].op)) {
910 mxvm::Variable &var = program->getVariable(operand[0].op);
911 if (var.type == mxvm::VarType::VAR_STRING) {
912 file = var.var_value.str_value;
913 } else if (var.type == mxvm::VarType::VAR_POINTER) {
914 file = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
915 }
916 } else {
917 file = operand[0].op;
918 }
919 int64_t ptsize = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
920
921 int64_t result = load_font(file.c_str(), ptsize);
922 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
923 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
924 program->vars["%rax"].var_value.int_value = result;
925}
926
927extern "C" void mxvm_sdl_draw_text(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
928 if (operand.size() != 9) {
929 throw mx::Exception("sdl_draw_text requires 9 arguments (renderer_id, font_id, text, x, y, r, g, b, a).");
930 }
931 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
932 int64_t font_id = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
933
934 std::string text;
935 if (program->isVariable(operand[2].op)) {
936 mxvm::Variable &var = program->getVariable(operand[2].op);
937 if (var.type == mxvm::VarType::VAR_STRING) {
938 text = var.var_value.str_value;
939 } else if (var.type == mxvm::VarType::VAR_POINTER) {
940 text = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
941 }
942 } else {
943 text = operand[2].op;
944 }
945
946 int64_t x = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
947 int64_t y = program->isVariable(operand[4].op) ? program->getVariable(operand[4].op).var_value.int_value : operand[4].op_value;
948 int64_t r = program->isVariable(operand[5].op) ? program->getVariable(operand[5].op).var_value.int_value : operand[5].op_value;
949 int64_t g = program->isVariable(operand[6].op) ? program->getVariable(operand[6].op).var_value.int_value : operand[6].op_value;
950 int64_t b = program->isVariable(operand[7].op) ? program->getVariable(operand[7].op).var_value.int_value : operand[7].op_value;
951 int64_t a = program->isVariable(operand[8].op) ? program->getVariable(operand[8].op).var_value.int_value : operand[8].op_value;
952
953 draw_text(renderer_id, font_id, text.c_str(), x, y, r, g, b, a);
954}
955
956extern "C" void mxvm_sdl_create_render_target(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
957 if (operand.size() != 3) {
958 throw mx::Exception("sdl_create_render_target requires 3 arguments (renderer_id, width, height).");
959 }
960
961 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
962 int64_t width = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
963 int64_t height = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
964
965 int64_t result = create_render_target(renderer_id, width, height);
966 program->vars["%rax"].type = mxvm::VarType::VAR_INTEGER;
967 program->vars["%rax"].var_value.type = mxvm::VarType::VAR_INTEGER;
968 program->vars["%rax"].var_value.int_value = result;
969}
970
971extern "C" void mxvm_sdl_set_render_target(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
972 if (operand.size() != 2) {
973 throw mx::Exception("sdl_set_render_target requires 2 arguments (renderer_id, target_id).");
974 }
975
976 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
977 int64_t target_id = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
978
979 set_render_target(renderer_id, target_id);
980}
981
982extern "C" void mxvm_sdl_destroy_render_target(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
983 if (operand.size() != 1) {
984 throw mx::Exception("sdl_destroy_render_target requires 1 argument (target_id).");
985 }
986
987 int64_t target_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
988
989 destroy_render_target(target_id);
990}
991
992extern "C" void mxvm_sdl_present_scaled(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
993 if (operand.size() != 4) {
994 throw mx::Exception("sdl_present_scaled requires 4 arguments (renderer_id, target_id, src_width, src_height).");
995 }
996
997 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
998 int64_t target_id = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
999 int64_t src_w = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
1000 int64_t src_h = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
1001
1002 present_scaled(renderer_id, target_id, src_w, src_h);
1003}
1004
1005extern "C" void mxvm_sdl_present_stretched(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
1006 if (operand.size() != 4) {
1007 throw mx::Exception("sdl_present_stretched requires 4 arguments (renderer_id, target_id, dst_width, dst_height).");
1008 }
1009
1010 int64_t renderer_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
1011 int64_t target_id = program->isVariable(operand[1].op) ? program->getVariable(operand[1].op).var_value.int_value : operand[1].op_value;
1012 int64_t dst_w = program->isVariable(operand[2].op) ? program->getVariable(operand[2].op).var_value.int_value : operand[2].op_value;
1013 int64_t dst_h = program->isVariable(operand[3].op) ? program->getVariable(operand[3].op).var_value.int_value : operand[3].op_value;
1014
1015 present_stretched(renderer_id, target_id, dst_w, dst_h);
1016}
1017
1018extern "C" void mxvm_sdl_set_window_icon(mxvm::Program *program, std::vector<mxvm::Operand> &operand) {
1019
1020 int64_t window_id = program->isVariable(operand[0].op) ? program->getVariable(operand[0].op).var_value.int_value : operand[0].op_value;
1021
1022 std::string path;
1023 if (program->isVariable(operand[1].op)) {
1024 mxvm::Variable &var = program->getVariable(operand[1].op);
1025 if (var.type == mxvm::VarType::VAR_STRING) {
1026 path = var.var_value.str_value;
1027 } else if (var.type == mxvm::VarType::VAR_POINTER) {
1028 path = std::string(reinterpret_cast<const char *>(var.var_value.ptr_value));
1029 }
1030 } else {
1031 path = operand[0].op;
1032 }
1033
1034 set_window_icon(window_id, path.c_str());
1035}
General-purpose exception with errno-aware factory method.
Definition exception.hpp:38
std::unordered_map< std::string, Variable > vars
variable symbol table
Definition icode.hpp:193
Complete MXVM program: instruction interpreter and native x64 code generator.
Definition icode.hpp:212
Variable & getVariable(const std::string &name)
Look up a variable by name, searching local scope then global.
bool isVariable(const std::string &name)
Check whether a variable exists in local or global scope.
Intermediate code representation, execution engine, and native x64 code generation (SysV + Win64).
Instruction set enum, operand/instruction structs, variable types, and Variable/Variable_Value defini...
SDL2 module C API — function declarations for SDL2 bindings exported to MXVM programs.
int64_t poll_event(void)
Poll for a pending SDL event.
Definition sdl.c:182
void quit(void)
Shut down SDL2 and release all resources.
Definition sdl.c:34
void fill_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h)
Draw a filled rectangle.
Definition sdl.c:261
int64_t get_queued_audio_size(void)
Get the number of bytes of queued audio.
Definition sdl.c:506
void set_draw_color(int64_t renderer_id, int64_t r, int64_t g, int64_t b, int64_t a)
Set the current draw color (RGBA).
Definition sdl.c:206
void get_renderer_output_size(int64_t renderer_id, int64_t *w, int64_t *h)
Get the renderer output size.
Definition sdl.c:596
int64_t create_window(const char *title, int64_t x, int64_t y, int64_t w, int64_t h, int64_t flags)
Create an SDL window.
Definition sdl.c:86
int64_t get_relative_mouse_state(int64_t *x, int64_t *y)
Get the relative mouse state.
Definition sdl.c:703
int64_t open_audio(int64_t freq, int64_t format, int64_t channels, int64_t samples)
Open the audio device.
Definition sdl.c:458
void render_texture(int64_t renderer_id, int64_t texture_id, int64_t src_x, int64_t src_y, int64_t src_w, int64_t src_h, int64_t dst_x, int64_t dst_y, int64_t dst_w, int64_t dst_h)
Render a portion of a texture to a destination rectangle.
Definition sdl.c:397
void set_clipboard_text(const char *text)
Set the system clipboard text.
Definition sdl.c:557
const char * get_clipboard_text(void)
Get the system clipboard text.
Definition sdl.c:561
void set_window_title(int64_t window_id, const char *title)
Set the title of a window.
Definition sdl.c:569
int64_t get_num_keys(void)
Get the total number of keyboard keys.
Definition sdl.c:551
int64_t get_key_code(void)
Get the key code from the last keyboard event.
Definition sdl.c:190
void set_render_target(int64_t renderer_id, int64_t target_id)
Set the current render target (0 to render to the window).
Definition sdl.c:173
int64_t get_mouse_state(int64_t *x, int64_t *y)
Get the current mouse state with pointer coordinates.
Definition sdl.c:693
void free_surface(int64_t surf_ptr)
Free a surface.
Definition sdl.c:679
int64_t get_mouse_button(void)
Get the mouse button from the last mouse event.
Definition sdl.c:202
int64_t update_texture(int64_t texture_id, const void *pixels, int64_t pitch)
Update a texture with new pixel data.
Definition sdl.c:427
int64_t load_wav(const char *file_path, int64_t *audio_buf, int64_t *audio_len, int64_t *audio_spec)
Load a WAV file.
Definition sdl.c:478
int64_t get_mouse_buttons(void)
Get the current mouse button bitmask (pointerless variant).
Definition sdl.c:514
int64_t create_rgb_surface(int64_t width, int64_t height, int64_t depth)
Create an RGB surface.
Definition sdl.c:663
void quit_text(void)
Shut down the SDL_ttf subsystem.
Definition sdl.c:613
void set_window_position(int64_t window_id, int64_t x, int64_t y)
Set the position of a window.
Definition sdl.c:575
int64_t get_relative_mouse_y(void)
Get the relative mouse y delta (pointerless variant).
Definition sdl.c:525
int64_t create_renderer(int64_t window_id, int64_t index, int64_t flags)
Create a hardware-accelerated renderer for a window.
Definition sdl.c:121
void destroy_renderer(int64_t renderer_id)
Destroy a renderer by ID.
Definition sdl.c:139
void delay(int64_t ms)
Pause execution for the given number of milliseconds.
Definition sdl.c:454
int64_t get_keyboard_state(int64_t *numkeys)
Get the keyboard state array.
Definition sdl.c:536
void get_window_size(int64_t window_id, int64_t *w, int64_t *h)
Get the size of a window.
Definition sdl.c:581
void clear_queued_audio(void)
Clear all queued audio data.
Definition sdl.c:510
int64_t get_event_type(void)
Get the type of the last polled event.
Definition sdl.c:186
int64_t init(void)
Initialize SDL2 subsystems.
Definition sdl.c:26
int64_t load_texture_color_key_rgb(int64_t renderer_id, const char *file_path, int64_t r, int64_t g, int64_t b)
Load a BMP file as a texture with a custom RGB color-key transparency.
Definition sdl.c:371
void draw_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h)
Draw a rectangle outline.
Definition sdl.c:254
int64_t get_relative_mouse_x(void)
Get the relative mouse x delta (pointerless variant).
Definition sdl.c:519
int64_t get_mouse_y(void)
Get the mouse y coordinate from the last mouse event.
Definition sdl.c:198
void show_cursor(int64_t show)
Show or hide the mouse cursor.
Definition sdl.c:605
void clear(int64_t renderer_id)
Clear the renderer with the current draw color.
Definition sdl.c:212
void pause_audio(int64_t pause_on)
Pause or unpause audio playback.
Definition sdl.c:474
void draw_text(int64_t renderer_id, int64_t font_id, const char *text, int64_t x, int64_t y, int64_t r, int64_t g, int64_t b, int64_t a)
Draw text with a font at the given position and color.
Definition sdl.c:640
void destroy_window(int64_t window_id)
Destroy a window by ID.
Definition sdl.c:106
void free_wav(int64_t audio_buf)
Free a loaded WAV buffer.
Definition sdl.c:496
int64_t get_relative_mouse_buttons(void)
Get the relative mouse button bitmask (pointerless variant).
Definition sdl.c:531
int64_t load_texture_color_key(int64_t renderer_id, const char *file_path)
Load a BMP file as a texture with black color-key transparency.
Definition sdl.c:330
void destroy_render_target(int64_t target_id)
Destroy a render target texture.
Definition sdl.c:178
int64_t is_key_pressed(int64_t scancode)
Check whether a specific key is currently pressed.
Definition sdl.c:543
int64_t get_mouse_x(void)
Get the mouse x coordinate from the last mouse event.
Definition sdl.c:194
int64_t create_texture(int64_t renderer_id, int64_t format, int64_t access, int64_t w, int64_t h)
Create a blank texture.
Definition sdl.c:268
void draw_line(int64_t renderer_id, int64_t x1, int64_t y1, int64_t x2, int64_t y2)
Draw a line between two points.
Definition sdl.c:248
void present_stretched(int64_t renderer_id, int64_t target_id, int64_t dst_width, int64_t dst_height)
Present a render target stretched to the given dimensions.
Definition sdl.c:233
void set_window_icon(uint64_t window_id, const char *path)
Set the window icon from an image file.
Definition sdl.c:113
int64_t queue_audio(const void *data, int64_t len)
Queue audio data for playback.
Definition sdl.c:502
void set_window_fullscreen(int64_t window_id, int64_t fullscreen)
Set or clear fullscreen mode for a window.
Definition sdl.c:590
void close_audio(void)
Close the audio device.
Definition sdl.c:470
int64_t load_font(const char *file, int64_t ptsize)
Load a TrueType font.
Definition sdl.c:626
int64_t create_render_target(int64_t renderer_id, int64_t width, int64_t height)
Create an off-screen render target texture.
Definition sdl.c:146
void present_scaled(int64_t renderer_id, int64_t target_id, int64_t scale_width, int64_t scale_height)
Present a render target scaled to the given dimensions.
Definition sdl.c:224
int64_t load_texture(int64_t renderer_id, const char *file_path)
Load a texture from an image file.
Definition sdl.c:306
void unlock_texture(int64_t texture_id)
Unlock a previously locked texture.
Definition sdl.c:444
void present(int64_t renderer_id)
Present the renderer's back buffer to the screen.
Definition sdl.c:218
int64_t get_ticks(void)
Get the number of milliseconds since SDL initialization.
Definition sdl.c:450
int64_t init_text(void)
Initialize the SDL_ttf text rendering subsystem.
Definition sdl.c:609
int64_t blit_surface(int64_t src_ptr, int64_t dst_ptr, int64_t x, int64_t y)
Blit a source surface onto a destination surface at (x, y).
Definition sdl.c:684
int64_t lock_texture(int64_t texture_id, void **pixels, int64_t *pitch)
Lock a texture for direct pixel access.
Definition sdl.c:434
void draw_point(int64_t renderer_id, int64_t x, int64_t y)
Draw a single pixel.
Definition sdl.c:242
void destroy_texture(int64_t texture_id)
Destroy a texture.
Definition sdl.c:286
void mxvm_sdl_set_window_title(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:92
void mxvm_sdl_delay(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:704
void mxvm_sdl_open_audio(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:715
void mxvm_sdl_get_relative_mouse_y(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:206
void mxvm_sdl_create_rgb_surface(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:361
void mxvm_sdl_render_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:677
void mxvm_sdl_fill_rect(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:542
void mxvm_sdl_present(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:488
void mxvm_sdl_create_render_target(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:956
void mxvm_sdl_show_cursor(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:350
void mxvm_sdl_update_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:827
void mxvm_sdl_get_renderer_output_size(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:164
void mxvm_sdl_get_relative_mouse_state(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:249
void mxvm_sdl_queue_audio(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:792
void mxvm_sdl_set_clipboard_text(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:321
void mxvm_sdl_free_wav(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:403
void mxvm_sdl_destroy_window(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:54
void mxvm_sdl_set_window_fullscreen(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:153
void mxvm_sdl_get_relative_mouse_buttons(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:213
void mxvm_sdl_get_relative_mouse_x(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:199
void mxvm_sdl_destroy_renderer(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:81
void mxvm_sdl_destroy_render_target(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:982
void mxvm_sdl_create_window(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:24
void mxvm_sdl_close_audio(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:731
void mxvm_sdl_load_font(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:904
void mxvm_sdl_destroy_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:575
void mxvm_sdl_get_key_code(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:430
void mxvm_sdl_create_renderer(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:65
void mxvm_sdl_get_mouse_state(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:220
void mxvm_sdl_load_texture_color_key_rgb(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
MXVM binding for load_texture_color_key_rgb.
Definition sdl.cpp:647
void mxvm_sdl_draw_rect(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:527
void mxvm_sdl_get_clipboard_text(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:341
void mxvm_sdl_get_mouse_x(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:438
void mxvm_sdl_free_surface(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:376
void mxvm_sdl_lock_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:852
void mxvm_sdl_pause_audio(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:735
void mxvm_sdl_get_window_size(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:126
void mxvm_sdl_draw_text(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:927
void mxvm_sdl_load_wav(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:745
void mxvm_sdl_quit_text(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:900
void mxvm_sdl_set_draw_color(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:462
void mxvm_sdl_get_num_keys(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:313
void mxvm_sdl_get_mouse_y(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:446
void mxvm_sdl_draw_point(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:499
void mxvm_sdl_set_render_target(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:971
void mxvm_sdl_load_texture_color_key(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:612
void mxvm_sdl_draw_line(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:512
void mxvm_sdl_is_key_pressed(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:300
void mxvm_sdl_present_stretched(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:1005
void mxvm_sdl_create_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:557
void mxvm_sdl_get_event_type(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:422
void mxvm_sdl_get_ticks(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:697
void mxvm_sdl_present_scaled(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:992
void mxvm_sdl_init(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:11
void mxvm_sdl_set_window_position(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:114
void mxvm_sdl_set_window_icon(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:1018
void mxvm_sdl_get_mouse_buttons(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:192
void mxvm_sdl_unlock_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:883
void mxvm_sdl_quit(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:19
void mxvm_sdl_blit_surface(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:386
void mxvm_sdl_poll_event(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:414
void mxvm_sdl_clear(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:477
void mxvm_sdl_init_text(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:893
void mxvm_sdl_get_keyboard_state(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:279
void mxvm_sdl_clear_queued_audio(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:822
void mxvm_sdl_get_queued_audio_size(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:815
void mxvm_sdl_get_mouse_button(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:454
void mxvm_sdl_load_texture(mxvm::Program *program, std::vector< mxvm::Operand > &operand)
Definition sdl.cpp:586
std::string str_value
Definition instruct.hpp:211
A named variable with type, value, and optional object association.
Definition instruct.hpp:292
Variable_Value var_value
Definition instruct.hpp:295