8 #include "keybindings.h"
9 #include "map_objects.h"
13 static void draw_map_objects (struct World *, struct MapObj *, 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 (struct World * world, struct MapObj * start, struct Map * map, struct Win * win) {
78 // Draw onto map in win the objects in the chain at start.
82 for (o = start; o != 0; o = o->next)
83 if ( o->pos.y >= map->offset.y && o->pos.y < map->offset.y + win->frame.size.y
84 && o->pos.x >= map->offset.x && o->pos.x < map->offset.x + win->frame.size.x) {
85 d = get_map_obj_def (world, o->type);
87 mvwaddch(win->frame.curses_win, o->pos.y - map->offset.y, o->pos.x - map->offset.x, c); } }
89 extern void draw_map_win (struct Win * win) {
90 // Draw map determined by map (from win->data) and various actors/objects into window. Respect scroll offset.
91 struct World * world = (struct World *) win->data;
92 struct Map * map = world->map;
93 struct Player * player = world->player;
94 char * cells = map->cells;
95 uint16_t width_map_av = map->size.x - map->offset.x;
96 uint16_t height_map_av = map->size.y - map->offset.y;
98 for (y = 0; y < win->frame.size.y; y++) {
99 z = map->offset.x + (map->offset.y + y) * (map->size.x);
100 for (x = 0; x < win->frame.size.x; x++) {
101 if (y < height_map_av && x < width_map_av) {
102 mvwaddch(win->frame.curses_win, y, x, cells[z]);
104 draw_map_objects (world, (struct MapObj *) world->item, map, win);
105 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
106 if ( player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
107 && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
108 mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@'); }
110 extern void draw_info_win (struct Win * win) {
111 // Draw info window by appending win->data integer value to "Turn: " display.
112 struct World * world = (struct World *) win->data;
114 snprintf(text, 100, "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
115 draw_with_linebreaks(win, text, 0); }
117 extern void draw_keys_win (struct Win * win) {
118 // Draw keybindings window.
119 struct World * world = (struct World *) win->data;
120 uint16_t offset, y, x;
121 offset = center_offset (world->keyswindata->select, world->keyswindata->max, win->frame.size.y - 1);
122 uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
123 char * keydesc = malloc(keydescwidth), * keyname;
125 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
126 if (0 == y && offset > 0) {
127 draw_scroll_hint(&win->frame, y, offset + 1, '^');
129 else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
130 draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
133 if (y == world->keyswindata->select - offset) {
135 if (1 == world->keyswindata->edit)
136 attri = attri | A_BLINK; }
137 keyname = get_keyname(world->keybindings[y + offset].key);
138 snprintf(keydesc, keydescwidth, "%-9s", keyname);
140 for (x = 0; x < win->frame.size.x; x++)
141 if (x < strlen(keydesc))
142 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
143 else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
144 mvwaddch(win->frame.curses_win, y, x,
145 world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
147 mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }