Skip to content

Commit

Permalink
add overloads, fix use after free
Browse files Browse the repository at this point in the history
  • Loading branch information
griffi-gh committed Oct 20, 2024
1 parent 98e5f7b commit 595ee31
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/src/escript.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "escript.hh"
#include "SDL_mixer.h"
#include "model.hh"
#include "game.hh"
#include "pkgman.hh"
Expand All @@ -7,6 +8,7 @@
#include "receiver.hh"
#include "soundmanager.hh"
#include "tms/backend/print.h"
#include "tms/math/misc.h"
#include "ui.hh"
#include "robotman.hh"
#include "robot_base.hh"
Expand Down Expand Up @@ -3125,7 +3127,9 @@ extern "C" {
len * sizeof(int16_t),
chunk_name);

free(pcm_buf);
// XXX: do not do this...
// Mix_QuickLoad_RAW just points to the buffer, doesnt actually load the data
// free(pcm_buf);

tms_debugf("Created Sfx %p", sfx);

Expand All @@ -3137,13 +3141,35 @@ extern "C" {
return 1;
}

// possible overloads:
// (this) - play globally with full volume
// (this, volume) - play globally with specified volume
// (this, volume, x, y) - play at specified position with specified volume
static int l_sfx_play(lua_State *L) {
// get this as SfxMT

// Get this as SfxMT
sm_sound *s = *(sm_sound**)luaL_checkudata(L, 1, "SfxMT");

// Get other args
float x = 0, y = 0;
float volume = 1.0f;
bool global = true;
if (lua_gettop(L) >= 2 && !lua_isnil(L, 2)) {
// XXX: should volume upper bound be clamped?
volume = tclampf(
luaL_checknumber(L, 2),
SM_MIN_VOLUME, 1.);
}
if (lua_gettop(L) >= 3 &&
!(lua_isnil(L, 3) && lua_isnil(L, 4))
) {
global = false;
x = luaL_checknumber(L, 3);
y = luaL_checknumber(L, 4);
}

// TODO: accept other arguments
tms_debugf("Playing Sfx %p", s);
sm::play(s, 0., 0., 0, 1.);
sm::play(s, x, y, 0, volume, false, 0, global);

return 0;
}
Expand All @@ -3159,7 +3185,9 @@ extern "C" {
// TODO: figure out how to handle audio that's still playing! (is it okay to free it after play is called?)
tms_debugf("deleting Sfx %p", s);
for (size_t i = 0; i < s->num_chunks; i++) {
SDL_free(s->chunks[i].chunk);
Mix_Chunk *chunk = s->chunks[i].chunk;
free(chunk->abuf);
SDL_free(chunk);
}
s->num_chunks = 0;
free(s);
Expand Down

0 comments on commit 595ee31

Please sign in to comment.