MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
sdl.c
Go to the documentation of this file.
1
6#include "mx_sdl.h"
7#include "SDL.h"
8#include "SDL_ttf.h"
9
10
11static SDL_Event g_event;
12static SDL_Window **g_windows = NULL;
13static SDL_Renderer **g_renderers = NULL;
14static SDL_Texture **g_textures = NULL;
15static int64_t g_window_count = 0;
16static int64_t g_renderer_count = 0;
17static int64_t g_texture_count = 0;
18static TTF_Font **g_fonts = NULL;
19static int64_t g_font_count = 0;
20
21static SDL_Texture **g_render_targets = NULL;
22static int64_t *g_target_widths = NULL;
23static int64_t *g_target_heights = NULL;
24static int64_t g_render_target_count = 0;
25
26int64_t init(void) {
27 SDL_SetMainReady();
28 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == 0) {
29 return 0;
30 }
31 return 1;
32}
33
34void quit(void) {
35 if (g_fonts) {
36 for (int64_t i = 0; i < g_font_count; ++i) {
37 if (g_fonts[i])
38 TTF_CloseFont(g_fonts[i]);
39 }
40 free(g_fonts);
41 g_fonts = NULL;
42 g_font_count = 0;
43 }
44
45 if (g_target_widths) {
46 free(g_target_widths);
47 g_target_widths = NULL;
48 }
49 if (g_target_heights) {
50 free(g_target_heights);
51 g_target_heights = NULL;
52 }
54
55 if (g_textures) {
56 for (int64_t i = 0; i < g_texture_count; ++i) {
57 if (g_textures[i])
58 SDL_DestroyTexture(g_textures[i]);
59 g_textures[i] = NULL;
60 }
61 free(g_textures);
62 g_textures = NULL;
64 }
65 if (g_renderers) {
66 for (int64_t i = 0; i < g_renderer_count; ++i) {
67 if (g_renderers[i])
68 SDL_DestroyRenderer(g_renderers[i]);
69 }
70 free(g_renderers);
71 g_renderers = NULL;
73 }
74 if (g_windows) {
75 for (int64_t i = 0; i < g_window_count; ++i) {
76 if (g_windows[i])
77 SDL_DestroyWindow(g_windows[i]);
78 }
79 free(g_windows);
80 g_windows = NULL;
82 }
83 SDL_Quit();
84}
85
86int64_t create_window(const char *title, int64_t x, int64_t y, int64_t w, int64_t h, int64_t flags) {
87 SDL_Window *window = SDL_CreateWindow(title, (int)x, (int)y, (int)w, (int)h, (Uint32)flags);
88 if (!window)
89 return -1;
90
91 void *tmp = realloc(g_windows, sizeof(SDL_Window *) * (g_window_count + 1));
92 if (!tmp) {
93 SDL_DestroyWindow(window);
94 return -1;
95 }
96 g_windows = tmp;
97 g_windows[g_window_count] = window;
98 SDL_Surface *surf = SDL_LoadBMP("icon.bmp");
99 if (surf != NULL) {
100 SDL_SetWindowIcon(window, surf);
101 SDL_FreeSurface(surf);
102 }
103 return g_window_count++;
104}
105
106void destroy_window(int64_t window_id) {
107 if (window_id >= 0 && window_id < g_window_count && g_windows[window_id]) {
108 SDL_DestroyWindow(g_windows[window_id]);
109 g_windows[window_id] = NULL;
110 }
111}
112
113void set_window_icon(uint64_t window_id, const char *path) {
114 SDL_Surface *surf = SDL_LoadBMP(path);
115 if (!surf)
116 return;
117 SDL_SetWindowIcon(g_windows[window_id], surf);
118 SDL_FreeSurface(surf);
119}
120
121int64_t create_renderer(int64_t window_id, int64_t index, int64_t flags) {
122 if (window_id < 0 || window_id >= g_window_count || !g_windows[window_id])
123 return -1;
124
125 SDL_Renderer *renderer = SDL_CreateRenderer(g_windows[window_id], (int)index, (Uint32)flags);
126 if (!renderer)
127 return -1;
128
129 void *tmp = realloc(g_renderers, sizeof(SDL_Renderer *) * (g_renderer_count + 1));
130 if (!tmp) {
131 SDL_DestroyRenderer(renderer);
132 return -1;
133 }
134 g_renderers = tmp;
135 g_renderers[g_renderer_count] = renderer;
136 return g_renderer_count++;
137}
138
139void destroy_renderer(int64_t renderer_id) {
140 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
141 SDL_DestroyRenderer(g_renderers[renderer_id]);
142 g_renderers[renderer_id] = NULL;
143 }
144}
145
146int64_t create_render_target(int64_t renderer_id, int64_t width, int64_t height) {
147 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
148 return -1;
149
150 SDL_RenderSetLogicalSize(g_renderers[renderer_id], (int)width, (int)height);
151
152 void *tmp2 = realloc(g_target_widths, sizeof(int64_t) * (g_render_target_count + 1));
153 void *tmp3 = realloc(g_target_heights, sizeof(int64_t) * (g_render_target_count + 1));
154 if (!tmp2 || !tmp3) {
155 if (tmp2)
156 g_target_widths = tmp2;
157 if (tmp3)
158 g_target_heights = tmp3;
159 return -1;
160 }
161 g_target_widths = tmp2;
162 g_target_heights = tmp3;
163
166
167 SDL_SetRenderDrawColor(g_renderers[renderer_id], 0, 0, 0, 255);
168 SDL_RenderClear(g_renderers[renderer_id]);
169
170 return g_render_target_count++;
171}
172
173void set_render_target(int64_t renderer_id, int64_t target_id) {
174 (void)renderer_id;
175 (void)target_id;
176}
177
178void destroy_render_target(int64_t target_id) {
179 (void)target_id;
180}
181
182int64_t poll_event(void) {
183 return SDL_PollEvent(&g_event);
184}
185
186int64_t get_event_type(void) {
187 return g_event.type;
188}
189
190int64_t get_key_code(void) {
191 return g_event.key.keysym.sym;
192}
193
194int64_t get_mouse_x(void) {
195 return g_event.motion.x;
196}
197
198int64_t get_mouse_y(void) {
199 return g_event.motion.y;
200}
201
202int64_t get_mouse_button(void) {
203 return g_event.button.button;
204}
205
206void set_draw_color(int64_t renderer_id, int64_t r, int64_t g, int64_t b, int64_t a) {
207 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
208 SDL_SetRenderDrawColor(g_renderers[renderer_id], (Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a);
209 }
210}
211
212void clear(int64_t renderer_id) {
213 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
214 SDL_RenderClear(g_renderers[renderer_id]);
215 }
216}
217
218void present(int64_t renderer_id) {
219 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
220 SDL_RenderPresent(g_renderers[renderer_id]);
221 }
222}
223
224void present_scaled(int64_t renderer_id, int64_t target_id, int64_t scale_width, int64_t scale_height) {
225 (void)target_id;
226 (void)scale_width;
227 (void)scale_height;
228 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
229 return;
230 SDL_RenderPresent(g_renderers[renderer_id]);
231}
232
233void present_stretched(int64_t renderer_id, int64_t target_id, int64_t dst_width, int64_t dst_height) {
234 (void)target_id;
235 (void)dst_width;
236 (void)dst_height;
237 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
238 return;
239 SDL_RenderPresent(g_renderers[renderer_id]);
240}
241
242void draw_point(int64_t renderer_id, int64_t x, int64_t y) {
243 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
244 SDL_RenderDrawPoint(g_renderers[renderer_id], (int)x, (int)y);
245 }
246}
247
248void draw_line(int64_t renderer_id, int64_t x1, int64_t y1, int64_t x2, int64_t y2) {
249 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
250 SDL_RenderDrawLine(g_renderers[renderer_id], (int)x1, (int)y1, (int)x2, (int)y2);
251 }
252}
253
254void draw_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h) {
255 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
256 SDL_Rect rect = {(int)x, (int)y, (int)w, (int)h};
257 SDL_RenderDrawRect(g_renderers[renderer_id], &rect);
258 }
259}
260
261void fill_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h) {
262 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
263 SDL_Rect rect = {(int)x, (int)y, (int)w, (int)h};
264 SDL_RenderFillRect(g_renderers[renderer_id], &rect);
265 }
266}
267
268int64_t create_texture(int64_t renderer_id, int64_t format, int64_t access, int64_t w, int64_t h) {
269 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
270 return -1;
271
272 SDL_Texture *texture = SDL_CreateTexture(g_renderers[renderer_id], (Uint32)format, (int)access, (int)w, (int)h);
273 if (!texture)
274 return -1;
275
276 void *tmp = realloc(g_textures, sizeof(SDL_Texture *) * (g_texture_count + 1));
277 if (!tmp) {
278 SDL_DestroyTexture(texture);
279 return -1;
280 }
281 g_textures = tmp;
282 g_textures[g_texture_count] = texture;
283 return g_texture_count++;
284}
285
286void destroy_texture(int64_t texture_id) {
287 if (texture_id >= 0 && texture_id < g_texture_count && g_textures[texture_id]) {
288 SDL_DestroyTexture(g_textures[texture_id]);
289 g_textures[texture_id] = NULL;
290 }
291}
292
306int64_t load_texture(int64_t renderer_id, const char *file_path) {
307 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
308 return -1;
309
310 SDL_Surface *surface = SDL_LoadBMP(file_path);
311 if (!surface)
312 return -2;
313
314 SDL_Texture *texture = SDL_CreateTextureFromSurface(g_renderers[renderer_id], surface);
315 SDL_FreeSurface(surface);
316 if (!texture)
317 return -3;
318
319 void *tmp = realloc(g_textures, sizeof(SDL_Texture *) * (g_texture_count + 1));
320 if (!tmp) {
321 SDL_DestroyTexture(texture);
322 return -1;
323 }
324 g_textures = tmp;
325 g_textures[g_texture_count] = texture;
326
327 return g_texture_count++;
328}
329
330int64_t load_texture_color_key(int64_t renderer_id, const char *file_path) {
331 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
332 return -1;
333
334 SDL_Surface *surface = SDL_LoadBMP(file_path);
335 if (!surface)
336 return -2;
337
338 SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 0, 0, 0));
339 SDL_Texture *texture = SDL_CreateTextureFromSurface(g_renderers[renderer_id], surface);
340 SDL_FreeSurface(surface);
341 if (!texture)
342 return -3;
343 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
344
345 void *tmp = realloc(g_textures, sizeof(SDL_Texture *) * (g_texture_count + 1));
346 if (!tmp) {
347 SDL_DestroyTexture(texture);
348 return -1;
349 }
350 g_textures = tmp;
351 g_textures[g_texture_count] = texture;
352
353 return g_texture_count++;
354}
355
371int64_t load_texture_color_key_rgb(int64_t renderer_id, const char *file_path, int64_t r, int64_t g, int64_t b) {
372 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
373 return -1;
374
375 SDL_Surface *surface = SDL_LoadBMP(file_path);
376 if (!surface)
377 return -2;
378
379 SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, (Uint8)r, (Uint8)g, (Uint8)b));
380 SDL_Texture *texture = SDL_CreateTextureFromSurface(g_renderers[renderer_id], surface);
381 SDL_FreeSurface(surface);
382 if (!texture)
383 return -3;
384 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
385
386 void *tmp = realloc(g_textures, sizeof(SDL_Texture *) * (g_texture_count + 1));
387 if (!tmp) {
388 SDL_DestroyTexture(texture);
389 return -1;
390 }
391 g_textures = tmp;
392 g_textures[g_texture_count] = texture;
393
394 return g_texture_count++;
395}
396
397void 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) {
398 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id] &&
399 texture_id >= 0 && texture_id < g_texture_count && g_textures[texture_id]) {
400 SDL_Renderer *renderer = g_renderers[renderer_id];
401 SDL_Texture *texture = g_textures[texture_id];
402
403 SDL_Rect *psrc = NULL;
404 SDL_Rect src_rect;
405 if (!((src_x == -1 || src_y == -1) && src_w == -1 && src_h == -1)) {
406 src_rect.x = (int)src_x;
407 src_rect.y = (int)src_y;
408 src_rect.w = (int)src_w;
409 src_rect.h = (int)src_h;
410 psrc = &src_rect;
411 }
412
413 SDL_Rect *pdst = NULL;
414 SDL_Rect dst_rect;
415 if (!(dst_x == -1 && dst_y == -1 && dst_w == -1 && dst_h == -1)) {
416 dst_rect.x = (int)dst_x;
417 dst_rect.y = (int)dst_y;
418 dst_rect.w = (int)dst_w;
419 dst_rect.h = (int)dst_h;
420 pdst = &dst_rect;
421 }
422
423 SDL_RenderCopy(renderer, texture, psrc, pdst);
424 }
425}
426
427int64_t update_texture(int64_t texture_id, const void *pixels, int64_t pitch) {
428 if (texture_id >= 0 && texture_id < g_texture_count && g_textures[texture_id]) {
429 return SDL_UpdateTexture(g_textures[texture_id], NULL, pixels, (int)pitch) == 0 ? 1 : 0;
430 }
431 return 0;
432}
433
434int64_t lock_texture(int64_t texture_id, void **pixels, int64_t *pitch) {
435 if (texture_id >= 0 && texture_id < g_texture_count && g_textures[texture_id]) {
436 int p;
437 int result = SDL_LockTexture(g_textures[texture_id], NULL, pixels, &p);
438 *pitch = p;
439 return result == 0 ? 1 : 0;
440 }
441 return 0;
442}
443
444void unlock_texture(int64_t texture_id) {
445 if (texture_id >= 0 && texture_id < g_texture_count && g_textures[texture_id]) {
446 SDL_UnlockTexture(g_textures[texture_id]);
447 }
448}
449
450int64_t get_ticks(void) {
451 return SDL_GetTicks();
452}
453
454void delay(int64_t ms) {
455 SDL_Delay((Uint32)ms);
456}
457
458int64_t open_audio(int64_t freq, int64_t format, int64_t channels, int64_t samples) {
459 SDL_AudioSpec wanted_spec;
460 wanted_spec.freq = (int)freq;
461 wanted_spec.format = (SDL_AudioFormat)format;
462 wanted_spec.channels = (Uint8)channels;
463 wanted_spec.samples = (Uint16)samples;
464 wanted_spec.callback = NULL;
465 wanted_spec.userdata = NULL;
466
467 return SDL_OpenAudio(&wanted_spec, NULL) == 0 ? 1 : 0;
468}
469
470void close_audio(void) {
471 SDL_CloseAudio();
472}
473
474void pause_audio(int64_t pause_on) {
475 SDL_PauseAudio(pause_on ? 1 : 0);
476}
477
478int64_t load_wav(const char *file_path, int64_t *audio_buf, int64_t *audio_len, int64_t *audio_spec) {
479 SDL_AudioSpec *spec = (SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec));
480 if (!spec)
481 return 0;
482 Uint8 *buf;
483 Uint32 len;
484
485 if (SDL_LoadWAV(file_path, spec, &buf, &len) == NULL) {
486 free(spec);
487 return 0;
488 }
489
490 *audio_buf = (int64_t)buf;
491 *audio_len = len;
492 *audio_spec = (int64_t)spec;
493 return 1;
494}
495
496void free_wav(int64_t audio_buf) {
497 if (audio_buf) {
498 SDL_FreeWAV((Uint8 *)audio_buf);
499 }
500}
501
502int64_t queue_audio(const void *data, int64_t len) {
503 return SDL_QueueAudio(1, data, (Uint32)len) == 0 ? 1 : 0;
504}
505
507 return SDL_GetQueuedAudioSize(1);
508}
509
511 SDL_ClearQueuedAudio(1);
512}
513
514int64_t get_mouse_buttons(void) {
515 int x, y;
516 return SDL_GetMouseState(&x, &y);
517}
518
519int64_t get_relative_mouse_x(void) {
520 int x, y;
521 SDL_GetRelativeMouseState(&x, &y);
522 return x;
523}
524
525int64_t get_relative_mouse_y(void) {
526 int x, y;
527 SDL_GetRelativeMouseState(&x, &y);
528 return y;
529}
530
532 int x, y;
533 return SDL_GetRelativeMouseState(&x, &y);
534}
535
536int64_t get_keyboard_state(int64_t *numkeys) {
537 int nk;
538 const Uint8 *state = SDL_GetKeyboardState(&nk);
539 *numkeys = nk;
540 return (int64_t)state;
541}
542
543int64_t is_key_pressed(int64_t scancode) {
544 int numkeys;
545 const Uint8 *state = SDL_GetKeyboardState(&numkeys);
546 if (scancode < 0 || scancode >= numkeys)
547 return 0;
548 return state[scancode] ? 1 : 0;
549}
550
551int64_t get_num_keys(void) {
552 int numkeys;
553 SDL_GetKeyboardState(&numkeys);
554 return numkeys;
555}
556
557void set_clipboard_text(const char *text) {
558 SDL_SetClipboardText(text);
559}
560
561const char *get_clipboard_text(void) {
562 static char *last_clipboard = NULL;
563 if (last_clipboard)
564 SDL_free(last_clipboard);
565 last_clipboard = SDL_GetClipboardText();
566 return last_clipboard;
567}
568
569void set_window_title(int64_t window_id, const char *title) {
570 if (window_id >= 0 && window_id < g_window_count && g_windows[window_id]) {
571 SDL_SetWindowTitle(g_windows[window_id], title);
572 }
573}
574
575void set_window_position(int64_t window_id, int64_t x, int64_t y) {
576 if (window_id >= 0 && window_id < g_window_count && g_windows[window_id]) {
577 SDL_SetWindowPosition(g_windows[window_id], (int)x, (int)y);
578 }
579}
580
581void get_window_size(int64_t window_id, int64_t *w, int64_t *h) {
582 if (window_id >= 0 && window_id < g_window_count && g_windows[window_id]) {
583 int ww, hh;
584 SDL_GetWindowSize(g_windows[window_id], &ww, &hh);
585 *w = ww;
586 *h = hh;
587 }
588}
589
590void set_window_fullscreen(int64_t window_id, int64_t fullscreen) {
591 if (window_id >= 0 && window_id < g_window_count && g_windows[window_id]) {
592 SDL_SetWindowFullscreen(g_windows[window_id], fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
593 }
594}
595
596void get_renderer_output_size(int64_t renderer_id, int64_t *w, int64_t *h) {
597 if (renderer_id >= 0 && renderer_id < g_renderer_count && g_renderers[renderer_id]) {
598 int ww, hh;
599 SDL_GetRendererOutputSize(g_renderers[renderer_id], &ww, &hh);
600 *w = ww;
601 *h = hh;
602 }
603}
604
605void show_cursor(int64_t show) {
606 SDL_ShowCursor(show ? SDL_ENABLE : SDL_DISABLE);
607}
608
609int64_t init_text(void) {
610 return TTF_Init() == 0 ? 1 : 0;
611}
612
613void quit_text(void) {
614 if (g_fonts) {
615 for (int64_t i = 0; i < g_font_count; ++i) {
616 if (g_fonts[i])
617 TTF_CloseFont(g_fonts[i]);
618 }
619 free(g_fonts);
620 g_fonts = NULL;
621 g_font_count = 0;
622 }
623 TTF_Quit();
624}
625
626int64_t load_font(const char *file, int64_t ptsize) {
627 TTF_Font *font = TTF_OpenFont(file, (int)ptsize);
628 if (!font)
629 return -1;
630 void *tmp = realloc(g_fonts, sizeof(TTF_Font *) * (g_font_count + 1));
631 if (!tmp) {
632 TTF_CloseFont(font);
633 return -1;
634 }
635 g_fonts = tmp;
636 g_fonts[g_font_count] = font;
637 return g_font_count++;
638}
639
640void 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) {
641 if (renderer_id < 0 || renderer_id >= g_renderer_count || !g_renderers[renderer_id])
642 return;
643 if (font_id < 0 || font_id >= g_font_count || !g_fonts[font_id])
644 return;
645
646 SDL_Color color = {(Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a};
647 SDL_Surface *surface = TTF_RenderUTF8_Blended(g_fonts[font_id], text, color);
648 if (!surface)
649 return;
650
651 SDL_Texture *texture = SDL_CreateTextureFromSurface(g_renderers[renderer_id], surface);
652 if (!texture) {
653 SDL_FreeSurface(surface);
654 return;
655 }
656
657 SDL_Rect dst = {(int)x, (int)y, surface->w, surface->h};
658 SDL_RenderCopy(g_renderers[renderer_id], texture, NULL, &dst);
659 SDL_DestroyTexture(texture);
660 SDL_FreeSurface(surface);
661}
662
663int64_t create_rgb_surface(int64_t width, int64_t height, int64_t depth) {
664 SDL_Surface *s = NULL;
665 int w = (int)width, h = (int)height, d = (int)depth;
666
667 if (d == 32)
668 s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_RGBA32);
669 else if (d == 24)
670 s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, SDL_PIXELFORMAT_RGB24);
671 else if (d == 16)
672 s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 16, SDL_PIXELFORMAT_RGB565);
673 else
674 return 0;
675
676 return (int64_t)s;
677}
678
679void free_surface(int64_t surf_ptr) {
680 if (surf_ptr)
681 SDL_FreeSurface((SDL_Surface *)surf_ptr);
682}
683
684int64_t blit_surface(int64_t src_ptr, int64_t dst_ptr, int64_t x, int64_t y) {
685 if (!src_ptr || !dst_ptr)
686 return 0;
687 SDL_Surface *src = (SDL_Surface *)src_ptr;
688 SDL_Surface *dst = (SDL_Surface *)dst_ptr;
689 SDL_Rect dst_rc = {(int)x, (int)y, 0, 0};
690 return SDL_BlitSurface(src, NULL, dst, &dst_rc) == 0 ? 1 : 0;
691}
692
693int64_t get_mouse_state(int64_t *x, int64_t *y) {
694 int ix, iy;
695 Uint32 m = SDL_GetMouseState(&ix, &iy);
696 if (x)
697 *x = ix;
698 if (y)
699 *y = iy;
700 return (int64_t)m;
701}
702
703int64_t get_relative_mouse_state(int64_t *x, int64_t *y) {
704 int ix, iy;
705 Uint32 m = SDL_GetRelativeMouseState(&ix, &iy);
706 if (x)
707 *x = ix;
708 if (y)
709 *y = iy;
710 return (int64_t)m;
711}
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
static SDL_Renderer ** g_renderers
Definition sdl.c:13
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
static SDL_Texture ** g_textures
Definition sdl.c:14
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
static int64_t g_texture_count
Definition sdl.c:17
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
static SDL_Texture ** g_render_targets
Definition sdl.c:21
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
static SDL_Window ** g_windows
Definition sdl.c:12
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
static SDL_Event g_event
Definition sdl.c:11
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 an SDL 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
static TTF_Font ** g_fonts
Definition sdl.c:18
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
static int64_t g_window_count
Definition sdl.c:15
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
static int64_t g_renderer_count
Definition sdl.c:16
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
static int64_t * g_target_heights
Definition sdl.c:23
static int64_t g_font_count
Definition sdl.c:19
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
static int64_t g_render_target_count
Definition sdl.c:24
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 BMP file as an SDL texture with black color-key transparency.
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
static int64_t * g_target_widths
Definition sdl.c:22