2 #include <stdlib.h> /* for malloc(), free() */
3 #include <stdint.h> /* for uint16_t */
4 #include <string.h> /* for strlen() */
5 #include <ncurses.h> /* for mvwaddch() */
6 #include "windows.h" /* for structs Win, Frame, for draw_scroll_hint() */
7 #include "misc.h" /* for center_offset() */
8 #include "keybindings.h" /* for struct KeyBinding, for get_keyname() */
9 #include "map_objects.h" /* for structs MapObj, Player */
10 #include "map.h" /* for Map struct */
11 #include "main.h" /* for World struct */
15 /* Draw onto "map" in "win" the objects in the chain at "start". */
16 static void draw_map_objects(struct World * world, struct MapObj * start,
17 struct Map * map, struct Win * win);
21 extern void draw_with_linebreaks(struct Win * win, char * text,
28 for (y = start_y; y < win->frame.size.y; y++)
34 for (x = 0; x < win->frame.size.x; x++)
46 mvwaddch(win->frame.curses_win, y, x, text[z]);
48 if ('\n' == text[z+1])
53 else if (0 == text[z+1])
65 extern void draw_text_from_bottom (struct Win * win, char * text)
67 /* Determine number of lines text would have in a window of win's width,
68 * but infinite height. Treat \n and \0 as control chars for incrementing
69 * y and stopping the loop. Make sure +they* don't count as cell space.
72 uint16_t x, y, offset;
74 for (y = 0; 0 == toggle; y++)
76 for (x = 0; x < win->frame.size.x; x++)
83 if ('\n' == text[z+1])
88 else if (0 == text[z+1])
97 /* Depending on what's bigger, determine start point in window or text. */
99 if (y < win->frame.size.y)
101 start_y = win->frame.size.y - y;
103 else if (y > win->frame.size.y)
105 offset = y - win->frame.size.y;
106 for (y = 0; y < offset; y++)
108 for (x = 0; x < win->frame.size.x; x++)
115 if ('\n' == text[z+1])
122 text = text + (sizeof(char) * (z + 1));
125 draw_with_linebreaks(win, text, start_y);
130 extern void draw_log_win(struct Win * win)
132 struct World * world = (struct World *) win->data;
133 draw_text_from_bottom(win, world->log);
138 extern void draw_map_win(struct Win * win)
140 struct World * world = (struct World *) win->data;
141 struct Map * map = world->map;
142 struct Player * player = world->player;
143 char * cells = map->cells;
144 uint16_t width_map_av = map->size.x - map->offset.x;
145 uint16_t height_map_av = map->size.y - map->offset.y;
147 for (y = 0; y < win->frame.size.y; y++)
149 z = map->offset.x + (map->offset.y + y) * (map->size.x);
150 for (x = 0; x < win->frame.size.x; x++)
152 if (y < height_map_av && x < width_map_av)
154 mvwaddch(win->frame.curses_win, y, x, cells[z]);
159 draw_map_objects (world, (struct MapObj *) world->item, map, win);
160 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
161 if ( player->pos.y >= map->offset.y
162 && player->pos.y < map->offset.y + win->frame.size.y
163 && player->pos.x >= map->offset.x
164 && player->pos.x < map->offset.x + win->frame.size.x)
166 mvwaddch(win->frame.curses_win,
167 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
174 extern void draw_info_win(struct Win * win)
176 struct World * world = (struct World *) win->data;
179 "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
180 draw_with_linebreaks(win, text, 0);
185 extern void draw_keys_win(struct Win * win)
187 struct World * world = (struct World *) win->data;
188 uint16_t offset, y, x;
189 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
190 win->frame.size.y - 1);
191 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
192 char * keydesc = malloc(keydescwidth), * keyname;
194 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
196 if (0 == y && offset > 0)
198 draw_scroll_hint(&win->frame, y, offset + 1, '^');
201 else if (win->frame.size.y == y + 1
202 && 0 < world->keyswindata->max
203 - (win->frame.size.y + offset - 1))
205 draw_scroll_hint(&win->frame, y,
206 world->keyswindata->max
207 - (offset + win->frame.size.y) + 2, 'v');
211 if (y == world->keyswindata->select - offset)
214 if (1 == world->keyswindata->edit)
216 attri = attri | A_BLINK;
219 keyname = get_keyname(world->keybindings[y + offset].key);
220 snprintf(keydesc, keydescwidth, "%-9s", keyname);
222 for (x = 0; x < win->frame.size.x; x++)
224 if (x < strlen(keydesc))
226 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
228 else if (strlen(keydesc) < x
229 && x < strlen(world->keybindings[y + offset].name)
230 + strlen(keydesc) + 1)
232 mvwaddch(win->frame.curses_win, y, x,
233 world->keybindings[y + offset]
234 .name[x - strlen(keydesc) - 1] | attri);
238 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
247 static void draw_map_objects(struct World * world, struct MapObj * start,
248 struct Map * map, struct Win * win)
251 struct MapObjDef * d;
253 for (o = start; o != 0; o = o->next)
255 if ( o->pos.y >= map->offset.y
256 && o->pos.y < map->offset.y + win->frame.size.y
257 && o->pos.x >= map->offset.x
258 && o->pos.x < map->offset.x + win->frame.size.x)
260 d = get_map_obj_def (world, o->type);
262 mvwaddch(win->frame.curses_win,
263 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);