From: Christian Heller Date: Thu, 23 May 2013 10:04:49 +0000 (+0200) Subject: Moved keybindings manipulation stuff into its own library. X-Git-Tag: tce~1280 X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Bdb.prefix%7D%7D/todo?a=commitdiff_plain;h=122747fbbdc061942de2fc2bca901a44a6b4ab61;p=plomrogue Moved keybindings manipulation stuff into its own library. --- diff --git a/Makefile b/Makefile index 5b7f7d9..72e9113 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,9 @@ roguelike: cc -Wall -g -o windows.o -c windows.c cc -Wall -g -o draw_wins.o -c draw_wins.c + cc -Wall -g -o keybindings.o -c keybindings.c cc -Wall -g -o roguelike.o -c roguelike.c - cc -Wall -g -o roguelike windows.o draw_wins.o roguelike.o -lncurses + cc -Wall -g -o roguelike windows.o draw_wins.o keybindings.o roguelike.o -lncurses clean: rm *.o; rm roguelike diff --git a/draw_wins.c b/draw_wins.c index 3eaf491..9b09593 100644 --- a/draw_wins.c +++ b/draw_wins.c @@ -2,7 +2,9 @@ #include #include #include "windows.h" +#include "draw_wins.h" #include "roguelike.h" +#include "keybindings.h" void draw_with_linebreaks (struct Win * win, char * text, int start_y) { // Write text into window content space. Start on row start_y. Fill unused rows with whitespace. diff --git a/keybindings.c b/keybindings.c new file mode 100644 index 0000000..1906d1d --- /dev/null +++ b/keybindings.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include "windows.h" +#include "roguelike.h" +#include "keybindings.h" + +void init_keybindings(struct World * world) { +// Initialize keybindings from file "keybindings". + FILE * file = fopen("keybindings", "r"); + int lines = 0; + int c = 0; + int linemax = 0; + int c_count = 0; + while (EOF != c) { + c_count++; + c = getc(file); + if ('\n' == c) { + if (c_count > linemax) + linemax = c_count + 1; + c_count = 0; + lines++; } } + struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); + fseek(file, 0, SEEK_SET); + char * command = malloc(linemax); + int commcount = 0; + char * cmdptr; + while (fgets(command, linemax, file)) { + keybindings[commcount].key = atoi(command); + cmdptr = strchr(command, ' ') + 1; + keybindings[commcount].name = malloc(strlen(cmdptr)); + memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1); + keybindings[commcount].name[strlen(cmdptr) - 1] = '\0'; + commcount++; } + free(command); + fclose(file); + struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData)); + keyswindata->max = lines - 1; + keyswindata->select = 0; + keyswindata->edit = 0; + world->keybindings = keybindings; + world->keyswindata = keyswindata; } + +void save_keybindings(struct World * world) { +// Write keybindings to keybindings file. + struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata; + struct KeyBinding * keybindings = world->keybindings; + FILE * file = fopen("keybindings", "w"); + int linemax = 0; + int i; + for (i = 0; i <= keyswindata->max; i++) + if (strlen(keybindings[i].name) > linemax) + linemax = strlen(keybindings[i].name); + linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte + char * line = malloc(linemax); + for (i = 0; i <= keyswindata->max; i++) { + snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name); + fwrite(line, sizeof(char), strlen(line), file); } + free(line); + fclose(file); } + +int get_action_key (struct KeyBinding * keybindings, char * name) { +// Return key matching name in keybindings. + int i = 0; + while (strcmp(keybindings[i].name, name) ) + i++; + return keybindings[i].key; } + +char * get_keyname(int keycode) { +// Translate some keycodes to readable names of max 9 chars. + char * keyname; + keyname = malloc(15); + if (32 < keycode && keycode < 127) + sprintf(keyname, "%c", keycode); + else if (keycode == 9) + sprintf(keyname, "TAB"); + else if (keycode == 10) + sprintf(keyname, "RETURN"); + else if (keycode == 27) + sprintf(keyname, "ESCAPE"); + else if (keycode == 32) + sprintf(keyname, "SPACE"); + else if (keycode == KEY_UP) + sprintf(keyname, "UP"); + else if (keycode == KEY_DOWN) + sprintf(keyname, "DOWN"); + else if (keycode == KEY_LEFT) + sprintf(keyname, "LEFT"); + else if (keycode == KEY_RIGHT) + sprintf(keyname, "RIGHT"); + else if (keycode == KEY_HOME) + sprintf(keyname, "HOME"); + else if (keycode == KEY_BACKSPACE) + sprintf(keyname, "BACKSPACE"); + else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) { + int f = keycode - KEY_F0; + sprintf(keyname, "F%d", f); } + else if (keycode == KEY_DC) + sprintf(keyname, "DELETE"); + else if (keycode == KEY_IC) + sprintf(keyname, "INSERT"); + else if (keycode == KEY_NPAGE) + sprintf(keyname, "NEXT PAGE"); + else if (keycode == KEY_PPAGE) + sprintf(keyname, "PREV PAGE"); + else if (keycode == KEY_END) + sprintf(keyname, "END"); + else + sprintf(keyname, "(unknown)"); + return keyname; } + +void keyswin_mod_key (struct World * world, struct WinMeta * win_meta) { +// In keybindings window, mark selection modifiable, modify key. Ensure max of three digits in key code field. + world->keyswindata->edit = 1; + draw_all_windows (win_meta); + int key = getch(); + if (key < 1000) + world->keybindings[world->keyswindata->select].key = key; + world->keyswindata->edit = 0; } + +void keyswin_move_selection (struct World * world, char dir) { +// In keybindings window, move selection upwards or downwards (if within limits of list length). + if ('u' == dir && world->keyswindata->select > 0) + world->keyswindata->select--; + else if ('d' == dir && world->keyswindata->select < world->keyswindata->max) + world->keyswindata->select++; } diff --git a/keybindings.h b/keybindings.h new file mode 100644 index 0000000..3295f2d --- /dev/null +++ b/keybindings.h @@ -0,0 +1,15 @@ +struct KeyBinding { + char * name; + int key; }; + +struct KeysWinData { + int max; + char edit; + int select; }; + +void init_keybindings(struct World *); +void save_keybindings(struct World *); +int get_action_key (struct KeyBinding *, char *); +char * get_keyname(int); +void keyswin_mod_key (struct World *, struct WinMeta *); +void keyswin_move_selection (struct World *, char); diff --git a/roguelike.c b/roguelike.c index 8235d7c..20181be 100644 --- a/roguelike.c +++ b/roguelike.c @@ -4,6 +4,7 @@ #include "windows.h" #include "draw_wins.h" #include "roguelike.h" +#include "keybindings.h" void toggle_window (struct WinMeta * win_meta, struct Win * win) { // Toggle display of window win. @@ -27,42 +28,6 @@ void growshrink_active_window (struct WinMeta * win_meta, char change) { width++; resize_active_window (win_meta, height, width); } } -void init_keybindings(struct World * world) { -// Initialize keybindings from file "keybindings". - FILE * file = fopen("keybindings", "r"); - int lines = 0; - int c = 0; - int linemax = 0; - int c_count = 0; - while (EOF != c) { - c_count++; - c = getc(file); - if ('\n' == c) { - if (c_count > linemax) - linemax = c_count + 1; - c_count = 0; - lines++; } } - struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding)); - fseek(file, 0, SEEK_SET); - char * command = malloc(linemax); - int commcount = 0; - char * cmdptr; - while (fgets(command, linemax, file)) { - keybindings[commcount].key = atoi(command); - cmdptr = strchr(command, ' ') + 1; - keybindings[commcount].name = malloc(strlen(cmdptr)); - memcpy(keybindings[commcount].name, cmdptr, strlen(cmdptr) - 1); - keybindings[commcount].name[strlen(cmdptr) - 1] = '\0'; - commcount++; } - free(command); - fclose(file); - struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData)); - keyswindata->max = lines - 1; - keyswindata->select = 0; - keyswindata->edit = 0; - world->keybindings = keybindings; - world->keyswindata = keyswindata; } - struct Map init_map () { // Initialize map with some experimental start values. struct Map map; @@ -128,90 +93,6 @@ void update_log (struct World * world, char * text) { free(world->log); world->log = new_text; } -void save_keybindings(struct World * world) { -// Write keybindings to keybindings file. - struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata; - struct KeyBinding * keybindings = world->keybindings; - FILE * file = fopen("keybindings", "w"); - int linemax = 0; - int i; - for (i = 0; i <= keyswindata->max; i++) - if (strlen(keybindings[i].name) > linemax) - linemax = strlen(keybindings[i].name); - linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte - char * line = malloc(linemax); - for (i = 0; i <= keyswindata->max; i++) { - snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name); - fwrite(line, sizeof(char), strlen(line), file); } - free(line); - fclose(file); } - -int get_action_key (struct KeyBinding * keybindings, char * name) { -// Return key matching name in keybindings. - int i = 0; - while (strcmp(keybindings[i].name, name) ) - i++; - return keybindings[i].key; } - -char * get_keyname(int keycode) { -// Translate some keycodes to readable names of max 9 chars. - char * keyname; - keyname = malloc(15); - if (32 < keycode && keycode < 127) - sprintf(keyname, "%c", keycode); - else if (keycode == 9) - sprintf(keyname, "TAB"); - else if (keycode == 10) - sprintf(keyname, "RETURN"); - else if (keycode == 27) - sprintf(keyname, "ESCAPE"); - else if (keycode == 32) - sprintf(keyname, "SPACE"); - else if (keycode == KEY_UP) - sprintf(keyname, "UP"); - else if (keycode == KEY_DOWN) - sprintf(keyname, "DOWN"); - else if (keycode == KEY_LEFT) - sprintf(keyname, "LEFT"); - else if (keycode == KEY_RIGHT) - sprintf(keyname, "RIGHT"); - else if (keycode == KEY_HOME) - sprintf(keyname, "HOME"); - else if (keycode == KEY_BACKSPACE) - sprintf(keyname, "BACKSPACE"); - else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) { - int f = keycode - KEY_F0; - sprintf(keyname, "F%d", f); } - else if (keycode == KEY_DC) - sprintf(keyname, "DELETE"); - else if (keycode == KEY_IC) - sprintf(keyname, "INSERT"); - else if (keycode == KEY_NPAGE) - sprintf(keyname, "NEXT PAGE"); - else if (keycode == KEY_PPAGE) - sprintf(keyname, "PREV PAGE"); - else if (keycode == KEY_END) - sprintf(keyname, "END"); - else - sprintf(keyname, "(unknown)"); - return keyname; } - -void keyswin_mod_key (struct World * world, struct WinMeta * win_meta) { -// In keybindings window, mark selection modifiable, modify key. Ensure max of three digits in key code field. - world->keyswindata->edit = 1; - draw_all_windows (win_meta); - int key = getch(); - if (key < 1000) - world->keybindings[world->keyswindata->select].key = key; - world->keyswindata->edit = 0; } - -void keyswin_move_selection (struct World * world, char dir) { -// In keybindings window, move selection upwards or downwards (if within limits of list length). - if ('u' == dir && world->keyswindata->select > 0) - world->keyswindata->select--; - else if ('d' == dir && world->keyswindata->select < world->keyswindata->max) - world->keyswindata->select++; } - char is_passable (struct World * world, int x, int y) { // Check if coordinate on (or beyond) map is accessible to movement. char passable = 0; diff --git a/roguelike.h b/roguelike.h new file mode 100644 index 0000000..da80489 --- /dev/null +++ b/roguelike.h @@ -0,0 +1,33 @@ +struct World { + struct KeyBinding * keybindings; + struct KeysWinData * keyswindata; + int turn; + char * log; + struct Map * map; + struct Monster * monster; + struct Player * player; }; + +struct Map { + int width; + int height; + int offset_x; + int offset_y; + char * cells; }; + +struct Player { + int y; + int x; }; + +struct Monster { + int y; + int x; }; + +void toggle_window (struct WinMeta *, struct Win *); +void growshrink_active_window (struct WinMeta *, char); +struct Map init_map (); +void map_scroll (struct Map *, char); +void next_turn (struct World *); +void update_log (struct World *, char *); +char is_passable (struct World *, int, int); +void move_player (struct World *, char); +void player_wait(struct World *);