27 struct KeyBinding * keybindings;
28 struct KeysWinData * keyswindata; };
30 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
31 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
34 int height_av = win->height - 1;
35 int width_av = win->width - 1;
38 for (y = start_y; y < height_av; y++) {
41 for (x = 0; x < width_av; x++) {
44 if ('\n' == text[z]) {
45 mvwaddch(win->curses_win, y+1, x+win->border_left, ' ');
49 mvwaddch(win->curses_win, y+1, x+win->border_left, text[z]);
50 if ('\n' == text[z+1]) {
53 else if (0 == text[z+1]) {
57 mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
59 void draw_text_from_bottom (struct Win * win) {
60 // Draw text in win->data from end/bottom to the top.
61 char * text = (char *) win->data;
62 int width_av = win->width - 1;
63 int height_av = win->height - 1;
67 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in a window of available
68 for (x = 0; x < width_av; x++) { // width, but infinite height.
70 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and
71 break; // stopping the loop. Make sure they don't count as cell space
72 if ('\n' == text[z+1]) { // themselves.
75 else if (0 == text[z+1]) {
80 if (y < height_av) { // Depending on what is bigger, determine start point in window or in text.
81 start_y = height_av - y;
82 for (y = 0; y < start_y; y++)
83 for (x = 0; x < width_av; x++)
84 mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); }
85 else if (y > height_av) {
86 offset = y - height_av;
87 for (y = 0; y < offset; y++)
88 for (x = 0; x < width_av; x++) {
92 if ('\n' == text[z+1]) {
95 text = text + (sizeof(char) * (z + 1)); }
96 draw_with_linebreaks(win, text, start_y); }
98 void draw_map (struct Win * win) {
99 // Draw map determined by win->data Map struct into window. Respect offset.
100 int height_av = win->height - 1;
101 int width_av = win->width - 1;
102 struct Map map = * (struct Map *) win->data;
103 char * cells = map.cells;
104 int width_map_av = map.width - map.offset_x;
105 int height_map_av = map.height - map.offset_y;
107 for (y = 0; y < height_av; y++) {
108 z = map.offset_x + (map.offset_y + y) * (map.width);
109 for (x = 0; x < width_av; x++) {
110 if (y < height_map_av && x < width_map_av) {
111 if (z == (map.width * map.player_y) + map.player_x)
112 mvwaddch(win->curses_win, y+1, x+win->border_left, '@');
114 mvwaddch(win->curses_win, y+1, x+win->border_left, cells[z]);
117 mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
119 void draw_info (struct Win * win) {
120 // Draw info window by appending win->data integer value to "Turn: " display.
121 int count = * (int *) win->data;
123 snprintf(text, 100, "Turn: %d", count);
124 draw_with_linebreaks(win, text, 0); }
126 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
127 // Toggle display of window win.
128 if (0 != win->curses_win)
129 suspend_window(win_meta, win);
131 append_window(win_meta, win); }
133 struct Map init_map () {
134 // Initialize map with some experimental start values.
142 map.cells = malloc(map.width * map.height);
144 for (y = 0; y < map.height; y++)
145 for (x = 0; x < map.width; x++)
146 map.cells[(y * map.width) + x] = '.';
147 map.cells[(5 * map.width) + 5] = 'X';
148 map.cells[(3 * map.width) + 8] = 'X';
149 map.cells[(8 * map.width) + 3] = 'X';
152 void update_info (struct Win * win) {
153 // Update info data by incrementing turn value.
154 * (int *) win->data = * (int *) win->data + 1; }
156 void update_log (struct Win * win, char * text) {
157 // Update log with new text to be appended.
159 int len_old = strlen(win->data);
160 int len_new = strlen(text);
161 int len_whole = len_old + len_new + 1;
162 new_text = calloc(len_whole, sizeof(char));
163 memcpy(new_text, win->data, len_old);
164 memcpy(new_text + len_old, text, len_new);
166 win->data = new_text; }
168 int get_action_key (struct KeyBinding * keybindings, char * name) {
169 // Return key matching name in keybindings.
171 while (strcmp(keybindings[i].name, name) )
173 return keybindings[i].key; }
175 void draw_keys_window (struct Win * win) {
176 // Draw keybinding window.
177 struct World * world = (struct World *) win->data;
178 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
179 struct KeyBinding * keybindings = world->keybindings;
180 int height_av = win->height - 1;
181 int width_av = win->width - 1;
183 if (keyswindata->max >= height_av) {
184 if (keyswindata->select > height_av / 2)
185 if (keyswindata->select < (keyswindata->max - (height_av / 2)))
186 offset = keyswindata->select - (height_av / 2);
188 offset = keyswindata->max - height_av + 1; }
189 int keydescwidth = 3;
190 char * keydesc = malloc(keydescwidth + 1);
193 for (y = 0; 0 != keybindings[offset + y].name && y < height_av; y++) {
195 if (y == keyswindata->select - offset) {
197 if (1 == keyswindata->edit)
198 attri = attri | A_BLINK; }
199 snprintf(keydesc, keydescwidth + 1, "%3d ", keybindings[y + offset].key);
200 for (x = 0; x < width_av; x++)
201 if (strlen(keydesc) > x)
202 mvwaddch(win->curses_win, y + 1, x + win->border_left, keydesc[x] | attri);
203 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
204 mvwaddch(win->curses_win, y + 1, x + win->border_left, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
206 mvwaddch(win->curses_win, y + 1, x + win->border_left, ' ' | attri); }
209 for (; y < height_av; y++)
210 for (x = 0; x < width_av; x++)
211 mvwaddch(win->curses_win, y + 1, x + win->border_left, ' '); }
213 void init_keybindings(struct World * world) {
214 // Initialize keybindings from file "keybindings".
215 FILE * file = fopen("keybindings", "r");
222 struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
223 fseek(file, 0, SEEK_SET);
224 char * command = malloc(100);
226 char * digits = malloc(10);
233 keybindings[keycount].name = calloc(commcount, sizeof(char));
234 memcpy(keybindings[keycount].name, command, commcount);
235 keybindings[keycount].key = key;
239 else if (-1 != digicount)
242 digimax = digicount - 1;
243 while (digicount > 0) {
245 key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
248 digits[digicount] = c;
251 command[commcount] = c;
257 struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
258 keyswindata->max = lines - 1;
259 keyswindata->select = 0;
260 keyswindata->edit = 0;
261 world->keybindings = keybindings;
262 world->keyswindata = keyswindata; }
266 init_keybindings(&world);
268 WINDOW * screen = initscr();
271 keypad(screen, TRUE);
274 struct WinMeta win_meta = init_win_meta(screen);
276 struct Win win_keys = init_window(&win_meta, "Keys");
277 win_keys.draw = draw_keys_window;
278 win_keys.data = &world;
280 struct Win win_map = init_window(&win_meta, "Map");
281 win_map.draw = draw_map;
282 struct Map map = init_map();
285 struct Win win_info = init_window(&win_meta, "Info");
286 win_info.draw = draw_info;
287 win_info.data = malloc(sizeof(int));
288 * (int *) win_info.data = 0;
290 struct Win win_log = init_window(&win_meta, "Log");
291 win_log.draw = draw_text_from_bottom;
292 win_log.data = calloc(1, sizeof(char));
293 update_log (&win_log, "Start!");
298 if (key == get_action_key(world.keybindings, "quit"))
300 else if (key == get_action_key(world.keybindings, "toggle keys window"))
301 toggle_window(&win_meta, &win_keys);
302 else if (key == get_action_key(world.keybindings, "toggle map window"))
303 toggle_window(&win_meta, &win_map);
304 else if (key == get_action_key(world.keybindings, "toggle info window"))
305 toggle_window(&win_meta, &win_info);
306 else if (key == get_action_key(world.keybindings, "toggle log window"))
307 toggle_window(&win_meta, &win_log);
308 else if (key == get_action_key(world.keybindings, "cycle forwards") && win_meta.active != 0)
309 cycle_active_window(&win_meta, 'n');
310 else if (key == get_action_key(world.keybindings, "cycle backwards") && win_meta.active != 0)
311 cycle_active_window(&win_meta, 'p');
312 else if (key == get_action_key(world.keybindings, "shift forwards") && win_meta.active != 0)
313 shift_window(&win_meta, 'f');
314 else if (key == get_action_key(world.keybindings, "shift backwards") && win_meta.active != 0)
315 shift_window(&win_meta, 'b');
316 else if (key == get_action_key(world.keybindings, "grow hor") && win_meta.active != 0)
317 resize_window(&win_meta, '*');
318 else if (key == get_action_key(world.keybindings, "shrink hor") && win_meta.active != 0)
319 resize_window(&win_meta, '_');
320 else if (key == get_action_key(world.keybindings, "grow ver") && win_meta.active != 0)
321 resize_window(&win_meta, '+');
322 else if (key == get_action_key(world.keybindings, "shrink ver") && win_meta.active != 0)
323 resize_window(&win_meta, '-');
324 else if (key == get_action_key(world.keybindings, "keys nav up") && world.keyswindata->select > 0) {
325 world.keyswindata->select--;
326 draw_all_windows (&win_meta); }
327 else if (key == get_action_key(world.keybindings, "keys nav down") && world.keyswindata->select < world.keyswindata->max) {
328 world.keyswindata->select++;
329 draw_all_windows (&win_meta); }
330 else if (key == get_action_key(world.keybindings, "keys mod")) {
331 world.keyswindata->edit = 1;
332 draw_all_windows (&win_meta);
334 world.keybindings[world.keyswindata->select].key = key;
335 world.keyswindata->edit = 0;
336 draw_all_windows (&win_meta); }
337 else if (key == get_action_key(world.keybindings, "map up") && map.offset_y > 0) {
339 draw_all_windows (&win_meta); }
340 else if (key == get_action_key(world.keybindings, "map down")) {
342 draw_all_windows (&win_meta); }
343 else if (key == get_action_key(world.keybindings, "map right")) {
345 draw_all_windows (&win_meta); }
346 else if (key == get_action_key(world.keybindings, "map left") && map.offset_x > 0) {
348 draw_all_windows (&win_meta); }
349 else if (key == get_action_key(world.keybindings, "player down") && map.player_y < map.height - 1) {
350 update_info (&win_info);
351 update_log (&win_log, "\nYou move south.");
353 draw_all_windows (&win_meta); }
354 else if (key == get_action_key(world.keybindings, "player up") && map.player_y > 0) {
355 update_info (&win_info);
356 update_log (&win_log, "\nYou move north.");
358 draw_all_windows (&win_meta); }
359 else if (key == get_action_key(world.keybindings, "player right") && map.player_x < map.width - 1) {
360 update_info (&win_info);
361 update_log (&win_log, "\nYou move east.");
363 draw_all_windows (&win_meta); }
364 else if (key == get_action_key(world.keybindings, "player left") && map.player_x > 0) {
365 update_info (&win_info);
366 update_log (&win_log, "\nYou move west.");
368 draw_all_windows (&win_meta); }
369 else if (key == get_action_key(world.keybindings, "wait") ) {
370 update_info (&win_info);
371 update_log (&win_log, "\nYou wait.");
372 draw_all_windows(&win_meta); } }