diff --git a/SDL.lean b/SDL.lean index e961e95..5333afb 100644 --- a/SDL.lean +++ b/SDL.lean @@ -20,6 +20,9 @@ opaque init : UInt32 → IO UInt32 @[extern "sdl_ttf_init"] opaque ttfInit : IO Bool +@[extern "sdl_mixer_init"] +opaque mixerInit : IO Bool + @[extern "sdl_quit"] opaque quit : IO Unit @@ -29,6 +32,9 @@ opaque createWindow : String → Int32 → Int32 → UInt32 → IO UInt32 @[extern "sdl_create_renderer"] opaque createRenderer : Unit → IO UInt32 +@[extern "sdl_create_mixer"] +opaque createMixer : Unit → IO UInt32 + @[extern "sdl_set_render_draw_color"] opaque setRenderDrawColor : UInt8 → UInt8 → UInt8 → UInt8 → IO Int32 diff --git a/TestApp.lean b/TestApp.lean index c148516..0c13b6e 100644 --- a/TestApp.lean +++ b/TestApp.lean @@ -100,6 +100,16 @@ partial def run : IO Unit := do SDL.quit return + unless (← SDL.mixerInit) do + IO.println "Failed to initialize SDL_mixer" + SDL.quit + return + + unless (← SDL.createMixer ()) != 0 do + IO.println "Failed to create audio mixer" + SDL.quit + return + let initialState : EngineState := { deltaTime := 0.0, lastTime := 0, running := true playerX := (SCREEN_WIDTH / 2), playerY := (SCREEN_HEIGHT / 2) diff --git a/c/sdl.c b/c/sdl.c index 84a4a94..0414f4e 100644 --- a/c/sdl.c +++ b/c/sdl.c @@ -2,12 +2,15 @@ #include #include #include +#include #include static SDL_Window* g_window = NULL; static SDL_Renderer* g_renderer = NULL; static SDL_Texture* g_texture = NULL; static TTF_Font* font = NULL; +static MIX_Mixer* mixer = NULL; +static MIX_Track* track = NULL; lean_obj_res sdl_init(uint32_t flags, lean_obj_arg w) { int32_t result = SDL_Init(flags); @@ -19,6 +22,22 @@ lean_obj_res sdl_ttf_init(lean_obj_arg w) { return lean_io_result_mk_ok(lean_box_uint32(result)); } +lean_obj_res sdl_mixer_init(lean_obj_arg w) { + bool result = MIX_Init(); + return lean_io_result_mk_ok(lean_box_uint32(result)); +} + +lean_obj_res sdl_create_mixer(lean_obj_arg w) { + mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL); + if (mixer == NULL) { + const char* error = SDL_GetError(); + SDL_Log("C: MIX_CreateMixerDevice failed: %s\n", error); + return lean_io_result_mk_ok(lean_box(0)); + } + return lean_io_result_mk_ok(lean_box(1)); +} + + lean_obj_res sdl_quit(lean_obj_arg w) { if (g_texture) { SDL_DestroyTexture(g_texture);