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