Implemented load audio and music files as well the basic audio playback

functionality.
This commit is contained in:
abdelrahman committed 2022-09-25 02:19:21 +01:00
1 parent dfee36ffe9
commit e3f4f9c94f
9 files changed
+464 -61

No files matched your search

+49 -26
View File
@@ -171,7 +171,16 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
AssetID existing_id =
get_asset_id_from_id_node(am, &(am->hashed_path_ids[hash_index]), path);
if (existing_id != INVALID_ID) {
if (am->assets[existing_id].type != type) {
// If the user tries to load an asset that is already loaded, but they
// try to load it as a type that doesn't match the asset's actual type,
// we return an INVALID_ID to ensure that the user doesn't try using the
// asset with functions that work on the wrong type
return INVALID_ID;
}
++(am->user_counter[existing_id]);
return existing_id;
}
@@ -189,16 +198,25 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
asset.data.texture = load_texture(renderer, path);
if (!(asset.data.texture)) {
printf("Error loading %s: %s\n", path, SDL_GetError());
loaded = false;
}
break;
case ASSET_TYPE_AUDIO_FX:
// TODO (Abdelrahman): Implement loading audio chunks
asset.data.chunk = Mix_LoadWAV(path);
if (!(asset.data.chunk)) {
loaded = false;
}
break;
case ASSET_TYPE_MUSIC:
// TODO (Abdelrahman): Implement loading music
asset.data.music = Mix_LoadMUS(path);
if (!(asset.data.music)) {
loaded = false;
}
break;
default:
loaded = false;
@@ -206,6 +224,7 @@ AssetID load_asset_to_manager(AssetManager *am, const char *path,
}
if (!loaded) {
printf("Error loading %s: %s\n", path, SDL_GetError());
return INVALID_ID;
}
@@ -266,7 +285,7 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
remove_id_node(&(am->hashed_path_ids[hash_index]), id);
free(am->assets[id].path);
am->assets[id].path = 0;
am->assets[id].path = NULL;
switch (am->assets[id].type) {
case ASSET_TYPE_IMAGE:
@@ -274,10 +293,12 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
am->assets[id].data.texture = NULL;
break;
case ASSET_TYPE_AUDIO_FX:
// TODO (Abdelrahman): Implement closing audio chunk
Mix_FreeChunk(am->assets[id].data.chunk);
am->assets[id].data.chunk = NULL;
break;
case ASSET_TYPE_MUSIC:
// TODO (Abdelrahman): Implement closing music
Mix_FreeMusic(am->assets[id].data.music);
am->assets[id].data.music = NULL;
break;
default:
break;
@@ -289,7 +310,7 @@ void remove_asset_from_manager(AssetManager *am, AssetID *id_ptr) {
}
SDL_Texture *get_texture(AssetManager *am, AssetID id) {
if (am->assets[id].type != ASSET_TYPE_IMAGE) {
if (id < 0 || am->assets[id].type != ASSET_TYPE_IMAGE) {
return NULL;
}
@@ -297,7 +318,7 @@ SDL_Texture *get_texture(AssetManager *am, AssetID id) {
}
Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id) {
if (am->assets[id].type != ASSET_TYPE_AUDIO_FX) {
if (id < 0 || am->assets[id].type != ASSET_TYPE_AUDIO_FX) {
return NULL;
}
@@ -305,46 +326,48 @@ Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id) {
}
Mix_Music *get_music(AssetManager *am, AssetID id) {
if (am->assets[id].type != ASSET_TYPE_MUSIC) {
if (id < 0 || am->assets[id].type != ASSET_TYPE_MUSIC) {
return NULL;
}
return am->assets[id].data.music;
}
void destroy_asset_manager(AssetManager *am) {
if (!am) {
void destroy_asset_manager(AssetManager **am) {
if (!(*am)) {
return;
}
for (size_t i = 0; i < am->count; ++i) {
am->user_counter[i] = 0;
for (size_t i = 0; i < (*am)->count; ++i) {
(*am)->user_counter[i] = 0;
size_t hash_index = hash_path(am->assets[i].path);
remove_id_node(&(am->hashed_path_ids[hash_index]), i);
size_t hash_index = hash_path((*am)->assets[i].path);
remove_id_node(&((*am)->hashed_path_ids[hash_index]), i);
free(am->assets[i].path);
am->assets[i].path = NULL;
free((*am)->assets[i].path);
(*am)->assets[i].path = NULL;
switch (am->assets[i].type) {
switch ((*am)->assets[i].type) {
case ASSET_TYPE_IMAGE:
SDL_DestroyTexture(am->assets[i].data.texture);
am->assets[i].data.texture = NULL;
SDL_DestroyTexture((*am)->assets[i].data.texture);
(*am)->assets[i].data.texture = NULL;
break;
case ASSET_TYPE_AUDIO_FX:
// TODO (Abdelrahman): Implement closing audio chunk
Mix_FreeChunk((*am)->assets[i].data.chunk);
(*am)->assets[i].data.chunk = NULL;
break;
case ASSET_TYPE_MUSIC:
// TODO (Abdelrahman): Implement closing music
Mix_FreeMusic((*am)->assets[i].data.music);
(*am)->assets[i].data.music = NULL;
break;
default:
break;
}
}
am->count = 0;
am->deleted_count = 0;
(*am)->count = 0;
(*am)->deleted_count = 0;
free(am);
am = NULL;
free(*am);
*am = NULL;
}