Implemented writing scene to file

dev
Shariq Shah 8 years ago
parent 62639d9689
commit 4774e83133
  1. 5
      README.md
  2. 5
      src/game/main.c
  3. 4
      src/game/platform.c
  4. 138
      src/libsymmetry/entity.c
  5. 3
      src/libsymmetry/entity.h
  6. 209
      src/libsymmetry/game.c
  7. 54
      src/libsymmetry/scene.c

@ -149,6 +149,10 @@
- ## TODO - ## 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 - 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 - Fix bugs with sound sources not updating
- Add creating distributable build and uploading to itch.io account support to GENie under windows and linux. - 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 * Added file copy and delete to platform api
* Made dll reloading workaround compatilble on linux * Made dll reloading workaround compatilble on linux
* Default keybindings as fallback * Default keybindings as fallback
* Implemented writing scene to file

@ -15,6 +15,9 @@ static bool reload_game = false;
#if defined(__MSC_VER) #if defined(__MSC_VER)
static const char* lib_name = "libSymmetry.dll"; static const char* lib_name = "libSymmetry.dll";
static const char* lib_copy_name = "libSymmetry.copy.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 #endif
void* game_lib_handle = NULL; void* game_lib_handle = NULL;
@ -229,7 +232,7 @@ void game_lib_reload(void)
bool game_lib_load(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)) if(!io_file_copy(DIRT_EXECUTABLE, lib_name, lib_copy_name))
{ {
log_error("main:game_lib_load", "Failed to copy dll"); log_error("main:game_lib_load", "Failed to copy dll");

@ -461,8 +461,10 @@ void* platform_load_library(const char *name)
memset(lib_name, '\0', MAX_LIB_NAME_LEN); memset(lib_name, '\0', MAX_LIB_NAME_LEN);
#ifdef __linux__ #ifdef __linux__
snprintf(lib_name, MAX_LIB_NAME_LEN, "./lib%s.so", name); 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); 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 #endif
void* lib_handle = SDL_LoadObject(lib_name); 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()); if(!lib_handle) log_error("platform:load_library", "Failed to load library '%s', SDL : (%s)", lib_name, SDL_GetError());

@ -15,6 +15,7 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <stdarg.h> #include <stdarg.h>
#include <ctype.h>
#define MAX_ENTITY_PROP_NAME_LEN 128 #define MAX_ENTITY_PROP_NAME_LEN 128
#define MAX_ENTITY_PROP_LEN 256 #define MAX_ENTITY_PROP_LEN 256
@ -195,35 +196,33 @@ struct Entity* entity_get_parent(int node)
return parent; 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; if(!file)
FILE* entity_file = platform->file.open(directory_type, filename, "w");
if(!entity_file)
{ {
log_error("entity:save", "Failed to open entity file %s for writing"); log_error("entity:write", "Invalid file handle");
return success; return false;
} }
/* First write all properties common to all entity types */ /* First write all properties common to all entity types */
fprintf(entity_file, "name: %s\n", entity->name); fprintf(file, "name: %s\n", entity->name);
fprintf(entity_file, "type: %d\n", entity->type); fprintf(file, "type: %d\n", entity->type);
fprintf(entity_file, "is_listener: %s\n", entity->is_listener ? "true" : "false"); fprintf(file, "is_listener: %s\n", entity->is_listener ? "true" : "false");
fprintf(entity_file, "renderable: %s\n", entity->renderable ? "true" : "false"); fprintf(file, "renderable: %s\n", entity->renderable ? "true" : "false");
struct Entity* parent = entity_get_parent(entity->id); 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 */ /* Transform */
fprintf(entity_file, "position: %.5f %.5f %.5f\n", fprintf(file, "position: %.5f %.5f %.5f\n",
entity->transform.position.x, entity->transform.position.x,
entity->transform.position.y, entity->transform.position.y,
entity->transform.position.z); 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.x,
entity->transform.scale.y, entity->transform.scale.y,
entity->transform.scale.z); 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.x,
entity->transform.rotation.y, entity->transform.rotation.y,
entity->transform.rotation.z, entity->transform.rotation.z,
@ -233,12 +232,12 @@ bool entity_save(struct Entity* entity, const char* filename, int directory_type
{ {
case ET_CAMERA: case ET_CAMERA:
{ {
fprintf(entity_file, "ortho: %s\n", entity->camera.ortho ? "true" : "false"); fprintf(file, "ortho: %s\n", entity->camera.ortho ? "true" : "false");
fprintf(entity_file, "resizeable: %s\n", entity->camera.resizeable ? "true" : "false"); fprintf(file, "resizeable: %s\n", entity->camera.resizeable ? "true" : "false");
fprintf(entity_file, "fov: %.5f\n", entity->camera.fov); fprintf(file, "fov: %.5f\n", entity->camera.fov);
fprintf(entity_file, "nearz: %.5f\n", entity->camera.nearz); fprintf(file, "nearz: %.5f\n", entity->camera.nearz);
fprintf(entity_file, "farz: %.5f\n", entity->camera.farz); fprintf(file, "farz: %.5f\n", entity->camera.farz);
fprintf(entity_file, "render_texture: %s\n", entity->camera.render_tex == -1 ? "false" : "true"); fprintf(file, "render_texture: %s\n", entity->camera.render_tex == -1 ? "false" : "true");
break; break;
} }
case ET_STATIC_MESH: 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 */ /* TODO: Change this after adding proper support for exported models from blender */
struct Material* material = material_get(entity->model.material); struct Material* material = material_get(entity->model.material);
struct Geometry* geom = geom_get(entity->model.geometry_index); struct Geometry* geom = geom_get(entity->model.geometry_index);
fprintf(entity_file, "material: %s\n", material->name); fprintf(file, "material: %s\n", material->name);
fprintf(entity_file, "geometry: %s\n", geom->filename); fprintf(file, "geometry: %s\n", geom->filename);
break; break;
} }
case ET_LIGHT: case ET_LIGHT:
{ {
fprintf(entity_file, "light_type: %d\n", entity->light.valid); fprintf(file, "light_type: %d\n", entity->light.valid);
fprintf(entity_file, "outer_angle: %.5f\n", entity->light.outer_angle); fprintf(file, "outer_angle: %.5f\n", entity->light.outer_angle);
fprintf(entity_file, "inner_angle: %.5f\n", entity->light.inner_angle); fprintf(file, "inner_angle: %.5f\n", entity->light.inner_angle);
fprintf(entity_file, "falloff: %.5f\n", entity->light.falloff); fprintf(file, "falloff: %.5f\n", entity->light.falloff);
fprintf(entity_file, "radius: %d\n", entity->light.radius); fprintf(file, "radius: %d\n", entity->light.radius);
fprintf(entity_file, "intensity: %.5f\n", entity->light.intensity); fprintf(file, "intensity: %.5f\n", entity->light.intensity);
fprintf(entity_file, "depth_bias: %.5f\n", entity->light.depth_bias); fprintf(file, "depth_bias: %.5f\n", entity->light.depth_bias);
fprintf(entity_file, "valid: %s\n", entity->light.valid ? "true" : "false"); fprintf(file, "valid: %s\n", entity->light.valid ? "true" : "false");
fprintf(entity_file, "cast_shadow: %s\n", entity->light.cast_shadow ? "true" : "false"); fprintf(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(file, "pcf_enabled: %s\n", entity->light.pcf_enabled ? "true" : "false");
fprintf(entity_file, "color: %.5f %.5f %.5f", fprintf(file, "color: %.5f %.5f %.5f",
entity->light.color.x, entity->light.color.x,
entity->light.color.y, entity->light.color.y,
entity->light.color.z); entity->light.color.z);
@ -270,26 +269,40 @@ bool entity_save(struct Entity* entity, const char* filename, int directory_type
} }
case ET_SOUND_SOURCE: case ET_SOUND_SOURCE:
{ {
fprintf(entity_file, "active: %s\n", entity->sound_source.active ? "true" : "false"); fprintf(file, "active: %s\n", entity->sound_source.active ? "true" : "false");
fprintf(entity_file, "relative: %s\n", entity->sound_source.relative ? "true" : "false"); fprintf(file, "relative: %s\n", entity->sound_source.relative ? "true" : "false");
break; break;
} }
}; };
fprintf(entity_file, "\n"); fprintf(file, "\n");
log_message("Entity %s written to %s", entity->name, filename); return true;
success = true;
fclose(entity_file);
return success;
} }
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) if(!entity_file)
{ {
log_error("entity:load", "Failed to open entity file %s for writing", filename); log_error("entity:save", "Failed to open entity file %s for writing");
return NULL; 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 = struct Entity entity =
@ -316,18 +329,18 @@ struct Entity* entity_load(const char* filename, int directory_type)
memset(prop_str, '\0', MAX_ENTITY_PROP_NAME_LEN); memset(prop_str, '\0', MAX_ENTITY_PROP_NAME_LEN);
memset(line_buffer, '\0', MAX_LINE_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++; current_line++;
memset(prop_str, '\0', MAX_ENTITY_PROP_NAME_LEN); memset(prop_str, '\0', MAX_ENTITY_PROP_NAME_LEN);
if(line_buffer[0] == '#') continue; 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, ":"); char* value_str = strstr(line_buffer, ":");
if(!value_str) 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; continue;
} }
@ -335,7 +348,7 @@ struct Entity* entity_load(const char* filename, int directory_type)
if(sscanf(line_buffer, " %1024[^: ] : %*s", prop_str) != 1) 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; 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) else if(strncmp("render_texture", prop_str, MAX_ENTITY_PROP_NAME_LEN) == 0)
{ {
variant_from_str(&var_value, value_str, VT_BOOL); 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 */ /* Light */
@ -507,7 +521,10 @@ struct Entity* entity_load(const char* filename, int directory_type)
} }
/* Do the things after assignment */ /* 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); struct Entity* new_entity = entity_create(entity_name, entity.type, parent_entity ? parent_entity->id : -1);
free(entity_name); free(entity_name);
transform_translate(new_entity, &entity.transform.position, TS_PARENT); 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) switch(new_entity->type)
{ {
case ET_CAMERA: 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_view(new_entity);
camera_update_proj(new_entity); camera_update_proj(new_entity);
break; break;
@ -539,7 +559,25 @@ struct Entity* entity_load(const char* filename, int directory_type)
break; break;
}; };
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); 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); fclose(entity_file);
return new_entity; return new_entity;
} }

@ -3,6 +3,7 @@
#include "../common/linmath.h" #include "../common/linmath.h"
#include "../common/num_types.h" #include "../common/num_types.h"
#include "../common/common.h"
#define MAX_ENTITY_NAME_LEN 128 #define MAX_ENTITY_NAME_LEN 128
#define MAX_SOUND_SOURCE_BUFFERS 5 #define MAX_SOUND_SOURCE_BUFFERS 5
@ -128,6 +129,8 @@ struct Entity* entity_get_all(void);
struct Entity* entity_get_parent(int node); struct Entity* entity_get_parent(int node);
bool entity_save(struct Entity* entity, const char* filename, int directory_type); bool entity_save(struct Entity* entity, const char* filename, int directory_type);
struct Entity* entity_load(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); const char* entity_type_name_get(struct Entity* entity);

@ -104,108 +104,117 @@ bool game_init(struct Window* window, struct Platform_Api* platform_api)
void scene_setup(void) void scene_setup(void)
{ {
struct Entity* player = scene_add_new("player", ET_CAMERA); /* struct Entity* player = scene_add_new("player", ET_CAMERA); */
game_state->player_node = player->id; /* game_state->player_node = player->id; */
vec3 viewer_pos = {10, 5, 100}; /* vec3 viewer_pos = {10, 5, 100}; */
transform_set_position(player, &viewer_pos); /* transform_set_position(player, &viewer_pos); */
int render_width, render_height; /* int render_width, render_height; */
render_width = 1024; /* render_width = 1024; */
render_height = 768; /* render_height = 768; */
camera_create(player, render_width, render_height); /* camera_create(player, render_width, render_height); */
camera_attach_fbo(player, render_width, render_height, 1, 1, 1); /* 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_fill(&player->camera.clear_color, 0.3f, 0.6f, 0.9f, 1.0f); */
vec4 color = {1.f, 1.f, 1.f, 1.f }; /* vec4 color = {1.f, 1.f, 1.f, 1.f }; */
struct Entity* new_ent = scene_add_new("Model_Entity", ET_STATIC_MESH); /* struct Entity* new_ent = scene_add_new("Model_Entity", ET_STATIC_MESH); */
vec3 position = {0, 0, -5}; /* vec3 position = {0, 0, -5}; */
transform_translate(new_ent, &position, TS_WORLD); /* transform_translate(new_ent, &position, TS_WORLD); */
new_ent->renderable = true; /* new_ent->renderable = true; */
model_create(new_ent, "default.pamesh", "Blinn_Phong"); /* model_create(new_ent, "default.pamesh", "Blinn_Phong"); */
model_set_material_param(new_ent, "diffuse_color", &color); /* model_set_material_param(new_ent, "diffuse_color", &color); */
int tex = texture_create_from_file("white.tga", TU_DIFFUSE); /* int tex = texture_create_from_file("white.tga", TU_DIFFUSE); */
model_set_material_param(new_ent, "diffuse_texture", &tex); /* model_set_material_param(new_ent, "diffuse_texture", &tex); */
vec3 scale = {1, 1, 1}; /* vec3 scale = {1, 1, 1}; */
transform_scale(new_ent, &scale); /* transform_scale(new_ent, &scale); */
/* struct Entity* sound_ent = scene_add_as_child("Sound_ENT", ET_SOUND_SOURCE, new_ent->id); */ /* /\* 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; */ /* /\* 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_create(true, 1, &sound_source->source_handle, &sound_source->buffer_handles[0]); *\/ */
/* platform->sound.source_load_wav(sound_source->source_handle, */ /* /\* platform->sound.source_load_wav(sound_source->source_handle, *\/ */
/* sound_source->buffer_handles[0], */ /* /\* sound_source->buffer_handles[0], *\/ */
/* "BigExplosion.wav"); */ /* /\* "BigExplosion.wav"); *\/ */
/* //sound_source_relative_set(source, true); */ /* /\* //sound_source_relative_set(source, true); *\/ */
/* platform->sound.source_volume_set(sound_source->source_handle, 1.f); */ /* /\* platform->sound.source_volume_set(sound_source->source_handle, 1.f); *\/ */
/* platform->sound.source_loop_set(sound_source->source_handle, true); */ /* /\* platform->sound.source_loop_set(sound_source->source_handle, true); *\/ */
/* platform->sound.source_play(sound_source->source_handle); */ /* /\* platform->sound.source_play(sound_source->source_handle); *\/ */
int parent_node = new_ent->id; /* int parent_node = new_ent->id; */
int num_suz = 10; /* int num_suz = 10; */
srand(time(NULL)); /* srand(time(NULL)); */
for(int i = 0; i < num_suz; i++) /* for(int i = 0; i < num_suz; i++) */
{ /* { */
int x = rand() % num_suz; /* int x = rand() % num_suz; */
int y = rand() % num_suz; /* int y = rand() % num_suz; */
int z = rand() % num_suz; /* int z = rand() % num_suz; */
x++; y++; z++; /* x++; y++; z++; */
struct Entity* suz = scene_add_as_child("Suzanne", ET_STATIC_MESH, parent_node); /* struct Entity* suz = scene_add_as_child("Suzanne", ET_STATIC_MESH, parent_node); */
//struct Entity* suz = scene_add_new("Suzanne", ET_STATIC_MESH); /* //struct Entity* suz = scene_add_new("Suzanne", ET_STATIC_MESH); */
suz->renderable = true; /* suz->renderable = true; */
model_create(suz, "default.pamesh", "Blinn_Phong"); /* model_create(suz, "default.pamesh", "Blinn_Phong"); */
model_set_material_param(suz, "diffuse_color", &color); /* model_set_material_param(suz, "diffuse_color", &color); */
float spec_str = 80.f; /* float spec_str = 80.f; */
model_set_material_param(suz, "specular_strength", &spec_str); /* model_set_material_param(suz, "specular_strength", &spec_str); */
vec3 s_pos = {x, 0, z}; /* vec3 s_pos = {x, 0, z}; */
transform_translate(suz, &s_pos, TS_WORLD); /* transform_translate(suz, &s_pos, TS_WORLD); */
} /* } */
struct Entity* ground = scene_add_new("Ground", ET_STATIC_MESH); /* struct Entity* ground = scene_add_new("Ground", ET_STATIC_MESH); */
ground->renderable = true; /* ground->renderable = true; */
model_create(ground, "default.pamesh", "Blinn_Phong"); /* model_create(ground, "default.pamesh", "Blinn_Phong"); */
model_set_material_param(ground, "diffuse_color", &color); /* model_set_material_param(ground, "diffuse_color", &color); */
int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE); /* int white_tex = texture_create_from_file("white.tga", TU_DIFFUSE); */
model_set_material_param(ground, "diffuse_texture", &white_tex); /* model_set_material_param(ground, "diffuse_texture", &white_tex); */
float spec_str = 80.f; /* float spec_str = 80.f; */
model_set_material_param(ground, "specular_strength", &spec_str); /* model_set_material_param(ground, "specular_strength", &spec_str); */
vec3 pos = {0, -5, 0}; /* vec3 pos = {0, -5, 0}; */
vec3 scale_ground = {400.f, 2.f, 400.f}; /* vec3 scale_ground = {400.f, 2.f, 400.f}; */
transform_set_position(ground, &pos); /* transform_set_position(ground, &pos); */
transform_scale(ground, &scale_ground); /* transform_scale(ground, &scale_ground); */
/* struct Entity* screen = scene_add_new("Screen", NULL); */ /* /\* struct Entity* screen = scene_add_new("Screen", NULL); *\/ */
/* struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); */ /* /\* struct Model* screen_model = entity_component_add(screen, C_MODEL, NULL, NULL); *\/ */
/* screen_model->geometry_index = geom_find("Quad"); */ /* /\* screen_model->geometry_index = geom_find("Quad"); *\/ */
/* struct Entity* screen_camera = scene_add_as_child("Screen_Camera", NULL, screen->node); */ /* /\* 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); */ /* /\* struct Transform* screen_camera_tran = entity_component_get(screen_camera, C_TRANSFORM); *\/ */
/* transform_rotate(screen_camera_tran, &UNIT_Y, 180.f, TS_WORLD); */ /* /\* transform_rotate(screen_camera_tran, &UNIT_Y, 180.f, TS_WORLD); *\/ */
/* struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 50, 50); */ /* /\* struct Camera* cam = entity_component_add(screen_camera, C_CAMERA, 50, 50); *\/ */
/* cam->nearz = 0.1f; */ /* /\* cam->nearz = 0.1f; *\/ */
/* cam->farz = 50.f; */ /* /\* cam->farz = 50.f; *\/ */
/* camera_update_proj(cam); */ /* /\* camera_update_proj(cam); *\/ */
/* camera_attach_fbo(cam, 128, 128, 1, 1, 0); */ /* /\* 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_color", &color); *\/ */
/* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); */ /* /\* model_set_material_param(screen_model, "diffuse_texture", &cam->render_tex); *\/ */
const int MAX_LIGHTS = 3; /* const int MAX_LIGHTS = 3; */
for(int i = 0; i < MAX_LIGHTS; i++) /* 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, &lt_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; log_message("Scene loaded!");
int z = rand() % MAX_LIGHTS; struct Entity* player = entity_find("player");
x++; z++; game_state->player_node = player->id;
struct Entity* light_ent = scene_add_new("Light_Ent", ET_LIGHT);
vec3 lt_pos = {x * 20, 0, z * 20};
transform_set_position(light_ent, &lt_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); */
//struct Entity* light = entity_load("light.ent", DT_INSTALL);
} }
void debug(float dt) void debug(float dt)

@ -124,7 +124,6 @@ struct Entity* scene_get_parent(struct Entity* entity)
bool scene_load(const char* filename, int directory_type) bool scene_load(const char* filename, int directory_type)
{ {
bool success = false;
FILE* entity_file = platform->file.open(directory_type, filename, "r"); FILE* entity_file = platform->file.open(directory_type, filename, "r");
if(!entity_file) if(!entity_file)
{ {
@ -132,13 +131,62 @@ bool scene_load(const char* filename, int directory_type)
return false; 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 scene_save(const char* filename, int directory_type)
{ {
bool success = false; 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; return success;
} }

Loading…
Cancel
Save