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 */
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);
68 /* Draw map cell "c" into win "w" at position "y"/"x" with attribute "a". */
69 static void draw_mapcell(struct Win * w, char c, uint8_t y, uint8_t x,attr_t a);
73 static void try_resize_winmap(struct Win * win, int new_size_y, int new_size_x)
75 if (win->winmap_size.y >= new_size_y && win->winmap_size.x >= new_size_x)
79 if (win->winmap_size.y > new_size_y)
81 new_size_y = win->winmap_size.y;
83 else if (win->winmap_size.x > new_size_x)
85 new_size_x = win->winmap_size.x;
87 chtype * old_winmap = win->winmap;
88 uint32_t new_size = sizeof(chtype) * new_size_y * new_size_x;
89 win->winmap = try_malloc(new_size, __func__);
91 for (y = 0; y < new_size_y; y++)
93 for (x = 0; y < win->winmap_size.y && x < win->winmap_size.x; x++)
95 chtype ch = old_winmap[(y * win->winmap_size.x) + x];
96 win->winmap[(y * new_size_x) + x] = ch;
98 for (; x < new_size_x; x++)
100 win->winmap[(y * new_size_x) + x] = ' ';
104 win->winmap_size.y = new_size_y;
105 win->winmap_size.x = new_size_x;
110 static void set_ch_on_yx(struct Win * win, int y, int x, chtype ch)
112 win->winmap[(y * win->winmap_size.x) + x] = ch;
117 static void add_line(struct Win * win, char * line, attr_t attri,
118 uint16_t * offset, uint8_t last)
120 exit_err(strlen(line) > UINT16_MAX, "Line in add_line() too big.");
121 if (0 == win->linebreak)
123 add_line_long(win, line, attri);
125 else if (1 == win->linebreak)
127 add_line_wide(win, line, attri);
129 else if (2 == win->linebreak)
131 add_line_compact(win, line, attri, offset, last);
137 static void add_line_wide(struct Win * win, char * line, attr_t attri)
139 uint16_t y_start = win->winmap_size.y;
140 uint16_t len_line = strlen(line);
141 try_resize_winmap(win, y_start + 1, win->frame_size.x);
142 try_resize_winmap(win, y_start + 1, len_line);
144 for (x = 0; x < len_line; x++)
146 set_ch_on_yx(win, y_start, x, line[x] | attri);
150 for (; x < win->frame_size.x; x++)
152 set_ch_on_yx(win, y_start, x, ' ' | attri);
159 static void add_line_long(struct Win * win, char * line, attr_t attri)
161 uint16_t y_start = win->winmap_size.y;
162 uint16_t len_line = strlen(line);
163 try_resize_winmap(win, y_start + 1, win->frame_size.x);
165 for (z = 0, y = y_start; z < len_line; y++)
167 for (x = 0; x < win->winmap_size.x && z < len_line; x++, z++)
169 set_ch_on_yx(win, y, x, line[z] | attri);
173 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
180 static void add_line_compact(struct Win * win, char * line, attr_t attri,
181 uint16_t * offset, uint8_t last)
183 uint16_t y_start = win->winmap_size.y - (win->winmap_size.y > 0);
184 try_resize_winmap(win, y_start + 1, win->frame_size.x);
185 uint16_t len_line = strlen(line);
186 char * separator = last ? "" : " / ";
187 uint32_t len_line_new = len_line + strlen(separator);
188 char * line_new = try_malloc(len_line_new + 1, __func__);
189 int test = sprintf(line_new, "%s%s", line, separator);
190 exit_trouble(test < 0, __func__, "sprintf");
194 for (z = 0, y = y_start; z < len_line_new; y++)
196 for (x = * offset; x < win->winmap_size.x && z < len_line_new; x++, z++)
198 set_ch_on_yx(win, y, x, line_new[z] | attri);
199 if (z + 1 == (uint32_t) len_line)
205 if (z < len_line_new)
207 try_resize_winmap(win, y + 1 + 1, win->winmap_size.x);
216 static void add_text_with_linebreaks(struct Win * win, char * text)
220 char * text_copy = strdup(text);
221 char * start = text_copy;
222 while (NULL != (limit = strchr(start, '\n')))
225 add_line(win, start, 0, &offset, 0);
228 add_line(win, start, 0, &offset, 1);
234 static void draw_text_from_bottom(struct Win * win, char * text)
236 add_text_with_linebreaks(win, text);
237 if (win->winmap_size.y > win->frame_size.y)
239 win->center.y = win->winmap_size.y - 1;
241 else if (win->winmap_size.y < win->frame_size.y)
243 uint16_t new_y_start = win->frame_size.y - win->winmap_size.y;
244 memset(&win->winmap_size, 0, sizeof(struct yx_uint16));
249 add_line_wide(win, " ", 0);
251 while (new_y_start > win->winmap_size.y);
252 if (2 == win->linebreak) /* add_text_with_linebreaks() will start not */
253 { /* not after, but within the last line then. */
254 add_line_wide(win, " ", 0);
256 add_text_with_linebreaks(win, text);
262 static void draw_keybinding_config(struct Win * win, struct KeyBindingDB * kbdb,
265 if (0 == kbdb->n_of_kbs)
267 add_line(win, "(none)", 0, &offset, 0);
271 for (kb_n = 0; kb_n < kbdb->n_of_kbs; kb_n++)
274 if (kb_n == kbdb->select)
279 attri = attri | A_BLINK;
281 win->center.y = win->winmap_size.y;
283 struct KeyBinding kb = kbdb->kbs[kb_n];
284 char * keyname = get_keyname_to_keycode(kb.keycode);
285 uint16_t size = strlen(keyname) + 3 + strlen(kb.command->dsc_long) + 1;
286 char * kb_line = try_malloc(size, __func__);
287 int test = sprintf(kb_line, "%s - %s", keyname, kb.command->dsc_long);
288 exit_trouble(test < 0, __func__, "sprintf");
290 add_line(win, kb_line, attri, &offset, (kbdb->n_of_kbs == kb_n + 1));
297 static char * get_keyname_to_command(char * command_name)
300 for (i = 0; i < world.kb_wingeom.n_of_kbs; i++)
302 if (!strcmp(world.kb_wingeom.kbs[i].command->dsc_short, command_name))
304 return get_keyname_to_keycode(world.kb_wingeom.kbs[i].keycode);
312 static char * winconf_geom_helper(struct Win * win, char axis, char * sep,
313 char * newlines, char * value_prefix)
315 char * p0 = 'v'==axis? "Height" : "Width";
316 char * p1 = " to save (grow/shrink/toggle positivity with ";
317 char * p2 = get_keyname_to_command('v'==axis? "grow_v" : "grow_h");
318 char * p4 = get_keyname_to_command('v'==axis? "shri_v" : "shri_v");
319 char * p6 = get_keyname_to_command('v'==axis? "to_height_t" : "to_width_t");
320 char p8[6 + 1]; /* 6: int16_t value max strlen */
321 int test = sprintf(p8,"%d", 'v'==axis?win->target_height:win->target_width);
322 exit_trouble(test < 0, __func__, "sprintf");
324 char * p10 = "in_cells";
325 if (1 == ('v'==axis? win->target_height_type : win->target_width_type))
327 p10 = "non-positive diff: cells to screen maximum";
330 uint8_t size = strlen(p0) + strlen(p1) + strlen(p2) + strlen(sep)
331 + strlen(p4) + strlen(sep) + strlen(p6) +strlen(value_prefix)
332 + strlen(p8) + strlen(p9) + strlen(p10) + strlen(p11)
334 char * msg = try_malloc(size, __func__);
335 sprintf(msg, "%s%s%s%s%s%s%s%s%s%s%s%s%s", p0, p1, p2, sep, p4, sep, p6,
336 value_prefix, p8, p9, p10, p11, newlines);
342 static void draw_mapcell(struct Win * w, char c, uint8_t y, uint8_t x, attr_t a)
344 set_ch_on_yx(w, y, x * 2 + (y % 2), c | a);
345 if (x + (y % 2) < world.map.length)
347 set_ch_on_yx(w, y, x * 2 + (y % 2) + 1, ' ' | a);
353 extern void draw_win_log(struct Win * win)
359 draw_text_from_bottom(win, world.log);
364 extern void draw_win_map(struct Win * win)
367 init_pair(1, COLOR_WHITE, COLOR_BLUE);
368 init_pair(2, COLOR_BLUE, COLOR_BLACK);
369 attr_t attr_mem = COLOR_PAIR(2);
370 attr_t attr_sha = COLOR_PAIR(1);
371 try_resize_winmap(win, world.map.length, world.map.length * 2);
372 for (y = 0; y < world.map.length; y++)
374 for (x = 0; x < world.map.length; x++)
376 attr_t a=' '==world.mem_map[y*world.map.length+x]?attr_sha:attr_mem;
377 char c = world.mem_map[y*world.map.length + x];
378 draw_mapcell(win, c, y, x, a);
381 for (y = 0; y < world.map.length; y++)
383 for (x = 0; x < world.map.length; x++)
385 if (' ' != world.map.cells[y*world.map.length + x])
387 char c = world.map.cells[y*world.map.length + x];
388 draw_mapcell(win, c, y, x, 0);
394 y = world.look_pos.y;
395 x = world.look_pos.x;
396 char c = world.map.cells[y * world.map.length + x];
397 c = ' ' == c ? world.mem_map[y * world.map.length + x] : c;
398 draw_mapcell(win, c, y, x, A_REVERSE);
404 extern void draw_win_info(struct Win * win)
406 char * dsc_turn = "Turn: ";
407 char * dsc_hitpoints = "\nHitpoints: ";
408 uint16_t maxl = strlen(dsc_turn) + 5 + strlen(dsc_hitpoints) + 3;
409 char * text = try_malloc(maxl + 1, __func__);
410 int test = sprintf(text, "%s%d%s%d", dsc_turn, world.turn, dsc_hitpoints,
411 world.player_lifepoints);
412 exit_trouble(test < 0, __func__, "sprintf");
413 add_text_with_linebreaks(win, text);
419 extern void draw_win_inventory(struct Win * win)
422 char * text_copy = strdup(world.player_inventory);
423 char * start = text_copy;
425 uint8_t found_selection = 0;
427 while (NULL != (limit = strchr(start, '\n')))
431 if (i == world.player_inventory_select)
435 win->center.y = win->winmap_size.y;
437 add_line(win, start, attri, &offset, 0);
441 win->center.y = !found_selection ? win->winmap_size.y : win->center.y;
442 add_line(win, start, !found_selection * A_REVERSE, &offset, 1);
448 extern void draw_win_terrain_stack(struct Win * win)
450 if (world.things_here)
452 add_text_with_linebreaks(win, world.things_here);
458 extern void draw_win_keybindings_global(struct Win * win)
460 win->center.y = world.kb_global.select;
461 draw_keybinding_config(win, &world.kb_global, 0);
466 extern void draw_win_keybindings_winconf_geometry(struct Win * win)
468 win->center.y = world.kb_wingeom.select;
469 draw_keybinding_config(win, &world.kb_wingeom, 0);
474 extern void draw_win_keybindings_winconf_keybindings(struct Win * win)
476 win->center.y = world.kb_winkeys.select;
477 draw_keybinding_config(win, &world.kb_winkeys, 0);
482 extern void draw_winconf_keybindings(struct Win * win)
484 char * title = "Window's keys:";
486 add_line(win, title, 0, &offset, 0);
487 add_line(win, " ", 0, &offset, 0);
488 draw_keybinding_config(win, &win->kb, offset);
493 extern void draw_winconf_geometry(struct Win * win)
496 char * newlines = "\n\n";
497 char * value_prefix = "): ";
498 char * title = "Window's geometry:\n\nShift up/down with ";
499 char * key_shift_b = get_keyname_to_command("shift_b");
500 char * key_shift_f = get_keyname_to_command("shift_f");
501 char * height = winconf_geom_helper(win, 'v', sep, newlines, value_prefix);
502 char * width = winconf_geom_helper(win, 'h', sep, newlines, value_prefix);
503 char * breaks_title = "Linebreak type (toggle with ";
504 char * key_to_break = get_keyname_to_command("to_break");
505 char * breaks_type = "long";
508 breaks_type = (1 == win->linebreak) ? "wide" : "compact";
510 uint16_t text_size = strlen(title) + strlen(key_shift_b) + strlen(sep)
511 + strlen(key_shift_f) + strlen(newlines)
512 + strlen(height) + strlen(width)
513 + strlen(breaks_title) + strlen(key_to_break)
514 + strlen(value_prefix) + strlen(breaks_type);
515 char * text = try_malloc(text_size + 1, __func__);
516 int test = sprintf(text, "%s%s%s%s%s%s%s%s%s%s%s", title, key_shift_b, sep,
517 key_shift_f, newlines, height, width, breaks_title,
518 key_to_break, value_prefix, breaks_type);
521 exit_trouble(test < 0, __func__, "sprintf");
522 add_text_with_linebreaks(win, text);