29 struct KeyBinding * keybindings;
30 struct KeysWinData * keyswindata; };
32 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
33 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
38 for (y = start_y; y < win->height; y++) {
41 for (x = 0; x < win->width; x++) {
44 if ('\n' == text[z]) {
48 mvwaddch(win->curses_win, y, x, text[z]);
49 if ('\n' == text[z+1]) {
52 else if (0 == text[z+1]) {
56 void draw_text_from_bottom (struct Win * win, char * text) {
57 // Draw text from end/bottom to the top.
61 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in a window of
62 for (x = 0; x < win->width; x++) { // available width, but infinite height.
64 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and
65 break; // stopping the loop. Make sure they don't count as cell space
66 if ('\n' == text[z+1]) { // themselves.
69 else if (0 == text[z+1]) {
74 if (y < win->height) // Depending on what is bigger, determine start point in window or in text.
75 start_y = win->height - y;
76 else if (y > win->height) {
77 offset = y - win->height;
78 for (y = 0; y < offset; y++)
79 for (x = 0; x < win->width; x++) {
83 if ('\n' == text[z+1]) {
86 text = text + (sizeof(char) * (z + 1)); }
87 draw_with_linebreaks(win, text, start_y); }
89 void draw_log (struct Win * win) {
90 // Draw log text from world struct in win->data from bottom to top.
91 struct World world = * (struct World *) win->data;
92 draw_text_from_bottom(win, world.log); }
94 void draw_map (struct Win * win) {
95 // Draw map determined by win->data Map struct into window. Respect offset.
96 struct Map map = * (struct Map *) win->data;
97 char * cells = map.cells;
98 int width_map_av = map.width - map.offset_x;
99 int height_map_av = map.height - map.offset_y;
101 for (y = 0; y < win->height; y++) {
102 z = map.offset_x + (map.offset_y + y) * (map.width);
103 for (x = 0; x < win->width; x++) {
104 if (y < height_map_av && x < width_map_av) {
105 if (z == (map.width * map.player_y) + map.player_x)
106 mvwaddch(win->curses_win, y, x, '@');
108 mvwaddch(win->curses_win, y, x, cells[z]);
111 void draw_info (struct Win * win) {
112 // Draw info window by appending win->data integer value to "Turn: " display.
113 struct World world = * (struct World *) win->data;
114 int count = world.turn;
116 snprintf(text, 100, "Turn: %d", count);
117 draw_with_linebreaks(win, text, 0); }
119 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
120 // Toggle display of window win.
121 if (0 != win->curses_win)
122 suspend_window(win_meta, win);
124 append_window(win_meta, win); }
126 struct Map init_map () {
127 // Initialize map with some experimental start values.
135 map.cells = malloc(map.width * map.height);
137 for (y = 0; y < map.height; y++)
138 for (x = 0; x < map.width; x++)
139 map.cells[(y * map.width) + x] = '.';
140 map.cells[(5 * map.width) + 5] = 'X';
141 map.cells[(3 * map.width) + 8] = 'X';
142 map.cells[(8 * map.width) + 3] = 'X';
145 void update_info (struct World * world) {
146 // Update info data by incrementing turn value.
149 void update_log (struct World * world, char * text) {
150 // Update log with new text to be appended.
152 int len_old = strlen(world->log);
153 int len_new = strlen(text);
154 int len_whole = len_old + len_new + 1;
155 new_text = calloc(len_whole, sizeof(char));
156 memcpy(new_text, world->log, len_old);
157 memcpy(new_text + len_old, text, len_new);
159 world->log = new_text; }
161 int get_action_key (struct KeyBinding * keybindings, char * name) {
162 // Return key matching name in keybindings.
164 while (strcmp(keybindings[i].name, name) )
166 return keybindings[i].key; }
168 char * get_keyname(int keycode) {
169 // Translate some keycodes to readable names of max 9 chars.
171 keyname = malloc(15);
172 if (32 < keycode && keycode < 127)
173 sprintf(keyname, "%c", keycode);
174 else if (keycode == 9)
175 sprintf(keyname, "TAB");
176 else if (keycode == 10)
177 sprintf(keyname, "RETURN");
178 else if (keycode == 27)
179 sprintf(keyname, "ESCAPE");
180 else if (keycode == 32)
181 sprintf(keyname, "SPACE");
182 else if (keycode == KEY_UP)
183 sprintf(keyname, "UP");
184 else if (keycode == KEY_DOWN)
185 sprintf(keyname, "DOWN");
186 else if (keycode == KEY_LEFT)
187 sprintf(keyname, "LEFT");
188 else if (keycode == KEY_RIGHT)
189 sprintf(keyname, "RIGHT");
190 else if (keycode == KEY_HOME)
191 sprintf(keyname, "HOME");
192 else if (keycode == KEY_BACKSPACE)
193 sprintf(keyname, "BACKSPACE");
194 else if (keycode >= KEY_F0 && keycode <= KEY_F(63)) {
195 int f = keycode - KEY_F0;
196 sprintf(keyname, "F%d", f); }
197 else if (keycode == KEY_DC)
198 sprintf(keyname, "DELETE");
199 else if (keycode == KEY_IC)
200 sprintf(keyname, "INSERT");
201 else if (keycode == KEY_NPAGE)
202 sprintf(keyname, "NEXT PAGE");
203 else if (keycode == KEY_PPAGE)
204 sprintf(keyname, "PREV PAGE");
205 else if (keycode == KEY_END)
206 sprintf(keyname, "END");
208 sprintf(keyname, "(unknown)");
211 void draw_keys_window (struct Win * win) {
212 // Draw keybinding window.
213 struct World * world = (struct World *) win->data;
214 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
215 struct KeyBinding * keybindings = world->keybindings;
217 if (keyswindata->max >= win->height) {
218 if (keyswindata->select > win->height / 2) {
219 if (keyswindata->select < (keyswindata->max - (win->height / 2)))
220 offset = keyswindata->select - (win->height / 2);
222 offset = keyswindata->max - win->height + 1; } }
223 int keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
224 char * keydesc = malloc(keydescwidth);
228 for (y = 0; y <= keyswindata->max && y < win->height; y++) {
230 if (y == keyswindata->select - offset) {
232 if (1 == keyswindata->edit)
233 attri = attri | A_BLINK; }
234 keyname = get_keyname(keybindings[y + offset].key);
235 snprintf(keydesc, keydescwidth, "%-9s", keyname);
237 for (x = 0; x < win->width; x++)
238 if (x < strlen(keydesc))
239 mvwaddch(win->curses_win, y, x, keydesc[x] | attri);
240 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
241 mvwaddch(win->curses_win, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
243 mvwaddch(win->curses_win, y, x, ' ' | attri); }
246 void init_keybindings(struct World * world) {
247 // Initialize keybindings from file "keybindings".
248 FILE * file = fopen("keybindings", "r");
257 if (c_count > linemax)
261 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
262 fseek(file, 0, SEEK_SET);
263 char * command = malloc(linemax);
265 char * digits = malloc(3);
272 keybindings[keycount].name = calloc(commcount + 1, sizeof(char));
273 memcpy(keybindings[keycount].name, command, commcount);
274 keybindings[keycount].key = key;
278 else if (-1 != digicount) {
281 digimax = digicount - 1;
282 while (digicount > 0) {
284 key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
287 digits[digicount] = c;
290 command[commcount] = c;
296 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
297 keyswindata->max = lines - 1;
298 keyswindata->select = 0;
299 keyswindata->edit = 0;
300 world->keybindings = keybindings;
301 world->keyswindata = keyswindata; }
303 void save_keybindings(struct World * world) {
304 // Write keybindings to keybindings file.
305 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
306 struct KeyBinding * keybindings = world->keybindings;
307 FILE * file = fopen("keybindings", "w");
310 for (i = 0; i <= keyswindata->max; i++)
311 if (strlen(keybindings[i].name) > linemax)
312 linemax = strlen(keybindings[i].name);
313 linemax = linemax + 6; // + 6 = + 3 digits + whitespace + newline + null byte
314 char * line = malloc(linemax);
315 for (i = 0; i <= keyswindata->max; i++) {
316 snprintf(line, linemax, "%d %s\n", keybindings[i].key, keybindings[i].name);
317 fwrite(line, sizeof(char), strlen(line), file); }
323 init_keybindings(&world);
325 WINDOW * screen = initscr();
328 keypad(screen, TRUE);
330 struct WinMeta win_meta = init_win_meta(screen);
332 struct Win win_keys = init_window(&win_meta, "Keys");
333 win_keys.draw = draw_keys_window;
334 win_keys.data = &world;
336 struct Win win_map = init_window(&win_meta, "Map");
337 win_map.draw = draw_map;
338 struct Map map = init_map();
342 struct Win win_info = init_window(&win_meta, "Info");
343 win_info.draw = draw_info;
344 win_info.data = &world;
346 world.log = calloc(1, sizeof(char));
347 struct Win win_log = init_window(&win_meta, "Log");
348 win_log.draw = draw_log;
349 win_log.data = &world;
350 update_log (&world, "Start!");
354 draw_all_windows (&win_meta);
356 if (key == get_action_key(world.keybindings, "quit"))
358 else if (key == get_action_key(world.keybindings, "scroll pad right"))
359 win_meta.pad_offset++;
360 else if (key == get_action_key(world.keybindings, "scroll pad left") && win_meta.pad_offset > 0)
361 win_meta.pad_offset--;
362 else if (key == get_action_key(world.keybindings, "toggle keys window"))
363 toggle_window(&win_meta, &win_keys);
364 else if (key == get_action_key(world.keybindings, "toggle map window"))
365 toggle_window(&win_meta, &win_map);
366 else if (key == get_action_key(world.keybindings, "toggle info window"))
367 toggle_window(&win_meta, &win_info);
368 else if (key == get_action_key(world.keybindings, "toggle log window"))
369 toggle_window(&win_meta, &win_log);
370 else if (key == get_action_key(world.keybindings, "cycle forwards") && win_meta.active != 0)
371 cycle_active_window(&win_meta, 'n');
372 else if (key == get_action_key(world.keybindings, "cycle backwards") && win_meta.active != 0)
373 cycle_active_window(&win_meta, 'p');
374 else if (key == get_action_key(world.keybindings, "shift forwards") && win_meta.active != 0)
375 shift_window(&win_meta, 'f');
376 else if (key == get_action_key(world.keybindings, "shift backwards") && win_meta.active != 0)
377 shift_window(&win_meta, 'b');
378 else if (key == get_action_key(world.keybindings, "grow horizontally") && win_meta.active != 0)
379 resize_window(&win_meta, '*');
380 else if (key == get_action_key(world.keybindings, "shrink horizontally") && win_meta.active != 0)
381 resize_window(&win_meta, '_');
382 else if (key == get_action_key(world.keybindings, "grow vertically") && win_meta.active != 0)
383 resize_window(&win_meta, '+');
384 else if (key == get_action_key(world.keybindings, "shrink vertically") && win_meta.active != 0)
385 resize_window(&win_meta, '-');
386 else if (key == get_action_key(world.keybindings, "save keys"))
387 save_keybindings(&world);
388 else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0)
389 world.keyswindata->select--;
390 else if (key == get_action_key(world.keybindings, "keys nav down") && world.keyswindata->select < world.keyswindata->max)
391 world.keyswindata->select++;
392 else if (key == get_action_key(world.keybindings, "keys mod")) {
393 world.keyswindata->edit = 1;
394 draw_all_windows (&win_meta);
396 if (key < 1000) // ensure maximum of three digits in key code field
397 world.keybindings[world.keyswindata->select].key = key;
398 world.keyswindata->edit = 0; }
399 else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0)
401 else if (key == get_action_key(world.keybindings, "map down"))
403 else if (key == get_action_key(world.keybindings, "map right"))
405 else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0)
407 else if (key == get_action_key(world.keybindings, "player down") && map.player_y < map.height - 1) {
408 update_info (&world);
409 update_log (&world, "\nYou move south.");
411 else if (key == get_action_key(world.keybindings, "player up") && map.player_y > 0) {
412 update_info (&world);
413 update_log (&world, "\nYou move north.");
415 else if (key == get_action_key(world.keybindings, "player right") && map.player_x < map.width - 1) {
416 update_info (&world);
417 update_log (&world, "\nYou move east.");
419 else if (key == get_action_key(world.keybindings, "player left") && map.player_x > 0) {
420 update_info (&world);
421 update_log (&world, "\nYou move west.");
423 else if (key == get_action_key(world.keybindings, "wait") ) {
424 update_info (&world);
425 update_log (&world, "\nYou wait."); } }
428 for (key = 0; key <= world.keyswindata->max; key++)
429 free(world.keybindings[key].name);
430 free(world.keybindings);
431 free(world.keyswindata);