#include "blocks.h" #include "state.h" #include tc_block_entry_s * tc_new_block_entry(char *name) { tc_block_entry_s *entry = malloc(sizeof(tc_block_entry_s)); entry->name = name; entry->attributes_capacity = 16; entry->num_attributes = 0; entry->attributes = calloc( sizeof(tc_block_attribute_s), entry->attributes_capacity ); if(tc_game_state_g.block_registry.num_blocks >= tc_game_state_g.block_registry.blocks_capacity) { tc_game_state_g.block_registry.blocks = realloc(tc_game_state_g.block_registry.blocks, sizeof(tc_block_entry_s) * tc_game_state_g.block_registry.blocks_capacity ); } tc_game_state_g.block_registry.blocks[tc_game_state_g.block_registry.num_blocks] = entry; ++tc_game_state_g.block_registry.num_blocks; return entry; } void tc_add_attribute_to_block_entry(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; ++entry->num_attributes; } tc_block_attribute_s * tc_block_resolve_name_to_attribute(tc_block_entry_s *block, char *name) { uint32_t attribute_index = 0; while(attribute_index < block->num_attributes) { if(!strcmp(block->attributes[attribute_index].name, name)) return &block->attributes[attribute_index]; ++attribute_index; } return NULL; } 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_entry_s * tc_resolve_id_to_block_entry(uint32_t id) { uint32_t index = 0; while(index < tc_game_state_g.block_registry.num_blocks) { if(tc_game_state_g.block_registry.blocks[index]->identifier == id) { 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.num_blocks = 0; registry.blocks = calloc(sizeof(tc_block_entry_s), registry.blocks_capacity); return registry; } tc_block_entry_s * tc_new_cube_block(tc_cube_info_s info) { tc_block_entry_s *entry = tc_new_block_entry(info.name); entry->identifier = info.identifier; tc_block_attribute_s block_type_attrib; block_type_attrib.name = "block_type"; block_type_attrib.value.string = "basic_cube"; tc_add_attribute_to_block_entry(entry, block_type_attrib); tc_block_attribute_s top_attrib; top_attrib.name = "top_texture"; top_attrib.value.pointer = tc_resolve_id_to_image(info.texture_top); tc_add_attribute_to_block_entry(entry, top_attrib); tc_block_attribute_s bottom_attrib; bottom_attrib.name = "bottom_texture"; bottom_attrib.value.pointer = tc_resolve_id_to_image(info.texture_bottom); tc_add_attribute_to_block_entry(entry, bottom_attrib); tc_block_attribute_s east_attrib; east_attrib.name = "east_texture"; east_attrib.value.pointer = tc_resolve_id_to_image(info.texture_east); tc_add_attribute_to_block_entry(entry, east_attrib); tc_block_attribute_s west_attrib; west_attrib.name = "west_texture"; west_attrib.value.pointer = tc_resolve_id_to_image(info.texture_west); tc_add_attribute_to_block_entry(entry, west_attrib); tc_block_attribute_s back_attrib; back_attrib.name = "back_texture"; back_attrib.value.pointer = tc_resolve_id_to_image(info.texture_back); tc_add_attribute_to_block_entry(entry, back_attrib); tc_block_attribute_s front_attrib; front_attrib.name = "front_texture"; front_attrib.value.pointer = tc_resolve_id_to_image(info.texture_front); tc_add_attribute_to_block_entry(entry, front_attrib); return entry; } tc_cube_info_s tc_gen_unitexture_cube_info (char *name, uint32_t identifier, uint32_t texture) { tc_cube_info_s cube_info; cube_info.name = name; cube_info.identifier = identifier; cube_info.texture_top = texture; cube_info.texture_bottom = texture; cube_info.texture_east = texture; cube_info.texture_west = texture; cube_info.texture_back = texture; cube_info.texture_front = texture; return cube_info; } void tc_create_blocks() { tc_image_s *dirt_texture = tc_resolve_name_to_image("dirt"); tc_image_s *grass_texture = tc_resolve_name_to_image("grass"); tc_image_s *stone_texture = tc_resolve_name_to_image("stone"); tc_cube_info_s dirt = tc_gen_unitexture_cube_info("Dirt", 1, dirt_texture->image_id); tc_cube_info_s grass = tc_gen_unitexture_cube_info("Grass", 2, grass_texture->image_id); tc_cube_info_s stone = tc_gen_unitexture_cube_info("Stone", 3, stone_texture->image_id); tc_new_cube_block(dirt); tc_new_cube_block(grass); tc_new_cube_block(stone); }