Connected the AssetManager to the GameEngine and added function in the
GameEngine API to allow for loading and removing assets as well as rendering textures using their AssetID
This commit is contained in:
1 parent
c4f1775392
commit
3f71cf0f3f
11 files changed
+156
-33
No files matched your search
+48
-12
@@ -1,10 +1,10 @@
|
||||
#include "AssetManager.h"
|
||||
#include "Renderer.h"
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_mixer.h"
|
||||
#include "SDL_surface.h"
|
||||
#include "SDL_render.h"
|
||||
#include "constants.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -15,7 +15,7 @@ typedef struct asset {
|
||||
AssetType type;
|
||||
char *path;
|
||||
union {
|
||||
SDL_Surface *surface;
|
||||
SDL_Texture *texture;
|
||||
Mix_Chunk *chunk;
|
||||
Mix_Music *music;
|
||||
} data;
|
||||
@@ -42,7 +42,8 @@ AssetManager *create_asset_manager() {
|
||||
return am;
|
||||
}
|
||||
|
||||
AssetID load_asset(AssetManager *am, const char *path, AssetType type) {
|
||||
AssetID load_asset_to_manager(AssetManager *am, const char *path,
|
||||
AssetType type, Renderer *renderer) {
|
||||
if (!am || type == ASSET_TYPE_COUNT) {
|
||||
return -1;
|
||||
}
|
||||
@@ -56,16 +57,23 @@ AssetID load_asset(AssetManager *am, const char *path, AssetType type) {
|
||||
|
||||
switch (type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
asset.data.surface = IMG_Load(path);
|
||||
if (!renderer) {
|
||||
loaded = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!(asset.data.surface)) {
|
||||
asset.data.texture = load_texture(renderer, path);
|
||||
|
||||
if (!(asset.data.texture)) {
|
||||
loaded = false;
|
||||
}
|
||||
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
// TODO (Abdelrahman): Implement loading audio chunks
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
// TODO (Abdelrahman): Implement loading music
|
||||
break;
|
||||
default:
|
||||
loaded = false;
|
||||
@@ -95,7 +103,7 @@ AssetID load_asset(AssetManager *am, const char *path, AssetType type) {
|
||||
|
||||
switch (type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
asset.data.surface = NULL;
|
||||
asset.data.texture = NULL;
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
asset.data.chunk = NULL;
|
||||
@@ -110,7 +118,7 @@ AssetID load_asset(AssetManager *am, const char *path, AssetType type) {
|
||||
return id;
|
||||
}
|
||||
|
||||
void close_asset(AssetManager *am, AssetID id) {
|
||||
void remove_asset_from_manager(AssetManager *am, AssetID id) {
|
||||
if (!am || am->count == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -120,12 +128,14 @@ void close_asset(AssetManager *am, AssetID id) {
|
||||
|
||||
switch (am->assets[id].type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
SDL_FreeSurface(am->assets[id].data.surface);
|
||||
am->assets[id].data.surface = NULL;
|
||||
SDL_DestroyTexture(am->assets[id].data.texture);
|
||||
am->assets[id].data.texture = NULL;
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
// TODO (Abdelrahman): Implement closing audio chunk
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
// TODO (Abdelrahman): Implement closing music
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@@ -136,6 +146,30 @@ void close_asset(AssetManager *am, AssetID id) {
|
||||
am->count -= 1;
|
||||
}
|
||||
|
||||
SDL_Texture *get_texture(AssetManager *am, AssetID id) {
|
||||
if (am->assets[id].type != ASSET_TYPE_IMAGE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return am->assets[id].data.texture;
|
||||
}
|
||||
|
||||
Mix_Chunk *get_audio_chunk(AssetManager *am, AssetID id) {
|
||||
if (am->assets[id].type != ASSET_TYPE_AUDIO_FX) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return am->assets[id].data.chunk;
|
||||
}
|
||||
|
||||
Mix_Music *get_music(AssetManager *am, AssetID id) {
|
||||
if (am->assets[id].type != ASSET_TYPE_MUSIC) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return am->assets[id].data.music;
|
||||
}
|
||||
|
||||
void destroy_asset_manager(AssetManager *am) {
|
||||
if (!am) {
|
||||
return;
|
||||
@@ -147,12 +181,14 @@ void destroy_asset_manager(AssetManager *am) {
|
||||
|
||||
switch (am->assets[i].type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
SDL_FreeSurface(am->assets[i].data.surface);
|
||||
am->assets[i].data.surface = 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
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
// TODO (Abdelrahman): Implement closing music
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user