home · contact · privacy
Minor comment expansion.
[plomrogue] / roguelike.c
1 #include <ncurses.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "windows.h"
6
7 struct Map {
8   int width;
9   int height;
10   int offset_x;
11   int offset_y;
12   int player_x;
13   int player_y;
14   char * cells; };
15
16 void draw_with_linebreaks (struct Win * win, char * text, int start_y) {
17 // Write text into window content space. Start on row start_y. Fill unused rows with whitespace.
18   int x, y;
19   char toggle;
20   int height_av = win->height - 1;
21   int width_av = win->width - 1;
22   char fin = 0;
23   int z = -1;
24   for (y = start_y; y < height_av; y++) {
25     if (0 == fin)
26       toggle = 0;
27     for (x = 0; x < width_av; x++) {
28        if (0 == toggle) {
29          z++;
30          if ('\n' == text[z]) {
31            mvwaddch(win->curses_win, y+1, x+win->border_left, ' ');
32            toggle = 1;
33            continue; }
34          else
35            mvwaddch(win->curses_win, y+1, x+win->border_left, text[z]);
36          if ('\n' == text[z+1]) {
37            z++;
38            toggle = 1; }
39          else if (0 == text[z+1]) {
40             toggle = 1;
41             fin = 1; } }
42        else
43          mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
44
45 void draw_text_from_bottom (struct Win * win) {
46 // Draw text in win->data from end/bottom to the top.
47   char * text = (char *) win->data;
48   int width_av = win->width - 1;
49   int height_av = win->height - 1;
50   char toggle = 0;
51   int x, y, offset;
52   int z = -1;
53   for (y = 0; 0 == toggle; y++)         // Determine number of lines text would have in a window of available
54     for (x = 0; x < width_av; x++) {    // width, but infinite height.
55       z++;
56       if ('\n' == text[z])              // Treat \n and \0 as control characters for incrementing y and
57         break;                          // stopping the loop. Make sure they don't count as cell space
58       if ('\n' == text[z+1]) {          // themselves.
59         z++;
60         break; }
61       else if (0 == text[z+1]) {
62         toggle = 1;
63         break; } }
64   z = -1;
65   int start_y = 0;
66   if (y < height_av) {             // Depending on what is bigger, determine start point in window or in text.
67     start_y = height_av - y;
68     for (y = 0; y < start_y; y++)
69       for (x = 0; x < width_av; x++)
70         mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); }
71   else if (y > height_av) {
72     offset = y - height_av;
73     for (y = 0; y < offset; y++)
74       for (x = 0; x < width_av; x++) {
75         z++;
76         if ('\n' == text[z])
77           break;
78         if ('\n' == text[z+1]) {
79           z++;
80           break; } }
81     text = text + (sizeof(char) * (z + 1)); }
82   draw_with_linebreaks(win, text, start_y); }
83
84 void draw_map (struct Win * win) {
85 // Draw map determined by win->data Map struct into window. Respect offset.
86   int height_av = win->height - 1;
87   int width_av = win->width - 1;
88   struct Map map = * (struct Map *) win->data;
89   char * cells = map.cells;
90   int width_map_av = map.width - map.offset_x;
91   int height_map_av = map.height - map.offset_y;
92   int x, y, z;
93   for (y = 0; y < height_av; y++) {
94     z = map.offset_x + (map.offset_y + y) * (map.width);
95     for (x = 0; x < width_av; x++) {
96       if (y < height_map_av && x < width_map_av) {
97         if (z == (map.width * map.player_y) + map.player_x)
98           mvwaddch(win->curses_win, y+1, x+win->border_left, '@');
99         else
100           mvwaddch(win->curses_win, y+1, x+win->border_left, cells[z]);
101         z++; }
102       else
103         mvwaddch(win->curses_win, y+1, x+win->border_left, ' '); } } }
104
105 void draw_info (struct Win * win) {
106 // Draw info window by appending win->data integer value to "Turn: " display.
107   int count = * (int *) win->data;
108   char text[100];
109   snprintf(text, 100, "Turn: %d", count);
110   draw_with_linebreaks(win, text, 0); }
111
112 void toggle_window (struct WinMeta * win_meta, struct Win * win) {
113 // Toggle display of window win.
114   if (0 != win->curses_win)
115     suspend_window(win_meta, win);
116   else
117     append_window(win_meta, win); }
118
119 struct Map init_map () {
120 // Initialize map with some experimental start values.
121   struct Map map;
122   map.width = 128;
123   map.height = 128;
124   map.offset_x = 0;
125   map.offset_y = 0;
126   map.player_x = 2;
127   map.player_y = 2;
128   map.cells = malloc(map.width * map.height);
129   int x, y;
130   for (y = 0; y < map.height; y++)
131     for (x = 0; x < map.width; x++)
132       map.cells[(y * map.width) + x] = '.';
133   map.cells[(5 * map.width) + 5] = 'X';
134   map.cells[(3 * map.width) + 8] = 'X';
135   map.cells[(8 * map.width) + 3] = 'X';
136   return map; }
137
138 void update_info (struct Win * win) {
139 // Update info data by incrementing turn value.
140   * (int *) win->data = * (int *) win->data + 1; }
141
142 void update_log (struct Win * win, char * text) {
143 // Update log with new text to be appended.
144   char * new_text;
145   int len_old = strlen(win->data);
146   int len_new = strlen(text);
147   int len_whole = len_old + len_new + 1;
148   new_text = calloc(len_whole, sizeof(char));
149   memcpy(new_text, win->data, len_old);
150   memcpy(new_text + len_old, text, len_new);
151   free(win->data);
152   win->data = new_text; }
153
154 int main () {
155   WINDOW * screen = initscr();
156   noecho();
157   curs_set(0);
158   struct WinMeta win_meta = init_win_meta(screen);
159
160   struct Win win_map = init_window(&win_meta, "Map");
161   win_map.draw = draw_map;
162   struct Map map = init_map();
163   win_map.data = &map;
164
165   struct Win win_info = init_window(&win_meta, "Info");
166   win_info.draw = draw_info;
167   win_info.data = malloc(sizeof(int));
168   * (int *) win_info.data = 0;
169
170   struct Win win_log = init_window(&win_meta, "Log");
171   win_log.draw = draw_text_from_bottom;
172   win_log.data = calloc(1, sizeof(char));
173   update_log (&win_log, "Start!");
174
175   char key;
176   while (1) {
177     key = getch();
178     if      (key == 'Q')                                    // quit
179       break;
180     else if (key == '1')                                    // toggle map window
181       toggle_window(&win_meta, &win_map);
182     else if (key == '2')                                    // toggle info window
183       toggle_window(&win_meta, &win_info);
184     else if (key == '3')                                    // toggle log window
185       toggle_window(&win_meta, &win_log);
186     else if (key == 'x') {                                  // scroll map down
187       map.offset_y++;
188       draw_all_windows (&win_meta); }
189     else if (key == 'w' && map.offset_y > 0) {              // scroll map up
190       map.offset_y--;
191       draw_all_windows (&win_meta); }
192     else if (key == 'd') {                                  // scroll map right
193       map.offset_x++;
194       draw_all_windows (&win_meta); }
195     else if (key == 'a' && map.offset_x > 0) {              // scroll map left
196       map.offset_x--;
197       draw_all_windows (&win_meta); }
198     else if (key == 'b' && map.player_y < map.height - 1) { // move player south
199       update_info (&win_info);
200       update_log (&win_log, "\nYou move south.");
201       map.player_y++;
202       draw_all_windows (&win_meta); }
203     else if (key == 't' && map.player_y > 0) {              // move player north
204       update_info (&win_info);
205       update_log (&win_log, "\nYou move north.");
206       map.player_y--;
207       draw_all_windows (&win_meta); }
208     else if (key == 'h' && map.player_x < map.width - 1) {  // move player east
209       update_info (&win_info);
210       update_log (&win_log, "\nYou move east.");
211       map.player_x++;
212       draw_all_windows (&win_meta); }
213     else if (key == 'f' && map.player_x > 0) {              // move player west
214       update_info (&win_info);
215       update_log (&win_log, "\nYou move west.");
216       map.player_x--;
217       draw_all_windows (&win_meta); }
218     else if (key == '.') {                                  // wait
219       update_info (&win_info);
220       update_log (&win_log, "\nYou wait.");
221       draw_all_windows(&win_meta); }
222     else if (key == '>' && win_meta.active != 0)            // cycle forwards in windows chain
223       cycle_active_window(&win_meta, 'n');
224     else if (key == '<' && win_meta.active != 0)            // cycle backwards in windows chain
225       cycle_active_window(&win_meta, 'p');
226     else if (key == 'y' && win_meta.active != 0)            // shift window forwards
227       shift_window(&win_meta, 'f');
228     else if (key == 'Y' && win_meta.active != 0)            // shift window backwards
229       shift_window(&win_meta, 'b');
230     else if (key == '*' && win_meta.active != 0)            // grow window horizontally
231       resize_window(&win_meta, '*');
232     else if (key == '_' && win_meta.active != 0)            // shrink window horizontally
233       resize_window(&win_meta, '_');
234     else if (key == '+' && win_meta.active != 0)            // grow window vertically
235       resize_window(&win_meta, '+');
236     else if (key == '-' && win_meta.active != 0)            // shrink window vertically
237       resize_window(&win_meta, '-'); }
238
239   endwin();
240   return 0; }