summaryrefslogtreecommitdiff
path: root/src/audio
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio')
-rw-r--r--src/audio/audio.c51
-rw-r--r--src/audio/audio.h11
2 files changed, 32 insertions, 30 deletions
diff --git a/src/audio/audio.c b/src/audio/audio.c
index 45d11c2..412a434 100644
--- a/src/audio/audio.c
+++ b/src/audio/audio.c
@@ -1,5 +1,5 @@
#include <lua.h>
-#include <honeysuckle.h>
+#include <lauxlib.h>
#define STB_VORBIS_HEADER_ONLY
#include <stb_vorbis.c>
@@ -11,64 +11,57 @@
/* stb_vorbis defines this for some reason? */
#undef L
+#include "util/util.h"
#include "audio.h"
-int audio_engine_init(lua_State *L);
-int audio_engine_uninit(lua_State *L);
-int audio_engine_play_sound(lua_State *L);
+const char *audio_engine_tname = "audio.Engine";
void setup_audio(lua_State *L, int honey_tbl)
{
- hs_create_table(L,
- hs_str_cfunc("engine_init", audio_engine_init),
- hs_str_cfunc("engine_uninit", audio_engine_uninit),
- hs_str_cfunc("engine_play_sound", audio_engine_play_sound),
+ luaL_newmetatable(L, audio_engine_tname); lua_pop(L, 1);
+
+ struct honey_tbl_t audio[] = {
+ #define X(name, func) H_FUNC(name, func),
+ AUDIO_FUNCTIONS
+ #undef X
/* ma_result values */
- hs_str_int("MA_SUCCESS", MA_SUCCESS),
- );
+ H_ENUM(MA_SUCCESS),
+
+ H_END,
+ };
+ create_table(L, audio);
lua_setfield(L, honey_tbl, "audio");
}
int audio_engine_init(lua_State *L)
{
- ma_engine *engine = malloc(sizeof(ma_engine));
- if (engine == NULL)
- hs_throw_error(L, "failed to allocate memory for engine");
-
+ ma_engine *engine = lua_newuserdata(L, sizeof(ma_engine));
ma_result result = ma_engine_init(NULL, engine);
if (result != MA_SUCCESS)
- hs_throw_error(L, "failed to initialize audio engine: %d", result);
-
- lua_pushlightuserdata(L, engine);
+ luaL_error(L, "failed to initialize audio engine: %d", result);
+
+ luaL_getmetatable(L, audio_engine_tname);
+ lua_setmetatable(L, -2);
return 1;
}
int audio_engine_uninit(lua_State *L)
{
- ma_engine *engine;
- void *engine_ptr;
- hs_parse_args(L, hs_light(engine_ptr));
- engine = engine_ptr;
-
+ ma_engine *engine = luaL_checkudata(L, 1, audio_engine_tname);
ma_engine_uninit(engine);
- free(engine);
return 0;
}
int audio_engine_play_sound(lua_State *L)
{
- ma_engine *engine;
- void *engine_ptr;
- char *filename;
- hs_parse_args(L, hs_light(engine_ptr), hs_str(filename));
- engine = engine_ptr;
-
+ ma_engine *engine = luaL_checkudata(L, 1, audio_engine_tname);
+ char *filename = luaL_checkstring(L, 2);
ma_engine_play_sound(engine, filename, NULL);
return 0;
}
diff --git a/src/audio/audio.h b/src/audio/audio.h
index 1aa7f43..319a86c 100644
--- a/src/audio/audio.h
+++ b/src/audio/audio.h
@@ -3,6 +3,15 @@
#include <lua.h>
-void setup_audio(lua_State *L, int honey_tbl);
+extern const char *audio_engine_tname;
+
+#define AUDIO_FUNCTIONS \
+ X("engine_init", audio_engine_init) \
+ X("engine_uninit", audio_engine_uninit) \
+ X("engine_play_sound", audio_engine_play_sound) \
+
+#define X(name, func) int func(lua_State *L);
+AUDIO_FUNCTIONS
+#undef X
#endif