From fb77575f04bcb10035bbce6a07651aafc170f080 Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Sun, 26 Mar 2017 01:17:30 +0500 Subject: [PATCH] Fixed bugs with key release by changing key polling behaviour --- orgfile.org | 5 ++++- src/game.c | 34 ++++++++++++++++++---------------- src/input.c | 25 ++++++------------------- src/input.h | 2 +- src/model.c | 17 ++--------------- src/platform.c | 2 +- src/platform.h | 2 +- 7 files changed, 33 insertions(+), 54 deletions(-) diff --git a/orgfile.org b/orgfile.org index 2cf0072..0a3cba0 100644 --- a/orgfile.org +++ b/orgfile.org @@ -111,7 +111,8 @@ x Font atlas proper cleanup - Decoupled event handling of gui and input if possible - Custom rendering for gui ** TODO Allow passsing base path as commandline argument? -** TODO Fix Key release not being reported +** 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? ** DONE OpenAL not working in releasebuilds - State "DONE" from "TODO" [2017-03-25 Sat 02:06] @@ -124,6 +125,8 @@ x Font atlas proper cleanup ** TODO Array-based Hashmaps ** TODO Sprite sheet animations ** TODO Replace orgfile with simple text readme and reduce duplication? +** TODO Ray picking +** TODO Do input maps really need to be queried by their string names? ** DONE Live data views in editor - State "DONE" from "TODO" [2017-03-22 Wed 02:14] ** DONE Camera resize on window reisze diff --git a/src/game.c b/src/game.c index d675e9b..5f885fb 100644 --- a/src/game.c +++ b/src/game.c @@ -87,7 +87,8 @@ void scene_setup(void) int turn_up_keys[1] = {KEY_I}; int turn_down_keys[1] = {KEY_K}; int sprint_keys[2] = {KEY_LSHIFT, KEY_RSHIFT}; - int recompute_keys[2] = {KEY_F5, KEY_H}; + int recompute_keys[2] = {KEY_F5, KEY_H}; + int ed_toggle_keys[1] = {KEY_F1}; input_map_create("Move_Forward", forward_keys, 2); input_map_create("Move_Backward", backward_keys, 2); input_map_create("Move_Up", up_keys, 1); @@ -100,6 +101,7 @@ void scene_setup(void) input_map_create("Turn_Down", turn_down_keys, 1); input_map_create("Sprint", sprint_keys, 2); input_map_create("Recompute", recompute_keys, 2); + input_map_create("Editor_Toggle", ed_toggle_keys, 1); struct Entity* player = scene_add_new("player", "None"); game_state->player_node = player->node; @@ -149,8 +151,8 @@ void scene_setup(void) int y = rand() % num_suz; int z = rand() % num_suz; x++; y++; z++; - //struct Entity* suz = scene_add_as_child("Suzanne", NULL, parent_node); - struct Entity* suz = scene_add_new("Suzanne", NULL); + struct Entity* suz = scene_add_as_child("Suzanne", NULL, parent_node); + //struct Entity* suz = scene_add_new("Suzanne", NULL); struct Model* suz_model = entity_component_add(suz, C_MODEL, "suzanne.pamesh", "Blinn_Phong"); model_set_material_param(suz_model, "diffuse_color", &color); float spec_str = 80.f; @@ -211,7 +213,7 @@ void scene_setup(void) void debug(float dt) { //struct Entity* entity = entity_get(player_node); - struct Entity* entity = !input_key_state_get('C', KS_PRESSED) ? entity_get(game_state->player_node) : scene_find("Screen_Camera"); + struct Entity* entity = entity_get(game_state->player_node); struct Camera* cam = entity_component_get(entity, C_CAMERA); camera_set_primary_viewer(cam); struct Transform* transform = entity_component_get(entity, C_TRANSFORM); @@ -235,8 +237,8 @@ void debug(float dt) log_message("Regenerating Bounding Volumes"); geom_bounding_volume_generate_all(); } - /* if(input_key_state_get(KEY_TAB, KS_PRESSED)) */ - /* if(input_key_state_get(KEY_TAB, KS_PRESSED) && input_key_state_get(KEY_LSHIFT, KS_PRESSED)) input_mouse_mode_set(MM_NORMAL); */ + /* if(input_is_key_pressed(KEY_TAB, KS_PRESSED)) */ + /* if(input_is_key_pressed(KEY_TAB, KS_PRESSED) && input_is_key_pressed(KEY_LSHIFT, KS_PRESSED)) input_mouse_mode_set(MM_NORMAL); */ if(input_mousebutton_state_get(MSB_RIGHT, KS_PRESSED)) { @@ -308,7 +310,7 @@ void debug(float dt) //log_message("Position : %s", tostr_vec3(&transform->position)); } - if(input_key_state_get(KEY_SPACE, KS_PRESSED)) + if(input_is_key_pressed(KEY_SPACE)) { struct Entity* model = scene_find("Model_Entity"); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); @@ -316,7 +318,7 @@ void debug(float dt) transform_rotate(mod_tran, &x_axis, 25.f * dt, TS_WORLD); } - if(input_key_state_get(KEY_M, KS_PRESSED)) + if(input_is_key_pressed(KEY_M)) { struct Entity* model = scene_find("Model_Entity"); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); @@ -326,7 +328,7 @@ void debug(float dt) transform_translate(mod_tran, &amount, TS_LOCAL); } - if(input_key_state_get(KEY_N, KS_PRESSED)) + if(input_is_key_pressed(KEY_N)) { struct Entity* model = scene_find("Model_Entity"); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); @@ -336,14 +338,14 @@ void debug(float dt) transform_translate(mod_tran, &amount, TS_LOCAL); } - /* if(input_key_state_get(GLFW_KEY_C, KS_PRESSED)) */ + /* if(input_is_key_pressed(GLFW_KEY_C, KS_PRESSED)) */ /* { */ /* struct Entity* cam_ent = scene_find("Screen_Camera"); */ /* struct Camera* cam = entity_component_get(cam_ent, C_CAMERA); */ /* camera_set_primary_viewer(cam); */ /* } */ - /* if(input_key_state_get(GLFW_KEY_V, KS_PRESSED)) */ + /* if(input_is_key_pressed(GLFW_KEY_V, KS_PRESSED)) */ /* { */ /* struct Camera* cam = entity_component_get(entity, C_CAMERA); */ /* camera_set_priimary_viewer(cam); */ @@ -370,15 +372,15 @@ void run(void) } void update(float dt, int* window_should_close) -{ - input_update(); - - if(input_key_state_get(KEY_ESCAPE, KS_PRESSED)) *window_should_close = 1; - if(input_key_state_get(KEY_F1, KS_PRESSED)) editor_toggle(); +{ + if(input_is_key_pressed(KEY_ESCAPE)) *window_should_close = 1; + if(input_map_state_get("Editor_Toggle", KS_RELEASED)) editor_toggle(); debug(dt); //debug_gui(dt); editor_update(dt); + input_update(); /* This should always be the last thing. Probably + * put this in post update? */ } void debug_gui(float dt) diff --git a/src/input.c b/src/input.c index 132af86..a8e9955 100644 --- a/src/input.c +++ b/src/input.c @@ -70,6 +70,7 @@ void input_mouse_pos_set(int xpos, int ypos) void input_on_key(int key, int scancode, int state, int mod_ctrl, int mod_shift) { + log_message("Key %s", state == KS_PRESSED ? "Pressed" : "Released"); for(int i = 0; i < array_len(input_map_list); i++) { struct Input_Map* map = &input_map_list[i]; @@ -119,25 +120,11 @@ int input_map_state_get(const char* map_name, int state) result = 1; } return result; - - //return state == current_state ? 1 : 0; - /* if(state == KS_PRESSED) */ - /* { */ - /* if(current_state == KS_PRESSED)// || current_state == GLFW_REPEAT) */ - /* return 1; */ - /* else */ - /* return 0; */ - /* } */ - /* else */ - /* { */ - /* return state == current_state ? 1 : 0; */ - /* } */ -} - -int input_key_state_get(int key, int state_type) -{ - int current_state = platform_key_state_get(key); - return state_type == current_state ? 1 : 0; +} + +int input_is_key_pressed(int key) +{ + return platform_is_key_pressed(key); } int input_mousebutton_state_get(uint button, int state_type) diff --git a/src/input.h b/src/input.h index cc8ddc4..4644e77 100644 --- a/src/input.h +++ b/src/input.h @@ -396,7 +396,7 @@ enum Keyboard_Scancode void input_init(void); void input_cleanup(void); int input_mousebutton_state_get(uint button, int state_type); -int input_key_state_get(int key, int state_type); +int input_is_key_pressed(int key); void input_mouse_pos_get(int* xpos, int* ypos); void input_mouse_delta_get(int* xpos, int* ypos); // Use with relative mouse mode void input_mouse_pos_set(int xpos, int ypos); diff --git a/src/model.c b/src/model.c index 0b1ee46..531a3d2 100644 --- a/src/model.c +++ b/src/model.c @@ -403,22 +403,9 @@ void model_render_all_debug(struct Camera* camera, struct Entity* entity = entity_get(model->node); struct Transform* transform = entity_component_get(entity, C_TRANSFORM); int geometry_ac = model->geometry_index; - struct Bounding_Sphere* sphere = geom_bounding_sphere_get(geometry_ac); - struct Transform temp_trans; - memcpy(&temp_trans, transform, sizeof(struct Transform)); - temp_trans.node = -1; - vec3_scale(&temp_trans.scale, &temp_trans.scale, sphere->radius); - transform_update_transmat(&temp_trans); - struct Entity* parent = entity_get(entity->parent); - if(parent) - { - struct Transform* parent_transform = entity_component_get(parent, C_TRANSFORM); - mat4_mul(&temp_trans.trans_mat, &temp_trans.trans_mat, &parent_transform->trans_mat); - } - //log_message("Radius %.3f", sphere->radius); mat4_identity(&mvp); - //mat4_mul(&mvp, &camera->view_proj_mat, &transform->trans_mat); - mat4_mul(&mvp, &camera->view_proj_mat, &temp_trans.trans_mat); + mat4_mul(&mvp, &camera->view_proj_mat, &transform->trans_mat); + //mat4_mul(&mvp, &camera->view_proj_mat, &temp_trans.trans_mat); shader_set_uniform_mat4(debug_shader, "mvp", &mvp); geom_render(geometry, draw_mode); } diff --git a/src/platform.c b/src/platform.c index c6d6e11..65e671a 100644 --- a/src/platform.c +++ b/src/platform.c @@ -266,7 +266,7 @@ void platform_windowresize_callback_set(Windowresize_Event_Func func) platform_state->on_windowresize_func = func; } -int platform_key_state_get(int key) +int platform_is_key_pressed(int key) { /* Returns 1 if key is pressed, 0 otherwise */ const Uint8* keyboard_state = SDL_GetKeyboardState(NULL); diff --git a/src/platform.h b/src/platform.h index e1e45af..8141901 100644 --- a/src/platform.h +++ b/src/platform.h @@ -36,7 +36,7 @@ void platform_mousemotion_callback_set(Mousemotion_Event_Func func); void platform_mousewheel_callback_set(Mousewheel_Event_Func func); void platform_windowresize_callback_set(Windowresize_Event_Func func); void platform_textinput_callback_set(Textinput_Event_Func func); -int platform_key_state_get(int key); +int platform_is_key_pressed(int key); int platform_mousebutton_state_get(uint button); void platform_mouse_position_get(int* x, int* y); void platform_mouse_delta_get(int* x, int* y); // Use with relative mouse mode