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() */
16 #include "wincontrol.h" /* for WinConf struct, get_winconf_by_win() */
20 /* Write "text" into window "win" as far as possible. Start on row "start_y". */
21 static void draw_with_linebreaks(struct Win * win, char * text,
24 /* Write "text" not starting from the top but from the bottom of "win". */
25 static void draw_text_from_bottom(struct Win * win, char * text);
27 /* Draw onto "map" in "win" the objects in the chain at "start". */
28 static void draw_map_objects(struct World * world, struct MapObj * start,
29 struct Map * map, struct Win * win);
33 static void draw_with_linebreaks(struct Win * win, char * text,
40 for (y = start_y; y < win->frame.size.y; y++)
46 for (x = 0; x < win->frame.size.x; x++)
58 mvwaddch(win->frame.curses_win, y, x, text[z]);
60 if ('\n' == text[z+1])
65 else if (0 == text[z+1])
77 static void draw_text_from_bottom (struct Win * win, char * text)
79 /* Determine number of lines text would have in a window of win's width,
80 * but infinite height. Treat \n and \0 as control chars for incrementing
81 * y and stopping the loop. Make sure +they* don't count as cell space.
84 uint16_t x, y, offset;
86 for (y = 0; 0 == toggle; y++)
88 for (x = 0; x < win->frame.size.x; x++)
95 if ('\n' == text[z+1])
100 else if (0 == text[z+1])
109 /* Depending on what's bigger, determine start point in window or text. */
110 uint16_t start_y = 0;
111 if (y < win->frame.size.y)
113 start_y = win->frame.size.y - y;
115 else if (y > win->frame.size.y)
117 offset = y - win->frame.size.y;
118 for (y = 0; y < offset; y++)
120 for (x = 0; x < win->frame.size.x; x++)
127 if ('\n' == text[z+1])
134 text = text + (sizeof(char) * (z + 1));
137 draw_with_linebreaks(win, text, start_y);
142 static void draw_map_objects(struct World * world, struct MapObj * start,
143 struct Map * map, struct Win * win)
146 struct MapObjDef * d;
148 for (o = start; o != 0; o = o->next)
150 if ( o->pos.y >= map->offset.y
151 && o->pos.y < map->offset.y + win->frame.size.y
152 && o->pos.x >= map->offset.x
153 && o->pos.x < map->offset.x + win->frame.size.x)
155 d = get_map_obj_def (world, o->type);
157 mvwaddch(win->frame.curses_win,
158 o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
165 extern void draw_log_win(struct Win * win)
167 struct World * world = (struct World *) win->data;
168 draw_text_from_bottom(win, world->log);
173 extern void draw_map_win(struct Win * win)
175 struct World * world = (struct World *) win->data;
176 struct Map * map = world->map;
177 struct Player * player = world->player;
178 char * cells = map->cells;
179 uint16_t width_map_av = map->size.x - map->offset.x;
180 uint16_t height_map_av = map->size.y - map->offset.y;
182 for (y = 0; y < win->frame.size.y; y++)
184 z = map->offset.x + (map->offset.y + y) * (map->size.x);
185 for (x = 0; x < win->frame.size.x; x++)
187 if (y < height_map_av && x < width_map_av)
189 mvwaddch(win->frame.curses_win, y, x, cells[z]);
194 draw_map_objects (world, (struct MapObj *) world->item, map, win);
195 draw_map_objects (world, (struct MapObj *) world->monster, map, win);
196 if ( player->pos.y >= map->offset.y
197 && player->pos.y < map->offset.y + win->frame.size.y
198 && player->pos.x >= map->offset.x
199 && player->pos.x < map->offset.x + win->frame.size.x)
201 mvwaddch(win->frame.curses_win,
202 player->pos.y - map->offset.y, player->pos.x - map->offset.x,
209 extern void draw_info_win(struct Win * win)
211 struct World * world = (struct World *) win->data;
212 char * dsc_turn = "Turn: ";
213 char * dsc_hitpoints = "\nHitpoints: ";
214 char * dsc_score = "\nScore: ";
215 uint16_t maxl = strlen(dsc_turn) + strlen(dsc_hitpoints) + strlen(dsc_score)
216 + 10 + 5 + 10; /* max strlens of numbers to be used */
217 char * text = malloc(maxl + 1);
218 sprintf(text, "%s%d%s%d%s%d",
219 dsc_turn, world->turn,
220 dsc_hitpoints, world->player->hitpoints,
221 dsc_score, world->score);
222 draw_with_linebreaks(win, text, 0);
228 extern void draw_keys_win(struct Win * win)
230 struct World * world = (struct World *) win->data;
231 uint16_t offset, y, x;
232 offset = center_offset(world->keyswindata->select, world->keyswindata->max,
233 win->frame.size.y - 1);
234 uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
235 char * keydesc = malloc(keydescwidth), * keyname;
236 char * err_hint = "Trouble with draw_scroll_hint() in draw_keys_win().";
239 for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
241 if (0 == y && offset > 0)
243 exit_err(draw_scroll_hint(&win->frame, y, offset + 1, '^'),
247 else if (win->frame.size.y == y + 1
248 && 0 < world->keyswindata->max
249 - (win->frame.size.y + offset - 1))
251 exit_err(draw_scroll_hint(&win->frame, y,
252 world->keyswindata->max
253 - (offset + win->frame.size.y) + 2, 'v'),
258 if (y == world->keyswindata->select - offset)
261 if (1 == world->keyswindata->edit)
263 attri = attri | A_BLINK;
266 keyname = get_keyname(world->keybindings[y + offset].key);
267 snprintf(keydesc, keydescwidth, "%-9s", keyname);
269 cmd_dsc = get_command_longdsc(world,
270 world->keybindings[y + offset].name);
271 for (x = 0; x < win->frame.size.x; x++)
273 if (x < strlen(keydesc))
275 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
277 else if ( strlen(keydesc) < x
278 && x < strlen(cmd_dsc) + strlen(keydesc) + 1)
280 mvwaddch(win->frame.curses_win, y, x,
281 cmd_dsc[x - strlen(keydesc) - 1] | attri);
285 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
294 extern void draw_winconf(struct Win * win)
296 struct World * world = (struct World *) win->data;
297 struct WinConf * wcp = get_winconf_by_win(world, win);
298 char * title = "Window configuration:\n";
299 char * h_t_d = "\nWill save height as: ";
300 char * h_pos = "height in positive cells";
301 char * h_neg = "negative diff to maximum height";
302 char * h_d = "\nHeight to be saved: ";
303 char * w_t_d = "\n\nWill save width as: ";
304 char * w_pos = "width in positive cells";
305 char * w_neg = "negative diff to maximum width";
306 char * w_d = "\nWidth to be saved: ";
309 if (1 == wcp->height_type)
313 if (1 == wcp->width_type)
317 uint16_t maxl = strlen(title)
318 + strlen(h_t_d) + strlen(h_t) + strlen(h_d) + 6
319 + strlen(w_t_d) + strlen(w_t) + strlen(w_d) + 6 + 1;
320 char * text = malloc(maxl + 1);
321 sprintf(text, "%s%s%s%s%d%s%s%s%d", title, h_t_d, h_t, h_d, wcp->height,
322 w_t_d, w_t, w_d, wcp->width);
323 draw_with_linebreaks(win, text, 0);