8 #include "keybindings.h"
9 #include "objects_on_map.h"
11 static void draw_map_objects (void *, struct Map *, struct Win *);
13 extern void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
14 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
19 for (y = start_y; y < win->frame.size.y; y++) {
22 for (x = 0; x < win->frame.size.x; x++) {
25 if ('\n' == text[z]) {
29 mvwaddch(win->frame.curses_win, y, x, text[z]);
30 if ('\n' == text[z+1]) {
33 else if (0 == text[z+1]) {
37 extern void draw_text_from_bottom (struct Win * win, char * text) {
38 // Draw text from end/bottom to the top.
40 uint16_t x, y, offset;
42 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
43 for (x = 0; x < win->frame.size.x; x++) { // a window of available width, but infinite height.
45 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
46 break; // the loop. Make sure they don't count as cell space themselves.
47 if ('\n' == text[z+1]) {
50 else if (0 == text[z+1]) {
55 if (y < win->frame.size.y) // Depending on what is bigger, determine start point in window or in text.
56 start_y = win->frame.size.y - y;
57 else if (y > win->frame.size.y) {
58 offset = y - win->frame.size.y;
59 for (y = 0; y < offset; y++)
60 for (x = 0; x < win->frame.size.x; x++) {
64 if ('\n' == text[z+1]) {
67 text = text + (sizeof(char) * (z + 1)); }
68 draw_with_linebreaks(win, text, start_y); }
70 extern void draw_log_win (struct Win * win) {
71 // Draw log text from world struct in win->data from bottom to top.
72 struct World * world = (struct World *) win->data;
73 draw_text_from_bottom(win, world->log); }
75 static void draw_map_objects (void * start, struct Map * map, struct Win * win) {
76 // Draw onto map in win the objects in the chain at start.
77 struct ChainMapObject * cmo;
78 for (cmo = start; cmo != 0; cmo = cmo->next)
79 if ( cmo->pos.y >= map->offset.y && cmo->pos.y < map->offset.y + win->frame.size.y
80 && cmo->pos.x >= map->offset.x && cmo->pos.x < map->offset.x + win->frame.size.x)
81 mvwaddch(win->frame.curses_win, cmo->pos.y - map->offset.y, cmo->pos.x - map->offset.x, cmo->name); }
83 extern void draw_map_win (struct Win * win) {
84 // Draw map determined by map (from win->data) and various actors/objects into window. Respect scroll offset.
85 struct World * world = (struct World *) win->data;
86 struct Map * map = world->map;
87 struct Player * player = world->player;
88 char * cells = map->cells;
89 uint16_t width_map_av = map->size.x - map->offset.x;
90 uint16_t height_map_av = map->size.y - map->offset.y;
92 for (y = 0; y < win->frame.size.y; y++) {
93 z = map->offset.x + (map->offset.y + y) * (map->size.x);
94 for (x = 0; x < win->frame.size.x; x++) {
95 if (y < height_map_av && x < width_map_av) {
96 mvwaddch(win->frame.curses_win, y, x, cells[z]);
98 draw_map_objects (world->item, map, win);
99 draw_map_objects (world->monster, map, win);
100 if ( player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
101 && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
102 mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@'); }
104 extern void draw_info_win (struct Win * win) {
105 // Draw info window by appending win->data integer value to "Turn: " display.
106 struct World * world = (struct World *) win->data;
108 snprintf(text, 100, "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
109 draw_with_linebreaks(win, text, 0); }
111 extern void draw_keys_win (struct Win * win) {
112 // Draw keybindings window.
113 struct World * world = (struct World *) win->data;
114 uint16_t offset, y, x;
115 offset = center_offset (world->keyswindata->select, world->keyswindata->max, win->frame.size.y - 1);
116 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
117 char * keydesc = malloc(keydescwidth), * keyname;
119 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
120 if (0 == y && offset > 0) {
121 draw_scroll_hint(&win->frame, y, offset + 1, '^');
123 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
124 draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
127 if (y == world->keyswindata->select - offset) {
129 if (1 == world->keyswindata->edit)
130 attri = attri | A_BLINK; }
131 keyname = get_keyname(world->keybindings[y + offset].key);
132 snprintf(keydesc, keydescwidth, "%-9s", keyname);
134 for (x = 0; x < win->frame.size.x; x++)
135 if (x < strlen(keydesc))
136 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
137 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
138 mvwaddch(win->frame.curses_win, y, x,
139 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
141 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }