Added modifiers to keymaps

dev
shariq 8 years ago
parent a573482f4c
commit 77737dfd3c
  1. 4
      assets/config.cfg
  2. 12
      orgfile.org
  3. 8
      src/array.c
  4. 2
      src/array.h
  5. 6
      src/editor.c
  6. 90
      src/game.c
  7. 48
      src/input.c
  8. 18
      src/input.h
  9. 21
      src/light.c
  10. 4
      src/platform.c
  11. 2
      src/platform.h

@ -1,5 +1,5 @@
msaa_levels: 4 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 render_width: 800
fog_density: 0.0020 fog_density: 0.0020
render_height: 600 render_height: 600
@ -10,4 +10,4 @@ msaa_enabled: true
fog_start_dist: 20.0000 fog_start_dist: 20.0000
debug_draw_enabled: false debug_draw_enabled: false
debug_draw_mode: 0 debug_draw_mode: 0
ambient_light: 0.100, 0.100, 0.120 ambient_light: 0.100, 0.100, 0.100

@ -11,8 +11,8 @@ All the code in this repository is under GPLv3, see LICENSE for more information
** File format specifications ** File format specifications
*** Entity *** Entity
// Comment, Sample entity definition in file, paremeters left out are set to defaults # Comment, Sample entity definition in file, paremeters left out are set to defaults
// Empty line at the end specifies end of entity definition # Empty line at the end specifies end of entity definition
entity: "Something" entity: "Something"
position: 0 0 0 position: 0 0 0
scale: 1 1 1 scale: 1 1 1
@ -36,6 +36,13 @@ msaa: true
msaa_levels: 8 msaa_levels: 8
*** Keybindings *** 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 *** Level/Scene
*** Materials *** Materials
*** Mesh/Geometry *** Mesh/Geometry
@ -98,6 +105,7 @@ msaa_levels: 8
- State "DONE" from "TODO" [2015-10-13 Tue 19:38] - 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 Add modifiers to input maps to enable combinations for example, c-x, m-k etc
** TODO Only allocate hashmap bucket when required ** 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 Heirarchical Transforms
** DONE Materials with textures ** DONE Materials with textures
- State "DONE" from "TODO" [2015-10-15 Thu 21:21] - State "DONE" from "TODO" [2015-10-15 Thu 21:21]

@ -25,11 +25,11 @@ static struct Array* array_get_ptr(void* array_data)
void* array_new_(size_t object_size, int capacity) void* array_new_(size_t object_size, int capacity)
{ {
if(capacity == 0) capacity = ARRAY_MIN_CAPACITY; int initial_capacity = capacity == 0 ? ARRAY_MIN_CAPACITY : capacity;
struct Array* new_array = malloc(sizeof(*new_array) + (object_size * capacity)); struct Array* new_array = malloc(sizeof(*new_array) + (object_size * initial_capacity));
new_array->object_size = object_size; new_array->object_size = object_size;
new_array->length = 0; new_array->length = capacity;
new_array->capacity = capacity; new_array->capacity = initial_capacity;
return new_array->data; return new_array->data;
} }

@ -15,7 +15,7 @@ int array_copy_(void* arr_src, void** arr_dest);
/* Public Api */ /* Public Api */
#define array_new(type) (type*) array_new_(sizeof(type), 0); // Use this for array creation #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_clear(array) array_reset(array, 0)
#define array_grow(array, type) (type*) array_grow_((void**)&array) #define array_grow(array, type) (type*) array_grow_((void**)&array)
#define array_push(array, value, type) {type* new_val = array_grow(array, type); \ #define array_push(array, value, type) {type* new_val = array_grow(array, type); \

@ -50,7 +50,7 @@ void editor_init(void)
editor_state.renderer_settings_window = 0; editor_state.renderer_settings_window = 0;
editor_state.debug_vars_window = 1; editor_state.debug_vars_window = 1;
editor_state.top_panel_height = 30; 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); 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); nk_slider_float(context, 0.f, &render_settings->fog.density, 1.f, 0.005);
if(nk_input_is_mouse_hovering_rect(&context->input, bounds)) 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); snprintf(float_str, 6, "%.4f", render_settings->fog.density);
float_str[5] = '\0'; float_str[6] = '\0';
nk_tooltip(context, float_str); nk_tooltip(context, float_str);
} }

@ -89,36 +89,36 @@ int game_init(struct Window* window)
void scene_setup(void) void scene_setup(void)
{ {
int forward_keys[2] = {KEY_W, KEY_UP}; struct Key_Combination forward_keys[2] = {{KEY_W, KMD_NONE}, {KEY_UP, KMD_ALT}};
int backward_keys[2] = {KEY_S, KEY_DOWN}; struct Key_Combination backward_keys[2] = {{KEY_S, KMD_NONE}, {KEY_DOWN, KMD_NONE}};
int up_keys[2] = {KEY_Q}; /* struct Key_Combination up_keys[2] = {KEY_Q}; */
int down_keys[2] = {KEY_E}; /* struct Key_Combination down_keys[2] = {KEY_E}; */
int left_keys[2] = {KEY_A, KEY_LEFT}; /* struct Key_Combination left_keys[2] = {KEY_A, KEY_LEFT}; */
int right_keys[2] = {KEY_D, KEY_RIGHT}; /* struct Key_Combination right_keys[2] = {KEY_D, KEY_RIGHT}; */
int turn_right_keys[1] = {KEY_L}; /* struct Key_Combination turn_right_keys[1] = {KEY_L}; */
int turn_left_keys[1] = {KEY_J}; /* struct Key_Combination turn_left_keys[1] = {KEY_J}; */
int turn_up_keys[1] = {KEY_I}; /* struct Key_Combination turn_up_keys[1] = {KEY_I}; */
int turn_down_keys[1] = {KEY_K}; /* struct Key_Combination turn_down_keys[1] = {KEY_K}; */
int sprint_keys[2] = {KEY_LSHIFT, KEY_RSHIFT}; /* struct Key_Combination sprint_keys[2] = {KEY_LSHIFT, KEY_RSHIFT}; */
int recompute_keys[2] = {KEY_F5, KEY_H}; /* struct Key_Combination recompute_keys[2] = {KEY_F5, KEY_H}; */
int ed_toggle_keys[1] = {KEY_F1}; /* struct Key_Combination ed_toggle_keys[1] = {KEY_F1}; */
int win_fullscr_keys[1] = {KEY_F11}; /* struct Key_Combination win_fullscr_keys[1] = {KEY_F11}; */
int win_max_keys[1] = {KEY_F12}; /* struct Key_Combination win_max_keys[1] = {KEY_F12}; */
input_map_create("Move_Forward", forward_keys, 2); input_map_create("Move_Forward", forward_keys, 2);
input_map_create("Move_Backward", backward_keys, 2); input_map_create("Move_Backward", backward_keys, 2);
input_map_create("Move_Up", up_keys, 1); /* input_map_create("Move_Up", up_keys, 1); */
input_map_create("Move_Down", down_keys, 1); /* input_map_create("Move_Down", down_keys, 1); */
input_map_create("Move_Left", left_keys, 2); /* input_map_create("Move_Left", left_keys, 2); */
input_map_create("Move_Right", right_keys, 2); /* input_map_create("Move_Right", right_keys, 2); */
input_map_create("Turn_Right", turn_right_keys, 1); /* input_map_create("Turn_Right", turn_right_keys, 1); */
input_map_create("Turn_Left", turn_left_keys, 1); /* input_map_create("Turn_Left", turn_left_keys, 1); */
input_map_create("Turn_Up", turn_up_keys, 1); /* input_map_create("Turn_Up", turn_up_keys, 1); */
input_map_create("Turn_Down", turn_down_keys, 1); /* input_map_create("Turn_Down", turn_down_keys, 1); */
input_map_create("Sprint", sprint_keys, 2); /* input_map_create("Sprint", sprint_keys, 2); */
input_map_create("Recompute", recompute_keys, 2); /* input_map_create("Recompute", recompute_keys, 2); */
input_map_create("Editor_Toggle", ed_toggle_keys, 1); /* input_map_create("Editor_Toggle", ed_toggle_keys, 1); */
input_map_create("Window_Fullscreen", win_fullscr_keys, 1); /* input_map_create("Window_Fullscreen", win_fullscr_keys, 1); */
input_map_create("Window_Maximize", win_max_keys, 1); /* input_map_create("Window_Maximize", win_max_keys, 1); */
struct Entity* player = scene_add_new("player", "None"); struct Entity* player = scene_add_new("player", "None");
game_state->player_node = player->node; game_state->player_node = player->node;
@ -272,16 +272,16 @@ void debug(float dt)
vec3 rot_axis_left_right = {0, 1, 0}; vec3 rot_axis_left_right = {0, 1, 0};
/* Look around */ /* Look around */
if(input_map_state_get("Turn_Up", KS_PRESSED)) turn_up_down += 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_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_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_Left", KS_PRESSED)) turn_left_right -= turn_speed; */
if(input_map_state_get("Recompute", KS_PRESSED)) /* if(input_map_state_get("Recompute", KS_PRESSED)) */
{ /* { */
log_message("Regenerating Bounding Volumes"); /* log_message("Regenerating Bounding Volumes"); */
geom_bounding_volume_generate_all(); /* geom_bounding_volume_generate_all(); */
} /* } */
/* if(input_is_key_pressed(KEY_TAB, KS_PRESSED)) */ /* 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_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 */ /* 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_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_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_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_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_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_Down", KS_PRESSED)) offset.y -= move_speed; */
vec3_scale(&offset, &offset, dt); vec3_scale(&offset, &offset, dt);
if(offset.x != 0 || offset.y != 0 || offset.z != 0) 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) void update(float dt, int* window_should_close)
{ {
if(input_is_key_pressed(KEY_ESCAPE)) *window_should_close = 1; 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("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_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("Window_Maximize", KS_RELEASED)) window_fullscreen_set(game_state->window, 0); */
debug(dt); debug(dt);
//debug_gui(dt); //debug_gui(dt);

@ -7,17 +7,14 @@
#include "log.h" #include "log.h"
#include "gui.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 struct Input_Map
{ {
const char* name; const char* name;
int* keys; struct Key_Combination* keys;
int state; 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_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_mousemotion(int x, int y, int xrel, int yrel);
static void input_on_mousewheel(int x, int y); 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); 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) /* if(repeat) */
{ /* { */
return; /* Ignore key 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++) for(int i = 0; i < array_len(input_map_list); i++)
{ {
struct Input_Map* map = &input_map_list[i]; struct Input_Map* map = &input_map_list[i];
for(int j = 0; j < array_len(map->keys); j++) 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; map->state = state;
break; 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) 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? Do we even need that?
*/ */
/* TODO: This is temporary. After proper event loop is added this code should not be here */ /* 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; 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); assert(name && keys && num_keys > 0);
struct Input_Map* new_map = array_grow(input_map_list, struct Input_Map); struct Input_Map* new_map = array_grow(input_map_list, struct Input_Map);
new_map->name = name; 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; new_map->state = KS_INACTIVE;
for(size_t i = 0; i < num_keys; i++) for(size_t i = 0; i < num_keys; i++)
{ new_map->keys[i] = keys[i];
array_push(new_map->keys, keys[i], int); 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) void input_update(void)
@ -176,7 +185,7 @@ int input_map_remove(const char* name)
return success; 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); assert(name && keys && num_keys > 0);
int success = 0; int success = 0;
@ -184,9 +193,12 @@ int input_map_keys_set(const char* name, int* keys, int num_keys)
if(index > -1) if(index > -1)
{ {
struct Input_Map* map = &input_map_list[index]; struct Input_Map* map = &input_map_list[index];
if(array_len(map->keys) != num_keys)
array_reset(map->keys, num_keys); array_reset(map->keys, num_keys);
for(int i = 0; i < num_keys; i++) for(int i = 0; i < num_keys; i++)
map->keys[i] = keys[i]; map->keys[i] = keys[i];
map->state = KS_INACTIVE;
success = 1; success = 1;
} }
if(!success) if(!success)

@ -7,6 +7,20 @@
#include <SDL2/SDL_mouse.h> #include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
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 enum Key_State
{ {
KS_INACTIVE = -1, KS_INACTIVE = -1,
@ -404,9 +418,9 @@ void input_mouse_mode_set(enum Mouse_Mode mode);
int input_mouse_mode_get(void); int input_mouse_mode_get(void);
void input_update(void); void input_update(void);
int input_map_state_get(const char* map_name, int state); 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_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); int input_map_name_set(const char* name, const char* new_name);
#endif #endif

@ -1,6 +1,8 @@
#include "light.h" #include "light.h"
#include "log.h"
#include "array.h" #include "array.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
static struct Light* light_list; static struct Light* light_list;
static int* empty_indices; static int* empty_indices;
@ -25,8 +27,9 @@ void light_init(void)
max_lights = 128; max_lights = 128;
light_list = array_new_cap(struct Light, max_lights); light_list = array_new_cap(struct Light, max_lights);
for(int i = 0; i < max_lights; i++) light_list[i].valid = 0; 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); valid_light_indices = array_new_cap(int, max_lights);
memset(valid_light_indices, -1, max_lights);
} }
void light_cleanup(void) void light_cleanup(void)
@ -59,8 +62,20 @@ int light_create(int node, int light_type)
} }
else else
{ {
new_light = array_grow(light_list, struct Light); for(index = 0; index < max_lights; index++)
index = array_len(light_list) - 1; {
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->node = node;
new_light->valid = 1; new_light->valid = 1;

@ -227,7 +227,9 @@ void platform_poll_events(int* out_quit)
int repeat = event.key.repeat; int repeat = event.key.repeat;
int mod_ctrl = (event.key.keysym.mod & KMOD_CTRL); int mod_ctrl = (event.key.keysym.mod & KMOD_CTRL);
int mod_shift = (event.key.keysym.mod & KMOD_SHIFT); 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; break;
} }
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:

@ -4,7 +4,7 @@
#include "num_types.h" #include "num_types.h"
// Function Pointer decls // 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 (*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 (*Mousemotion_Event_Func) (int x, int y, int xrel, int yrel);
typedef void (*Mousewheel_Event_Func) (int x, int y); typedef void (*Mousewheel_Event_Func) (int x, int y);

Loading…
Cancel
Save