4 #include <stdlib.h> /* for malloc(), free() */
5 #include <stdint.h> /* for uint16_t */
6 #include <string.h> /* for strlen() */
7 #include <ncurses.h> /* for mvwaddch() */
8 #include "windows.h" /* for structs Win, Frame, for draw_scroll_hint() */
9 #include "misc.h" /* for center_offset() */
10 #include "keybindings.h" /* for struct KeyBinding, for get_keyname() */
11 #include "map_objects.h" /* for structs MapObj, Player */
12 #include "map.h" /* for Map struct */
13 #include "main.h" /* for World struct */
17 /* Draw onto "map" in "win" the objects in the chain at "start". */
18 static void draw_map_objects(struct World * world, struct MapObj * start,
19 struct Map * map, struct Win * win);
23 extern void draw_with_linebreaks(struct Win * win, char * text,
30 for (y = start_y; y < win->frame.size.y; y++)
36 for (x = 0; x < win->frame.size.x; x++)
48 mvwaddch(win->frame.curses_win, y, x, text[z]);
50 if ('\n' == text[z+1])
55 else if (0 == text[z+1])
67 extern void draw_text_from_bottom (struct Win * win, char * text)
69 /* Determine number of lines text would have in a window of win's width,
70 * but infinite height. Treat \n and \0 as control chars for incrementing
71 * y and stopping the loop. Make sure +they* don't count as cell space.
74 uint16_t x, y, offset;
76 for (y = 0; 0 == toggle; y++)
78 for (x = 0; x < win->frame.size.x; x++)
85 if ('\n' == text[z+1])
90 else if (0 == text[z+1])
99 /* Depending on what's bigger, determine start point in window or text. */
100 uint16_t start_y = 0;
101 if (y < win->frame.size.y)
103 start_y = win->frame.size.y - y;
105 else if (y > win->frame.size.y)
107 offset = y - win->frame.size.y;
108 for (y = 0; y < offset; y++)
110 for (x = 0; x < win->frame.size.x; x++)
117 if ('\n' == text[z+1])
124 text = text + (sizeof(char) * (z + 1));
127 draw_with_linebreaks(win, text, start_y);
132 extern void draw_log_win(struct Win * win)
134 struct World * world = (struct World *) win->data;
135 draw_text_from_bottom(win, world->log);
140 extern void draw_map_win(struct Win * win)
142 struct World * world = (struct World *) win->data;
143 struct Map * map = world->map;
144 struct Player * player = world->player;
145 char * cells = map->cells;
146 uint16_t width_map_av = map->size.x - map->offset.x;
147 uint16_t height_map_av = map->size.y - map->offset.y;
149 for (y = 0; y < win->frame.size.y; y++)
151 z = map->offset.x + (map->offset.y + y) * (map->size.x);
152 for (x = 0; x < win->frame.size.x; x++)
154 if (y < height_map_av && x < width_map_av)
156 mvwaddch(win->frame.curses_win, y, x, cells[z]);
161 draw_map_objects (world, (struct MapObj *) world->item, map, win);
162 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
163 if ( player->pos.y >= map->offset.y
164 && player->pos.y < map->offset.y + win->frame.size.y
165 && player->pos.x >= map->offset.x
166 && player->pos.x < map->offset.x + win->frame.size.x)
168 mvwaddch(win->frame.curses_win,
169 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
176 extern void draw_info_win(struct Win * win)
178 struct World * world = (struct World *) win->data;
181 "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
182 draw_with_linebreaks(win, text, 0);
187 extern void draw_keys_win(struct Win * win)
189 struct World * world = (struct World *) win->data;
190 uint16_t offset, y, x;
191 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
192 win->frame.size.y - 1);
193 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
194 char * keydesc = malloc(keydescwidth), * keyname;
196 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
198 if (0 == y && offset > 0)
200 draw_scroll_hint(&win->frame, y, offset + 1, '^');
203 else if (win->frame.size.y == y + 1
204 && 0 < world->keyswindata->max
205 - (win->frame.size.y + offset - 1))
207 draw_scroll_hint(&win->frame, y,
208 world->keyswindata->max
209 - (offset + win->frame.size.y) + 2, 'v');
213 if (y == world->keyswindata->select - offset)
216 if (1 == world->keyswindata->edit)
218 attri = attri | A_BLINK;
221 keyname = get_keyname(world->keybindings[y + offset].key);
222 snprintf(keydesc, keydescwidth, "%-9s", keyname);
224 for (x = 0; x < win->frame.size.x; x++)
226 if (x < strlen(keydesc))
228 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
230 else if (strlen(keydesc) < x
231 && x < strlen(world->keybindings[y + offset].name)
232 + strlen(keydesc) + 1)
234 mvwaddch(win->frame.curses_win, y, x,
235 world->keybindings[y + offset]
236 .name[x - strlen(keydesc) - 1] | attri);
240 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
249 static void draw_map_objects(struct World * world, struct MapObj * start,
250 struct Map * map, struct Win * win)
253 struct MapObjDef * d;
255 for (o = start; o != 0; o = o->next)
257 if ( o->pos.y >= map->offset.y
258 && o->pos.y < map->offset.y + win->frame.size.y
259 && o->pos.x >= map->offset.x
260 && o->pos.x < map->offset.x + win->frame.size.x)
262 d = get_map_obj_def (world, o->type);
264 mvwaddch(win->frame.curses_win,
265 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);