aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp
index eeed38f..c497786 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -119,6 +119,33 @@ int l_keycode_from_string(lua_State *L)
return 1;
}
+int l_play_sound(lua_State* L) {
+ const char* filepath = luaL_checkstring(L, 1);
+ Mix_Chunk* sound = Mix_LoadWAV(filepath);
+ if (!sound) {
+ std::cerr << "Failed to load sound: " << Mix_GetError() << std::endl;
+ return 0;
+ }
+ Mix_PlayChannel(-1, sound, 0); // Play once on any free channel
+ return 0;
+}
+
+int l_play_sound_loop(lua_State* L) {
+ const char* filepath = luaL_checkstring(L, 1);
+ Mix_Chunk* sound = Mix_LoadWAV(filepath);
+ if (!sound) {
+ std::cerr << "Failed to load sound: " << Mix_GetError() << std::endl;
+ return 0;
+ }
+ Mix_PlayChannel(-1, sound, -1); // Play forever until stopped on any free channel
+ return 0;
+}
+
+int l_stop_all_sounds(lua_State* L) {
+ Mix_HaltChannel(-1); // Stops all channels immediately
+ return 0; // No return values to Lua
+}
+
int l_queue_button_for_render(lua_State *L)
{
const char *text = luaL_checkstring(L, 1);
@@ -333,6 +360,9 @@ void expose_c_functions()
lua_register(L, "getTextWidth", l_get_text_width);
lua_register(L, "quitGame", l_quit_game);
lua_register(L, "queueRectForRender", l_queue_rect_for_render);
+ lua_register(L, "playSound", l_play_sound);
+ lua_register(L, "playSoundLoop", l_play_sound_loop);
+ lua_register(L, "stopAllSounds", l_stop_all_sounds);
}
int main()
@@ -370,6 +400,12 @@ int main()
SDL_SetWindowResizable(window, SDL_TRUE);
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
+ if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) == -1)
+ {
+ std::cerr << "Mix_OpenAudio failed: " << Mix_GetError() << std::endl;
+ return 1;
+ }
+
if (TTF_Init() == -1)
{
std::cerr << "TTF_Init failed: " << TTF_GetError() << std::endl;
@@ -549,7 +585,7 @@ int main()
textList.clear();
SDL_RenderPresent(renderer);
-
+
// Frame limiting
Uint32 frameTime = SDL_GetTicks() - frameStart;
const int targetFrameTimeMs = 1000 / 60; // 60 FPS