Skip to content

Commit

Permalink
On sound finish callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyGaul committed Jul 21, 2024
1 parent f401005 commit 87e9df7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Binary file added content/jump.wav
Binary file not shown.
12 changes: 11 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ REF_FUNCTION(dump_lua_api);
// -------------------------------------------------------------------------------------------------
// Main

void mount_content_directory_as(const char* dir)
{
Path path = fs_get_base_directory();
path.normalize();
path.pop(2); // Pop out of build/debug/
path += "/content";
fs_mount(path.c_str(), dir);
}

int main(int argc, char* argv[])
{
make_app("CF in Lua", 0, 0, 640, 480, APP_OPTIONS_WINDOW_POS_CENTERED | APP_OPTIONS_NO_AUDIO, argv[0]);
make_app("CF in Lua", 0, 0, 640, 480, APP_OPTIONS_WINDOW_POS_CENTERED, argv[0]);
mount_content_directory_as("/");
::L = luaL_newstate();
luaL_openlibs(L);
REF_BindLua(L);
Expand Down
11 changes: 8 additions & 3 deletions src/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ sprite_set_scale(spr, 4,4)
dbg_thickness = 0
fps = 1

jump = audio_load_wav("jump.wav")
function on_sound_finish(snd)
print("Sound finished: "..tostring(snd))
end
sound_set_on_finish("on_sound_finish")

function dbg_draw_polygon(verts, cr, cg, cb, ca)
draw_push_color(cr, cg, cb, ca)
draw_polyline(verts, dbg_thickness, true)
Expand Down Expand Up @@ -121,7 +127,8 @@ function on_update()
REF_SyncGlobals()

if key_just_pressed(KEY_SPACE) then
message_box(MESSAGE_BOX_TYPE_ERROR, "PEN15", "TEXT OF THE ERROR MESSAGE")
local def = sound_params_defaults()
play_sound(jump, def)
end

b2World_Step(world, DELTA_TIME_FIXED, 4)
Expand Down Expand Up @@ -155,8 +162,6 @@ function main()
local text = "<blue>Hello</blue>"
draw_text(text, 150,0, #text)
end
draw_circle(-100,100, 50, 10)
draw_quad(-100,-100, 100,100, 5, 5)

draw_polyline({ -300,200, 0,250, 300,200 }, 5, false)

Expand Down
13 changes: 13 additions & 0 deletions src/wrap_cf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ REF_FUNCTION(app_get_power_percentage_left);
// Audio

REF_HANDLE_TYPE(CF_Audio);
REF_FUNCTION_EX(audio_cull_duplicates, cf_audio_cull_duplicates);
REF_FUNCTION(audio_load_ogg);
REF_FUNCTION(audio_load_wav);
REF_FUNCTION(audio_load_ogg_from_memory);
Expand Down Expand Up @@ -321,6 +322,7 @@ REF_STRUCT(SoundParams,
REF_MEMBER(pitch),
);

REF_HANDLE_TYPE(CF_Sound);
REF_HANDLE_TYPE(Sound);

SoundParams sound_params_defaults() { return SoundParams(); }
Expand All @@ -338,6 +340,17 @@ REF_FUNCTION(sound_set_pitch);
REF_FUNCTION(sound_set_sample_index);
REF_FUNCTION(sound_stop);

void wrap_on_sound_finish(CF_Sound snd, void* udata)
{
const char* lua_fn_name = (const char*)udata;
REF_CallLuaFunction(L, lua_fn_name, { }, snd);
}
void wrap_sound_set_on_finish(const char* lua_fn_name)
{
cf_sound_set_on_finish_callback(wrap_on_sound_finish, (void*)lua_fn_name, true);
}
REF_FUNCTION_EX(sound_set_on_finish, wrap_sound_set_on_finish);

// -------------------------------------------------------------------------------------------------
// Clipboard

Expand Down

0 comments on commit 87e9df7

Please sign in to comment.