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() */
15 #include "command_db.h" /* for get_command_longdesc() */
19 /* Write "text" into window "win" as far as possible. Start on row "start_y". */
20 static void draw_with_linebreaks(struct Win * win, char * text,
23 /* Write "text" not starting from the top but from the bottom of "win". */
24 static void draw_text_from_bottom(struct Win * win, char * text);
26 /* Draw onto "map" in "win" the objects in the chain at "start". */
27 static void draw_map_objects(struct World * world, struct MapObj * start,
28 struct Map * map, struct Win * win);
32 static void draw_with_linebreaks(struct Win * win, char * text,
39 for (y = start_y; y < win->frame.size.y; y++)
45 for (x = 0; x < win->frame.size.x; x++)
57 mvwaddch(win->frame.curses_win, y, x, text[z]);
59 if ('\n' == text[z+1])
64 else if (0 == text[z+1])
76 static void draw_text_from_bottom (struct Win * win, char * text)
78 /* Determine number of lines text would have in a window of win's width,
79 * but infinite height. Treat \n and \0 as control chars for incrementing
80 * y and stopping the loop. Make sure +they* don't count as cell space.
83 uint16_t x, y, offset;
85 for (y = 0; 0 == toggle; y++)
87 for (x = 0; x < win->frame.size.x; x++)
94 if ('\n' == text[z+1])
99 else if (0 == text[z+1])
108 /* Depending on what's bigger, determine start point in window or text. */
109 uint16_t start_y = 0;
110 if (y < win->frame.size.y)
112 start_y = win->frame.size.y - y;
114 else if (y > win->frame.size.y)
116 offset = y - win->frame.size.y;
117 for (y = 0; y < offset; y++)
119 for (x = 0; x < win->frame.size.x; x++)
126 if ('\n' == text[z+1])
133 text = text + (sizeof(char) * (z + 1));
136 draw_with_linebreaks(win, text, start_y);
141 static void draw_map_objects(struct World * world, struct MapObj * start,
142 struct Map * map, struct Win * win)
145 struct MapObjDef * d;
147 for (o = start; o != 0; o = o->next)
149 if ( o->pos.y >= map->offset.y
150 && o->pos.y < map->offset.y + win->frame.size.y
151 && o->pos.x >= map->offset.x
152 && o->pos.x < map->offset.x + win->frame.size.x)
154 d = get_map_obj_def (world, o->type);
156 mvwaddch(win->frame.curses_win,
157 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
164 extern void draw_log_win(struct Win * win)
166 struct World * world = (struct World *) win->data;
167 draw_text_from_bottom(win, world->log);
172 extern void draw_map_win(struct Win * win)
174 struct World * world = (struct World *) win->data;
175 struct Map * map = world->map;
176 struct Player * player = world->player;
177 char * cells = map->cells;
178 uint16_t width_map_av = map->size.x - map->offset.x;
179 uint16_t height_map_av = map->size.y - map->offset.y;
181 for (y = 0; y < win->frame.size.y; y++)
183 z = map->offset.x + (map->offset.y + y) * (map->size.x);
184 for (x = 0; x < win->frame.size.x; x++)
186 if (y < height_map_av && x < width_map_av)
188 mvwaddch(win->frame.curses_win, y, x, cells[z]);
193 draw_map_objects (world, (struct MapObj *) world->item, map, win);
194 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
195 if ( player->pos.y >= map->offset.y
196 && player->pos.y < map->offset.y + win->frame.size.y
197 && player->pos.x >= map->offset.x
198 && player->pos.x < map->offset.x + win->frame.size.x)
200 mvwaddch(win->frame.curses_win,
201 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
208 extern void draw_info_win(struct Win * win)
210 struct World * world = (struct World *) win->data;
211 char * dsc_turn = "Turn: ";
212 char * dsc_hitpoints = "\nHitpoints: ";
213 char * dsc_score = "\nScore: ";
214 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
215 + 10 + 5 + 10; /* max strlens of numbers to be used */
216 char * text = malloc(maxl + 1);
217 sprintf(text, "%s%d%s%d%s%d",
218 dsc_turn, world->turn,
219 dsc_hitpoints, world->player->hitpoints,
220 dsc_score, world->score);
221 draw_with_linebreaks(win, text, 0);
227 extern void draw_keys_win(struct Win * win)
229 struct World * world = (struct World *) win->data;
230 uint16_t offset, y, x;
231 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
232 win->frame.size.y - 1);
233 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
234 char * keydesc = malloc(keydescwidth), * keyname;
235 char * err_hint = "Trouble with draw_scroll_hint() in draw_keys_win().";
238 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
240 if (0 == y && offset > 0)
242 exit_err(draw_scroll_hint(&win->frame, y, offset + 1, '^'),
246 else if (win->frame.size.y == y + 1
247 && 0 < world->keyswindata->max
248 - (win->frame.size.y + offset - 1))
250 exit_err(draw_scroll_hint(&win->frame, y,
251 world->keyswindata->max
252 - (offset + win->frame.size.y) + 2, 'v'),
257 if (y == world->keyswindata->select - offset)
260 if (1 == world->keyswindata->edit)
262 attri = attri | A_BLINK;
265 keyname = get_keyname(world->keybindings[y + offset].key);
266 snprintf(keydesc, keydescwidth, "%-9s", keyname);
268 cmd_dsc = get_command_longdsc(world,
269 world->keybindings[y + offset].name);
270 for (x = 0; x < win->frame.size.x; x++)
272 if (x < strlen(keydesc))
274 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
276 else if ( strlen(keydesc) < x
277 && x < strlen(cmd_dsc) + strlen(keydesc) + 1)
279 mvwaddch(win->frame.curses_win, y, x,
280 cmd_dsc[x - strlen(keydesc) - 1] | attri);
284 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);