Added block registry

This commit is contained in:
Eric-Paul Ickhorn 2023-10-10 21:23:07 +02:00
parent 6227c571aa
commit fec3d35fe9
4 changed files with 100 additions and 7 deletions

50
code/source-c/blocks.c Normal file
View File

@ -0,0 +1,50 @@
#include "blocks.h"
#include "state.h"
#include <stdlib.h>
tc_block_entry_s tc_new_block_entry(char *name)
{
tc_block_entry_s entry;
entry.name = name;
entry.attributes_capacity = 16;
entry.num_attributes = 0;
entry.attributes = calloc(sizeof(tc_block_attribute_s), entry.attributes_capacity);
return entry;
}
void tc_add_attribute_to_block(tc_block_entry_s *entry, tc_block_attribute_s attribute)
{
if(entry->num_attributes >= entry->attributes_capacity)
{
entry->attributes_capacity *= 2;
entry->attributes =
realloc(entry->attributes, sizeof(tc_block_attribute_s) * entry->attributes_capacity);
}
entry->attributes[entry->num_attributes] = attribute;
}
tc_block_entry_s * tc_get_block(char *name)
{
uint32_t index = 0;
while(index < tc_game_state_g.block_registry.num_blocks)
{
if(!strcmp(tc_game_state_g.block_registry.blocks[index].name, name))
return &tc_game_state_g.block_registry.blocks[index];
++index;
}
return NULL;
}
tc_block_registry_s tc_init_blocks()
{
tc_block_registry_s registry;
registry.blocks_capacity = 256;
registry.blocks = calloc(sizeof(tc_block_entry_s), registry.blocks_capacity);
return registry;
}

46
code/source-c/blocks.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef TC_BLOCK_H
#define TC_BLOCK_H
#include <stdbool.h>
#include <stdint.h>
#include <glad/glad.h>
typedef struct tc_block_attribute
{
uint32_t len_name;
uint32_t len_content;
char *name;
void *content;
} tc_block_attribute_s;
typedef struct tc_block_entry
{
char *name;
uint32_t attributes_capacity;
uint32_t num_attributes;
tc_block_attribute_s *attributes;
} tc_block_entry_s;
typedef struct tc_block_registry
{
uint32_t blocks_capacity;
uint32_t num_blocks;
tc_block_entry_s *blocks;
} tc_block_registry_s;
tc_block_entry_s tc_new_block_entry (char *name);
void tc_add_attribute_to_block
(tc_block_entry_s *entry, tc_block_attribute_s attribute);
tc_block_entry_s * tc_get_block (char *name);
tc_block_registry_s tc_init_blocks ();
#endif

View File

@ -258,15 +258,10 @@ bool update()
return true;
}
void flushput(const char *message)
{
puts(message);
fflush(stdout);
}
int main(int argc, char **argv)
{
tc_game_state_g = tc_init();
tc_game_state_g = tc_init();
tc_game_state_g.block_registry = tc_init_blocks();
block = tc_new_block_at_3f(0.0, 0.0, 0.0);

View File

@ -7,6 +7,7 @@
#include <SDL2/SDL.h>
#include "shaders.h"
#include "blocks.h"
typedef struct tc_vec3
{
@ -53,6 +54,7 @@ typedef struct tc_renderer
typedef struct
{
tc_renderer_s renderer;
tc_block_registry_s block_registry;
} techneck_s;