From bc4af3b47260ee0f79343303b135d1ea32cde4d4 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 17 Feb 2023 00:48:10 -0600 Subject: begin remove honeysuckle refactor --- src/audio/audio.c | 51 ++++++++++++++++++++++----------------------------- src/audio/audio.h | 11 ++++++++++- 2 files changed, 32 insertions(+), 30 deletions(-) (limited to 'src/audio') 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 -#include +#include #define STB_VORBIS_HEADER_ONLY #include @@ -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 -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 -- cgit v1.2.1