init mixer

This commit is contained in:
Srayan Jana
2025-09-24 15:11:41 -07:00
parent c463220e23
commit 8c83559d85
3 changed files with 35 additions and 0 deletions
+6
View File
@@ -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
+10
View File
@@ -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)
+19
View File
@@ -2,12 +2,15 @@
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <lean/lean.h>
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);