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 */
14 #include "rexit.h" /* for err_exit() */
18 /* Write "text" into window "win" as far as possible. Start on row "start_y". */
19 static void draw_with_linebreaks(struct Win * win, char * text,
22 /* Write "text" not starting from the top but from the bottom of "win". */
23 static void draw_text_from_bottom(struct Win * win, char * text);
25 /* Draw onto "map" in "win" the objects in the chain at "start". */
26 static void draw_map_objects(struct World * world, struct MapObj * start,
27 struct Map * map, struct Win * win);
31 static void draw_with_linebreaks(struct Win * win, char * text,
38 for (y = start_y; y < win->frame.size.y; y++)
44 for (x = 0; x < win->frame.size.x; x++)
56 mvwaddch(win->frame.curses_win, y, x, text[z]);
58 if ('\n' == text[z+1])
63 else if (0 == text[z+1])
75 static void draw_text_from_bottom (struct Win * win, char * text)
77 /* Determine number of lines text would have in a window of win's width,
78 * but infinite height. Treat \n and \0 as control chars for incrementing
79 * y and stopping the loop. Make sure +they* don't count as cell space.
82 uint16_t x, y, offset;
84 for (y = 0; 0 == toggle; y++)
86 for (x = 0; x < win->frame.size.x; x++)
93 if ('\n' == text[z+1])
98 else if (0 == text[z+1])
107 /* Depending on what's bigger, determine start point in window or text. */
108 uint16_t start_y = 0;
109 if (y < win->frame.size.y)
111 start_y = win->frame.size.y - y;
113 else if (y > win->frame.size.y)
115 offset = y - win->frame.size.y;
116 for (y = 0; y < offset; y++)
118 for (x = 0; x < win->frame.size.x; x++)
125 if ('\n' == text[z+1])
132 text = text + (sizeof(char) * (z + 1));
135 draw_with_linebreaks(win, text, start_y);
140 static void draw_map_objects(struct World * world, struct MapObj * start,
141 struct Map * map, struct Win * win)
144 struct MapObjDef * d;
146 for (o = start; o != 0; o = o->next)
148 if ( o->pos.y >= map->offset.y
149 && o->pos.y < map->offset.y + win->frame.size.y
150 && o->pos.x >= map->offset.x
151 && o->pos.x < map->offset.x + win->frame.size.x)
153 d = get_map_obj_def (world, o->type);
155 mvwaddch(win->frame.curses_win,
156 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
163 extern void draw_log_win(struct Win * win)
165 struct World * world = (struct World *) win->data;
166 draw_text_from_bottom(win, world->log);
171 extern void draw_map_win(struct Win * win)
173 struct World * world = (struct World *) win->data;
174 struct Map * map = world->map;
175 struct Player * player = world->player;
176 char * cells = map->cells;
177 uint16_t width_map_av = map->size.x - map->offset.x;
178 uint16_t height_map_av = map->size.y - map->offset.y;
180 for (y = 0; y < win->frame.size.y; y++)
182 z = map->offset.x + (map->offset.y + y) * (map->size.x);
183 for (x = 0; x < win->frame.size.x; x++)
185 if (y < height_map_av && x < width_map_av)
187 mvwaddch(win->frame.curses_win, y, x, cells[z]);
192 draw_map_objects (world, (struct MapObj *) world->item, map, win);
193 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
194 if ( player->pos.y >= map->offset.y
195 && player->pos.y < map->offset.y + win->frame.size.y
196 && player->pos.x >= map->offset.x
197 && player->pos.x < map->offset.x + win->frame.size.x)
199 mvwaddch(win->frame.curses_win,
200 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
207 extern void draw_info_win(struct Win * win)
209 struct World * world = (struct World *) win->data;
212 "Turn: %d\nHitpoints: %d\nScore: %d",
213 world->turn, world->player->hitpoints, world->score);
214 draw_with_linebreaks(win, text, 0);
219 extern void draw_keys_win(struct Win * win)
221 struct World * world = (struct World *) win->data;
222 uint16_t offset, y, x;
223 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
224 win->frame.size.y - 1);
225 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
226 char * keydesc = malloc(keydescwidth), * keyname;
227 char * err_hint = "Trouble with draw_scroll_hint() in draw_keys_win().";
229 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
231 if (0 == y && offset > 0)
233 exit_err(draw_scroll_hint(&win->frame, y, offset + 1, '^'),
237 else if (win->frame.size.y == y + 1
238 && 0 < world->keyswindata->max
239 - (win->frame.size.y + offset - 1))
241 exit_err(draw_scroll_hint(&win->frame, y,
242 world->keyswindata->max
243 - (offset + win->frame.size.y) + 2, 'v'),
248 if (y == world->keyswindata->select - offset)
251 if (1 == world->keyswindata->edit)
253 attri = attri | A_BLINK;
256 keyname = get_keyname(world->keybindings[y + offset].key);
257 snprintf(keydesc, keydescwidth, "%-9s", keyname);
259 for (x = 0; x < win->frame.size.x; x++)
261 if (x < strlen(keydesc))
263 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
265 else if (strlen(keydesc) < x
266 && x < strlen(world->keybindings[y + offset].name)
267 + strlen(keydesc) + 1)
269 mvwaddch(win->frame.curses_win, y, x,
270 world->keybindings[y + offset]
271 .name[x - strlen(keydesc) - 1] | attri);
275 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);