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 /* Write "text" into window "win" as far as possible. Start on row "start_y". */
18 static void draw_with_linebreaks(struct Win * win, char * text,
21 /* Write "text" not starting from the top but from the bottom of "win". */
22 static void draw_text_from_bottom(struct Win * win, char * text);
24 /* Draw onto "map" in "win" the objects in the chain at "start". */
25 static void draw_map_objects(struct World * world, struct MapObj * start,
26 struct Map * map, struct Win * win);
30 extern void draw_log_win(struct Win * win)
32 struct World * world = (struct World *) win->data;
33 draw_text_from_bottom(win, world->log);
38 extern void draw_map_win(struct Win * win)
40 struct World * world = (struct World *) win->data;
41 struct Map * map = world->map;
42 struct Player * player = world->player;
43 char * cells = map->cells;
44 uint16_t width_map_av = map->size.x - map->offset.x;
45 uint16_t height_map_av = map->size.y - map->offset.y;
47 for (y = 0; y < win->frame.size.y; y++)
49 z = map->offset.x + (map->offset.y + y) * (map->size.x);
50 for (x = 0; x < win->frame.size.x; x++)
52 if (y < height_map_av && x < width_map_av)
54 mvwaddch(win->frame.curses_win, y, x, cells[z]);
59 draw_map_objects (world, (struct MapObj *) world->item, map, win);
60 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
61 if ( player->pos.y >= map->offset.y
62 && player->pos.y < map->offset.y + win->frame.size.y
63 && player->pos.x >= map->offset.x
64 && player->pos.x < map->offset.x + win->frame.size.x)
66 mvwaddch(win->frame.curses_win,
67 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
74 extern void draw_info_win(struct Win * win)
76 struct World * world = (struct World *) win->data;
79 "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
80 draw_with_linebreaks(win, text, 0);
85 extern void draw_keys_win(struct Win * win)
87 struct World * world = (struct World *) win->data;
88 uint16_t offset, y, x;
89 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
90 win->frame.size.y - 1);
91 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
92 char * keydesc = malloc(keydescwidth), * keyname;
94 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
96 if (0 == y && offset > 0)
98 draw_scroll_hint(&win->frame, y, offset + 1, '^');
101 else if (win->frame.size.y == y + 1
102 && 0 < world->keyswindata->max
103 - (win->frame.size.y + offset - 1))
105 draw_scroll_hint(&win->frame, y,
106 world->keyswindata->max
107 - (offset + win->frame.size.y) + 2, 'v');
111 if (y == world->keyswindata->select - offset)
114 if (1 == world->keyswindata->edit)
116 attri = attri | A_BLINK;
119 keyname = get_keyname(world->keybindings[y + offset].key);
120 snprintf(keydesc, keydescwidth, "%-9s", keyname);
122 for (x = 0; x < win->frame.size.x; x++)
124 if (x < strlen(keydesc))
126 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
128 else if (strlen(keydesc) < x
129 && x < strlen(world->keybindings[y + offset].name)
130 + strlen(keydesc) + 1)
132 mvwaddch(win->frame.curses_win, y, x,
133 world->keybindings[y + offset]
134 .name[x - strlen(keydesc) - 1] | attri);
138 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
147 static void draw_with_linebreaks(struct Win * win, char * text,
154 for (y = start_y; y < win->frame.size.y; y++)
160 for (x = 0; x < win->frame.size.x; x++)
172 mvwaddch(win->frame.curses_win, y, x, text[z]);
174 if ('\n' == text[z+1])
179 else if (0 == text[z+1])
191 static void draw_text_from_bottom (struct Win * win, char * text)
193 /* Determine number of lines text would have in a window of win's width,
194 * but infinite height. Treat \n and \0 as control chars for incrementing
195 * y and stopping the loop. Make sure +they* don't count as cell space.
198 uint16_t x, y, offset;
200 for (y = 0; 0 == toggle; y++)
202 for (x = 0; x < win->frame.size.x; x++)
209 if ('\n' == text[z+1])
214 else if (0 == text[z+1])
223 /* Depending on what's bigger, determine start point in window or text. */
224 uint16_t start_y = 0;
225 if (y < win->frame.size.y)
227 start_y = win->frame.size.y - y;
229 else if (y > win->frame.size.y)
231 offset = y - win->frame.size.y;
232 for (y = 0; y < offset; y++)
234 for (x = 0; x < win->frame.size.x; x++)
241 if ('\n' == text[z+1])
248 text = text + (sizeof(char) * (z + 1));
251 draw_with_linebreaks(win, text, start_y);
256 static void draw_map_objects(struct World * world, struct MapObj * start,
257 struct Map * map, struct Win * win)
260 struct MapObjDef * d;
262 for (o = start; o != 0; o = o->next)
264 if ( o->pos.y >= map->offset.y
265 && o->pos.y < map->offset.y + win->frame.size.y
266 && o->pos.x >= map->offset.x
267 && o->pos.x < map->offset.x + win->frame.size.x)
269 d = get_map_obj_def (world, o->type);
271 mvwaddch(win->frame.curses_win,
272 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);