Implemented writing entity to text file

dev
shariq 8 years ago
parent 31c59dd7a9
commit 34ccb51d2e
  1. 17
      orgfile.org
  2. 91
      src/entity.c
  3. 1
      src/entity.h
  4. 6
      src/game.c
  5. 2
      src/renderer.c
  6. 12
      src/scene.c
  7. 5
      src/scene.h

@ -23,6 +23,8 @@ diffuse_color: 1 0 0 1
diffuse_texture: "checkered.tga"
specular: 0.55
- Add to_string functions for major structs like transform, model etc to ease in conversion?
*** Configuration Variables a.k.a cfg-vars
# Comment
render_width: 1024
@ -54,6 +56,20 @@ Quit: Left Ctrl-Q
Sprint: Left Shift
*** Level/Scene
- Binary format with header attached at the top
- Save child entities first
- Copy paste all entites in the file one by one. Since the entites all look
the same in memory and are made up of tagged unions, a simple memcpy approach
should suffice. The problem is entity heirarchies. There are multiple approaches to
solve this problem.
-- Save a sorted list of entites to file i.e. before saving create a new list that does
not have the empty array slots in the entity list and then just copy and paste. This
is the simplest way to solve the problem as we don't have to worry about indexes of
parent/child entites in heirarchy. We can take the whole array and paste it to the
file but creating a copy of entity list for this purpose only would be slow and consume a lot of memory.
-- Instead of creating a copy of the entity list for sorting and saving, sort the actual entity list
and update all references as necessary then save the array to file.
-- Just write the name of the parent entity as parent. Make sure that all entity names are unique.
*** Materials
*** Mesh/Geometry
** Notes on entity Systems
@ -223,6 +239,7 @@ x Font atlas proper cleanup
** DONE Fix Key release not being reported
- State "DONE" from "TODO" [2017-03-26 Sun 01:16]
** TODO Better handling incase assets folder is not found?
** TODO Write entity to/from file
** DONE OpenAL not working in releasebuilds
- State "DONE" from "TODO" [2017-03-25 Sat 02:06]
** DONE 3d sound using OpenAL

@ -7,6 +7,9 @@
#include "light.h"
#include "model.h"
#include "sound.h"
#include "material.h"
#include "geometry.h"
#include "file_io.h"
#include <stdlib.h>
#include <string.h>
@ -155,3 +158,91 @@ struct Entity* entity_get_parent(int node)
if(entity) parent = entity_get(entity->transform.parent);
return parent;
}
bool entity_save(struct Entity* entity, const char* filename, int directory_type)
{
bool success = false;
FILE* entity_file = io_file_open(directory_type, filename, "w");
if(!entity_file)
{
log_error("entity:save", "Failed to open entity file %s for writing");
return success;
}
/* 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: %d\n", entity->is_listener);
fprintf(entity_file, "renderable: %d\n", entity->renderable);
struct Entity* parent = entity_get_parent(entity->id);
fprintf(entity_file, "parent: %s\n", parent->name);
/* Transform */
fprintf(entity_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",
entity->transform.scale.x,
entity->transform.scale.y,
entity->transform.scale.z);
fprintf(entity_file, "rotation: %.5f %.5f %.5f %.5f\n",
entity->transform.rotation.x,
entity->transform.rotation.y,
entity->transform.rotation.z,
entity->transform.rotation.w);
switch(entity->type)
{
case ET_CAMERA:
{
fprintf(entity_file, "ortho: %d\n", entity->camera.ortho);
fprintf(entity_file, "resizeable: %d\n", entity->camera.resizeable);
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: %d\n", entity->camera.render_tex == -1 ? 0 : 1);
break;
}
case ET_STATIC_MESH:
{
/* 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);
break;
}
case ET_LIGHT:
{
fprintf(entity_file, "type: %df\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: %df\n", entity->light.valid);
fprintf(entity_file, "cast_shadow: %df\n", entity->light.cast_shadow);
fprintf(entity_file, "pcf_enabled: %df\n", entity->light.pcf_enabled);
fprintf(entity_file, "color: %.5f %.5f %.5f",
entity->light.color.x,
entity->light.color.y,
entity->light.color.z);
break;
}
case ET_SOUND_SOURCE:
{
fprintf(entity_file, "active: %df\n", entity->sound_source.active);
fprintf(entity_file, "relative: %df\n", entity->sound_source.relative);
break;
}
};
fprintf(entity_file, "\n");
log_message("Entity %s written to %s", entity->name, filename);
success = true;
fclose(entity_file);
return success;
}

@ -120,5 +120,6 @@ struct Entity* entity_get(int index);
struct Entity* entity_find(const char* name);
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);
#endif

@ -181,9 +181,9 @@ void scene_setup(void)
}
log_message("Sizeof Entity : %d", sizeof(struct Entity));
/* struct Entity* sun = scene_add_new("Sun", NULL); */
/* struct Light* sun_light = entity_component_add(sun, C_LIGHT, LT_DIR); */
/* sun_light->intensity = 0.8f; */
struct Entity* light_ent = entity_find("Light_Ent");
entity_save(light_ent, "light.ent", DT_INSTALL);
}
void debug(float dt)

@ -121,6 +121,7 @@ void renderer_init(void)
void renderer_draw(struct Entity* active_viewer)
{
/* Render each camera output into it's framebuffer or to the default framebuffer */
struct Entity* entity_list = entity_get_all();
for(int i = 0; i < array_len(entity_list); i++)
{
@ -128,7 +129,6 @@ void renderer_draw(struct Entity* active_viewer)
if(entity_list[i].type != ET_CAMERA) continue;
struct Camera* camera = &viewer->camera;
/* if(camera->fbo == -1) continue; */
int fbo = camera->fbo == -1 ? def_fbo : camera->fbo;
framebuffer_bind(fbo);
{

@ -120,3 +120,15 @@ struct Entity* scene_get_parent(struct Entity* entity)
parent = entity_get(entity->transform.parent);
return parent;
}
bool scene_load(const char* filename)
{
bool success = false;
return success;
}
bool scene_save(const char* filename)
{
bool success = false;
return success;
}

@ -1,6 +1,8 @@
#ifndef SCENE_H
#define SCENE_H
#include "num_types.h"
struct Entity;
void scene_init(void);
@ -13,5 +15,8 @@ struct Entity* scene_find(const char* name);
struct Entity* scene_get_root(void);
struct Entity* scene_get_child_by_name(struct Entity* parent, const char* name);
struct Entity* scene_get_parent(struct Entity* entity);
bool scene_load(const char* filename);
bool scene_save(const char* filename);
#endif

Loading…
Cancel
Save