Merge branch 'testing'

This commit is contained in:
Allanis 2013-06-10 19:20:15 +01:00
commit 5864bf9cdf
8 changed files with 175 additions and 17 deletions

34
snd/music.lua Normal file
View File

@ -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

View File

@ -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;

View File

@ -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.

View File

@ -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))

View File

@ -2,9 +2,7 @@
#include <string.h>
#include <malloc.h>
#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];

View File

@ -3,6 +3,10 @@
#include <vorbis/vorbisfile.h>
#include <SDL.h>
#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;
}

View File

@ -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);