8 #include "keybindings.h"
10 void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
11 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
16 for (y = start_y; y < win->height; y++) {
19 for (x = 0; x < win->width; x++) {
22 if ('\n' == text[z]) {
26 mvwaddch(win->curses, y, x, text[z]);
27 if ('\n' == text[z+1]) {
30 else if (0 == text[z+1]) {
34 void draw_text_from_bottom (struct Win * win, char * text) {
35 // Draw text from end/bottom to the top.
37 uint16_t x, y, offset;
39 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
40 for (x = 0; x < win->width; x++) { // a window of available width, but infinite height.
42 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
43 break; // the loop. Make sure they don't count as cell space themselves.
44 if ('\n' == text[z+1]) {
47 else if (0 == text[z+1]) {
52 if (y < win->height) // Depending on what is bigger, determine start point in window or in text.
53 start_y = win->height - y;
54 else if (y > win->height) {
55 offset = y - win->height;
56 for (y = 0; y < offset; y++)
57 for (x = 0; x < win->width; x++) {
61 if ('\n' == text[z+1]) {
64 text = text + (sizeof(char) * (z + 1)); }
65 draw_with_linebreaks(win, text, start_y); }
67 void draw_log_win (struct Win * win) {
68 // Draw log text from world struct in win->data from bottom to top.
69 struct World * world = (struct World *) win->data;
70 draw_text_from_bottom(win, world->log); }
72 void draw_map_win (struct Win * win) {
73 // Draw map determined by win->data Map struct into window. Respect offset.
74 struct World * world = (struct World *) win->data;
75 struct Map * map = world->map;
76 struct Player * player = world->player;
77 struct Monster * monster = world->monster;
78 char * cells = map->cells;
79 uint16_t width_map_av = map->width - map->offset_x;
80 uint16_t height_map_av = map->height - map->offset_y;
82 for (y = 0; y < win->height; y++) {
83 z = map->offset_x + (map->offset_y + y) * (map->width);
84 for (x = 0; x < win->width; x++) {
85 if (y < height_map_av && x < width_map_av) {
86 if (z == (map->width * player->y) + player->x)
87 mvwaddch(win->curses, y, x, '@');
88 else if (z == (map->width * monster->y) + monster->x)
89 mvwaddch(win->curses, y, x, 'M');
91 mvwaddch(win->curses, y, x, cells[z]);
94 void draw_info_win (struct Win * win) {
95 // Draw info window by appending win->data integer value to "Turn: " display.
96 struct World * world = (struct World *) win->data;
97 uint16_t count = world->turn;
99 snprintf(text, 100, "Turn: %d", count);
100 draw_with_linebreaks(win, text, 0); }
102 void draw_keys_win (struct Win * win) {
103 // Draw keybinding window.
104 struct World * world = (struct World *) win->data;
105 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
106 struct KeyBinding * keybindings = world->keybindings;
108 if (keyswindata->max >= win->height) {
109 if (keyswindata->select > win->height / 2) {
110 if (keyswindata->select < (keyswindata->max - (win->height / 2)))
111 offset = keyswindata->select - (win->height / 2);
113 offset = keyswindata->max - win->height + 1; } }
114 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
115 char * keydesc = malloc(keydescwidth);
119 for (y = 0; y <= keyswindata->max && y < win->height; y++) {
121 if (y == keyswindata->select - offset) {
123 if (1 == keyswindata->edit)
124 attri = attri | A_BLINK; }
125 keyname = get_keyname(keybindings[y + offset].key);
126 snprintf(keydesc, keydescwidth, "%-9s", keyname);
128 for (x = 0; x < win->width; x++)
129 if (x < strlen(keydesc))
130 mvwaddch(win->curses, y, x, keydesc[x] | attri);
131 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
132 mvwaddch(win->curses, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
134 mvwaddch(win->curses, y, x, ' ' | attri); }