From 6621d248b8e2b6ead7fbfcd8470a87e3a175a3bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 12 May 2021 19:26:41 +0200 Subject: [PATCH] introduce basic asset system --- code/game/header/assets.h | 37 ++++++----- code/game/header/utils/raylib_helpers.h | 10 +++ code/game/source/assets.c | 82 ++++++++++++++++++++++++- code/game/source/assets_list.c | 5 ++ code/game/source/platform_raylib.c | 3 + 5 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 code/game/source/assets_list.c diff --git a/code/game/header/assets.h b/code/game/header/assets.h index 35966f8..f170be8 100644 --- a/code/game/header/assets.h +++ b/code/game/header/assets.h @@ -1,21 +1,28 @@ #pragma once #include "system.h" -typedef struct { - uint16_t id; - uint8_t kind; - char* filename; -} asset_info; +#define ASSET_INVALID 0xFF -enum { - ASSET_KIND_IMAGE, - ASSET_KIND_SOUND, - ASSET_KIND_FORCE_8 = UINT8_MAX, -}; - -enum { +typedef enum { ASSET_PLAYER, - ASSET_FORCE_16 = UINT16_MAX, -}; + ASSET_THING, + + MAX_ASSETS, + FORCE_ASSET_UINT16 = UINT16_MAX +} asset_id; -extern asset_info assets[]; +typedef enum { + AKIND_TEXTURE, + AKIND_SOUND, + + FORCE_AKIND_UINT8 = UINT8_MAX +} asset_kind; + +int32_t assets_setup(void); +void assets_destroy(void); + +uint16_t assets_find(asset_id id); + +asset_kind assets_get_kind(uint16_t id); +void *assets_get_snd(uint16_t id); +void *assets_get_tex(uint16_t id); diff --git a/code/game/header/utils/raylib_helpers.h b/code/game/header/utils/raylib_helpers.h index d65bf5d..ac6c2e0 100644 --- a/code/game/header/utils/raylib_helpers.h +++ b/code/game/header/utils/raylib_helpers.h @@ -52,4 +52,14 @@ void DrawRectangleEco(float posX, float posY, int width, int height, Color color static inline Image GetBlockImage(uint8_t id) { return *(Image*)blocks_get_img(id); +} + +static inline +Image GetSpriteImage(uint16_t id) { + return *(Image*)assets_get_tex(id); +} + +static inline +Sound GetSound(uint16_t id) { + return *(Sound*)assets_get_snd(id); } \ No newline at end of file diff --git a/code/game/source/assets.c b/code/game/source/assets.c index b411c01..5092d3f 100644 --- a/code/game/source/assets.c +++ b/code/game/source/assets.c @@ -1,5 +1,81 @@ #include "assets.h" +#include "raylib.h" -asset_info assets[] = { - {.id = ASSET_PLAYER, .kind = ASSET_KIND_IMAGE, .filename = "player.png"} -}; +#define ASSETS_COUNT (sizeof(assets)/sizeof(asset)) + +typedef struct { + asset_id id; + asset_kind kind; + + union { + Texture2D tex; + Sound snd; + }; + + // NOTE(zaklaus): metadata +} asset; + +#include "assets_list.c" + +int32_t assets_setup(void) { + for (uint32_t i=0; ikind) { + case AKIND_TEXTURE: { + // TODO(zaklaus): introduce texgen + Image img = GenImageColor(1, 1, RAYWHITE); + b->tex = LoadTextureFromImage(img); + UnloadImage(img); + }break; + + case AKIND_SOUND: { + // TODO(zaklaus): soundgen + }break; + + default: { + // TODO(zaklaus): assert + }break; + } + } + return 0; +} + +void assets_destroy(void) { + for (uint32_t i=0; i