forked from MoonsideGames/FAudioGMS
ID re-use system
parent
f449b135c2
commit
60ca2077d7
|
@ -43,6 +43,38 @@ static inline void Log(char *string)
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct IdStack
|
||||||
|
{
|
||||||
|
uint32_t* array;
|
||||||
|
uint32_t count;
|
||||||
|
uint32_t capacity;
|
||||||
|
} IdStack;
|
||||||
|
|
||||||
|
static inline void IdStack_Init(IdStack *stack)
|
||||||
|
{
|
||||||
|
stack->array = NULL;
|
||||||
|
stack->capacity = 0;
|
||||||
|
stack->count = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void IdStack_Push(IdStack *stack, uint32_t id)
|
||||||
|
{
|
||||||
|
if (stack->count >= stack->capacity)
|
||||||
|
{
|
||||||
|
stack->array = SDL_realloc(stack->array, (stack->capacity + 1) * sizeof(uint32_t));
|
||||||
|
stack->capacity += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
stack->array[stack->count] = id;
|
||||||
|
stack->count += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint32_t IdStack_Pop(IdStack* stack)
|
||||||
|
{
|
||||||
|
stack->count -= 1;
|
||||||
|
return stack->array[stack->count];
|
||||||
|
}
|
||||||
|
|
||||||
typedef enum FAudioGMS_SoundState
|
typedef enum FAudioGMS_SoundState
|
||||||
{
|
{
|
||||||
SoundState_Playing,
|
SoundState_Playing,
|
||||||
|
@ -99,9 +131,11 @@ typedef struct FAudioGMS_Device
|
||||||
|
|
||||||
FAudioGMS_StaticSound **staticSounds;
|
FAudioGMS_StaticSound **staticSounds;
|
||||||
uint32_t staticSoundCount;
|
uint32_t staticSoundCount;
|
||||||
|
IdStack staticSoundIndexStack;
|
||||||
|
|
||||||
FAudioGMS_SoundInstance **soundInstances;
|
FAudioGMS_SoundInstance **soundInstances;
|
||||||
uint32_t soundInstanceCount;
|
uint32_t soundInstanceCount;
|
||||||
|
IdStack soundInstanceIndexStack;
|
||||||
} FAudioGMS_Device;
|
} FAudioGMS_Device;
|
||||||
|
|
||||||
static FAudioGMS_Device *device = NULL;
|
static FAudioGMS_Device *device = NULL;
|
||||||
|
@ -181,9 +215,11 @@ void FAudioGMS_Init()
|
||||||
|
|
||||||
device->staticSounds = NULL;
|
device->staticSounds = NULL;
|
||||||
device->staticSoundCount = 0;
|
device->staticSoundCount = 0;
|
||||||
|
IdStack_Init(&device->staticSoundIndexStack);
|
||||||
|
|
||||||
device->soundInstances = NULL;
|
device->soundInstances = NULL;
|
||||||
device->soundInstanceCount = 0;
|
device->soundInstanceCount = 0;
|
||||||
|
IdStack_Init(&device->soundInstanceIndexStack);
|
||||||
|
|
||||||
Log("FAudio initialized successfully!");
|
Log("FAudio initialized successfully!");
|
||||||
printf("Device: %ls", device->deviceDetails.DisplayName);
|
printf("Device: %ls", device->deviceDetails.DisplayName);
|
||||||
|
@ -195,6 +231,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_Destroy(FAudioGMS_SoundInstance* in
|
||||||
if (instance != NULL)
|
if (instance != NULL)
|
||||||
{
|
{
|
||||||
device->soundInstances[instance->id] = NULL;
|
device->soundInstances[instance->id] = NULL;
|
||||||
|
IdStack_Push(&device->soundInstanceIndexStack, instance->id);
|
||||||
FAudioSourceVoice_Stop(instance->handle, 0, 0);
|
FAudioSourceVoice_Stop(instance->handle, 0, 0);
|
||||||
FAudioSourceVoice_FlushSourceBuffers(instance->handle);
|
FAudioSourceVoice_FlushSourceBuffers(instance->handle);
|
||||||
FAudioVoice_DestroyVoice(instance->handle);
|
FAudioVoice_DestroyVoice(instance->handle);
|
||||||
|
@ -224,6 +261,7 @@ static void FAudioGMS_INTERNAL_StaticSound_Destroy(FAudioGMS_StaticSound* sound)
|
||||||
if (sound != NULL)
|
if (sound != NULL)
|
||||||
{
|
{
|
||||||
device->staticSounds[sound->id] = NULL;
|
device->staticSounds[sound->id] = NULL;
|
||||||
|
IdStack_Push(&device->staticSoundIndexStack, sound->id);
|
||||||
SDL_free((void*)sound->buffer.pAudioData);
|
SDL_free((void*)sound->buffer.pAudioData);
|
||||||
SDL_free(sound);
|
SDL_free(sound);
|
||||||
}
|
}
|
||||||
|
@ -338,10 +376,18 @@ double FAudioGMS_StaticSound_LoadWAV(char *filePath)
|
||||||
sound->loopStart = 0;
|
sound->loopStart = 0;
|
||||||
sound->loopLength = 0;
|
sound->loopLength = 0;
|
||||||
|
|
||||||
/* FIXME: id re-use system */
|
if (device->staticSoundIndexStack.count > 0)
|
||||||
sound->id = device->staticSoundCount;
|
{
|
||||||
|
sound->id = IdStack_Pop(&device->staticSoundIndexStack);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sound->id = device->staticSoundCount;
|
||||||
|
|
||||||
|
device->staticSounds = SDL_realloc(device->staticSounds, (device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound*));
|
||||||
|
device->staticSoundCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
device->staticSounds = SDL_realloc(device->staticSounds, (device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound*));
|
|
||||||
device->staticSounds[device->staticSoundCount] = sound;
|
device->staticSounds[device->staticSoundCount] = sound;
|
||||||
device->staticSoundCount += 1;
|
device->staticSoundCount += 1;
|
||||||
|
|
||||||
|
@ -594,12 +640,19 @@ static FAudioGMS_SoundInstance* FAudioGMS_INTERNAL_SoundInstance_CreateFromStati
|
||||||
|
|
||||||
instance->soundState = SoundState_Stopped;
|
instance->soundState = SoundState_Stopped;
|
||||||
|
|
||||||
/* FIXME: id re-use system */
|
if (device->soundInstanceIndexStack.count > 0)
|
||||||
instance->id = device->soundInstanceCount;
|
{
|
||||||
|
instance->id = IdStack_Pop(&device->soundInstanceIndexStack);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
instance->id = device->soundInstanceCount;
|
||||||
|
|
||||||
|
device->soundInstances = SDL_realloc(device->soundInstances, (device->soundInstanceCount + 1) * sizeof(FAudioGMS_SoundInstance*));
|
||||||
|
device->soundInstanceCount += 1;
|
||||||
|
}
|
||||||
|
|
||||||
device->soundInstances = SDL_realloc(device->soundInstances, (device->soundInstanceCount + 1) * sizeof(FAudioGMS_SoundInstance*));
|
|
||||||
device->soundInstances[device->soundInstanceCount] = instance;
|
device->soundInstances[device->soundInstanceCount] = instance;
|
||||||
device->soundInstanceCount += 1;
|
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue