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