28 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) == 0) {
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);
93 SDL_DestroyWindow(window);
98 SDL_Surface *surf = SDL_LoadBMP(
"icon.bmp");
100 SDL_SetWindowIcon(window, surf);
101 SDL_FreeSurface(surf);
114 SDL_Surface *surf = SDL_LoadBMP(path);
117 SDL_SetWindowIcon(
g_windows[window_id], surf);
118 SDL_FreeSurface(surf);
125 SDL_Renderer *renderer = SDL_CreateRenderer(
g_windows[window_id], (
int)index, (Uint32)flags);
131 SDL_DestroyRenderer(renderer);
150 SDL_RenderSetLogicalSize(
g_renderers[renderer_id], (
int)width, (
int)height);
154 if (!tmp2 || !tmp3) {
167 SDL_SetRenderDrawColor(
g_renderers[renderer_id], 0, 0, 0, 255);
183 return SDL_PollEvent(&
g_event);
206void set_draw_color(int64_t renderer_id, int64_t r, int64_t g, int64_t b, int64_t a) {
208 SDL_SetRenderDrawColor(
g_renderers[renderer_id], (Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a);
224void present_scaled(int64_t renderer_id, int64_t target_id, int64_t scale_width, int64_t scale_height) {
233void present_stretched(int64_t renderer_id, int64_t target_id, int64_t dst_width, int64_t dst_height) {
244 SDL_RenderDrawPoint(
g_renderers[renderer_id], (
int)x, (
int)y);
248void draw_line(int64_t renderer_id, int64_t x1, int64_t y1, int64_t x2, int64_t y2) {
250 SDL_RenderDrawLine(
g_renderers[renderer_id], (
int)x1, (
int)y1, (
int)x2, (
int)y2);
254void draw_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h) {
256 SDL_Rect rect = {(int)x, (
int)y, (int)w, (
int)h};
257 SDL_RenderDrawRect(
g_renderers[renderer_id], &rect);
261void fill_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h) {
263 SDL_Rect rect = {(int)x, (
int)y, (int)w, (
int)h};
264 SDL_RenderFillRect(
g_renderers[renderer_id], &rect);
268int64_t
create_texture(int64_t renderer_id, int64_t format, int64_t access, int64_t w, int64_t h) {
272 SDL_Texture *texture = SDL_CreateTexture(
g_renderers[renderer_id], (Uint32)format, (
int)access, (
int)w, (
int)h);
278 SDL_DestroyTexture(texture);
310 SDL_Surface *surface = SDL_LoadBMP(file_path);
314 SDL_Texture *texture = SDL_CreateTextureFromSurface(
g_renderers[renderer_id], surface);
315 SDL_FreeSurface(surface);
321 SDL_DestroyTexture(texture);
334 SDL_Surface *surface = SDL_LoadBMP(file_path);
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);
343 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
347 SDL_DestroyTexture(texture);
375 SDL_Surface *surface = SDL_LoadBMP(file_path);
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);
384 SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
388 SDL_DestroyTexture(texture);
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) {
401 SDL_Texture *texture =
g_textures[texture_id];
403 SDL_Rect *psrc = NULL;
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;
413 SDL_Rect *pdst = NULL;
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;
423 SDL_RenderCopy(renderer, texture, psrc, pdst);
429 return SDL_UpdateTexture(
g_textures[texture_id], NULL, pixels, (
int)pitch) == 0 ? 1 : 0;
434int64_t
lock_texture(int64_t texture_id,
void **pixels, int64_t *pitch) {
437 int result = SDL_LockTexture(
g_textures[texture_id], NULL, pixels, &p);
439 return result == 0 ? 1 : 0;
451 return SDL_GetTicks();
455 SDL_Delay((Uint32)ms);
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;
467 return SDL_OpenAudio(&wanted_spec, NULL) == 0 ? 1 : 0;
475 SDL_PauseAudio(pause_on ? 1 : 0);
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));
485 if (SDL_LoadWAV(file_path, spec, &buf, &len) == NULL) {
490 *audio_buf = (int64_t)buf;
492 *audio_spec = (int64_t)spec;
498 SDL_FreeWAV((Uint8 *)audio_buf);
503 return SDL_QueueAudio(1, data, (Uint32)len) == 0 ? 1 : 0;
507 return SDL_GetQueuedAudioSize(1);
511 SDL_ClearQueuedAudio(1);
516 return SDL_GetMouseState(&x, &y);
521 SDL_GetRelativeMouseState(&x, &y);
527 SDL_GetRelativeMouseState(&x, &y);
533 return SDL_GetRelativeMouseState(&x, &y);
538 const Uint8 *state = SDL_GetKeyboardState(&nk);
540 return (int64_t)state;
545 const Uint8 *state = SDL_GetKeyboardState(&numkeys);
546 if (scancode < 0 || scancode >= numkeys)
548 return state[scancode] ? 1 : 0;
553 SDL_GetKeyboardState(&numkeys);
558 SDL_SetClipboardText(text);
562 static char *last_clipboard = NULL;
564 SDL_free(last_clipboard);
565 last_clipboard = SDL_GetClipboardText();
566 return last_clipboard;
571 SDL_SetWindowTitle(
g_windows[window_id], title);
577 SDL_SetWindowPosition(
g_windows[window_id], (
int)x, (
int)y);
584 SDL_GetWindowSize(
g_windows[window_id], &ww, &hh);
592 SDL_SetWindowFullscreen(
g_windows[window_id], fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
599 SDL_GetRendererOutputSize(
g_renderers[renderer_id], &ww, &hh);
606 SDL_ShowCursor(show ? SDL_ENABLE : SDL_DISABLE);
610 return TTF_Init() == 0 ? 1 : 0;
627 TTF_Font *font = TTF_OpenFont(file, (
int)ptsize);
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) {
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);
651 SDL_Texture *texture = SDL_CreateTextureFromSurface(
g_renderers[renderer_id], surface);
653 SDL_FreeSurface(surface);
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);
664 SDL_Surface *s = NULL;
665 int w = (int)width, h = (
int)height, d = (int)depth;
668 s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 32, SDL_PIXELFORMAT_RGBA32);
670 s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, SDL_PIXELFORMAT_RGB24);
672 s = SDL_CreateRGBSurfaceWithFormat(0, w, h, 16, SDL_PIXELFORMAT_RGB565);
681 SDL_FreeSurface((SDL_Surface *)surf_ptr);
684int64_t
blit_surface(int64_t src_ptr, int64_t dst_ptr, int64_t x, int64_t y) {
685 if (!src_ptr || !dst_ptr)
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;
695 Uint32 m = SDL_GetMouseState(&ix, &iy);
705 Uint32 m = SDL_GetRelativeMouseState(&ix, &iy);
SDL2 module C API — function declarations for SDL2 bindings exported to MXVM programs.
int64_t poll_event(void)
Poll for a pending SDL event.
void quit(void)
Shut down SDL2 and release all resources.
void fill_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h)
Draw a filled rectangle.
int64_t get_queued_audio_size(void)
Get the number of bytes of queued audio.
static SDL_Renderer ** g_renderers
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).
void get_renderer_output_size(int64_t renderer_id, int64_t *w, int64_t *h)
Get the renderer output size.
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.
int64_t get_relative_mouse_state(int64_t *x, int64_t *y)
Get the relative mouse state.
static SDL_Texture ** g_textures
int64_t open_audio(int64_t freq, int64_t format, int64_t channels, int64_t samples)
Open the audio device.
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.
void set_clipboard_text(const char *text)
Set the system clipboard text.
const char * get_clipboard_text(void)
Get the system clipboard text.
void set_window_title(int64_t window_id, const char *title)
Set the title of a window.
static int64_t g_texture_count
int64_t get_num_keys(void)
Get the total number of keyboard keys.
int64_t get_key_code(void)
Get the key code from the last keyboard event.
void set_render_target(int64_t renderer_id, int64_t target_id)
Set the current render target (0 to render to the window).
int64_t get_mouse_state(int64_t *x, int64_t *y)
Get the current mouse state with pointer coordinates.
void free_surface(int64_t surf_ptr)
Free a surface.
int64_t get_mouse_button(void)
Get the mouse button from the last mouse event.
int64_t update_texture(int64_t texture_id, const void *pixels, int64_t pitch)
Update a texture with new pixel data.
int64_t load_wav(const char *file_path, int64_t *audio_buf, int64_t *audio_len, int64_t *audio_spec)
Load a WAV file.
int64_t get_mouse_buttons(void)
Get the current mouse button bitmask (pointerless variant).
static SDL_Texture ** g_render_targets
int64_t create_rgb_surface(int64_t width, int64_t height, int64_t depth)
Create an RGB surface.
void quit_text(void)
Shut down the SDL_ttf subsystem.
void set_window_position(int64_t window_id, int64_t x, int64_t y)
Set the position of a window.
int64_t get_relative_mouse_y(void)
Get the relative mouse y delta (pointerless variant).
int64_t create_renderer(int64_t window_id, int64_t index, int64_t flags)
Create a hardware-accelerated renderer for a window.
void destroy_renderer(int64_t renderer_id)
Destroy a renderer by ID.
static SDL_Window ** g_windows
void delay(int64_t ms)
Pause execution for the given number of milliseconds.
int64_t get_keyboard_state(int64_t *numkeys)
Get the keyboard state array.
void get_window_size(int64_t window_id, int64_t *w, int64_t *h)
Get the size of a window.
void clear_queued_audio(void)
Clear all queued audio data.
int64_t get_event_type(void)
Get the type of the last polled event.
int64_t init(void)
Initialize SDL2 subsystems.
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.
void draw_rect(int64_t renderer_id, int64_t x, int64_t y, int64_t w, int64_t h)
Draw a rectangle outline.
int64_t get_relative_mouse_x(void)
Get the relative mouse x delta (pointerless variant).
int64_t get_mouse_y(void)
Get the mouse y coordinate from the last mouse event.
void show_cursor(int64_t show)
Show or hide the mouse cursor.
void clear(int64_t renderer_id)
Clear the renderer with the current draw color.
static TTF_Font ** g_fonts
void pause_audio(int64_t pause_on)
Pause or unpause audio playback.
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.
void destroy_window(int64_t window_id)
Destroy a window by ID.
void free_wav(int64_t audio_buf)
Free a loaded WAV buffer.
int64_t get_relative_mouse_buttons(void)
Get the relative mouse button bitmask (pointerless variant).
static int64_t g_window_count
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.
static int64_t g_renderer_count
void destroy_render_target(int64_t target_id)
Destroy a render target texture.
int64_t is_key_pressed(int64_t scancode)
Check whether a specific key is currently pressed.
int64_t get_mouse_x(void)
Get the mouse x coordinate from the last mouse event.
int64_t create_texture(int64_t renderer_id, int64_t format, int64_t access, int64_t w, int64_t h)
Create a blank texture.
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.
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.
void set_window_icon(uint64_t window_id, const char *path)
Set the window icon from an image file.
static int64_t * g_target_heights
static int64_t g_font_count
int64_t queue_audio(const void *data, int64_t len)
Queue audio data for playback.
void set_window_fullscreen(int64_t window_id, int64_t fullscreen)
Set or clear fullscreen mode for a window.
static int64_t g_render_target_count
void close_audio(void)
Close the audio device.
int64_t load_font(const char *file, int64_t ptsize)
Load a TrueType font.
int64_t create_render_target(int64_t renderer_id, int64_t width, int64_t height)
Create an off-screen render target texture.
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.
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.
void unlock_texture(int64_t texture_id)
Unlock a previously locked texture.
void present(int64_t renderer_id)
Present the renderer's back buffer to the screen.
int64_t get_ticks(void)
Get the number of milliseconds since SDL initialization.
int64_t init_text(void)
Initialize the SDL_ttf text rendering subsystem.
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).
int64_t lock_texture(int64_t texture_id, void **pixels, int64_t *pitch)
Lock a texture for direct pixel access.
void draw_point(int64_t renderer_id, int64_t x, int64_t y)
Draw a single pixel.
void destroy_texture(int64_t texture_id)
Destroy a texture.
static int64_t * g_target_widths