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 static void draw_with_linebreaks(struct Win * win, char * text,
37 for (y = start_y; y < win->frame.size.y; y++)
43 for (x = 0; x < win->frame.size.x; x++)
55 mvwaddch(win->frame.curses_win, y, x, text[z]);
57 if ('\n' == text[z+1])
62 else if (0 == text[z+1])
74 static void draw_text_from_bottom (struct Win * win, char * text)
76 /* Determine number of lines text would have in a window of win's width,
77 * but infinite height. Treat \n and \0 as control chars for incrementing
78 * y and stopping the loop. Make sure +they* don't count as cell space.
81 uint16_t x, y, offset;
83 for (y = 0; 0 == toggle; y++)
85 for (x = 0; x < win->frame.size.x; x++)
92 if ('\n' == text[z+1])
97 else if (0 == text[z+1])
106 /* Depending on what's bigger, determine start point in window or text. */
107 uint16_t start_y = 0;
108 if (y < win->frame.size.y)
110 start_y = win->frame.size.y - y;
112 else if (y > win->frame.size.y)
114 offset = y - win->frame.size.y;
115 for (y = 0; y < offset; y++)
117 for (x = 0; x < win->frame.size.x; x++)
124 if ('\n' == text[z+1])
131 text = text + (sizeof(char) * (z + 1));
134 draw_with_linebreaks(win, text, start_y);
139 static void draw_map_objects(struct World * world, struct MapObj * start,
140 struct Map * map, struct Win * win)
143 struct MapObjDef * d;
145 for (o = start; o != 0; o = o->next)
147 if ( o->pos.y >= map->offset.y
148 && o->pos.y < map->offset.y + win->frame.size.y
149 && o->pos.x >= map->offset.x
150 && o->pos.x < map->offset.x + win->frame.size.x)
152 d = get_map_obj_def (world, o->type);
154 mvwaddch(win->frame.curses_win,
155 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
162 extern void draw_log_win(struct Win * win)
164 struct World * world = (struct World *) win->data;
165 draw_text_from_bottom(win, world->log);
170 extern void draw_map_win(struct Win * win)
172 struct World * world = (struct World *) win->data;
173 struct Map * map = world->map;
174 struct Player * player = world->player;
175 char * cells = map->cells;
176 uint16_t width_map_av = map->size.x - map->offset.x;
177 uint16_t height_map_av = map->size.y - map->offset.y;
179 for (y = 0; y < win->frame.size.y; y++)
181 z = map->offset.x + (map->offset.y + y) * (map->size.x);
182 for (x = 0; x < win->frame.size.x; x++)
184 if (y < height_map_av && x < width_map_av)
186 mvwaddch(win->frame.curses_win, y, x, cells[z]);
191 draw_map_objects (world, (struct MapObj *) world->item, map, win);
192 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
193 if ( player->pos.y >= map->offset.y
194 && player->pos.y < map->offset.y + win->frame.size.y
195 && player->pos.x >= map->offset.x
196 && player->pos.x < map->offset.x + win->frame.size.x)
198 mvwaddch(win->frame.curses_win,
199 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
206 extern void draw_info_win(struct Win * win)
208 struct World * world = (struct World *) win->data;
211 "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
212 draw_with_linebreaks(win, text, 0);
217 extern void draw_keys_win(struct Win * win)
219 struct World * world = (struct World *) win->data;
220 uint16_t offset, y, x;
221 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
222 win->frame.size.y - 1);
223 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
224 char * keydesc = malloc(keydescwidth), * keyname;
226 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
228 if (0 == y && offset > 0)
230 draw_scroll_hint(&win->frame, y, offset + 1, '^');
233 else if (win->frame.size.y == y + 1
234 && 0 < world->keyswindata->max
235 - (win->frame.size.y + offset - 1))
237 draw_scroll_hint(&win->frame, y,
238 world->keyswindata->max
239 - (offset + win->frame.size.y) + 2, 'v');
243 if (y == world->keyswindata->select - offset)
246 if (1 == world->keyswindata->edit)
248 attri = attri | A_BLINK;
251 keyname = get_keyname(world->keybindings[y + offset].key);
252 snprintf(keydesc, keydescwidth, "%-9s", keyname);
254 for (x = 0; x < win->frame.size.x; x++)
256 if (x < strlen(keydesc))
258 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
260 else if (strlen(keydesc) < x
261 && x < strlen(world->keybindings[y + offset].name)
262 + strlen(keydesc) + 1)
264 mvwaddch(win->frame.curses_win, y, x,
265 world->keybindings[y + offset]
266 .name[x - strlen(keydesc) - 1] | attri);
270 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);