Implemented the core functionality of the ObjectStore and started
implementing the functionality for the AssetManager
This commit is contained in:
1 parent
79f28100bd
commit
c4f1775392
14 files changed
+330
-383
No files matched your search
@@ -0,0 +1,167 @@
|
||||
#include "AssetManager.h"
|
||||
#include "SDL_image.h"
|
||||
#include "SDL_mixer.h"
|
||||
#include "SDL_surface.h"
|
||||
#include "constants.h"
|
||||
#include "reusable_ids.h"
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct asset {
|
||||
AssetType type;
|
||||
char *path;
|
||||
union {
|
||||
SDL_Surface *surface;
|
||||
Mix_Chunk *chunk;
|
||||
Mix_Music *music;
|
||||
} data;
|
||||
} Asset;
|
||||
|
||||
struct asset_manager {
|
||||
size_t count;
|
||||
size_t deleted_count;
|
||||
AssetID reusable_ids[MAX_ASSET_COUNT];
|
||||
Asset assets[MAX_ASSET_COUNT];
|
||||
};
|
||||
|
||||
AssetManager *create_asset_manager() {
|
||||
AssetManager *am = (AssetManager *)malloc(sizeof(AssetManager));
|
||||
if (!am) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
am->count = 0;
|
||||
am->deleted_count = 0;
|
||||
memset(am->reusable_ids, 0, sizeof(am->reusable_ids));
|
||||
memset(am->assets, 0, sizeof(am->assets));
|
||||
|
||||
return am;
|
||||
}
|
||||
|
||||
AssetID load_asset(AssetManager *am, const char *path, AssetType type) {
|
||||
if (!am || type == ASSET_TYPE_COUNT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
Asset asset = {};
|
||||
bool loaded = true;
|
||||
|
||||
// TODO (Abdelrahman): Implement a way to make sure that an asset with the
|
||||
// same path is not already loaded. If the user attempts to load an asset
|
||||
// that already exists, the function should return -1 to indicate an error
|
||||
|
||||
switch (type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
asset.data.surface = IMG_Load(path);
|
||||
|
||||
if (!(asset.data.surface)) {
|
||||
loaded = false;
|
||||
}
|
||||
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
break;
|
||||
default:
|
||||
loaded = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!loaded) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
AssetID id;
|
||||
|
||||
if (am->deleted_count > 0) {
|
||||
id = (AssetID)reuse_id((int32_t *)am->reusable_ids, am->deleted_count);
|
||||
am->deleted_count -= 1;
|
||||
am->count += 1;
|
||||
} else {
|
||||
id = (am->count)++;
|
||||
}
|
||||
|
||||
am->assets[id].type = type;
|
||||
|
||||
am->assets[id].path = (char *)malloc(strlen(path) + 1);
|
||||
strcpy(am->assets[id].path, path);
|
||||
|
||||
am->assets[id].data = asset.data;
|
||||
|
||||
switch (type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
asset.data.surface = NULL;
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
asset.data.chunk = NULL;
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
asset.data.music = NULL;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
void close_asset(AssetManager *am, AssetID id) {
|
||||
if (!am || am->count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(am->assets[id].path);
|
||||
am->assets[id].path = 0;
|
||||
|
||||
switch (am->assets[id].type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
SDL_FreeSurface(am->assets[id].data.surface);
|
||||
am->assets[id].data.surface = NULL;
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
am->reusable_ids[(am->deleted_count)++] = id;
|
||||
|
||||
am->count -= 1;
|
||||
}
|
||||
|
||||
void destroy_asset_manager(AssetManager *am) {
|
||||
if (!am) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < am->count; ++i) {
|
||||
free(am->assets[i].path);
|
||||
am->assets[i].path = NULL;
|
||||
|
||||
switch (am->assets[i].type) {
|
||||
case ASSET_TYPE_IMAGE:
|
||||
SDL_FreeSurface(am->assets[i].data.surface);
|
||||
am->assets[i].data.surface = NULL;
|
||||
break;
|
||||
case ASSET_TYPE_AUDIO_FX:
|
||||
break;
|
||||
case ASSET_TYPE_MUSIC:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
am->count = 0;
|
||||
am->deleted_count = 0;
|
||||
|
||||
free(am);
|
||||
am = NULL;
|
||||
}
|
||||
Reference in new issue
Block a user