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