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