1 /* src/client/draw_wins.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
10 #include <ncurses.h> /* attr_t, chtype, init_pair(), A_REVERSE, COLOR_PAIR() */
11 #include <stddef.h> /* NULL */
12 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX, UINT32_MAX */
13 #include <stdio.h> /* sprintf() */
14 #include <stdlib.h> /* free() */
15 #include <string.h> /* memset(), strcmp(), strchr(), strdup/(), strlen() */
16 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
17 #include "../common/try_malloc.h" /* try_malloc() */
18 #include "keybindings.h" /* struct KeyBindingDB, get_keyname_to_keycode() */
19 #include "windows.h" /* yx_uint16, Win */
20 #include "world.h" /* global world */
24 /* Apply to the winmap of Win "w" the new sizes "new_size_y" and "new_size_x"
25 * to the degree that they extend it. Re-shape the window content accordingly.
27 static void try_resize_winmap(struct Win * w, int new_size_y, int new_size_x);
29 /* In Win "w", write "ch" to coordinate "y"/"x". */
30 static void set_ch_on_yx(struct Win * w, int y, int x, chtype ch);
32 /* Add "line" into window "w" with "attri" set on all chars. add_line() selects
33 * one of the other three functions based on the "win"->linebreak mode.
35 * "wide" writes lines to full width without in-between breaks. "long" breaks
36 * lines on the right window border. "compact" replaces newlines with printable
37 * separator characters. "offset" and "last" are only relevant to the "compact"
38 * mode. "offset" reports the horizontal offset at which the line is drawn from
39 * the lowest existing winmap line on; it is updated to the next offset after
40 * the new line, to be used by the next add_line_compact() call. "last" marks
41 * the line to add as the last in the window, so no further separator is added.
43 static void add_line(struct Win * win, char * line, attr_t attri,
44 uint16_t * offset, uint8_t last);
45 static void add_line_wide(struct Win * win, char * line, attr_t attri);
46 static void add_line_long(struct Win * win, char * line, attr_t attri);
47 static void add_line_compact(struct Win * win, char * line, attr_t attri,
48 uint16_t * offset, uint8_t last);
50 /* Add linebreaks-containing "text" to "win", formatted as per "win"->linebreak.
52 * draw_text_from_bottom() prepends empty lines to "text" to push its last line
53 * to the low frame edge when the frame is of greater height than the winmap
54 * would be otherwise; else, set "win"->center.y to the winmap's lowest line.
56 static void add_text_with_linebreaks(struct Win * win, char * text);
57 static void draw_text_from_bottom(struct Win * win, char * text);
59 /* Draw from line "start" on config view for keybindings defined at "kb". */
60 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
63 /* String building helper functions to draw_winconf_geometry(). */
64 static char * get_keyname_to_command(char * command_name);
65 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
66 char * newlines, char * value_prefix);
70 /* try_resize_winmap() only realloc's Win->winmap if this is set. Use this to
71 * skip inflationary malloc's by successive try_resize_winmap() calls when the
72 * memory needed can be pre-calculated.
74 static uint8_t do_realloc_winmap = 1;
78 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
80 if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
84 if (win->winmap_size.y > new_size_y)
86 new_size_y = win->winmap_size.y;
88 else if (win->winmap_size.x > new_size_x)
90 new_size_x = win->winmap_size.x;
92 if (do_realloc_winmap)
94 chtype * old_winmap = win->winmap;
95 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
96 win->winmap = try_malloc(new_size, __func__);
98 for (y = 0; y < new_size_y; y++)
100 for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
102 chtype ch = old_winmap[(y * win->winmap_size.x) + x];
103 win->winmap[(y * new_size_x) + x] = ch;
105 for (; x < new_size_x; x++)
107 win->winmap[(y * new_size_x) + x] = ' ';
112 win->winmap_size.y = new_size_y;
113 win->winmap_size.x = new_size_x;
118 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
120 win->winmap[(y * win->winmap_size.x) + x] = ch;
125 static void add_line(struct Win * win, char * line, attr_t attri,
126 uint16_t * offset, uint8_t last)
128 exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
129 if (0 == win->linebreak)
131 add_line_long(win, line, attri);
133 else if (1 == win->linebreak)
135 add_line_wide(win, line, attri);
137 else if (2 == win->linebreak)
139 add_line_compact(win, line, attri, offset, last);
145 static void add_line_wide(struct Win * win, char * line, attr_t attri)
147 uint16_t y_start = win->winmap_size.y;
148 uint16_t len_line = strlen(line);
149 try_resize_winmap(win, y_start + 1, win->frame_size.x);
150 try_resize_winmap(win, y_start + 1, len_line);
152 for (x = 0; x < len_line; x++)
154 set_ch_on_yx(win, y_start, x, line[x] | attri);
158 for (; x < win->frame_size.x; x++)
160 set_ch_on_yx(win, y_start, x, ' ' | attri);
167 static void add_line_long(struct Win * win, char * line, attr_t attri)
169 uint16_t y_start = win->winmap_size.y;
170 uint16_t len_line = strlen(line);
171 try_resize_winmap(win, y_start + 1, win->frame_size.x);
173 for (z = 0, y = y_start; z < len_line; y++)
175 for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
177 set_ch_on_yx(win, y, x, line[z] | attri);
181 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
188 static void add_line_compact(struct Win * win, char * line, attr_t attri,
189 uint16_t * offset, uint8_t last)
191 uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
192 try_resize_winmap(win, y_start + 1, win->frame_size.x);
193 uint16_t len_line = strlen(line);
194 char * separator = last ? "" : " / ";
195 uint32_t len_line_new = len_line + strlen(separator);
196 char * line_new = try_malloc(len_line_new + 1, __func__);
197 int test = sprintf(line_new, "%s%s", line, separator);
198 exit_trouble(test < 0, __func__, "sprintf");
202 for (z = 0, y = y_start; z < len_line_new; y++)
204 for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
206 set_ch_on_yx(win, y, x, line_new[z] | attri);
207 if (z + 1 == (uint32_t) len_line)
213 if (z < len_line_new)
215 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
224 static void add_text_with_linebreaks(struct Win * win, char * text)
228 char * text_copy = strdup(text);
229 char * start = text_copy;
230 while (NULL != (limit = strchr(start, '\n')))
233 add_line(win, start, 0, &offset, 0);
236 add_line(win, start, 0, &offset, 1);
242 static void draw_text_from_bottom(struct Win * win, char * text)
244 add_text_with_linebreaks(win, text);
245 if (win->winmap_size.y > win->frame_size.y)
247 win->center.y = win->winmap_size.y - 1;
249 else if (win->winmap_size.y < win->frame_size.y)
251 uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
252 memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
257 add_line_wide(win, " ", 0);
259 while (new_y_start > win->winmap_size.y);
260 if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
261 { /* after, but within the last line then. */
262 add_line_wide(win, " ", 0);
264 add_text_with_linebreaks(win, text);
270 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
273 if (0 == kbdb->n_of_kbs)
275 add_line(win, "(none)", 0, &offset, 0);
279 for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
282 if (kb_n == kbdb->select)
287 attri = attri | A_BLINK;
289 win->center.y = win->winmap_size.y;
291 struct KeyBinding kb = kbdb->kbs[kb_n];
292 char * keyname = get_keyname_to_keycode(kb.keycode);
293 uint16_t size = strlen(keyname) + 3 + strlen(kb.command->dsc_long) + 1;
294 char * kb_line = try_malloc(size, __func__);
295 int test = sprintf(kb_line, "%s - %s", keyname, kb.command->dsc_long);
296 exit_trouble(test < 0, __func__, "sprintf");
298 add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
305 static char * get_keyname_to_command(char * command_name)
308 for (i = 0; i < world.kb_wingeom.n_of_kbs; i++)
310 if (!strcmp(world.kb_wingeom.kbs[i].command->dsc_short, command_name))
312 return get_keyname_to_keycode(world.kb_wingeom.kbs[i].keycode);
320 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
321 char * newlines, char * value_prefix)
323 char * p0 = 'v'==axis? "Height" : "Width";
324 char * p1 = " to save (grow/shrink/toggle positivity with ";
325 char * p2 = get_keyname_to_command('v'==axis? "grow_v" : "grow_h");
326 char * p4 = get_keyname_to_command('v'==axis? "shri_v" : "shri_v");
327 char * p6 = get_keyname_to_command('v'==axis? "to_height_t" : "to_width_t");
328 char p8[6 + 1]; /* 6: int16_t value max strlen */
329 int test = sprintf(p8,"%d", 'v'==axis?win->target_height:win->target_width);
330 exit_trouble(test < 0, __func__, "sprintf");
332 char * p10 = "in_cells";
333 if (1 == ('v'==axis? win->target_height_type : win->target_width_type))
335 p10 = "non-positive diff: cells to screen maximum";
338 uint8_t size = strlen(p0) + strlen(p1) + strlen(p2) + strlen(sep)
339 + strlen(p4) + strlen(sep) + strlen(p6) +strlen(value_prefix)
340 + strlen(p8) + strlen(p9) + strlen(p10) + strlen(p11)
341 + strlen(newlines) + 1;
342 char * msg = try_malloc(size, __func__);
343 sprintf(msg, "%s%s%s%s%s%s%s%s%s%s%s%s%s", p0, p1, p2, sep, p4, sep, p6,
344 value_prefix, p8, p9, p10, p11, newlines);
350 extern void draw_win_log(struct Win * win)
356 uint32_t x, i, n_postbreak_lines;
357 for (i = 0, x = 0, n_postbreak_lines = 1; i < strlen(world.log); i++)
359 exit_err(i == UINT32_MAX, "Log too large.");
361 if (x > win->frame_size.x || '\n' == world.log[i])
367 if (n_postbreak_lines > win->frame_size.y)
369 uint32_t size = n_postbreak_lines * (win->frame_size.x + 1);
370 win->winmap = try_malloc(sizeof(chtype) * size, __func__);
371 for (i = 0; i < size; win->winmap[i] = ' ', i++);
372 do_realloc_winmap = 0;
373 draw_text_from_bottom(win, world.log);
374 do_realloc_winmap = 1;
377 draw_text_from_bottom(win, world.log);
382 extern void draw_win_map(struct Win * win)
385 init_pair(1, COLOR_WHITE, COLOR_WHITE); //
386 init_pair(2, COLOR_WHITE, COLOR_BLUE); //
387 init_pair(3, COLOR_BLUE, COLOR_WHITE); //
388 // attr_t attr_mem = COLOR_PAIR(2);
389 // attr_t attr_sha = COLOR_PAIR(1);
390 try_resize_winmap(win, world.map.length, world.map.length * 2 + 1);
391 for (y = 0; y < world.map.length; y++)
393 for (x = 0; x < world.map.length; x++)
395 char c_m = world.mem_map[y * world.map.length + x]; //
396 attr_t a = COLOR_PAIR(2); //
399 a = COLOR_PAIR(1); //
401 else if (c_m == 'X') //
403 a = COLOR_PAIR(3); //
405 // attr_t a=' '==world.mem_map[y*world.map.length+x]?attr_sha:attr_mem;
406 char c = world.mem_map[y*world.map.length + x];
407 set_ch_on_yx(win, y, x * 2 + (y % 2), c | a);
408 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | a);
411 init_pair(4, COLOR_BLUE, COLOR_BLACK); //
412 init_pair(5, COLOR_YELLOW, COLOR_BLACK); //
413 init_pair(6, COLOR_RED, COLOR_WHITE); //
414 init_pair(7, COLOR_WHITE, COLOR_RED); //
415 init_pair(8, COLOR_GREEN, COLOR_BLACK); //
416 init_pair(9, COLOR_MAGENTA, COLOR_BLACK); //
417 init_pair(10, COLOR_CYAN, COLOR_BLACK); //
418 init_pair(11, COLOR_BLACK, COLOR_GREEN); //
419 init_pair(12, COLOR_BLACK, COLOR_MAGENTA); //
420 for (y = 0; y < world.map.length; y++)
422 for (x = 0; x < world.map.length; x++)
424 char c = world.map.cells[y*world.map.length + x]; //
426 // if (' ' != world.map.cells[y*world.map.length + x])
428 attr_t a = COLOR_PAIR(4); //
429 if ('.' == c || ':' == c) //
431 a = COLOR_PAIR(5); //
433 else if ('@' == c) //
435 a = COLOR_PAIR(6); //
437 else if ('a' == c || 'd' == c || 'b' == c) //
439 a = COLOR_PAIR(7); //
441 else if ('#' == c) //
443 a = COLOR_PAIR(8); //
445 else if ('$' == c || '%' == c) //
447 a = COLOR_PAIR(9); //
449 else if ('m' == c) //
451 a = COLOR_PAIR(10); //
453 else if ('X' == c) //
455 a = COLOR_PAIR(11); //
457 else if ('_' == c) //
459 a = COLOR_PAIR(12); //
461 // char c = world.map.cells[y*world.map.length + x];
462 set_ch_on_yx(win, y, x * 2 + (y % 2), c | a); //
463 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | a); //
464 // set_ch_on_yx(win, y, x * 2 + (y % 2), c);
465 // set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ');
471 y = world.look_pos.y;
472 x = world.look_pos.x;
473 char c = world.map.cells[y * world.map.length + x];
474 c = ' ' == c ? world.mem_map[y * world.map.length + x] : c;
475 set_ch_on_yx(win, y, x * 2 + (y % 2), c | A_REVERSE);
476 set_ch_on_yx(win, y, x * 2 + (y % 2) + 1, ' ' | A_REVERSE);
482 extern void draw_win_info(struct Win * win)
484 char * dsc_turn = "Turn: ";
485 char * dsc_hitpoints = "\nHitpoints: ";
486 char * dsc_satiation = "\nSatiation: ";
487 char * dsc_godsmood = "\nGod's mood: "; // 7DRL
488 char * dsc_godsfavor = "\nGod's favor: "; // 7DRL
489 uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3 //
490 + strlen(dsc_satiation) + 6 + strlen(dsc_godsmood) + 6 //
491 + strlen(dsc_godsfavor) + 6; //
492 char * text = try_malloc(maxl + 1, __func__);
493 int test = sprintf(text, "%s%d%s%d%s%d%s%d%s%d", dsc_turn, world.turn,
494 dsc_hitpoints, world.player_lifepoints, dsc_satiation,
495 world.player_satiation, dsc_godsmood, world.godsmood, //
496 dsc_godsfavor, world.godsfavor); //
497 exit_trouble(test < 0, __func__, "sprintf");
498 add_text_with_linebreaks(win, text);
504 extern void draw_win_inventory(struct Win * win)
507 char * text_copy = strdup(world.player_inventory);
508 char * start = text_copy;
510 uint8_t found_selection = 0;
512 while (NULL != (limit = strchr(start, '\n')))
516 if (i == world.player_inventory_select)
520 win->center.y = win->winmap_size.y;
522 add_line(win, start, attri, &offset, 0);
526 win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
527 add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
533 extern void draw_win_terrain_stack(struct Win * win)
535 char * wait_response = "(polling)";
536 char * text = world.things_here ? world.things_here : wait_response;
537 add_text_with_linebreaks(win, text);
542 extern void draw_win_keybindings_global(struct Win * win)
544 win->center.y = world.kb_global.select;
545 draw_keybinding_config(win, &world.kb_global, 0);
550 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
552 win->center.y = world.kb_wingeom.select;
553 draw_keybinding_config(win, &world.kb_wingeom, 0);
558 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
560 win->center.y = world.kb_winkeys.select;
561 draw_keybinding_config(win, &world.kb_winkeys, 0);
566 extern void draw_winconf_keybindings(struct Win * win)
568 char * title = "Window's keys:";
570 add_line(win, title, 0, &offset, 0);
571 add_line(win, " ", 0, &offset, 0);
572 draw_keybinding_config(win, &win->kb, offset);
577 extern void draw_winconf_geometry(struct Win * win)
580 char * newlines = "\n\n";
581 char * value_prefix = "): ";
582 char * title = "Window's geometry:\n\nShift up/down with ";
583 char * key_shift_b = get_keyname_to_command("shift_b");
584 char * key_shift_f = get_keyname_to_command("shift_f");
585 char * height = winconf_geom_helper(win, 'v', sep, newlines, value_prefix);
586 char * width = winconf_geom_helper(win, 'h', sep, newlines, value_prefix);
587 char * breaks_title = "Linebreak type (toggle with ";
588 char * key_to_break = get_keyname_to_command("to_break");
589 char * breaks_type = "long";
592 breaks_type = (1 == win->linebreak) ? "wide" : "compact";
594 uint16_t text_size = strlen(title) + strlen(key_shift_b) + strlen(sep)
595 + strlen(key_shift_f) + strlen(newlines)
596 + strlen(height) + strlen(width)
597 + strlen(breaks_title) + strlen(key_to_break)
598 + strlen(value_prefix) + strlen(breaks_type);
599 char * text = try_malloc(text_size + 1, __func__);
600 int test = sprintf(text, "%s%s%s%s%s%s%s%s%s%s%s", title, key_shift_b, sep,
601 key_shift_f, newlines, height, width, breaks_title,
602 key_to_break, value_prefix, breaks_type);
605 exit_trouble(test < 0, __func__, "sprintf");
606 add_text_with_linebreaks(win, text);