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