diff --git a/src/entity.c b/src/entity.c index e7aba38..78cabaa 100644 --- a/src/entity.c +++ b/src/entity.c @@ -2,6 +2,7 @@ #include "array.h" #include "log.h" #include "string_utils.h" +#include "transform.h" #include #include @@ -32,11 +33,9 @@ void entity_remove(int index) for(int i = 0; i < MAX_COMPONENTS; i++) { - enum Component component = entity->components[i]; - switch(component) + switch((enum Component)i) { - case C_TRANSFORM: - break; + case C_TRANSFORM: transform_remove(entity->components[i]); break; case C_MODEL: break; case C_RIGIDBODY: @@ -47,37 +46,42 @@ void entity_remove(int index) /* Only called for MAX_COMPONENTS, do nothing */ break; } + entity->components[i] = -1; } + entity->node = -1; + free(entity->name); + free(entity->tag); + entity->name = entity->tag = NULL; + array_push(empty_indices, index, int); } struct Entity* entity_create(const char* name, const char* tag) { struct Entity* new_entity = NULL; int index = -1; - int empty_len = array_len(empty_indices); - if(empty_len > 0) + + if(array_len(empty_indices) > 0) { - index = empty_indices[empty_len - 1]; + index = *array_get_last(empty_indices, int); array_pop(empty_indices); new_entity = &entity_list[index]; } else { new_entity = array_grow(entity_list, struct Entity); + new_entity->name = new_entity->tag = NULL; index = array_len(entity_list) - 1; } if(new_entity->name) free(new_entity->name); - if(new_entity->name) free(new_entity->tag); + if(new_entity->tag) free(new_entity->tag); new_entity->name = name ? str_new(name) : str_new("DEFAULT_NAME"); new_entity->tag = tag ? str_new(tag) : str_new("DEFAULT_TAG"); new_entity->node = index; - - /* TODO: Add transform here by default maybe? */ + new_entity->components[C_TRANSFORM] = transform_create(new_entity->node); - return new_entity; - + return new_entity; } struct Entity* entity_get(int index) @@ -97,13 +101,14 @@ struct Entity* entity_find(const char* name) for(int i = 0; i < array_len(entity_list); i++) { struct Entity* curr_ent = &entity_list[i]; + if(!entity->name) + continue; if(strcmp(curr_ent->name, name) == 0) { entity = curr_ent; break; } } - return entity; } int entity_component_remove(struct Entity* entity, enum Component component) @@ -113,6 +118,8 @@ int entity_component_remove(struct Entity* entity, enum Component component) switch(component) { case C_TRANSFORM: + log_error("entity:remove", "Cannot remove Tranform component"); + success = 0; break; case C_MODEL: break; @@ -134,8 +141,7 @@ void* entity_component_get(struct Entity* entity, enum Component component) assert(entity); switch(component) { - case C_TRANSFORM: - break; + case C_TRANSFORM: comp_obj = transform_get(entity->components[C_TRANSFORM]); break; case C_MODEL: break; case C_RIGIDBODY: @@ -156,7 +162,7 @@ void* entity_component_add(struct Entity* entity, enum Component component) switch(component) { case C_TRANSFORM: - break; + log_error("entity:add_component", "Entity already has Transform component"); case C_MODEL: break; case C_RIGIDBODY: diff --git a/src/entity.h b/src/entity.h index e430ddb..6c21889 100644 --- a/src/entity.h +++ b/src/entity.h @@ -4,11 +4,9 @@ #include "components.h" #include "num_types.h" -typedef int32 Node; - struct Entity { - Node node; + int node; char* name; char* tag; int components[MAX_COMPONENTS]; diff --git a/src/game.c b/src/game.c index bad7560..5afcaf2 100644 --- a/src/game.c +++ b/src/game.c @@ -13,10 +13,14 @@ #include "entity.h" #include "geometry.h" #include "array.h" +#include "transform.h" void run(void); void update(void); void render(void); +void debug(void); + +struct Entity* entity = NULL; void game_init(void) { @@ -26,6 +30,7 @@ void game_init(void) renderer_init(window); io_file_init("/mnt/Dev/Projects/Symmetry/assets/");/* TODO: Implement proper way of getting binary directory */ shader_init(); + transform_init(); entity_init(); geom_init(); @@ -40,9 +45,19 @@ void game_init(void) input_map_create("Test2", keys4, 1); int shader = shader_create("phong.vert", "phong.frag"); + entity = entity_create("Test", "None"); + run(); } +void debug(void) +{ + struct Transform* transform = entity_component_get(entity, C_TRANSFORM); + vec3 offset = {0, 5, 0}; + transform_translate(transform, offset, TS_WORLD); + log_message("Position : %.3f, %.3f, %.3f", transform->position[0], transform->position[1], transform->position[2]); +} + void run(void) { while(!window_should_close()) @@ -59,6 +74,9 @@ void update(void) input_update(); if(input_key_state_get(GLFW_KEY_ESCAPE, GLFW_PRESS)) window_set_should_close(1); + + if(input_map_state_get("MoveUp", GLFW_PRESS)) + debug(); } void render(void) @@ -70,6 +88,7 @@ void game_cleanup(void) { entity_cleanup(); geom_cleanup(); + transform_cleanup(); input_cleanup(); renderer_cleanup(); io_file_cleanup(); diff --git a/src/transform.c b/src/transform.c index 857beb4..0f8c3de 100644 --- a/src/transform.c +++ b/src/transform.c @@ -1,3 +1,117 @@ #include "transform.h" +#include "array.h" +#include +struct Transform* transform_list; +int* empty_indices; +void transform_init(void) +{ + transform_list = array_new(struct Transform); + empty_indices = array_new(int); +} + +void transform_cleanup(void) +{ + array_free(transform_list); + array_free(empty_indices); +} + +int transform_create(int node) +{ + int index = -1; + if(node > -1) + { + struct Transform* new_transform = NULL; + if(array_len(empty_indices) > 0) + { + index = *array_get_last(empty_indices, int); + array_pop(empty_indices); + new_transform = &transform_list[index]; + } + else + { + new_transform = array_grow(transform_list, struct Transform); + index = array_len(transform_list) - 1; + } + new_transform->node = node; + new_transform->position[0] = new_transform->position[1] = new_transform->position[2] = 0; + new_transform->scale[0] = new_transform->scale[1] = new_transform->scale[2] = 1; + quat_identity(new_transform->rotation); + transform_update_transmat(new_transform); + } + return index; +} + +void transform_translate(struct Transform* transform, vec3 amount, enum Transform_Space space) +{ + if(space == TS_LOCAL) + quat_mul_vec3(amount, transform->rotation, amount); + vec3_add(transform->position, transform->position, amount); + transform_update_transmat(transform); +} +void transform_rotate(struct Transform* transform, + vec3 axis, + float angle, + enum Transform_Space space) +{ + quat new_rot; + quat_identity(new_rot); + quat_rotate(new_rot, angle, axis); + quat_norm(new_rot, new_rot); + if(space == TS_LOCAL) + quat_mul(transform->rotation, transform->rotation, new_rot); + else if(space == TS_WORLD) + quat_mul(transform->rotation, new_rot, transform->rotation); + transform_update_transmat(transform); +} +void transform_scale(struct Transform* transform, vec3 scale) +{ + transform->scale[0] = scale[0]; + transform->scale[1] = scale[1]; + transform->scale[2] = scale[2]; + transform_update_transmat(transform); +} + +void transform_get_forward(struct Transform* transform, vec3 res) +{ + res[0] = 0; res[1] = 0; res[2] = 1; + quat_mul_vec3(res, transform->rotation, res); +} + +void transform_get_up(struct Transform* transform, vec3 res) +{ + res[0] = 0; res[1] = 1; res[2] = 0; + quat_mul_vec3(res, transform->rotation, res); +} + +void transform_get_right(struct Transform* transform, vec3 res) +{ + res[0] = 1; res[1] = 0; res[2] = 0; + quat_mul_vec3(res, transform->rotation, res); +} + + +void transform_update_transmat(struct Transform* transform) +{ + mat4 scale, rot, tran; + mat4_scale_aniso(scale, scale, transform->scale[0], transform->scale[1], transform->scale[2]); + mat4_from_quat(rot, transform->rotation); + mat4_translate(tran, transform->position[0], transform->position[1], transform->position[2]); + mat4_mul(transform->trans_mat, transform->trans_mat, scale); + mat4_mul(transform->trans_mat, transform->trans_mat, rot); + mat4_mul(transform->trans_mat, transform->trans_mat, tran); +} + +struct Transform* transform_get(int index) +{ + assert(index > -1 && index < array_len(transform_list)); + return &transform_list[index]; +} + +void transform_remove(int index) +{ + assert(index > -1 && index < array_len(transform_list)); + transform_list[index].node = -1; + array_push(empty_indices, index, int); +} diff --git a/src/transform.h b/src/transform.h index 3c08b89..76ea47f 100644 --- a/src/transform.h +++ b/src/transform.h @@ -3,15 +3,31 @@ #include "linmath.h" +enum Transform_Space { TS_LOCAL, TS_WORLD}; + struct Transform { + int node; vec3 position; vec3 scale; quat rotation; + mat4 trans_mat; }; -void transform_initialize(void); +struct Transform* transform_get(int index); +void transform_remove(int index); +void transform_init(void); void transform_cleanup(void); int transform_create(int node); +void transform_translate(struct Transform* transform, vec3 amount, enum Transform_Space space); +void transform_rotate(struct Transform* transform, + vec3 axis, + float angle, + enum Transform_Space space); +void transform_scale(struct Transform* transform, vec3 scale); +void transform_get_forward(struct Transform* transform, vec3 res); +void transform_get_up(struct Transform* transform, vec3 res); +void transform_get_right(struct Transform* transform, vec3 res); +void transform_update_transmat(struct Transform* transform); #endif diff --git a/src/window_system.c b/src/window_system.c index ca1b62d..cf5fd3e 100644 --- a/src/window_system.c +++ b/src/window_system.c @@ -28,6 +28,9 @@ int window_init(const char* title, int width, int height) GLFW_VERSION_REVISION); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + glfwWindowHint(GLFW_FOCUSED, GL_TRUE); + //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); + active_window = glfwCreateWindow(width, height, title, NULL, NULL); if(!active_window) {