79 lines
1.8 KiB
C
79 lines
1.8 KiB
C
#include "engineAssets.h"
|
|
#include <raylib.h>
|
|
#include <string.h>
|
|
|
|
#define ASSET_CACHE_CAPACITY 64
|
|
#define ASSET_PATH_MAX 128
|
|
|
|
typedef struct {
|
|
char path[ASSET_PATH_MAX];
|
|
Texture2D texture;
|
|
} CachedTexture;
|
|
|
|
typedef struct {
|
|
char path[ASSET_PATH_MAX];
|
|
Sound sound;
|
|
} CachedSound;
|
|
|
|
static CachedTexture textureCache[ASSET_CACHE_CAPACITY];
|
|
static int textureCacheCount = 0;
|
|
|
|
static CachedSound soundCache[ASSET_CACHE_CAPACITY];
|
|
static int soundCacheCount = 0;
|
|
|
|
Texture2D LoadTextureCached(const char *path) {
|
|
for (int i = 0; i < textureCacheCount; i++) {
|
|
if (strcmp(textureCache[i].path, path) == 0) {
|
|
return textureCache[i].texture;
|
|
}
|
|
}
|
|
|
|
Texture2D texture = LoadTexture(path);
|
|
|
|
if (textureCacheCount >= ASSET_CACHE_CAPACITY) {
|
|
TraceLog(LOG_WARNING, "ASSETS: Texture cache full, not caching %s", path);
|
|
return texture;
|
|
}
|
|
|
|
CachedTexture *entry = &textureCache[textureCacheCount++];
|
|
strncpy(entry->path, path, ASSET_PATH_MAX - 1);
|
|
entry->path[ASSET_PATH_MAX - 1] = '\0';
|
|
entry->texture = texture;
|
|
|
|
return texture;
|
|
}
|
|
|
|
Sound LoadSoundCached(const char *path) {
|
|
for (int i = 0; i < soundCacheCount; i++) {
|
|
if (strcmp(soundCache[i].path, path) == 0) {
|
|
return soundCache[i].sound;
|
|
}
|
|
}
|
|
|
|
Sound sound = LoadSound(path);
|
|
|
|
if (soundCacheCount >= ASSET_CACHE_CAPACITY) {
|
|
TraceLog(LOG_WARNING, "ASSETS: Sound cache full, not caching %s", path);
|
|
return sound;
|
|
}
|
|
|
|
CachedSound *entry = &soundCache[soundCacheCount++];
|
|
strncpy(entry->path, path, ASSET_PATH_MAX - 1);
|
|
entry->path[ASSET_PATH_MAX - 1] = '\0';
|
|
entry->sound = sound;
|
|
|
|
return sound;
|
|
}
|
|
|
|
void UnloadCachedAssets(void) {
|
|
for (int i = 0; i < textureCacheCount; i++) {
|
|
UnloadTexture(textureCache[i].texture);
|
|
}
|
|
textureCacheCount = 0;
|
|
|
|
for (int i = 0; i < soundCacheCount; i++) {
|
|
UnloadSound(soundCache[i].sound);
|
|
}
|
|
soundCacheCount = 0;
|
|
}
|