Added saving input maps to file

dev
shariq 8 years ago
parent 529629d32b
commit bd96f9ab45
  1. 11
      assets/keybindings.cfg
  2. 8
      src/game.c
  3. 40
      src/input.c
  4. 2
      src/input.h
  5. 8
      src/platform.c
  6. 1
      src/platform.h

@ -1,6 +1,7 @@
# This is a comment, this should be ignored
Move_Forward: E
Move_Backward: S,Down
Move_Left: A,Left
Move_Right: D,Right
Sprint: Left Shift
Move_Forward : Left Ctrl-E, W, Left Alt-Left Shift-Up
Move_Backward : S, Down, S, Down
Move_Left : A, Left
Move_Right : D, Right
Sprint : Left Shift

@ -89,8 +89,8 @@ int game_init(struct Window* window)
void scene_setup(void)
{
struct Key_Combination forward_keys[2] = {{KEY_W, KMD_NONE}, {KEY_UP, KMD_ALT | KMD_SHIFT}};
struct Key_Combination backward_keys[2] = {{KEY_S, KMD_NONE}, {KEY_DOWN, KMD_NONE}};
/* struct Key_Combination forward_keys[2] = {{KEY_W, KMD_NONE}, {KEY_UP, KMD_ALT | KMD_SHIFT}}; */
/* 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}; */
@ -104,8 +104,8 @@ void scene_setup(void)
/* 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_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); */

@ -22,7 +22,7 @@ static void input_on_mousemotion(int x, int y, int xrel, int yrel);
static void input_on_mousewheel(int x, int y);
static int map_find(const char* name);
static struct Input_Map* input_map_list;
static struct Input_Map* input_map_list = NULL;
void input_init(void)
{
@ -37,6 +37,7 @@ void input_init(void)
void input_cleanup(void)
{
input_keybinds_save("keybindings_save_test.cfg");
for(int i = 0; i < array_len(input_map_list); i++)
{
struct Input_Map* map = &input_map_list[i];
@ -55,7 +56,7 @@ int input_keybinds_load(const char* filename)
FILE* config_file = io_file_open(filename, "r");
if(!config_file)
{
log_error("input:load", "Could not open %s", filename);
log_error("input:keybinds_load", "Could not open %s", filename);
return success;
}
@ -164,7 +165,35 @@ int input_keybinds_load(const char* filename)
int input_keybinds_save(const char* filename)
{
int success = 0;
FILE* config_file = io_file_open(filename, "w");
if(!config_file)
{
log_error("input:keybinds_save", "Could not open %s", filename);
return success;
}
for(int i = 0; i < array_len(input_map_list); i++)
{
struct Input_Map* map = &input_map_list[i];
fprintf(config_file, "%s : ", map->name);
for(int j = 0; j < array_len(map->keys); j++)
{
if(j != 0) fprintf(config_file, ", ");
struct Key_Combination* key_comb = &map->keys[j];
if((key_comb->mods & KMD_ALT) == KMD_ALT) fprintf(config_file, "%s-", platform_key_name_get(KEY_LALT));
if((key_comb->mods & KMD_SHIFT) == KMD_SHIFT) fprintf(config_file, "%s-", platform_key_name_get(KEY_LSHIFT));
if((key_comb->mods & KMD_CTRL) == KMD_CTRL) fprintf(config_file, "%s-", platform_key_name_get(KEY_LCTRL));
fprintf(config_file, "%s", platform_key_name_get(key_comb->key));
}
fprintf(config_file, "\n");
}
fclose(config_file);
log_message("Keybindings saved to %s", filename);
success = 1;
return success;
}
void input_on_mousemotion(int x, int y, int xrel, int yrel)
@ -269,17 +298,20 @@ int input_mousebutton_state_get(uint button, int state_type)
return state_type == current_state ? 1 : 0;
}
void input_map_create(const char* name, struct Key_Combination* keys, size_t num_keys)
void input_map_create(const char* name, struct Key_Combination* keys, int num_keys)
{
assert(name && keys && num_keys > 0);
int index = map_find(name);
if(index > -1)
{
struct Input_Map* map = &input_map_list[index];
for(int i = 0; i < num_keys; i++)
{
struct Key_Combination* new_comb = array_grow(map->keys, struct Key_Combination);
*new_comb = *keys;
*new_comb = keys[i];
log_message("Added new Key combination to input map : %s", name);
}
}
else
{
struct Input_Map* new_map = array_grow(input_map_list, struct Input_Map);

@ -422,7 +422,7 @@ 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, struct Key_Combination* keys, size_t num_keys);
void input_map_create(const char* name, struct Key_Combination* keys, int 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_name_set(const char* name, const char* new_name);

@ -229,7 +229,7 @@ void platform_poll_events(int* out_quit)
int mod_shift = (event.key.keysym.mod & KMOD_SHIFT) ? 1 : 0;
int mod_alt = (event.key.keysym.mod & KMOD_ALT) ? 1 : 0;
platform_state->on_keyboard_func(key, scancode, state, repeat, mod_ctrl, mod_shift, mod_alt);
//log_message("Key name : %s", SDL_GetKeyName(key));
log_message("Key name : %s", SDL_GetKeyName(key));
break;
}
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
@ -402,3 +402,9 @@ int platform_key_from_name(const char* key_name)
log_message("trimmed : %s", trimmed_key_name);
return SDL_GetKeyFromName(trimmed_key_name);
}
const char* platform_key_name_get(int key)
{
if(key < 0) return "SDLK_UNKNOWN";
return SDL_GetKeyName(key);
}

@ -50,5 +50,6 @@ char* platform_base_path_get(void);
void platform_clipboard_text_set(const char* text);
char* platform_clipboard_text_get(void);
int platform_key_from_name(const char* key_name);
const char* platform_key_name_get(int key);
#endif

Loading…
Cancel
Save