home · contact · privacy
Added file new comment to draw_wins.c
[plomrogue] / src / draw_wins.c
1 /* draw_wins.c */
2
3 #include "draw_wins.h"
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
15
16
17 /* Draw onto "map" in "win" the objects in the chain at "start". */
18 static void draw_map_objects(struct World * world, struct MapObj * start,
19                              struct Map * map, struct Win * win);
20
21
22
23 extern void draw_with_linebreaks(struct Win * win, char * text,
24                                  uint16_t start_y)
25 {
26     uint16_t x, y;
27     char toggle;
28     char fin = 0;
29     int16_t z = -1;
30     for (y = start_y; y < win->frame.size.y; y++)
31     {
32         if (0 == fin)
33         {
34             toggle = 0;
35         }
36         for (x = 0; x < win->frame.size.x; x++)
37         {
38             if (0 == toggle)
39             {
40                 z++;
41                 if ('\n' == text[z])
42                 {
43                     toggle = 1;
44                     continue;
45                 }
46                 else
47                 {
48                     mvwaddch(win->frame.curses_win, y, x, text[z]);
49                 }
50                 if ('\n' == text[z+1])
51                 {
52                     z++;
53                     toggle = 1;
54                 }
55                 else if (0 == text[z+1])
56                 {
57                     toggle = 1;
58                     fin = 1;
59                 }
60             }
61         }
62     }
63 }
64
65
66
67 extern void draw_text_from_bottom (struct Win * win, char * text)
68 {
69     /* Determine number of lines text would have in a window of win's width,
70      * but infinite height. Treat \n and \0 as control chars for incrementing
71      * y and stopping the loop. Make sure +they* don't count as cell space.
72      */
73     char toggle = 0;
74     uint16_t x, y, offset;
75     int16_t z = -1;
76     for (y = 0; 0 == toggle; y++)
77     {
78         for (x = 0; x < win->frame.size.x; x++)
79         {
80             z++;
81             if ('\n' == text[z])
82             {
83                 break;
84             }
85             if ('\n' == text[z+1])
86             {
87                 z++;
88                 break;
89             }
90             else if (0 == text[z+1])
91             {
92                 toggle = 1;
93                 break;
94             }
95         }
96     }
97     z = -1;
98
99     /* Depending on what's bigger, determine start point in window or text. */
100     uint16_t start_y = 0;
101     if (y < win->frame.size.y)
102     {
103         start_y = win->frame.size.y - y;
104     }
105     else if (y > win->frame.size.y)
106     {
107         offset = y - win->frame.size.y;
108         for (y = 0; y < offset; y++)
109         {
110             for (x = 0; x < win->frame.size.x; x++)
111             {
112                 z++;
113                 if ('\n' == text[z])
114                 {
115                     break;
116                 }
117                 if ('\n' == text[z+1])
118                 {
119                     z++;
120                     break;
121                 }
122             }
123         }
124         text = text + (sizeof(char) * (z + 1));
125     }
126
127     draw_with_linebreaks(win, text, start_y);
128 }
129
130
131
132 extern void draw_log_win(struct Win * win)
133 {
134     struct World * world = (struct World *) win->data;
135     draw_text_from_bottom(win, world->log);
136 }
137
138
139
140 extern void draw_map_win(struct Win * win)
141 {
142     struct World * world = (struct World *) win->data;
143     struct Map * map = world->map;
144     struct Player * player = world->player;
145     char * cells = map->cells;
146     uint16_t width_map_av  = map->size.x  - map->offset.x;
147     uint16_t height_map_av = map->size.y - map->offset.y;
148     uint16_t x, y, z;
149     for (y = 0; y < win->frame.size.y; y++)
150     {
151         z = map->offset.x + (map->offset.y + y) * (map->size.x);
152         for (x = 0; x < win->frame.size.x; x++)
153         {
154             if (y < height_map_av && x < width_map_av)
155             {
156                 mvwaddch(win->frame.curses_win, y, x, cells[z]);
157                 z++;
158             }
159         }
160     }
161     draw_map_objects (world, (struct MapObj *) world->item, map, win);
162     draw_map_objects (world, (struct MapObj *) world->monster, map, win);
163     if (   player->pos.y >= map->offset.y
164         && player->pos.y < map->offset.y + win->frame.size.y
165         && player->pos.x >= map->offset.x
166         && player->pos.x < map->offset.x + win->frame.size.x)
167     {
168         mvwaddch(win->frame.curses_win,
169                  player->pos.y - map->offset.y, player->pos.x - map->offset.x,
170                  '@');
171     }
172 }
173
174
175
176 extern void draw_info_win(struct Win * win)
177 {
178     struct World * world = (struct World *) win->data;
179     char text[100];
180     snprintf(text, 100,
181              "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
182     draw_with_linebreaks(win, text, 0);
183 }
184
185
186
187 extern void draw_keys_win(struct Win * win)
188 {
189     struct World * world = (struct World *) win->data;
190     uint16_t offset, y, x;
191     offset = center_offset(world->keyswindata->select, world->keyswindata->max,
192                            win->frame.size.y - 1);
193     uint8_t keydescwidth = 9 + 1; /* max length assured by get_keyname() + \0 */
194     char * keydesc = malloc(keydescwidth), * keyname;
195     attr_t attri;
196     for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++)
197     {
198         if (0 == y && offset > 0)
199         {
200             draw_scroll_hint(&win->frame, y, offset + 1, '^');
201             continue;
202         }
203         else if (win->frame.size.y == y + 1
204                  && 0 < world->keyswindata->max
205                         - (win->frame.size.y + offset - 1))
206         {
207             draw_scroll_hint(&win->frame, y,
208                              world->keyswindata->max
209                              - (offset + win->frame.size.y) + 2, 'v');
210             continue;
211         }
212         attri = 0;
213         if (y == world->keyswindata->select - offset)
214         {
215             attri = A_REVERSE;
216             if (1 == world->keyswindata->edit)
217             {
218                 attri = attri | A_BLINK;
219             }
220         }
221         keyname = get_keyname(world->keybindings[y + offset].key);
222         snprintf(keydesc, keydescwidth, "%-9s", keyname);
223         free(keyname);
224         for (x = 0; x < win->frame.size.x; x++)
225         {
226             if (x < strlen(keydesc))
227             {
228                 mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
229             }
230             else if (strlen(keydesc) < x
231                      && x < strlen(world->keybindings[y + offset].name)
232                             + strlen(keydesc) + 1)
233             {
234                 mvwaddch(win->frame.curses_win, y, x,
235                          world->keybindings[y + offset]
236                          .name[x - strlen(keydesc) - 1] | attri);
237             }
238             else
239             {
240                 mvwaddch(win->frame.curses_win, y, x, ' ' | attri);
241             }
242         }
243     }
244     free(keydesc);
245 }
246
247
248
249 static void draw_map_objects(struct World * world, struct MapObj * start,
250                              struct Map * map, struct Win * win)
251 {
252     struct MapObj * o;
253     struct MapObjDef * d;
254     char c;
255     for (o = start; o != 0; o = o->next)
256     {
257         if (   o->pos.y >= map->offset.y
258             && o->pos.y < map->offset.y + win->frame.size.y
259             && o->pos.x >= map->offset.x
260             && o->pos.x < map->offset.x + win->frame.size.x)
261         {
262             d = get_map_obj_def (world, o->type);
263             c = d->mapchar;
264             mvwaddch(win->frame.curses_win,
265                      o->pos.y - map->offset.y, o->pos.x - map->offset.x, c);
266         }
267     }
268 }