8 #include "keybindings.h"
9 #include "map_objects.h"
13 static void draw_map_objects (void *, struct Map *, struct Win *);
15 extern void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
16 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
21 for (y = start_y; y < win->frame.size.y; y++) {
24 for (x = 0; x < win->frame.size.x; x++) {
27 if ('\n' == text[z]) {
31 mvwaddch(win->frame.curses_win, y, x, text[z]);
32 if ('\n' == text[z+1]) {
35 else if (0 == text[z+1]) {
39 extern void draw_text_from_bottom (struct Win * win, char * text) {
40 // Draw text from end/bottom to the top.
42 uint16_t x, y, offset;
44 for (y = 0; 0 == toggle; y++) // Determine number of lines text would have in
45 for (x = 0; x < win->frame.size.x; x++) { // a window of available width, but infinite height.
47 if ('\n' == text[z]) // Treat \n and \0 as control characters for incrementing y and stopping
48 break; // the loop. Make sure they don't count as cell space themselves.
49 if ('\n' == text[z+1]) {
52 else if (0 == text[z+1]) {
57 if (y < win->frame.size.y) // Depending on what is bigger, determine start point in window or in text.
58 start_y = win->frame.size.y - y;
59 else if (y > win->frame.size.y) {
60 offset = y - win->frame.size.y;
61 for (y = 0; y < offset; y++)
62 for (x = 0; x < win->frame.size.x; x++) {
66 if ('\n' == text[z+1]) {
69 text = text + (sizeof(char) * (z + 1)); }
70 draw_with_linebreaks(win, text, start_y); }
72 extern void draw_log_win (struct Win * win) {
73 // Draw log text from world struct in win->data from bottom to top.
74 struct World * world = (struct World *) win->data;
75 draw_text_from_bottom(win, world->log); }
77 static void draw_map_objects (void * start, struct Map * map, struct Win * win) {
78 // Draw onto map in win the objects in the chain at start.
79 struct ChainMapObject * cmo;
80 for (cmo = start; cmo != 0; cmo = cmo->next)
81 if ( cmo->pos.y >= map->offset.y && cmo->pos.y < map->offset.y + win->frame.size.y
82 && cmo->pos.x >= map->offset.x && cmo->pos.x < map->offset.x + win->frame.size.x)
83 mvwaddch(win->frame.curses_win, cmo->pos.y - map->offset.y, cmo->pos.x - map->offset.x, cmo->name); }
85 extern void draw_map_win (struct Win * win) {
86 // Draw map determined by map (from win->data) and various actors/objects into window. Respect scroll offset.
87 struct World * world = (struct World *) win->data;
88 struct Map * map = world->map;
89 struct Player * player = world->player;
90 char * cells = map->cells;
91 uint16_t width_map_av = map->size.x - map->offset.x;
92 uint16_t height_map_av = map->size.y - map->offset.y;
94 for (y = 0; y < win->frame.size.y; y++) {
95 z = map->offset.x + (map->offset.y + y) * (map->size.x);
96 for (x = 0; x < win->frame.size.x; x++) {
97 if (y < height_map_av && x < width_map_av) {
98 mvwaddch(win->frame.curses_win, y, x, cells[z]);
100 draw_map_objects (world->item, map, win);
101 draw_map_objects (world->monster, map, win);
102 if ( player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
103 && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
104 mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@'); }
106 extern void draw_info_win (struct Win * win) {
107 // Draw info window by appending win->data integer value to "Turn: " display.
108 struct World * world = (struct World *) win->data;
110 snprintf(text, 100, "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
111 draw_with_linebreaks(win, text, 0); }
113 extern void draw_keys_win (struct Win * win) {
114 // Draw keybindings window.
115 struct World * world = (struct World *) win->data;
116 uint16_t offset, y, x;
117 offset = center_offset (world->keyswindata->select, world->keyswindata->max, win->frame.size.y - 1);
118 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
119 char * keydesc = malloc(keydescwidth), * keyname;
121 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
122 if (0 == y && offset > 0) {
123 draw_scroll_hint(&win->frame, y, offset + 1, '^');
125 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
126 draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
129 if (y == world->keyswindata->select - offset) {
131 if (1 == world->keyswindata->edit)
132 attri = attri | A_BLINK; }
133 keyname = get_keyname(world->keybindings[y + offset].key);
134 snprintf(keydesc, keydescwidth, "%-9s", keyname);
136 for (x = 0; x < win->frame.size.x; x++)
137 if (x < strlen(keydesc))
138 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
139 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
140 mvwaddch(win->frame.curses_win, y, x,
141 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
143 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }