Browse Source

cleaning up entity init and setup for grid movement

master
cinnaboot 8 years ago
parent
commit
1c703baba1
  1. 3
      data/hashmap_scene.json
  2. 106
      src/entity.cpp
  3. 20
      src/entity.h
  4. 11
      src/hexgame.cpp
  5. 11
      src/hexgrid.cpp
  6. 4
      src/hexgrid.h
  7. 6
      src/renderer.cpp
  8. 2
      src/scene_loader.h

3
data/hashmap_scene.json

@ -38,7 +38,7 @@
"model_file" : "block2.dae", "model_file" : "block2.dae",
"rotation" : {"x":0,"y":0,"z":0,"w":0}, "rotation" : {"x":0,"y":0,"z":0,"w":0},
"scale" : {"x":5,"y":5,"z":5}, "scale" : {"x":5,"y":5,"z":5},
"grid_pos": {"q":-2,"r":-2,"s":-4}, "grid_pos": {"q":-2,"r":-2,"s":4},
"use_grid": true "use_grid": true
}, },
{ {
@ -47,7 +47,6 @@
"position" : {"x":0,"y":0,"z":-1}, "position" : {"x":0,"y":0,"z":-1},
"rotation" : {"x":0,"y":0,"z":0,"w":0}, "rotation" : {"x":0,"y":0,"z":0,"w":0},
"scale" : {"x":300,"y":300,"z":300}, "scale" : {"x":300,"y":300,"z":300},
"grid_pos": {"q":0,"r":0,"s":0},
"use_grid": false "use_grid": false
} }
], ],

106
src/entity.cpp

@ -4,33 +4,104 @@
#include "entity.h" #include "entity.h"
void initEntityRG(Entity& e, rg_shader_program& shader, bool use_normals);
bool initEntityROs(Entity& e);
bool convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool use_texture); bool convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool use_texture);
bool bool
entInit(Entity& e, const char* data_dir, rg_shader_program& shader, v3f world_pos) entInit(Entity& e, const char* data_dir, rg_shader_program& shader, hexgrid& hg)
{ {
e.world_transform = glm::mat4(1.0);
entScale(e, e.scale);
uint max_len = 256; // TODO: define max_len for property strings uint max_len = 256; // TODO: define max_len for property strings
char full_path[max_len]; char full_path[max_len];
if (utilConcatPath(full_path, data_dir, e.model_filename, max_len)) { if (utilConcatPath(full_path, data_dir, e.model_filename, max_len)) {
if (meLoadFromFile(e.mesh_group, data_dir, full_path)) { if (meLoadFromFile(e.mesh_group, data_dir, full_path)) {
// initialize the entity's render group initEntityRG(e, shader, e.mesh_group.use_normals);
if (e.use_grid) {
if (!entPositionToGrid(e, hg, e.grid_pos.q, e.grid_pos.r, e.grid_pos.s)) {
LOG(ERROR) << "tried to place entity at invalid grid coord\n";
goto cleanup;
}
} else {
entSetWorldPosition(e, e.translation.x, e.translation.y, e.translation.z);
}
if (!initEntityROs(e))
goto cleanup;
} else {
goto cleanup;
}
} else {
LOG(ERROR) << data_dir << " + " << e.model_filename << " is too long\n";
goto cleanup;
}
return true;
cleanup:
entFree(e);
return false;
}
void
entFree(Entity& e)
{
meFreeMeshGroup(e.mesh_group);
rgFree(e.ren_group);
}
bool
entPositionToGrid(Entity& e, hexgrid& hg, int q, int r, int s)
{
if (e.container_hex) {
e.container_hex->contained_entity = nullptr;
e.container_hex = nullptr;
}
hex_info* hxi = hgGetSingleHex(hg, q, r, s);
if (hxi != nullptr) {
e.container_hex = hxi;
hxi->contained_entity = &e;
v3f pos = hgGetWorldPostion(hg, hxi->hex.q, hxi->hex.r, hxi->hex.s);
entSetWorldPosition(e, pos.x, pos.y, pos.z);
return true;
}
return false;
}
void
entSetWorldPosition(Entity& e, float x, float y, float z)
{
e.world_transform[3][0] = e.translation.x = x;
e.world_transform[3][1] = e.translation.y = y;
e.world_transform[3][2] = e.translation.z = z;
}
void
initEntityRG(Entity& e, rg_shader_program& shader, bool use_normals)
{
render_group* rg = UTIL_ALLOC(1, render_group); render_group* rg = UTIL_ALLOC(1, render_group);
e.ren_group = rg; e.ren_group = rg;
e.ren_group->shader = shader; e.ren_group->shader = shader;
rg->draw_indexed = true; rg->draw_indexed = true;
rg->draw_mode = GL_TRIANGLES; rg->draw_mode = GL_TRIANGLES;
rg->use_normals = e.mesh_group.use_normals; rg->use_normals = use_normals;
uint num_meshes = rg->num_objects = e.mesh_group.num_meshes; uint num_meshes = rg->num_objects = e.mesh_group.num_meshes;
rg->render_objects = UTIL_ALLOC(num_meshes, render_object*); rg->render_objects = UTIL_ALLOC(num_meshes, render_object*);
}
// apply translation/rotation/scaling bool
glm::vec3 v3_world(world_pos.x, world_pos.y, world_pos.z); initEntityROs(Entity& e)
e.world_transform = glm::mat4(1.0); {
entTranslate(e, v3_world); render_group* rg = e.ren_group;
entScale(e, e.scale);
for (uint i = 0; i < num_meshes; i++) for (uint i = 0; i < e.mesh_group.num_meshes; i++)
{ {
meMeshInfo* mesh = e.mesh_group.meshes[i]; meMeshInfo* mesh = e.mesh_group.meshes[i];
uint buffer_len = mesh->num_vertices * 3; uint buffer_len = mesh->num_vertices * 3;
@ -55,25 +126,10 @@ entInit(Entity& e, const char* data_dir, rg_shader_program& shader, v3f world_po
} }
} }
} }
} else {
meFreeMeshGroup(e.mesh_group);
return false;
}
} else {
LOG(ERROR) << data_dir << " + " << e.model_filename << " is too long\n";
return false;
}
return true; return true;
} }
void
entFree(Entity& e)
{
meFreeMeshGroup(e.mesh_group);
rgFree(e.ren_group);
}
bool bool
convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool use_texture) convertMeshInfo(meMeshInfo* mesh, render_object* ro, bool use_normals, bool use_texture)
{ {

20
src/entity.h

@ -4,6 +4,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "hexgrid.h"
#include "mesh.h" #include "mesh.h"
#include "render_group.h" #include "render_group.h"
@ -21,18 +22,15 @@ struct Entity
glm::vec3 translation; glm::vec3 translation;
glm::vec4 rotation; glm::vec4 rotation;
bool use_grid; // NOTE: determines if entity is position with grid_pos or translation
ent_grid_position grid_pos;
meMeshGroup mesh_group; meMeshGroup mesh_group;
render_group* ren_group; render_group* ren_group;
char model_filename[256]; char model_filename[256];
};
bool entInit(Entity& e, const char* data_dir, rg_shader_program& shader, v3f world_pos); bool use_grid; // NOTE: determines if entity is position with grid_pos or translation
ent_grid_position grid_pos;
void entFree(Entity& e); hex_info* container_hex;
};
inline void inline void
entTranslate(Entity& e, glm::vec3 v) entTranslate(Entity& e, glm::vec3 v)
@ -45,3 +43,11 @@ entScale(Entity& e, glm::vec3 v)
{ {
e.world_transform = glm::scale(e.world_transform, v); e.world_transform = glm::scale(e.world_transform, v);
} }
bool entInit(Entity& e, const char* data_dir, rg_shader_program& shader, hexgrid& hg);
void entFree(Entity& e);
bool entPositionToGrid(Entity& e, hexgrid& hg, int q, int r, int s);
void entSetWorldPosition(Entity& e, float x, float y, float z);

11
src/hexgame.cpp

@ -71,16 +71,11 @@ loadSceneFromJson(game_state* s, render_state* rs)
ret = false; ret = false;
} }
// NOTE: hexgrid must be initialized before entities to be able to use grid coordinates
// for entity positions
if (slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES)) { if (slParseEntities(sd, gs->entities, gs->entity_count, MAX_ENTITIES)) {
for (uint i = 0; i < gs->entity_count; i++) { for (uint i = 0; i < gs->entity_count; i++) {
Entity& e = gs->entities[i]; if (!entInit(gs->entities[i], DATA_DIR, rs->default_shader, gs->grid)) {
v3f world_pos;
if (e.use_grid)
world_pos = hgGetWorldPostion(gs->grid, e.grid_pos.q, e.grid_pos.r, e.grid_pos.s);
else
world_pos = v3f(e.translation.x, e.translation.y, e.translation.z);
if (!entInit(e, DATA_DIR, rs->default_shader, world_pos)) {
LOG(ERROR) << "Error initilaizing entity\n"; LOG(ERROR) << "Error initilaizing entity\n";
return false; return false;
} }

11
src/hexgrid.cpp

@ -216,6 +216,17 @@ hgGetSingleHex(hexgrid& hg, real32 x, real32 y)
return nullptr; return nullptr;
} }
hex_info*
hgGetSingleHex(hexgrid& hg, int q, int r, int s)
{
auto it = hg.hex_map.find(Hex(q, r, s));
if (it != hg.hex_map.end())
return &it->second;
return nullptr;
}
v3f v3f
hgGetWorldPostion(hexgrid& hg, int q, int r, int s) hgGetWorldPostion(hexgrid& hg, int q, int r, int s)
{ {

4
src/hexgrid.h

@ -44,6 +44,8 @@ struct hex_info
real64 YPos; real64 YPos;
bool selected; bool selected;
std::vector<Point> vertices; std::vector<Point> vertices;
void* contained_entity;
}; };
struct hex_hashfunc struct hex_hashfunc
@ -97,6 +99,8 @@ void hgUpdateUVBuffer(hexgrid& hg, render_group* rg);
hex_info* hgGetSingleHex(hexgrid& hg, real32 x, real32 y); hex_info* hgGetSingleHex(hexgrid& hg, real32 x, real32 y);
hex_info* hgGetSingleHex(hexgrid& hg, int q, int r, int s);
v3f hgGetWorldPostion(hexgrid& hg, int q, int r, int s); v3f hgGetWorldPostion(hexgrid& hg, int q, int r, int s);
void hgResetHexes(hexgrid& hg); void hgResetHexes(hexgrid& hg);

6
src/renderer.cpp

@ -21,9 +21,9 @@
#define MAX_LIGHTS 10 // NOTE: needs to match the fragment shader source #define MAX_LIGHTS 10 // NOTE: needs to match the fragment shader source
#define DEFAULT_PALETTE_DIR "../data" #define DEFAULT_PALETTE_DIR "../data"
#define DEFAULT_PALETTE_FILE "palette.png" #define DEFAULT_PALETTE_FILE "palette.png"
#define CLEAR_COL_R 75.f / 255.f #define CLEAR_COL_R 55.f / 255.f
#define CLEAR_COL_G 135.f / 255.f #define CLEAR_COL_G 55.f / 255.f
#define CLEAR_COL_B 135.f / 255.f #define CLEAR_COL_B 55.f / 255.f
#define CLEAR_COL_A 1.f #define CLEAR_COL_A 1.f
#define USE_SECOND_MONITOR 1 #define USE_SECOND_MONITOR 1

2
src/scene_loader.h

@ -3,13 +3,11 @@
#include "camera.h" #include "camera.h"
#include "hexgrid.h" #include "hexgrid.h"
#include "renderer.h"
#include "render_group.h" #include "render_group.h"
#include "util.h" #include "util.h"
struct slSceneDoc; struct slSceneDoc;
struct render_state;
slSceneDoc* slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file); slSceneDoc* slLoadFile(const char* data_dir, const char* scene_file, const char* schema_file);

Loading…
Cancel
Save