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