ID re-use system

pull/1/head
cosmonaut 2021-10-24 00:24:57 -07:00
parent f449b135c2
commit 60ca2077d7
1 changed files with 60 additions and 7 deletions

View File

@ -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 = IdStack_Pop(&device->staticSoundIndexStack);
}
else
{
sound->id = device->staticSoundCount; sound->id = device->staticSoundCount;
device->staticSounds = SDL_realloc(device->staticSounds, (device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound*)); device->staticSounds = SDL_realloc(device->staticSounds, (device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound*));
device->staticSoundCount += 1;
}
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 = IdStack_Pop(&device->soundInstanceIndexStack);
}
else
{
instance->id = device->soundInstanceCount; instance->id = device->soundInstanceCount;
device->soundInstances = SDL_realloc(device->soundInstances, (device->soundInstanceCount + 1) * sizeof(FAudioGMS_SoundInstance*)); device->soundInstances = SDL_realloc(device->soundInstances, (device->soundInstanceCount + 1) * sizeof(FAudioGMS_SoundInstance*));
device->soundInstances[device->soundInstanceCount] = instance;
device->soundInstanceCount += 1; device->soundInstanceCount += 1;
}
device->soundInstances[device->soundInstanceCount] = instance;
return instance; return instance;
} }