eco2d/code/game/source/world/blocks.c

117 lines
2.9 KiB
C
Raw Normal View History

2021-07-18 18:30:27 +00:00
#define ZPL_NANO
2021-05-04 17:41:30 +00:00
#include "zpl.h"
2021-05-12 16:28:39 +00:00
#include "world/world.h"
2021-05-04 17:41:30 +00:00
#include "world/blocks.h"
2021-05-12 15:50:30 +00:00
#include "raylib.h"
2021-05-12 17:38:11 +00:00
#include "gen/texgen.h"
2021-07-18 18:30:27 +00:00
#include "world_view.h"
2021-05-04 17:41:30 +00:00
#define BLOCKS_COUNT (sizeof(blocks)/sizeof(block))
ZPL_TABLE(static, blocks__chunk_tbl, blocks__chunk_tbl_, RenderTexture2D);
2021-07-18 18:30:27 +00:00
static blocks__chunk_tbl baked_chunks;
static void chunks_unload_textures(uint64_t key, RenderTexture2D *value) {
2021-07-18 18:30:27 +00:00
(void)key;
UnloadRenderTexture(*value);
2021-07-18 18:30:27 +00:00
}
2021-05-04 17:41:30 +00:00
typedef struct {
char *name;
uint32_t flags;
uint32_t kind;
uint32_t biome;
char symbol;
float drag;
2021-05-12 15:50:30 +00:00
// NOTE(zaklaus): viewer data
Texture2D img;
2021-05-04 17:41:30 +00:00
} block;
#include "blocks_list.c"
2021-05-12 16:28:39 +00:00
int32_t blocks_setup(void) {
for (uint32_t i=0; i<BLOCKS_COUNT; i++) {
block *b = &blocks[i];
2021-05-12 17:38:11 +00:00
b->img = texgen_build_block(b->biome, b->kind);
2021-05-12 16:28:39 +00:00
}
2021-07-18 18:30:27 +00:00
blocks__chunk_tbl_init(&baked_chunks, zpl_heap());
2021-05-12 16:28:39 +00:00
return 0;
}
void blocks_destroy(void) {
for (uint32_t i=0; i<BLOCKS_COUNT; i++) {
UnloadTexture(blocks[i].img);
2021-05-12 16:28:39 +00:00
}
2021-07-18 18:30:27 +00:00
blocks__chunk_tbl_map_mut(&baked_chunks, chunks_unload_textures);
blocks__chunk_tbl_destroy(&baked_chunks);
2021-05-12 16:28:39 +00:00
}
2021-05-04 17:41:30 +00:00
uint8_t blocks_find(uint32_t biome, uint32_t kind) {
2021-05-12 15:50:30 +00:00
for (uint32_t i=0; i<BLOCKS_COUNT; i++) {
2021-05-04 17:41:30 +00:00
if (blocks[i].biome == biome && blocks[i].kind == kind)
return i;
}
return BLOCK_INVALID;
}
char *blocks_get_name(uint8_t id) {
return blocks[id].name;
}
char blocks_get_symbol(uint8_t id) {
return blocks[id].symbol;
}
uint32_t blocks_get_flags(uint8_t id) {
return blocks[id].flags;
}
uint32_t blocks_get_biome(uint8_t id) {
return blocks[id].biome;
}
uint32_t blocks_get_kind(uint8_t id) {
return blocks[id].kind;
}
2021-05-12 16:28:39 +00:00
float blocks_get_drag(uint8_t id) {
return blocks[id].drag;
}
void *blocks_get_img(uint8_t id) {
return (void*)&blocks[id].img;
2021-07-18 18:30:27 +00:00
}
void blocks_build_chunk_tex(uint64_t id, uint8_t *chunk_blocks, size_t blocks_len, void *raw_view) {
(void)blocks_len;
world_view *view = (world_view*)raw_view;
uint16_t dims = WORLD_BLOCK_SIZE * view->chunk_size;
RenderTexture2D canvas = LoadRenderTexture(dims, dims);
BeginTextureMode(canvas);
ClearBackground(WHITE);
2021-07-18 18:30:27 +00:00
for (int y = 0; y < view->chunk_size; y += 1) {
for (int x = 0; x < view->chunk_size; x += 1) {
#if 1
Texture2D blk = blocks[chunk_blocks[(y*view->chunk_size)+x]].img;
2021-07-18 18:30:27 +00:00
Rectangle src = {0, 0, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE};
Rectangle dst = {x*WORLD_BLOCK_SIZE, y*WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE};
DrawTexturePro(blk, src, dst, (Vector2){0.0f,0.0f}, 0.0f, WHITE);
#endif
2021-07-18 18:30:27 +00:00
}
}
EndTextureMode();
blocks__chunk_tbl_set(&baked_chunks, id, canvas);
2021-07-18 18:30:27 +00:00
}
void *blocks_get_chunk_tex(uint64_t id) {
return blocks__chunk_tbl_get(&baked_chunks, id);
}
void blocks_remove_chunk_tex(uint64_t id) {
UnloadRenderTexture(*blocks__chunk_tbl_get(&baked_chunks, id));
2021-07-18 18:30:27 +00:00
blocks__chunk_tbl_remove(&baked_chunks, id);
2021-05-12 16:28:39 +00:00
}