7 #include "keybindings.h"
9 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
10 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
15 for (y = start_y; y < win->height; y++) {
18 for (x = 0; x < win->width; x++) {
21 if ('\n' == text[z]) {
25 mvwaddch(win->curses, y, x, text[z]);
26 if ('\n' == text[z+1]) {
29 else if (0 == text[z+1]) {
33 void draw_text_from_bottom (struct Win * win, char * text) {
34 // Draw text from end/bottom to the top.
38 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
39 for (x = 0; x < win->width; x++) { // a window of available width, but infinite height.
41 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
42 break; // the loop. Make sure they don't count as cell space themselves.
43 if ('\n' == text[z+1]) {
46 else if (0 == text[z+1]) {
51 if (y < win->height) // Depending on what is bigger, determine start point in window or in text.
52 start_y = win->height - y;
53 else if (y > win->height) {
54 offset = y - win->height;
55 for (y = 0; y < offset; y++)
56 for (x = 0; x < win->width; x++) {
60 if ('\n' == text[z+1]) {
63 text = text + (sizeof(char) * (z + 1)); }
64 draw_with_linebreaks(win, text, start_y); }
66 void draw_log_win (struct Win * win) {
67 // Draw log text from world struct in win->data from bottom to top.
68 struct World * world = (struct World *) win->data;
69 draw_text_from_bottom(win, world->log); }
71 void draw_map_win (struct Win * win) {
72 // Draw map determined by win->data Map struct into window. Respect offset.
73 struct World * world = (struct World *) win->data;
74 struct Map * map = world->map;
75 struct Player * player = world->player;
76 struct Monster * monster = world->monster;
77 char * cells = map->cells;
78 int width_map_av = map->width - map->offset_x;
79 int height_map_av = map->height - map->offset_y;
81 for (y = 0; y < win->height; y++) {
82 z = map->offset_x + (map->offset_y + y) * (map->width);
83 for (x = 0; x < win->width; x++) {
84 if (y < height_map_av && x < width_map_av) {
85 if (z == (map->width * player->y) + player->x)
86 mvwaddch(win->curses, y, x, '@');
87 else if (z == (map->width * monster->y) + monster->x)
88 mvwaddch(win->curses, y, x, 'M');
90 mvwaddch(win->curses, y, x, cells[z]);
93 void draw_info_win (struct Win * win) {
94 // Draw info window by appending win->data integer value to "Turn: " display.
95 struct World * world = (struct World *) win->data;
96 int count = world->turn;
98 snprintf(text, 100, "Turn: %d", count);
99 draw_with_linebreaks(win, text, 0); }
101 void draw_keys_win (struct Win * win) {
102 // Draw keybinding window.
103 struct World * world = (struct World *) win->data;
104 struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
105 struct KeyBinding * keybindings = world->keybindings;
107 if (keyswindata->max >= win->height) {
108 if (keyswindata->select > win->height / 2) {
109 if (keyswindata->select < (keyswindata->max - (win->height / 2)))
110 offset = keyswindata->select - (win->height / 2);
112 offset = keyswindata->max - win->height + 1; } }
113 int keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
114 char * keydesc = malloc(keydescwidth);
118 for (y = 0; y <= keyswindata->max && y < win->height; y++) {
120 if (y == keyswindata->select - offset) {
122 if (1 == keyswindata->edit)
123 attri = attri | A_BLINK; }
124 keyname = get_keyname(keybindings[y + offset].key);
125 snprintf(keydesc, keydescwidth, "%-9s", keyname);
127 for (x = 0; x < win->width; x++)
128 if (x < strlen(keydesc))
129 mvwaddch(win->curses, y, x, keydesc[x] | attri);
130 else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
131 mvwaddch(win->curses, y, x, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
133 mvwaddch(win->curses, y, x, ' ' | attri); }