diff --git a/assets/config.cfg b/assets/config.cfg index 472d78c..23f1e33 100644 --- a/assets/config.cfg +++ b/assets/config.cfg @@ -1,5 +1,5 @@ msaa_levels: 4 -debug_draw_color: 0.000, 1.000, 0.000, 1.000 +debug_draw_color: 1.000, 0.000, 0.000, 1.000 render_width: 800 fog_density: 0.0020 render_height: 600 @@ -10,4 +10,4 @@ msaa_enabled: true fog_start_dist: 20.0000 debug_draw_enabled: false debug_draw_mode: 0 -ambient_light: 0.100, 0.100, 0.120 +ambient_light: 0.100, 0.100, 0.100 diff --git a/orgfile.org b/orgfile.org index 079a29e..624fbbe 100644 --- a/orgfile.org +++ b/orgfile.org @@ -11,8 +11,8 @@ All the code in this repository is under GPLv3, see LICENSE for more information ** File format specifications *** Entity -// Comment, Sample entity definition in file, paremeters left out are set to defaults -// Empty line at the end specifies end of entity definition +# Comment, Sample entity definition in file, paremeters left out are set to defaults +# Empty line at the end specifies end of entity definition entity: "Something" position: 0 0 0 scale: 1 1 1 @@ -36,6 +36,13 @@ msaa: true msaa_levels: 8 *** Keybindings +# All keys are parsed by comparing the output of SDL_GetKeyname +# Each line represents a keybinding +Move_Forward: W +# Multiple keys to a single binding are specified with commas +Move_Backward: S,Down +# Combinations are specified with a hyphen/dash +Quit: Left Ctrl-Q *** Level/Scene *** Materials *** Mesh/Geometry @@ -98,6 +105,7 @@ msaa_levels: 8 - State "DONE" from "TODO" [2015-10-13 Tue 19:38] ** TODO Add modifiers to input maps to enable combinations for example, c-x, m-k etc ** TODO Only allocate hashmap bucket when required +** TODO Mapping actions to keybindings, for example map action "Jump" to Space key etc ** DONE Heirarchical Transforms ** DONE Materials with textures - State "DONE" from "TODO" [2015-10-15 Thu 21:21] diff --git a/src/array.c b/src/array.c index 6bc9a74..21c9525 100644 --- a/src/array.c +++ b/src/array.c @@ -25,11 +25,11 @@ static struct Array* array_get_ptr(void* array_data) void* array_new_(size_t object_size, int capacity) { - if(capacity == 0) capacity = ARRAY_MIN_CAPACITY; - struct Array* new_array = malloc(sizeof(*new_array) + (object_size * capacity)); - new_array->object_size = object_size; - new_array->length = 0; - new_array->capacity = capacity; + int initial_capacity = capacity == 0 ? ARRAY_MIN_CAPACITY : capacity; + struct Array* new_array = malloc(sizeof(*new_array) + (object_size * initial_capacity)); + new_array->object_size = object_size; + new_array->length = capacity; + new_array->capacity = initial_capacity; return new_array->data; } diff --git a/src/array.h b/src/array.h index 62cdaa2..02eccaf 100644 --- a/src/array.h +++ b/src/array.h @@ -15,7 +15,7 @@ int array_copy_(void* arr_src, void** arr_dest); /* Public Api */ #define array_new(type) (type*) array_new_(sizeof(type), 0); // Use this for array creation -#define array_new_cap(type, capacity) (type*) array_new_(sizeof(type), capacity); // Use this for array with specific capacity +#define array_new_cap(type, capacity) (type*) array_new_(sizeof(type), capacity); // Use this for array with specific length and capacity #define array_clear(array) array_reset(array, 0) #define array_grow(array, type) (type*) array_grow_((void**)&array) #define array_push(array, value, type) {type* new_val = array_grow(array, type); \ diff --git a/src/editor.c b/src/editor.c index a3d9417..99e2742 100644 --- a/src/editor.c +++ b/src/editor.c @@ -50,7 +50,7 @@ void editor_init(void) editor_state.renderer_settings_window = 0; editor_state.debug_vars_window = 1; editor_state.top_panel_height = 30; - debug_vars_list = array_new_cap(struct Debug_Variable, 20); + debug_vars_list = array_new(struct Debug_Variable); empty_indices = array_new(int); } @@ -219,9 +219,9 @@ void editor_update(float dt) nk_slider_float(context, 0.f, &render_settings->fog.density, 1.f, 0.005); if(nk_input_is_mouse_hovering_rect(&context->input, bounds)) { - static char float_str[6]; + static char float_str[7]; snprintf(float_str, 6, "%.4f", render_settings->fog.density); - float_str[5] = '\0'; + float_str[6] = '\0'; nk_tooltip(context, float_str); } diff --git a/src/game.c b/src/game.c index 6c24eda..3347910 100644 --- a/src/game.c +++ b/src/game.c @@ -89,36 +89,36 @@ int game_init(struct Window* window) void scene_setup(void) { - int forward_keys[2] = {KEY_W, KEY_UP}; - int backward_keys[2] = {KEY_S, KEY_DOWN}; - int up_keys[2] = {KEY_Q}; - int down_keys[2] = {KEY_E}; - int left_keys[2] = {KEY_A, KEY_LEFT}; - int right_keys[2] = {KEY_D, KEY_RIGHT}; - int turn_right_keys[1] = {KEY_L}; - int turn_left_keys[1] = {KEY_J}; - 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 ed_toggle_keys[1] = {KEY_F1}; - int win_fullscr_keys[1] = {KEY_F11}; - int win_max_keys[1] = {KEY_F12}; + struct Key_Combination forward_keys[2] = {{KEY_W, KMD_NONE}, {KEY_UP, KMD_ALT}}; + struct Key_Combination backward_keys[2] = {{KEY_S, KMD_NONE}, {KEY_DOWN, KMD_NONE}}; + /* struct Key_Combination up_keys[2] = {KEY_Q}; */ + /* struct Key_Combination down_keys[2] = {KEY_E}; */ + /* struct Key_Combination left_keys[2] = {KEY_A, KEY_LEFT}; */ + /* struct Key_Combination right_keys[2] = {KEY_D, KEY_RIGHT}; */ + /* struct Key_Combination turn_right_keys[1] = {KEY_L}; */ + /* struct Key_Combination turn_left_keys[1] = {KEY_J}; */ + /* struct Key_Combination turn_up_keys[1] = {KEY_I}; */ + /* struct Key_Combination turn_down_keys[1] = {KEY_K}; */ + /* struct Key_Combination sprint_keys[2] = {KEY_LSHIFT, KEY_RSHIFT}; */ + /* struct Key_Combination recompute_keys[2] = {KEY_F5, KEY_H}; */ + /* struct Key_Combination ed_toggle_keys[1] = {KEY_F1}; */ + /* struct Key_Combination win_fullscr_keys[1] = {KEY_F11}; */ + /* struct Key_Combination win_max_keys[1] = {KEY_F12}; */ input_map_create("Move_Forward", forward_keys, 2); input_map_create("Move_Backward", backward_keys, 2); - input_map_create("Move_Up", up_keys, 1); - input_map_create("Move_Down", down_keys, 1); - input_map_create("Move_Left", left_keys, 2); - input_map_create("Move_Right", right_keys, 2); - input_map_create("Turn_Right", turn_right_keys, 1); - input_map_create("Turn_Left", turn_left_keys, 1); - input_map_create("Turn_Up", turn_up_keys, 1); - 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); - input_map_create("Window_Fullscreen", win_fullscr_keys, 1); - input_map_create("Window_Maximize", win_max_keys, 1); + /* input_map_create("Move_Up", up_keys, 1); */ + /* input_map_create("Move_Down", down_keys, 1); */ + /* input_map_create("Move_Left", left_keys, 2); */ + /* input_map_create("Move_Right", right_keys, 2); */ + /* input_map_create("Turn_Right", turn_right_keys, 1); */ + /* input_map_create("Turn_Left", turn_left_keys, 1); */ + /* input_map_create("Turn_Up", turn_up_keys, 1); */ + /* 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); */ + /* input_map_create("Window_Fullscreen", win_fullscr_keys, 1); */ + /* input_map_create("Window_Maximize", win_max_keys, 1); */ struct Entity* player = scene_add_new("player", "None"); game_state->player_node = player->node; @@ -272,16 +272,16 @@ void debug(float dt) vec3 rot_axis_left_right = {0, 1, 0}; /* Look around */ - if(input_map_state_get("Turn_Up", KS_PRESSED)) turn_up_down += turn_speed; - if(input_map_state_get("Turn_Down", KS_PRESSED)) turn_up_down -= turn_speed; - if(input_map_state_get("Turn_Right", KS_PRESSED)) turn_left_right += turn_speed; - if(input_map_state_get("Turn_Left", KS_PRESSED)) turn_left_right -= turn_speed; + /* if(input_map_state_get("Turn_Up", KS_PRESSED)) turn_up_down += turn_speed; */ + /* if(input_map_state_get("Turn_Down", KS_PRESSED)) turn_up_down -= turn_speed; */ + /* if(input_map_state_get("Turn_Right", KS_PRESSED)) turn_left_right += turn_speed; */ + /* if(input_map_state_get("Turn_Left", KS_PRESSED)) turn_left_right -= turn_speed; */ - if(input_map_state_get("Recompute", KS_PRESSED)) - { - log_message("Regenerating Bounding Volumes"); - geom_bounding_volume_generate_all(); - } + /* if(input_map_state_get("Recompute", KS_PRESSED)) */ + /* { */ + /* log_message("Regenerating Bounding Volumes"); */ + /* geom_bounding_volume_generate_all(); */ + /* } */ /* 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); */ @@ -340,13 +340,13 @@ void debug(float dt) } /* Movement */ - if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= move_scale; + /* if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= move_scale; */ if(input_map_state_get("Move_Forward", KS_PRESSED)) offset.z -= move_speed; if(input_map_state_get("Move_Backward", KS_PRESSED)) offset.z += move_speed; - if(input_map_state_get("Move_Left", KS_PRESSED)) offset.x -= move_speed; - if(input_map_state_get("Move_Right", KS_PRESSED)) offset.x += move_speed; - if(input_map_state_get("Move_Up", KS_PRESSED)) offset.y += move_speed; - if(input_map_state_get("Move_Down", KS_PRESSED)) offset.y -= move_speed; + /* if(input_map_state_get("Move_Left", KS_PRESSED)) offset.x -= move_speed; */ + /* if(input_map_state_get("Move_Right", KS_PRESSED)) offset.x += move_speed; */ + /* if(input_map_state_get("Move_Up", KS_PRESSED)) offset.y += move_speed; */ + /* if(input_map_state_get("Move_Down", KS_PRESSED)) offset.y -= move_speed; */ vec3_scale(&offset, &offset, dt); if(offset.x != 0 || offset.y != 0 || offset.z != 0) @@ -422,9 +422,9 @@ int run(void) void update(float dt, int* window_should_close) { if(input_is_key_pressed(KEY_ESCAPE)) *window_should_close = 1; - if(input_map_state_get("Editor_Toggle", KS_RELEASED)) editor_toggle(); - if(input_map_state_get("Window_Fullscreen", KS_RELEASED)) window_fullscreen_set(game_state->window, 1); - if(input_map_state_get("Window_Maximize", KS_RELEASED)) window_fullscreen_set(game_state->window, 0); + /* if(input_map_state_get("Editor_Toggle", KS_RELEASED)) editor_toggle(); */ + /* if(input_map_state_get("Window_Fullscreen", KS_RELEASED)) window_fullscreen_set(game_state->window, 1); */ + /* if(input_map_state_get("Window_Maximize", KS_RELEASED)) window_fullscreen_set(game_state->window, 0); */ debug(dt); //debug_gui(dt); diff --git a/src/input.c b/src/input.c index 94a33ea..4553669 100644 --- a/src/input.c +++ b/src/input.c @@ -7,17 +7,14 @@ #include "log.h" #include "gui.h" -/* #define KS_INACTIVE -1; /\* state for input map is set to KS_INACTIVE(KeyState_Inactive) when */ -/* the key is neither pressed nor released *\/ */ - struct Input_Map { - const char* name; - int* keys; - int state; + const char* name; + struct Key_Combination* keys; + int state; }; -static void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift); +static void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift, int mod_alt); static void input_on_mousebutton(int button, int state, int x, int y, int8 num_clicks); static void input_on_mousemotion(int x, int y, int xrel, int yrel); static void input_on_mousewheel(int x, int y); @@ -68,19 +65,28 @@ void input_mouse_pos_set(int xpos, int ypos) platform_mouse_global_position_set(xpos, ypos); } -void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift) +void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift, int mod_alt) { - if(repeat) - { - return; /* Ignore key repeat */ - } + /* if(repeat) */ + /* { */ + /* return; /\* Ignore key repeat *\/ */ + /* } */ + + int mods = KMD_NONE; + if(mod_ctrl) mods |= KMD_CTRL; + if(mod_shift) mods |= KMD_SHIFT; + if(mod_alt) mods |= KMD_ALT; for(int i = 0; i < array_len(input_map_list); i++) { struct Input_Map* map = &input_map_list[i]; for(int j = 0; j < array_len(map->keys); j++) { - if(map->keys[j] == key) + if(map->state == KS_PRESSED && state == KS_RELEASED && map->keys[j].mods == mods) + { + map->state = state; + } + if(map->keys[j].key == key && (map->keys[j].mods == mods)) { map->state = state; break; @@ -93,7 +99,7 @@ void input_on_key(int key, int scancode, int state, int repeat, int mod_ctrl, in void input_on_mousebutton(int button, int state, int x, int y, int8 num_clicks) { - /* Probably add 'mouse maps', same as input maps for keyvboard but with buttons + /* Probably add 'mouse maps', same as input maps for keyboard but with buttons Do we even need that? */ /* TODO: This is temporary. After proper event loop is added this code should not be here */ @@ -137,18 +143,21 @@ int input_mousebutton_state_get(uint button, int state_type) return state_type == current_state ? 1 : 0; } -void input_map_create(const char* name, int* keys, size_t num_keys) +void input_map_create(const char* name, struct Key_Combination* keys, size_t num_keys) { assert(name && keys && num_keys > 0); struct Input_Map* new_map = array_grow(input_map_list, struct Input_Map); new_map->name = name; - new_map->keys = array_new(int); + new_map->keys = array_new_cap(struct Key_Combination, num_keys); new_map->state = KS_INACTIVE; for(size_t i = 0; i < num_keys; i++) - { - array_push(new_map->keys, keys[i], int); - } + new_map->keys[i] = keys[i]; + log_message("Created Input Map : %s", name); + /* { */ + /* new_map->keys[i].key = keys[i].key; */ + /* new_map->keys[i].mods = keys[i].mods; */ + /* } */ } void input_update(void) @@ -176,7 +185,7 @@ int input_map_remove(const char* name) return success; } -int input_map_keys_set(const char* name, int* keys, int num_keys) +int input_map_keys_set(const char* name, struct Key_Combination* keys, int num_keys) { assert(name && keys && num_keys > 0); int success = 0; @@ -184,10 +193,13 @@ int input_map_keys_set(const char* name, int* keys, int num_keys) if(index > -1) { struct Input_Map* map = &input_map_list[index]; - array_reset(map->keys, num_keys); + if(array_len(map->keys) != num_keys) + array_reset(map->keys, num_keys); for(int i = 0; i < num_keys; i++) map->keys[i] = keys[i]; - success = 1; + + map->state = KS_INACTIVE; + success = 1; } if(!success) log_error("input:map_keys_set", "Map %s not found", name); diff --git a/src/input.h b/src/input.h index 4644e77..db83892 100644 --- a/src/input.h +++ b/src/input.h @@ -7,6 +7,20 @@ #include #include +struct Key_Combination +{ + int key; + int mods; +}; + +enum Key_Mod +{ + KMD_NONE = KMOD_NONE, + KMD_ALT = KMOD_ALT, + KMD_SHIFT = KMOD_SHIFT, + KMD_CTRL = KMOD_CTRL +}; + enum Key_State { KS_INACTIVE = -1, @@ -404,9 +418,9 @@ void input_mouse_mode_set(enum Mouse_Mode mode); int input_mouse_mode_get(void); void input_update(void); int input_map_state_get(const char* map_name, int state); -void input_map_create(const char* name, int* keys, size_t num_keys); +void input_map_create(const char* name, struct Key_Combination* keys, size_t num_keys); +int input_map_keys_set(const char* name, struct Key_Combination* keys, int num_keys); int input_map_remove(const char* name); -int input_map_keys_set(const char* name, int* keys, int num_keys); int input_map_name_set(const char* name, const char* new_name); #endif diff --git a/src/light.c b/src/light.c index fbd6cae..6867ce2 100644 --- a/src/light.c +++ b/src/light.c @@ -1,11 +1,13 @@ #include "light.h" +#include "log.h" #include "array.h" #include +#include static struct Light* light_list; static int* empty_indices; static int* valid_light_indices; -static int max_lights; +static int max_lights; struct Light* light_get(int index) { @@ -25,8 +27,9 @@ void light_init(void) max_lights = 128; light_list = array_new_cap(struct Light, max_lights); for(int i = 0; i < max_lights; i++) light_list[i].valid = 0; - empty_indices = array_new_cap(int, max_lights); + empty_indices = array_new(int); valid_light_indices = array_new_cap(int, max_lights); + memset(valid_light_indices, -1, max_lights); } void light_cleanup(void) @@ -59,8 +62,20 @@ int light_create(int node, int light_type) } else { - new_light = array_grow(light_list, struct Light); - index = array_len(light_list) - 1; + for(index = 0; index < max_lights; index++) + { + if(!light_list[index].valid) + break; + } + + if(index == max_lights - 1) + { + index = -1; + log_warning("Max light limit(%d) reached, cannot add light", max_lights); + return index; + } + + new_light = &light_list[index]; } new_light->node = node; new_light->valid = 1; diff --git a/src/platform.c b/src/platform.c index ab16463..d30f9ef 100644 --- a/src/platform.c +++ b/src/platform.c @@ -227,7 +227,9 @@ void platform_poll_events(int* out_quit) int repeat = event.key.repeat; int mod_ctrl = (event.key.keysym.mod & KMOD_CTRL); int mod_shift = (event.key.keysym.mod & KMOD_SHIFT); - platform_state->on_keyboard_func(key, scancode, state, repeat, mod_ctrl, mod_shift); + int mod_alt = (event.key.keysym.mod & KMOD_ALT); + platform_state->on_keyboard_func(key, scancode, state, repeat, mod_ctrl, mod_shift, mod_alt); + log_message("Key name : %s", SDL_GetKeyName(key)); break; } case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: diff --git a/src/platform.h b/src/platform.h index 4c42186..5afb667 100644 --- a/src/platform.h +++ b/src/platform.h @@ -4,7 +4,7 @@ #include "num_types.h" // Function Pointer decls -typedef void (*Keyboard_Event_Func) (int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift); +typedef void (*Keyboard_Event_Func) (int key, int scancode, int state, int repeat, int mod_ctrl, int mod_shift, int mod_alt); typedef void (*Mousebutton_Event_Func) (int button, int state, int x, int y, int8 num_clicks); typedef void (*Mousemotion_Event_Func) (int x, int y, int xrel, int yrel); typedef void (*Mousewheel_Event_Func) (int x, int y);