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