static int l_audio_newSource(lua_State *state) { char const* filename = l_tools_toStringOrError(state, 1); audio_SourceType type = audio_SourceType_stream; if(!lua_isnoneornil(state, 2)) { type = l_tools_toEnumOrError(state, 2, l_audio_SourceType); } // TODO handle load errors switch(type) { case audio_SourceType_static: { audio_StaticSource *src = lua_newuserdata(state, sizeof(audio_StaticSource)); audio_loadStatic(src, filename); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.staticMT); break; } case audio_SourceType_stream: { audio_StreamSource *src = lua_newuserdata(state, sizeof(audio_StreamSource)); audio_loadStream(src, filename); lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.streamMT); break; } } lua_setmetatable(state, -2); return 1; }
static int l_audio_newSource(lua_State *state) { char const* filename = l_tools_toStringOrError(state, 1); const char* type = luaL_optstring(state, 2, "static"); int err; if (strcmp(type,"stream") == 0) { audio_StreamSource *src = lua_newuserdata(state, sizeof(audio_StreamSource)); err = audio_loadStream(src, filename); moduleData.audio_type = audio_type_stream; } else if (strcmp(type, "static") == 0) { audio_StaticSource *src = lua_newuserdata(state, sizeof(audio_StaticSource)); err = audio_loadStatic(src, filename); moduleData.audio_type = audio_type_static; } lua_rawgeti(state, LUA_REGISTRYINDEX, moduleData.audioDataMT); if(err == -1){ lua_pushstring(state, "Could not load sound file: "); lua_pushstring(state, lua_tostring(state, 1)); lua_pushstring(state, ", reason: unknow file type"); lua_concat(state, 3); return lua_error(state); } else if(err == 0){ lua_pushstring(state, "Could not load sound file: "); lua_pushstring(state, lua_tostring(state, 1)); lua_pushstring(state, ", reason: file does not exist"); lua_concat(state, 3); return lua_error(state); } lua_setmetatable(state, -2); return 1; }
Obj* c_audio_newSource(void* root, Obj** env, Obj** list) { if (length(*list) < 1 || length(*list) > 2) error("audio newSource expects 1 or 2 args"); char* filename = creo_tostring(root, env, list, 0); char* type = creo_optstring(root, env, list, 1, "static"); int err; DEFINE1(ret); if (strcmp(type, "stream") == 0) { audio_StreamSource* src = malloc(sizeof(audio_StreamSource)); err = audio_loadStream(src, filename); moduleData.audio_type = audio_type_stream; *ret = make_custom(root, src); } else if (strcmp(type, "static") == 0) { audio_StaticSource* src = malloc(sizeof(audio_StaticSource)); err = audio_loadStatic(src, filename); moduleData.audio_type = audio_type_static; *ret = make_custom(root, src); } if (err == -1) error("Could not load file: %s%s", filename, " reason: unknown file type"); if (err == 0) error("Could not load file: %s%s", filename, " reason: file does not exist"); return *ret; }