From 82f724eb0467f32c5233ef59d5b562dcf142839c Mon Sep 17 00:00:00 2001 From: Allanis Date: Mon, 10 Jun 2013 19:19:26 +0100 Subject: [PATCH] [Add] Music is now controlled by lua, now we can set music based on scenario. ^.^ --- snd/music.lua | 34 +++++++ snd/music/{Machina.ogg => machina.ogg} | Bin src/land.c | 11 +-- src/lephisto.c | 3 +- src/lluadef.h | 4 + src/mission.c | 7 +- src/music.c | 128 ++++++++++++++++++++++++- src/music.h | 5 + 8 files changed, 175 insertions(+), 17 deletions(-) create mode 100644 snd/music.lua rename snd/music/{Machina.ogg => machina.ogg} (100%) diff --git a/snd/music.lua b/snd/music.lua new file mode 100644 index 0000000..6bb30db --- /dev/null +++ b/snd/music.lua @@ -0,0 +1,34 @@ +--[[ +-- Music will get called with a string parameter indicating status. +-- Valid Parameters: +-- load - game is loading. +-- land - player landed. +-- takeoff - player took off. +-- combat - Player just got a hostile on screen. +-- idle - Current playing music ran out. +--]]-- + +function choose(str) + if str == "load" then + music.load("machina") + music.play() + + elseif str == "land" then + music.load("agriculture") + music.play() + + elseif str == "takeoff" then + music.load("liftoff") + music.play() + + elseif str == "combat" then + music.load("galacticbattle") + music.play() + + elseif str == "idle" then + choose(last) -- This should be smarter in the future. + end + + last = str -- Save the last string so we can use it. +end + diff --git a/snd/music/Machina.ogg b/snd/music/machina.ogg similarity index 100% rename from snd/music/Machina.ogg rename to snd/music/machina.ogg diff --git a/src/land.c b/src/land.c index f4c4906..bd35231 100644 --- a/src/land.c +++ b/src/land.c @@ -14,6 +14,7 @@ #include "mission.h" #include "ltime.h" #include "save.h" +#include "music.h" #include "land.h" // Global/main window. @@ -46,9 +47,6 @@ #define MISSION_WIDTH 700 #define MISSION_HEIGHT 600 -#define MUSIC_TAKEOFF "liftoff" -#define MUSIC_LAND "agriculture" - // We use visited flags to not duplicate missions generated. #define VISITED_LAND (1<<0) #define VISITED_COMMODITY (1<<1) @@ -1021,8 +1019,7 @@ void land(Planet* p) { if(landed) return; // Change music. - music_load(MUSIC_LAND); - music_play(); + music_choose("land"); land_planet = p; land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT); @@ -1092,8 +1089,8 @@ void takeoff(void) { if(!landed) return; - music_load(MUSIC_TAKEOFF); - music_play(); + // The music. + music_choose("takeoff"); sw = land_planet->gfx_space->w; sh = land_planet->gfx_space->h; diff --git a/src/lephisto.c b/src/lephisto.c index a0d6c2c..b76b273 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -128,8 +128,7 @@ int main(int argc, char** argv) { LOG("Sound is disabled!"); else { if(sound_init()) WARN("Problem setting up sound!"); - music_load("Machina"); - music_play(); + music_choose("load"); } // Input. diff --git a/src/lluadef.h b/src/lluadef.h index 94dca7c..cf8275a 100644 --- a/src/lluadef.h +++ b/src/lluadef.h @@ -1,5 +1,9 @@ #pragma once +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" + // Debug stuff. #define LLUA_DEBUG(str, args...) \ (fprintf(stdout, "Lua: "str"\n", ## args)) diff --git a/src/mission.c b/src/mission.c index 1181762..458f560 100644 --- a/src/mission.c +++ b/src/mission.c @@ -2,9 +2,7 @@ #include #include -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" +#include "lluadef.h" #include "rng.h" #include "lephisto.h" @@ -22,9 +20,6 @@ #define MISSION_DATA "../dat/mission.xml" #define MISSION_LUA_PATH "../dat/missions/" -#define luaL_dobuffer(L, b, n, s) \ - (luaL_loadbuffer(L, b, n, s) || lua_pcall(L, 0, LUA_MULTRET, 0)) - // Current player missions. static unsigned int mission_id = 0; Mission player_missions[MISSION_MAX]; diff --git a/src/music.c b/src/music.c index 5ee2238..5a5738a 100644 --- a/src/music.c +++ b/src/music.c @@ -3,6 +3,10 @@ #include #include +#include "llua.h" +#include "lluadef.h" +#include "misn_lua.h" + #include "lephisto.h" #include "log.h" #include "pack.h" @@ -26,12 +30,29 @@ #define musicLock() SDL_mutexP(music_vorbis_lock) #define musicUnlock() SDL_mutexV(music_vorbis_lock) +#define MUSIC_LUA_PATH "../snd/music.lua" + // Gobal sound mutex. extern SDL_mutex* sound_lock; +// Global music lua. +static lua_State* music_lua = NULL; +// Functions. +static int musicL_load(lua_State* L); +static int musicL_play(lua_State* L); +static int musicL_stop(lua_State* L); +static int musicL_get(lua_State* L); +static const luaL_reg music_methods[] = { + { "load", musicL_load }, + { "play", musicL_play }, + { "stop", musicL_stop }, + { "get", musicL_get }, + {0, 0} +}; + // Saves the music to ram in this structure. typedef struct alMusic_ { - char name[32]; // Name. + char name[64]; // Name. Packfile file; OggVorbis_File stream; vorbis_info* info; @@ -66,10 +87,14 @@ ov_callbacks ovcall = { .tell_func = (long(*)(void*))ovpack_retneg }; +// Music stuff. static int stream_loadBuffer(ALuint buffer); static int music_find(void); static int music_loadOGG(const char* filename); static void music_free(void); +// Lua stuff. +static int music_luaInit(void); +static void music_luaQuit(void); // The thread. static unsigned int music_state = 0; @@ -183,7 +208,10 @@ int music_init(void) { alGenSources(1, &music_source); alSourcef(music_source, AL_GAIN, mvolume); alSourcef(music_source, AL_ROLLOFF_FACTOR, 0.); - alSourcei(music_source, AL_SOURCE_RELATIVE, AL_TRUE); + alSourcei(music_source, AL_SOURCE_RELATIVE, AL_FALSE); + + // Start the lua music stuff. + music_luaInit(); soundUnlock(); @@ -207,6 +235,9 @@ void music_exit(void) { free(music_selection[i]); free(music_selection); + // Bye bye Lua. + music_luaQuit(); + SDL_DestroyMutex(music_vorbis_lock); } @@ -217,6 +248,9 @@ static int music_loadOGG(const char* filename) { musicLock(); + // Set the new name. + strncpy(music_vorbis.name, filename, 64); + // Load the new ogg. pack_open(&music_vorbis.file, DATA, filename); ov_open_callbacks(&music_vorbis.file, &music_vorbis.stream, NULL, 0, ovcall); @@ -325,3 +359,93 @@ void music_kill(void) { if(!music_is(MUSIC_KILL)) music_set(MUSIC_KILL); } +// Music lua stuff. + +// Initialize. +static int music_luaInit(void) { + char* buf; + uint32_t bufsize; + + if(music_lua != NULL) + music_luaQuit(); + + music_lua = luaL_newstate(); + + lua_loadSpace(music_lua, 1); // Space and time are readonly. + lua_loadTime(music_lua, 1); + lua_loadRnd(music_lua); + lua_loadVar(music_lua, 1); // Also read only. + lua_loadMusic(music_lua, 0); // Write it. + + // Load the actual lua music code. + buf = pack_readfile(DATA, MUSIC_LUA_PATH, &bufsize); + if(luaL_dobuffer(music_lua, buf, bufsize, MUSIC_LUA_PATH) != 0) { + ERR("Error loading music file: %s" MUSIC_LUA_PATH); + ERR("%s", lua_tostring(music_lua, -1)); + WARN("Most likely lua file has improper syntax, please check"); + return -1; + } + free(buf); + + return 0; +} + +// Destroy. +static void music_luaQuit(void) { + if(music_lua == NULL) + return; + + lua_close(music_lua); + music_lua = NULL; +} + +int lua_loadMusic(lua_State* L, int read_only) { + (void)read_only; // Future proof. + luaL_register(L, "music", music_methods); + return 0; +} + +int music_choose(char* situation) { + lua_getglobal(music_lua, "choose"); + lua_pushstring(music_lua, situation); + if(lua_pcall(music_lua, 1, 0, 0)) { + // Error occured. + WARN("Error while choosing music: %s", (char*)lua_tostring(music_lua, -1)); + return -1; + } + return 0; +} + +// The music lua functions. +static int musicL_load(lua_State* L) { + char* str; + + // Check parameters. + LLUA_MIN_ARGS(1); + if(lua_isstring(L, 1)) str = (char*)lua_tostring(L, 1); + else LLUA_INVALID_PARAMETER(); + + music_load(str); + return 0; +} + +static int musicL_play(lua_State* L) { + (void)L; + music_play(); + return 0; +} + +static int musicL_stop(lua_State* L) { + (void)L; + music_stop(); + return 0; +} + +static int musicL_get(lua_State* L) { + musicLock(); + lua_pushstring(L, music_vorbis.name); + musicUnlock(); + + return 1; +} + diff --git a/src/music.h b/src/music.h index 3dad535..136afc9 100644 --- a/src/music.h +++ b/src/music.h @@ -1,4 +1,5 @@ #pragma once +#include "lua.h" // Thread. int music_thread(void* unused); @@ -15,3 +16,7 @@ void music_load(const char* name); void music_play(void); void music_stop(void); +// Lua control. +int lua_loadMusic(lua_State* L, int read_only); +int music_choose(char* situation); +