home · contact · privacy
Renamed "objects_on_map" library to "map_objects".
[plomrogue] / src / draw_wins.c
1 #include "draw_wins.h"
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
5 #include <ncurses.h>
6 #include "windows.h"
7 #include "misc.h"
8 #include "keybindings.h"
9 #include "map_objects.h"
10 #include "map.h"
11 #include "main.h"
12
13 static void draw_map_objects (void *, struct Map *, struct Win *);
14
15 extern void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) {
16 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
17   uint16_t x, y;
18   char toggle;
19   char fin = 0;
20   int16_t z = -1;
21   for (y = start_y; y < win->frame.size.y; y++) {
22     if (0 == fin)
23       toggle = 0;
24     for (x = 0; x < win->frame.size.x; x++) {
25        if (0 == toggle) {
26          z++;
27          if ('\n' == text[z]) {
28            toggle = 1;
29            continue; }
30          else
31            mvwaddch(win->frame.curses_win, y, x, text[z]);
32          if ('\n' == text[z+1]) {
33            z++;
34            toggle = 1; }
35          else if (0 == text[z+1]) {
36             toggle = 1;
37             fin = 1; } } } } }
38
39 extern void draw_text_from_bottom (struct Win * win, char * text) {
40 // Draw text from end/bottom to the top.
41   char toggle = 0;
42   uint16_t x, y, offset;
43   int16_t z = -1;
44   for (y = 0; 0 == toggle; y++)                           // Determine number of lines text would have in
45     for (x = 0; x < win->frame.size.x; x++) {             // a window of available width, but infinite height.
46       z++;
47       if ('\n' == text[z])            // Treat \n and \0 as control characters for incrementing y and stopping
48         break;                        // the loop. Make sure they don't count as cell space themselves.
49       if ('\n' == text[z+1]) {
50         z++;
51         break; }
52       else if (0 == text[z+1]) {
53         toggle = 1;
54         break; } }
55   z = -1;
56   uint16_t start_y = 0;
57   if (y < win->frame.size.y)       // Depending on what is bigger, determine start point in window or in text.
58     start_y = win->frame.size.y - y;
59   else if (y > win->frame.size.y) {
60     offset = y - win->frame.size.y;
61     for (y = 0; y < offset; y++)
62       for (x = 0; x < win->frame.size.x; x++) {
63         z++;
64         if ('\n' == text[z])
65           break;
66         if ('\n' == text[z+1]) {
67           z++;
68           break; } }
69     text = text + (sizeof(char) * (z + 1)); }
70   draw_with_linebreaks(win, text, start_y); }
71
72 extern void draw_log_win (struct Win * win) {
73 // Draw log text from world struct in win->data from bottom to top.
74   struct World * world = (struct World *) win->data;
75   draw_text_from_bottom(win, world->log); }
76
77 static void draw_map_objects (void * start, struct Map * map, struct Win * win) {
78 // Draw onto map in win the objects in the chain at start.
79   struct ChainMapObject * cmo;
80   for (cmo = start; cmo != 0; cmo = cmo->next)
81     if (   cmo->pos.y >= map->offset.y && cmo->pos.y < map->offset.y + win->frame.size.y
82         && cmo->pos.x >= map->offset.x && cmo->pos.x < map->offset.x + win->frame.size.x)
83       mvwaddch(win->frame.curses_win, cmo->pos.y - map->offset.y, cmo->pos.x - map->offset.x, cmo->name); }
84
85 extern void draw_map_win (struct Win * win) {
86 // Draw map determined by map (from win->data) and various actors/objects into window. Respect scroll offset.
87   struct World * world = (struct World *) win->data;
88   struct Map * map = world->map;
89   struct Player * player = world->player;
90   char * cells = map->cells;
91   uint16_t width_map_av  = map->size.x  - map->offset.x;
92   uint16_t height_map_av = map->size.y - map->offset.y;
93   uint16_t x, y, z;
94   for (y = 0; y < win->frame.size.y; y++) {
95     z = map->offset.x + (map->offset.y + y) * (map->size.x);
96     for (x = 0; x < win->frame.size.x; x++) {
97       if (y < height_map_av && x < width_map_av) {
98           mvwaddch(win->frame.curses_win, y, x, cells[z]);
99         z++; } } }
100   draw_map_objects (world->item, map, win);
101   draw_map_objects (world->monster, map, win);
102   if (   player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y
103       && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x)
104     mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@'); }
105
106 extern void draw_info_win (struct Win * win) {
107 // Draw info window by appending win->data integer value to "Turn: " display.
108   struct World * world = (struct World *) win->data;
109   char text[100];
110   snprintf(text, 100, "Turn: %d\nHitpoints: %d", world->turn, world->player->hitpoints);
111   draw_with_linebreaks(win, text, 0); }
112
113 extern void draw_keys_win (struct Win * win) {
114 // Draw keybindings window.
115   struct World * world = (struct World *) win->data;
116   uint16_t offset, y, x;
117   offset = center_offset (world->keyswindata->select, world->keyswindata->max, win->frame.size.y - 1);
118   uint8_t keydescwidth = 9 + 1; // max length assured by get_keyname() + \0
119   char * keydesc = malloc(keydescwidth), * keyname;
120   attr_t attri;
121   for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
122     if (0 == y && offset > 0) {
123       draw_scroll_hint(&win->frame, y, offset + 1, '^');
124       continue; }
125     else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
126       draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
127       continue; }
128     attri = 0;
129     if (y == world->keyswindata->select - offset) {
130       attri = A_REVERSE;
131       if (1 == world->keyswindata->edit)
132         attri = attri | A_BLINK; }
133     keyname = get_keyname(world->keybindings[y + offset].key);
134     snprintf(keydesc, keydescwidth, "%-9s", keyname);
135     free(keyname);
136     for (x = 0; x < win->frame.size.x; x++)
137       if (x < strlen(keydesc))
138         mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri);
139       else if (strlen(keydesc) < x && x < strlen(world->keybindings[y + offset].name) + strlen(keydesc) + 1)
140         mvwaddch(win->frame.curses_win, y, x,
141                  world->keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
142       else
143         mvwaddch(win->frame.curses_win, y, x, ' ' | attri); }
144   free(keydesc); }