introduce basic asset system

isolation_bkp/dynres
Dominik Madarász 2021-05-12 19:26:41 +02:00
parent b4ae2d01df
commit 6621d248b8
5 changed files with 119 additions and 18 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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; i<ASSETS_COUNT; i++) {
asset *b = &assets[i];
switch (b->kind) {
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<ASSETS_COUNT; i++) {
switch (assets[i].kind) {
case AKIND_TEXTURE: {
UnloadTexture(assets[i].tex);
}break;
case AKIND_SOUND: {
// TODO(zaklaus): soundgen
}break;
default: {
// TODO(zaklaus): assert
}break;
}
}
}
uint16_t assets_find(asset_id id) {
for (uint32_t i=0; i<ASSETS_COUNT; i++) {
if (assets[i].id == id)
return i;
}
return ASSET_INVALID;
}
asset_kind assets_get_kind(uint16_t id) {
return assets[id].kind;
}
void *assets_get_snd(uint16_t id) {
return (void*)&assets[id].snd;;
}
void *assets_get_tex(uint16_t id) {
return (void*)&assets[id].tex;
}

View File

@ -0,0 +1,5 @@
#include "assets.h"
static asset assets[] = {
};

View File

@ -8,6 +8,7 @@
#include "camera.h"
#include "math.h"
#include "world/blocks.h"
#include "assets.h"
#include "utils/raylib_helpers.h"
uint16_t screenWidth = 1600;
@ -44,10 +45,12 @@ void platform_init() {
EndDrawing();
blocks_setup();
assets_setup();
}
void platform_shutdown() {
blocks_destroy();
assets_destroy();
CloseWindow();
}