From 4774e83133b39ae536935bd6ae9f5a711563693f Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Wed, 6 Sep 2017 00:55:14 +0500 Subject: [PATCH] Implemented writing scene to file --- README.md | 5 + src/game/main.c | 5 +- src/game/platform.c | 4 +- src/libsymmetry/entity.c | 146 +++++++++++++++++----------- src/libsymmetry/entity.h | 3 + src/libsymmetry/game.c | 205 ++++++++++++++++++++------------------- src/libsymmetry/scene.c | 54 ++++++++++- 7 files changed, 265 insertions(+), 157 deletions(-) diff --git a/README.md b/README.md index 75c7128..0ad02e4 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,10 @@ - ## TODO + - Add camera fbo params into camera struct so they can be saved and loaded into file + - Finish loading scene from file + - Fix space not being added after some entities are written to file + - Update makefiles to be able to compile the code in it's current state - Find a solution for the asset import/export situation by either updating the blender exporter or adding assimp as dependancy - Fix bugs with sound sources not updating - Add creating distributable build and uploading to itch.io account support to GENie under windows and linux. @@ -308,3 +312,4 @@ * Added file copy and delete to platform api * Made dll reloading workaround compatilble on linux * Default keybindings as fallback + * Implemented writing scene to file diff --git a/src/game/main.c b/src/game/main.c index a4a8799..ff87a96 100644 --- a/src/game/main.c +++ b/src/game/main.c @@ -15,6 +15,9 @@ static bool reload_game = false; #if defined(__MSC_VER) static const char* lib_name = "libSymmetry.dll"; static const char* lib_copy_name = "libSymmetry.copy.dll"; +#elif defined(__MINGW32__) || defined(__MINGW64__) +static const char* lib_name = "libSymmetry.dll"; +static const char* lib_copy_name = "libSymmetry.copy.dll"; #endif void* game_lib_handle = NULL; @@ -229,7 +232,7 @@ void game_lib_reload(void) bool game_lib_load(void) { -#if defined(__MSC_VER) +#if defined(__MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__) if(!io_file_copy(DIRT_EXECUTABLE, lib_name, lib_copy_name)) { log_error("main:game_lib_load", "Failed to copy dll"); diff --git a/src/game/platform.c b/src/game/platform.c index 61fa319..3cceced 100644 --- a/src/game/platform.c +++ b/src/game/platform.c @@ -461,8 +461,10 @@ void* platform_load_library(const char *name) memset(lib_name, '\0', MAX_LIB_NAME_LEN); #ifdef __linux__ snprintf(lib_name, MAX_LIB_NAME_LEN, "./lib%s.so", name); -#else +#elif defined(_MSC_VER) snprintf(lib_name, MAX_LIB_NAME_LEN, "%s.dll", name); +#elif defined(__MING32__) || defined(__MINGW64__) + snprintf(lib_name, MAX_LIB_NAME_LEN, "./%s.dll", name); #endif void* lib_handle = SDL_LoadObject(lib_name); if(!lib_handle) log_error("platform:load_library", "Failed to load library '%s', SDL : (%s)", lib_name, SDL_GetError()); diff --git a/src/libsymmetry/entity.c b/src/libsymmetry/entity.c index 1510c4e..4eb6fe0 100644 --- a/src/libsymmetry/entity.c +++ b/src/libsymmetry/entity.c @@ -15,6 +15,7 @@ #include #include #include +#include #define MAX_ENTITY_PROP_NAME_LEN 128 #define MAX_ENTITY_PROP_LEN 256 @@ -195,35 +196,33 @@ struct Entity* entity_get_parent(int node) return parent; } -bool entity_save(struct Entity* entity, const char* filename, int directory_type) +bool entity_write(struct Entity* entity, FILE* file) { - bool success = false; - FILE* entity_file = platform->file.open(directory_type, filename, "w"); - if(!entity_file) + if(!file) { - log_error("entity:save", "Failed to open entity file %s for writing"); - return success; + log_error("entity:write", "Invalid file handle"); + return false; } /* First write all properties common to all entity types */ - fprintf(entity_file, "name: %s\n", entity->name); - fprintf(entity_file, "type: %d\n", entity->type); - fprintf(entity_file, "is_listener: %s\n", entity->is_listener ? "true" : "false"); - fprintf(entity_file, "renderable: %s\n", entity->renderable ? "true" : "false"); + fprintf(file, "name: %s\n", entity->name); + fprintf(file, "type: %d\n", entity->type); + fprintf(file, "is_listener: %s\n", entity->is_listener ? "true" : "false"); + fprintf(file, "renderable: %s\n", entity->renderable ? "true" : "false"); struct Entity* parent = entity_get_parent(entity->id); - fprintf(entity_file, "parent: %s\n", parent->name); + fprintf(file, "parent: %s\n", parent ? parent->name : "NONE"); /* Transform */ - fprintf(entity_file, "position: %.5f %.5f %.5f\n", + fprintf(file, "position: %.5f %.5f %.5f\n", entity->transform.position.x, entity->transform.position.y, entity->transform.position.z); - fprintf(entity_file, "scale: %.5f %.5f %.5f\n", + fprintf(file, "scale: %.5f %.5f %.5f\n", entity->transform.scale.x, entity->transform.scale.y, entity->transform.scale.z); - fprintf(entity_file, "rotation: %.5f %.5f %.5f %.5f\n", + fprintf(file, "rotation: %.5f %.5f %.5f %.5f\n", entity->transform.rotation.x, entity->transform.rotation.y, entity->transform.rotation.z, @@ -233,12 +232,12 @@ bool entity_save(struct Entity* entity, const char* filename, int directory_type { case ET_CAMERA: { - fprintf(entity_file, "ortho: %s\n", entity->camera.ortho ? "true" : "false"); - fprintf(entity_file, "resizeable: %s\n", entity->camera.resizeable ? "true" : "false"); - fprintf(entity_file, "fov: %.5f\n", entity->camera.fov); - fprintf(entity_file, "nearz: %.5f\n", entity->camera.nearz); - fprintf(entity_file, "farz: %.5f\n", entity->camera.farz); - fprintf(entity_file, "render_texture: %s\n", entity->camera.render_tex == -1 ? "false" : "true"); + fprintf(file, "ortho: %s\n", entity->camera.ortho ? "true" : "false"); + fprintf(file, "resizeable: %s\n", entity->camera.resizeable ? "true" : "false"); + fprintf(file, "fov: %.5f\n", entity->camera.fov); + fprintf(file, "nearz: %.5f\n", entity->camera.nearz); + fprintf(file, "farz: %.5f\n", entity->camera.farz); + fprintf(file, "render_texture: %s\n", entity->camera.render_tex == -1 ? "false" : "true"); break; } case ET_STATIC_MESH: @@ -246,23 +245,23 @@ bool entity_save(struct Entity* entity, const char* filename, int directory_type /* TODO: Change this after adding proper support for exported models from blender */ struct Material* material = material_get(entity->model.material); struct Geometry* geom = geom_get(entity->model.geometry_index); - fprintf(entity_file, "material: %s\n", material->name); - fprintf(entity_file, "geometry: %s\n", geom->filename); + fprintf(file, "material: %s\n", material->name); + fprintf(file, "geometry: %s\n", geom->filename); break; } case ET_LIGHT: { - fprintf(entity_file, "light_type: %d\n", entity->light.valid); - fprintf(entity_file, "outer_angle: %.5f\n", entity->light.outer_angle); - fprintf(entity_file, "inner_angle: %.5f\n", entity->light.inner_angle); - fprintf(entity_file, "falloff: %.5f\n", entity->light.falloff); - fprintf(entity_file, "radius: %d\n", entity->light.radius); - fprintf(entity_file, "intensity: %.5f\n", entity->light.intensity); - fprintf(entity_file, "depth_bias: %.5f\n", entity->light.depth_bias); - fprintf(entity_file, "valid: %s\n", entity->light.valid ? "true" : "false"); - fprintf(entity_file, "cast_shadow: %s\n", entity->light.cast_shadow ? "true" : "false"); - fprintf(entity_file, "pcf_enabled: %s\n", entity->light.pcf_enabled ? "true" : "false"); - fprintf(entity_file, "color: %.5f %.5f %.5f", + fprintf(file, "light_type: %d\n", entity->light.valid); + fprintf(file, "outer_angle: %.5f\n", entity->light.outer_angle); + fprintf(file, "inner_angle: %.5f\n", entity->light.inner_angle); + fprintf(file, "falloff: %.5f\n", entity->light.falloff); + fprintf(file, "radius: %d\n", entity->light.radius); + fprintf(file, "intensity: %.5f\n", entity->light.intensity); + fprintf(file, "depth_bias: %.5f\n", entity->light.depth_bias); + fprintf(file, "valid: %s\n", entity->light.valid ? "true" : "false"); + fprintf(file, "cast_shadow: %s\n", entity->light.cast_shadow ? "true" : "false"); + fprintf(file, "pcf_enabled: %s\n", entity->light.pcf_enabled ? "true" : "false"); + fprintf(file, "color: %.5f %.5f %.5f", entity->light.color.x, entity->light.color.y, entity->light.color.z); @@ -270,30 +269,44 @@ bool entity_save(struct Entity* entity, const char* filename, int directory_type } case ET_SOUND_SOURCE: { - fprintf(entity_file, "active: %s\n", entity->sound_source.active ? "true" : "false"); - fprintf(entity_file, "relative: %s\n", entity->sound_source.relative ? "true" : "false"); + fprintf(file, "active: %s\n", entity->sound_source.active ? "true" : "false"); + fprintf(file, "relative: %s\n", entity->sound_source.relative ? "true" : "false"); break; } }; - fprintf(entity_file, "\n"); - log_message("Entity %s written to %s", entity->name, filename); - success = true; - fclose(entity_file); - return success; + fprintf(file, "\n"); + return true; } -struct Entity* entity_load(const char* filename, int directory_type) +bool entity_save(struct Entity* entity, const char* filename, int directory_type) { - FILE* entity_file = platform->file.open(directory_type, filename, "r"); + FILE* entity_file = platform->file.open(directory_type, filename, "w"); if(!entity_file) { - log_error("entity:load", "Failed to open entity file %s for writing", filename); - return NULL; + log_error("entity:save", "Failed to open entity file %s for writing"); + return false; + } + + if(entity_write(entity, entity_file)) + log_message("Entity %s saved to %s", entity->name, filename); + else + log_error("entity:save", "Failed to save entity : %s to file : %s", entity->name, filename); + + fclose(entity_file); + return false; +} + +struct Entity* entity_read(FILE* file) +{ + if(!file) + { + log_error("entity:read", "Invalid file handle"); + return false; } struct Entity entity = - { + { .id = -1, .type = ET_NONE, .is_listener = false, @@ -301,7 +314,7 @@ struct Entity* entity_load(const char* filename, int directory_type) .marked_for_deletion = false, .name = "DEFAULT_ENTITY_NAME", .editor_selected = 0 - }; + }; int current_line = 0; char* material_name = NULL; @@ -316,18 +329,18 @@ struct Entity* entity_load(const char* filename, int directory_type) memset(prop_str, '\0', MAX_ENTITY_PROP_NAME_LEN); memset(line_buffer, '\0', MAX_LINE_LEN); - while(fgets(line_buffer, MAX_LINE_LEN -1, entity_file)) + while(fgets(line_buffer, MAX_LINE_LEN -1, file)) { current_line++; memset(prop_str, '\0', MAX_ENTITY_PROP_NAME_LEN); if(line_buffer[0] == '#') continue; - if(strlen(line_buffer) == 0) break; + if(strlen(line_buffer) == 0 || isspace(line_buffer[0])) break; char* value_str = strstr(line_buffer, ":"); if(!value_str) { - log_warning("Malformed value in entity file %s, line %d", filename, current_line); + log_warning("Malformed value in line %d", current_line); continue; } @@ -335,7 +348,7 @@ struct Entity* entity_load(const char* filename, int directory_type) if(sscanf(line_buffer, " %1024[^: ] : %*s", prop_str) != 1) { - log_warning("Unable to read property name in entity file %s, line %d", filename, current_line); + log_warning("Unable to read property name in line %d", current_line); continue; } @@ -414,7 +427,8 @@ struct Entity* entity_load(const char* filename, int directory_type) else if(strncmp("render_texture", prop_str, MAX_ENTITY_PROP_NAME_LEN) == 0) { variant_from_str(&var_value, value_str, VT_BOOL); - variant_copy_out(&entity.camera.fbo, &var_value); + entity.camera.fbo = var_value.val_bool ? 0 : -1; + //variant_copy_out(&entity.camera.fbo, &var_value); } /* Light */ @@ -507,7 +521,10 @@ struct Entity* entity_load(const char* filename, int directory_type) } /* Do the things after assignment */ - struct Entity* parent_entity = entity_find(parent_name); + struct Entity* parent_entity = NULL; + if(strcmp(parent_name, "NONE") == 0) + parent_entity = entity_find(parent_name); + struct Entity* new_entity = entity_create(entity_name, entity.type, parent_entity ? parent_entity->id : -1); free(entity_name); transform_translate(new_entity, &entity.transform.position, TS_PARENT); @@ -519,6 +536,9 @@ struct Entity* entity_load(const char* filename, int directory_type) switch(new_entity->type) { case ET_CAMERA: + new_entity->camera.fbo = -1; /* TODO: FIX by storing fbo params in camera struct and writing them into file when saving */ + new_entity->camera.render_tex = -1; + new_entity->camera.depth_tex = -1; camera_update_view(new_entity); camera_update_proj(new_entity); break; @@ -538,8 +558,26 @@ struct Entity* entity_load(const char* filename, int directory_type) &new_entity->sound_source.buffer_handles[0]); break; }; - - log_message("Entity %s loaded from %s", new_entity->name, filename); + + return new_entity; +} + +struct Entity* entity_load(const char* filename, int directory_type) +{ + FILE* entity_file = platform->file.open(directory_type, filename, "r"); + if(!entity_file) + { + log_error("entity:load", "Failed to open entity file %s for writing", filename); + return NULL; + } + + struct Entity* new_entity = NULL; + new_entity = entity_read(entity_file); + if(new_entity) + log_message("Entity %s loaded from %s", new_entity->name, filename); + else + log_error("entity:load", "Failed to load entity from %s", filename); + fclose(entity_file); return new_entity; } diff --git a/src/libsymmetry/entity.h b/src/libsymmetry/entity.h index 2f896a7..7448463 100644 --- a/src/libsymmetry/entity.h +++ b/src/libsymmetry/entity.h @@ -3,6 +3,7 @@ #include "../common/linmath.h" #include "../common/num_types.h" +#include "../common/common.h" #define MAX_ENTITY_NAME_LEN 128 #define MAX_SOUND_SOURCE_BUFFERS 5 @@ -128,6 +129,8 @@ struct Entity* entity_get_all(void); struct Entity* entity_get_parent(int node); bool entity_save(struct Entity* entity, const char* filename, int directory_type); struct Entity* entity_load(const char* filename, int directory_type); +bool entity_write(struct Entity* entity, FILE* file); +struct Entity* entity_read(FILE* file); const char* entity_type_name_get(struct Entity* entity); diff --git a/src/libsymmetry/game.c b/src/libsymmetry/game.c index bde8b2c..a355ee8 100644 --- a/src/libsymmetry/game.c +++ b/src/libsymmetry/game.c @@ -104,108 +104,117 @@ bool game_init(struct Window* window, struct Platform_Api* platform_api) void scene_setup(void) { - struct Entity* player = scene_add_new("player", ET_CAMERA); - game_state->player_node = player->id; - vec3 viewer_pos = {10, 5, 100}; - transform_set_position(player, &viewer_pos); - int render_width, render_height; - render_width = 1024; - render_height = 768; - camera_create(player, render_width, render_height); - camera_attach_fbo(player, render_width, render_height, 1, 1, 1); - vec4_fill(&player->camera.clear_color, 0.3f, 0.6f, 0.9f, 1.0f); - - vec4 color = {1.f, 1.f, 1.f, 1.f }; - struct Entity* new_ent = scene_add_new("Model_Entity", ET_STATIC_MESH); - vec3 position = {0, 0, -5}; - transform_translate(new_ent, &position, TS_WORLD); - new_ent->renderable = true; - model_create(new_ent, "default.pamesh", "Blinn_Phong"); - model_set_material_param(new_ent, "diffuse_color", &color); - int tex = texture_create_from_file("white.tga", TU_DIFFUSE); - model_set_material_param(new_ent, "diffuse_texture", &tex); - vec3 scale = {1, 1, 1}; - transform_scale(new_ent, &scale); - - /* struct Entity* sound_ent = scene_add_as_child("Sound_ENT", ET_SOUND_SOURCE, new_ent->id); */ - /* struct Sound_Source* sound_source = &sound_ent->sound_source; */ - /* platform->sound.source_create(true, 1, &sound_source->source_handle, &sound_source->buffer_handles[0]); */ - /* platform->sound.source_load_wav(sound_source->source_handle, */ - /* sound_source->buffer_handles[0], */ - /* "BigExplosion.wav"); */ - /* //sound_source_relative_set(source, true); */ - /* platform->sound.source_volume_set(sound_source->source_handle, 1.f); */ - /* platform->sound.source_loop_set(sound_source->source_handle, true); */ - /* platform->sound.source_play(sound_source->source_handle); */ - - int parent_node = new_ent->id; - int num_suz = 10; - srand(time(NULL)); - for(int i = 0; i < num_suz; i++) - { - int x = rand() % num_suz; - int y = rand() % num_suz; - int z = rand() % num_suz; - x++; y++; z++; - struct Entity* suz = scene_add_as_child("Suzanne", ET_STATIC_MESH, parent_node); - //struct Entity* suz = scene_add_new("Suzanne", ET_STATIC_MESH); - suz->renderable = true; - model_create(suz, "default.pamesh", "Blinn_Phong"); - model_set_material_param(suz, "diffuse_color", &color); - float spec_str = 80.f; - model_set_material_param(suz, "specular_strength", &spec_str); - vec3 s_pos = {x, 0, z}; - transform_translate(suz, &s_pos, TS_WORLD); - } + /* struct Entity* player = scene_add_new("player", ET_CAMERA); */ + /* game_state->player_node = player->id; */ + /* vec3 viewer_pos = {10, 5, 100}; */ + /* transform_set_position(player, &viewer_pos); */ + /* int render_width, render_height; */ + /* render_width = 1024; */ + /* render_height = 768; */ + /* camera_create(player, render_width, render_height); */ + /* camera_attach_fbo(player, render_width, render_height, 1, 1, 1); */ + /* vec4_fill(&player->camera.clear_color, 0.3f, 0.6f, 0.9f, 1.0f); */ + + /* vec4 color = {1.f, 1.f, 1.f, 1.f }; */ + /* struct Entity* new_ent = scene_add_new("Model_Entity", ET_STATIC_MESH); */ + /* vec3 position = {0, 0, -5}; */ + /* transform_translate(new_ent, &position, TS_WORLD); */ + /* new_ent->renderable = true; */ + /* model_create(new_ent, "default.pamesh", "Blinn_Phong"); */ + /* model_set_material_param(new_ent, "diffuse_color", &color); */ + /* int tex = texture_create_from_file("white.tga", TU_DIFFUSE); */ + /* model_set_material_param(new_ent, "diffuse_texture", &tex); */ + /* vec3 scale = {1, 1, 1}; */ + /* transform_scale(new_ent, &scale); */ + + /* /\* struct Entity* sound_ent = scene_add_as_child("Sound_ENT", ET_SOUND_SOURCE, new_ent->id); *\/ */ + /* /\* struct Sound_Source* sound_source = &sound_ent->sound_source; *\/ */ + /* /\* platform->sound.source_create(true, 1, &sound_source->source_handle, &sound_source->buffer_handles[0]); *\/ */ + /* /\* platform->sound.source_load_wav(sound_source->source_handle, *\/ */ + /* /\* sound_source->buffer_handles[0], *\/ */ + /* /\* "BigExplosion.wav"); *\/ */ + /* /\* //sound_source_relative_set(source, true); *\/ */ + /* /\* platform->sound.source_volume_set(sound_source->source_handle, 1.f); *\/ */ + /* /\* platform->sound.source_loop_set(sound_source->source_handle, true); *\/ */ + /* /\* platform->sound.source_play(sound_source->source_handle); *\/ */ + + /* int parent_node = new_ent->id; */ + /* int num_suz = 10; */ + /* srand(time(NULL)); */ + /* for(int i = 0; i < num_suz; i++) */ + /* { */ + /* int x = rand() % num_suz; */ + /* int y = rand() % num_suz; */ + /* int z = rand() % num_suz; */ + /* x++; y++; z++; */ + /* struct Entity* suz = scene_add_as_child("Suzanne", ET_STATIC_MESH, parent_node); */ + /* //struct Entity* suz = scene_add_new("Suzanne", ET_STATIC_MESH); */ + /* suz->renderable = true; */ + /* model_create(suz, "default.pamesh", "Blinn_Phong"); */ + /* model_set_material_param(suz, "diffuse_color", &color); */ + /* float spec_str = 80.f; */ + /* model_set_material_param(suz, "specular_strength", &spec_str); */ + /* vec3 s_pos = {x, 0, z}; */ + /* transform_translate(suz, &s_pos, TS_WORLD); */ + /* } */ - struct Entity* ground = scene_add_new("Ground", ET_STATIC_MESH); - ground->renderable = true; - model_create(ground, "default.pamesh", "Blinn_Phong"); - model_set_material_param(ground, "diffuse_color", &color); - int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE); - model_set_material_param(ground, "diffuse_texture", &white_tex); - float spec_str = 80.f; - model_set_material_param(ground, "specular_strength", &spec_str); - vec3 pos = {0, -5, 0}; - vec3 scale_ground = {400.f, 2.f, 400.f}; - transform_set_position(ground, &pos); - transform_scale(ground, &scale_ground); - - /* struct Entity* screen = scene_add_new("Screen", NULL); */ - /* struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); */ - /* screen_model->geometry_index = geom_find("Quad"); */ - /* struct Entity* screen_camera = scene_add_as_child("Screen_Camera", NULL, screen->node); */ - /* struct Transform* screen_camera_tran = entity_component_get(screen_camera, C_TRANSFORM); */ - /* transform_rotate(screen_camera_tran, &UNIT_Y, 180.f, TS_WORLD); */ - /* struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 50, 50); */ - /* cam->nearz = 0.1f; */ - /* cam->farz = 50.f; */ - /* camera_update_proj(cam); */ - /* camera_attach_fbo(cam, 128, 128, 1, 1, 0); */ - /* model_set_material_param(screen_model, "diffuse_color", &color); */ - /* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */ - - const int MAX_LIGHTS = 3; - for(int i = 0; i < MAX_LIGHTS; i++) + /* struct Entity* ground = scene_add_new("Ground", ET_STATIC_MESH); */ + /* ground->renderable = true; */ + /* model_create(ground, "default.pamesh", "Blinn_Phong"); */ + /* model_set_material_param(ground, "diffuse_color", &color); */ + /* int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE); */ + /* model_set_material_param(ground, "diffuse_texture", &white_tex); */ + /* float spec_str = 80.f; */ + /* model_set_material_param(ground, "specular_strength", &spec_str); */ + /* vec3 pos = {0, -5, 0}; */ + /* vec3 scale_ground = {400.f, 2.f, 400.f}; */ + /* transform_set_position(ground, &pos); */ + /* transform_scale(ground, &scale_ground); */ + + /* /\* struct Entity* screen = scene_add_new("Screen", NULL); *\/ */ + /* /\* struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); *\/ */ + /* /\* screen_model->geometry_index = geom_find("Quad"); *\/ */ + /* /\* struct Entity* screen_camera = scene_add_as_child("Screen_Camera", NULL, screen->node); *\/ */ + /* /\* struct Transform* screen_camera_tran = entity_component_get(screen_camera, C_TRANSFORM); *\/ */ + /* /\* transform_rotate(screen_camera_tran, &UNIT_Y, 180.f, TS_WORLD); *\/ */ + /* /\* struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 50, 50); *\/ */ + /* /\* cam->nearz = 0.1f; *\/ */ + /* /\* cam->farz = 50.f; *\/ */ + /* /\* camera_update_proj(cam); *\/ */ + /* /\* camera_attach_fbo(cam, 128, 128, 1, 1, 0); *\/ */ + /* /\* model_set_material_param(screen_model, "diffuse_color", &color); *\/ */ + /* /\* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); *\/ */ + + /* const int MAX_LIGHTS = 3; */ + /* for(int i = 0; i < MAX_LIGHTS; i++) */ + /* { */ + /* int x = rand() % MAX_LIGHTS; */ + /* int z = rand() % MAX_LIGHTS; */ + /* x++; z++; */ + /* struct Entity* light_ent = scene_add_new("Light_Ent", ET_LIGHT); */ + /* vec3 lt_pos = {x * 20, 0, z * 20}; */ + /* transform_set_position(light_ent, <_pos); */ + /* light_create(light_ent, LT_POINT); */ + /* vec3_fill(&light_ent->light.color, 1.f / (float)x, 1.f / ((rand() % 10) + 1.f), 1.f / (float)z); */ + /* light_ent->light.intensity = 1.f; */ + /* } */ + + /* /\* log_message("Sizeof Entity : %d", sizeof(struct Entity)); *\/ */ + + /* /\* struct Entity* light_ent = entity_find("Ground"); *\/ */ + /* /\* entity_save(light_ent, "ground.ent", DT_INSTALL); *\/ */ + + /* scene_save("test.scene", DIRT_INSTALL); */ + + /* //struct Entity* light = entity_load("light.ent", DT_INSTALL); */ + + if(scene_load("test.scene", DIRT_INSTALL)) { - int x = rand() % MAX_LIGHTS; - int z = rand() % MAX_LIGHTS; - x++; z++; - struct Entity* light_ent = scene_add_new("Light_Ent", ET_LIGHT); - vec3 lt_pos = {x * 20, 0, z * 20}; - transform_set_position(light_ent, <_pos); - light_create(light_ent, LT_POINT); - vec3_fill(&light_ent->light.color, 1.f / (float)x, 1.f / ((rand() % 10) + 1.f), 1.f / (float)z); - light_ent->light.intensity = 1.f; + log_message("Scene loaded!"); + struct Entity* player = entity_find("player"); + game_state->player_node = player->id; } - - /* log_message("Sizeof Entity : %d", sizeof(struct Entity)); */ - - /* struct Entity* light_ent = entity_find("Ground"); */ - /* entity_save(light_ent, "ground.ent", DT_INSTALL); */ - - //struct Entity* light = entity_load("light.ent", DT_INSTALL); } void debug(float dt) diff --git a/src/libsymmetry/scene.c b/src/libsymmetry/scene.c index caad57b..4828850 100644 --- a/src/libsymmetry/scene.c +++ b/src/libsymmetry/scene.c @@ -124,7 +124,6 @@ struct Entity* scene_get_parent(struct Entity* entity) bool scene_load(const char* filename, int directory_type) { - bool success = false; FILE* entity_file = platform->file.open(directory_type, filename, "r"); if(!entity_file) { @@ -132,13 +131,62 @@ bool scene_load(const char* filename, int directory_type) return false; } - struct Entity* root = entity_get(root_node); + int count = 0; + while(!feof(entity_file)) + { + struct Entity* new_entity = NULL; + new_entity = entity_read(entity_file); + if(!new_entity) + log_error("scene:load", "Error reading entity"); + else + { + log_message("Loaded %s", new_entity->name); + count++; + } + int c = fgetc(entity_file); + } - return success; + log_message("%d entites loaded from %s", count, filename); + fclose(entity_file); + return true; } bool scene_save(const char* filename, int directory_type) { bool success = false; + FILE* scene_file = platform->file.open(directory_type, filename, "w"); + if(!scene_file) + { + log_error("scene:save", "Failed to create scenefile %s for writing", filename); + return false; + } + + int* entities_to_write = array_new(int); + array_push(entities_to_write, root_node, int); + + bool done = false; + int count = 0; + while(!done) + { + struct Entity* entity = entity_get(entities_to_write[0]); + if(!entity_write(entity, scene_file)) + { + log_error("scene:save", "Failed to write '%s' to file", entity->name); + continue; + } + + log_message("Entity '%s' written to file", entity->name); + count++; + for(int i = 0; i < array_len(entity->transform.children); i++) + array_push(entities_to_write, entity->transform.children[i], int); + + array_remove_at(entities_to_write, 0); + if(array_len(entities_to_write) == 0) done = true; + } + + log_message("%d entities written to %s", count, filename); + array_free(entities_to_write); + fclose(scene_file); + return success; }