aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden.woodruff@gmail.com>2019-09-18 23:17:56 -0500
committerAiden Woodruff <aiden.woodruff@gmail.com>2019-09-18 23:17:56 -0500
commita8199f08984699e1bf44f3cb7e1d57c4493ef2d6 (patch)
tree06f04d178ce54199954cb2621c1f71c7256f77d3
parent13510113c2d07249c73e996e0dc96c01a7a54c76 (diff)
downloadlife-a8199f08984699e1bf44f3cb7e1d57c4493ef2d6.tar.gz
life-a8199f08984699e1bf44f3cb7e1d57c4493ef2d6.tar.bz2
life-a8199f08984699e1bf44f3cb7e1d57c4493ef2d6.zip
Use board in life, only allocate one intermap instead of many
Comment out everything gengetopt so it's compatible for windows Dimensions and coordinates can't be 0 Use other characters for [enter] Use chgat instead of printing spaces Include the correct_curses file in menus.h Use board in updatemap Only allocate one intermap instead of many
-rw-r--r--src/life.c59
-rw-r--r--src/life.h5
-rw-r--r--src/menus.h2
-rw-r--r--src/updatemap.c103
-rw-r--r--src/updatemap.h3
5 files changed, 89 insertions, 83 deletions
diff --git a/src/life.c b/src/life.c
index e0afc97..ed67aa9 100644
--- a/src/life.c
+++ b/src/life.c
@@ -21,9 +21,9 @@
21 21
22#ifndef stat_bar_print 22#ifndef stat_bar_print
23// Print text to status bar in the nice way, without erasing or refreshing 23// Print text to status bar in the nice way, without erasing or refreshing
24#define stat_bar_print(win,...) \ 24#define stat_bar_print(win,...) do { \
25 mvwprintw(win, 0, 0, __VA_ARGS__); \ 25 mvwprintw(win, 0, 0, __VA_ARGS__); \
26 for (int _i = getcurx(win); _i < getmaxx(win); _i++) waddch(win, ' '); 26 mvwchgat(stat_bar, 0, 0, -1, A_STANDOUT, COLOR_PAIR(0), NULL); } while (0)
27#endif 27#endif
28 28
29#ifndef CTRL 29#ifndef CTRL
@@ -32,16 +32,16 @@
32 32
33int main (int argc, char * argv[]) { 33int main (int argc, char * argv[]) {
34 srand(time(NULL)); 34 srand(time(NULL));
35 unsigned int RULE = 0; 35 unsigned int RULE = 6152;
36 int ruleint = 0; 36 int ruleint = 0;
37 int ch = 0; 37 int ch = 0;
38 int livecell = '#'; 38 int livecell = '#';
39 int deadcell = '.'; 39 int deadcell = '.';
40 int timeout_val = 50; 40 int timeout_val = 50;
41 int width = 36; 41 unsigned int width = 36;
42 int height = 18; 42 unsigned int height = 18;
43 int x = 0; 43 unsigned int x = 0;
44 int y = 0; 44 unsigned int y = 0;
45 int playing = 0; 45 int playing = 0;
46 int delaymax = 10; 46 int delaymax = 10;
47 int delay = 0; 47 int delay = 0;
@@ -51,16 +51,16 @@ int main (int argc, char * argv[]) {
51 WINDOW * entry = NULL; 51 WINDOW * entry = NULL;
52 WINDOW * rule_entry_box = NULL; 52 WINDOW * rule_entry_box = NULL;
53 WINDOW * rule_entry = NULL; 53 WINDOW * rule_entry = NULL;
54 char * map = NULL; 54 BOARD map = NULL, intermap = NULL; /*
55 struct gengetopt_args_info args_info; 55 struct gengetopt_args_info args_info;
56 if (cmdline_parser(argc, argv, &args_info) != 0) { 56 if (cmdline_parser(argc, argv, &args_info) != 0) {
57 fprintf(stderr, "Couldn't correctly parse commandline arguments.\n"); 57 fprintf(stderr, "Couldn't correctly parse commandline arguments.\n");
58 exit(EXIT_FAILURE); 58 exit(EXIT_FAILURE);
59 } 59 } */
60 initscr(); 60 initscr();
61 raw(); 61 raw();
62 curs_set(0); 62 curs_set(0);
63 noecho(); 63 noecho(); /*
64 RULE = args_info.ruleint_arg; 64 RULE = args_info.ruleint_arg;
65 if (args_info.width_given) { 65 if (args_info.width_given) {
66 if (args_info.width_arg > COLS) { 66 if (args_info.width_arg > COLS) {
@@ -111,7 +111,7 @@ int main (int argc, char * argv[]) {
111 width = COLS; 111 width = COLS;
112 height = LINES - 2; 112 height = LINES - 2;
113 } 113 }
114 cmdline_parser_free(&args_info); 114 cmdline_parser_free(&args_info); */
115 board = newwin(LINES - 2, COLS, 0, 0); 115 board = newwin(LINES - 2, COLS, 0, 0);
116 stat_bar = newwin(1, 0, LINES - 2, 0); 116 stat_bar = newwin(1, 0, LINES - 2, 0);
117 entry = newwin(1, 0, LINES - 1, 0); 117 entry = newwin(1, 0, LINES - 1, 0);
@@ -121,9 +121,8 @@ int main (int argc, char * argv[]) {
121 keypad(rule_entry, TRUE); 121 keypad(rule_entry, TRUE);
122 keypad(board, TRUE); 122 keypad(board, TRUE);
123 wstandout(stat_bar); 123 wstandout(stat_bar);
124 map = malloc((height * width)+1); 124 map = newboard(width, height);
125 memset(map, 0, height * width + 1); 125 intermap = dupboard(map);
126 memset(map, deadcell, height * width);
127 x = width >> 1; // width / 2, should be optimized anyway 126 x = width >> 1; // width / 2, should be optimized anyway
128 y = height >> 1; // height / 2, should be optimized anyway 127 y = height >> 1; // height / 2, should be optimized anyway
129 while (ch != 'q' && ch != 'Q') { 128 while (ch != 'q' && ch != 'Q') {
@@ -160,18 +159,16 @@ int main (int argc, char * argv[]) {
160 if (y < height - 1) y++; 159 if (y < height - 1) y++;
161 break; 160 break;
162 case ' ': 161 case ' ':
163 if (map[y*width+x] == deadcell) { 162 board_flip(map, x, y);
164 map[y*width+x] = livecell;
165 } else if (map[y*width+x] == livecell) {
166 map[(y*width)+x] = deadcell;
167 }
168 break; 163 break;
169 case '\n': 164 case '\n':
165 case '\r':
166 case KEY_ENTER:
170 playing = TRUE; 167 playing = TRUE;
171 wtimeout(board, timeout_val); 168 wtimeout(board, timeout_val);
172 break; 169 break;
173 case 'c': 170 case 'c':
174 memset(map, deadcell, width*height); 171 board_setall(map, 0);
175 break; 172 break;
176 case 'r': 173 case 'r':
177 werase(stat_bar); 174 werase(stat_bar);
@@ -223,9 +220,13 @@ int main (int argc, char * argv[]) {
223 break; 220 break;
224 } 221 }
225 } else { // ie PLAYING == TRUE 222 } else { // ie PLAYING == TRUE
226 if (ch == '\n') { 223 switch (ch) {
224 case '\n': case '\r': case KEY_ENTER:
227 playing = FALSE; 225 playing = FALSE;
228 wtimeout(board, -1); 226 wtimeout(board, -1);
227 break;
228 default:
229 ;
229 } 230 }
230 } 231 }
231 232
@@ -234,12 +235,13 @@ int main (int argc, char * argv[]) {
234 delaymax -= (delaymax > 1 ? 1 : 0); 235 delaymax -= (delaymax > 1 ? 1 : 0);
235 } else if (ch == '.') { 236 } else if (ch == '.') {
236 delaymax += (delaymax < 20 ? 1 : 0); 237 delaymax += (delaymax < 20 ? 1 : 0);
237 } else if (ch== CTRL('l')) { 238 } else if (ch == CTRL('l')) {
238 foreach(wclear, stdscr, entry, stat_bar, board); 239 foreach(wclear, stdscr, entry, stat_bar, board);
239 } 240 }
240 for (int i = 0; i < height; ++i) { 241 for (unsigned int y = 0; y < height; ++y)
241 mvwaddnstr(board, i, 0, map + (i * width), width); 242 for (unsigned int x = 0; x < width; ++x)
242 } 243 mvwaddch(board, y, x, board_getval(map, x, y) == 0 ? deadcell : livecell);
244
243 if (mvwchgat(board, y, x, 1, A_STANDOUT, COLOR_PAIR(0), NULL) == ERR) fprintf(stderr, "Error (mvwchgat)\n"); 245 if (mvwchgat(board, y, x, 1, A_STANDOUT, COLOR_PAIR(0), NULL) == ERR) fprintf(stderr, "Error (mvwchgat)\n");
244 246
245 // Emulate tabs 247 // Emulate tabs
@@ -254,7 +256,7 @@ int main (int argc, char * argv[]) {
254 if (delay >= delaymax) { 256 if (delay >= delaymax) {
255 delay = 0; 257 delay = 0;
256 generation++; 258 generation++;
257 update_map(map, width, height, livecell, deadcell, RULE); 259 update_map(map, intermap, width, height, RULE);
258 } 260 }
259 } 261 }
260 ch = wgetch(board); 262 ch = wgetch(board);
@@ -264,7 +266,8 @@ int main (int argc, char * argv[]) {
264 foreach(delwin, entry, stat_bar, board, rule_entry, rule_entry_box); 266 foreach(delwin, entry, stat_bar, board, rule_entry, rule_entry_box);
265 board = stat_bar = entry = rule_entry = rule_entry_box = NULL; 267 board = stat_bar = entry = rule_entry = rule_entry_box = NULL;
266 delwin(stdscr); 268 delwin(stdscr);
267 free(map); 269 delboard(map);
268 map = NULL; 270 delboard(intermap);
271 map = intermap = NULL;
269 return 0; 272 return 0;
270} 273}
diff --git a/src/life.h b/src/life.h
index bad4815..7043fde 100644
--- a/src/life.h
+++ b/src/life.h
@@ -26,10 +26,11 @@
26#include <ctype.h> 26#include <ctype.h>
27 27
28// Module Libraries 28// Module Libraries
29#include <ncurses.h> 29#include "correct_curses.h"
30 30
31// Project Libraries 31// Project Libraries
32#include "life-macros.h" 32#include "life-macros.h"
33#include "cmdline-life.h" 33// #include "cmdline-life.h"
34#include "updatemap.h" 34#include "updatemap.h"
35#include "menus.h" 35#include "menus.h"
36#include "board.h" \ No newline at end of file
diff --git a/src/menus.h b/src/menus.h
index 26da8f5..a544d8d 100644
--- a/src/menus.h
+++ b/src/menus.h
@@ -21,7 +21,7 @@
21#include <string.h> 21#include <string.h>
22#include <stdlib.h> 22#include <stdlib.h>
23 23
24#include <ncurses.h> 24#include "correct_curses.h"
25 25
26#include "life-macros.h" 26#include "life-macros.h"
27 27
diff --git a/src/updatemap.c b/src/updatemap.c
index 539def8..f4952a9 100644
--- a/src/updatemap.c
+++ b/src/updatemap.c
@@ -19,56 +19,57 @@
19 */ 19 */
20#include "updatemap.h" 20#include "updatemap.h"
21 21
22char * update_map (char * base_map, const int width, const int height, char livecell, char deadcell, const unsigned int ruleint) { 22void update_map (BOARD base_map, BOARD intermap, const unsigned int width, const unsigned int height, const unsigned int ruleint) {
23 char * intermap = (char*) strndup(base_map, width * height); 23 cpyboard(intermap, base_map);
24 for (int i = 0, x = 0, y = 0, surround = 0; i < width*height; i++, surround = 0) { 24 for (int i = 0, x = 0, y = 0, surround = 0; i < width * height; i++, surround = 0) {
25 x = i % width; 25 x = i % width;
26 y = (i - x) / width; 26 y = (i - x) / width;
27 /* 27 /*
28 Where i is the current cell: 28 Where i is the current cell:
29 A1 | A2 | A3 29 A1 | A2 | A3
30 ----|----|---- 30 ----|----|----
31 B1 | i | B3 31 B1 | i | B3
32 ----|----|---- 32 ----|----|----
33 C1 | C2 | C3 33 C1 | C2 | C3
34 */ 34 */
35 if (x > 0) { 35 if (x > 0) {
36 if (base_map[i-1] == livecell) surround++; // B1 36 if (board_getval_i(base_map, i - 1) == 1) ++surround; // B1
37 if (y > 0) { 37 if (y > 0) {
38 if (base_map[i-width-1] == livecell) surround++; // A1 38 if (board_getval_i(base_map, i - width - 1) == 1) ++surround; // A1
39 } 39 }
40 if (y < height - 1) { 40 if (y < height - 1) {
41 if (base_map[i-1+width] == livecell) surround++; // C1 41 if (board_getval_i(base_map, i - 1 + width) == 1) ++surround; // C1
42 } 42 }
43 } 43 }
44 if (x < width - 1) { 44 if (x < width - 1) {
45 if (base_map[i+1] == livecell) surround++; // B3 45 if (board_getval_i(base_map, i + 1) == 1) ++surround; // B3
46 if (y > 0) { 46 if (y > 0) {
47 if (base_map[i+1-width] == livecell) surround++; // A3 47 if (board_getval_i(base_map, i + 1 - width) == 1) ++surround; // A3
48 } 48 }
49 if (y < height - 1) { 49 if (y < height - 1) {
50 if (base_map[i + 1 + width] == livecell) surround++; // C3 50 if (board_getval_i(base_map, i + 1 + width) == 1) ++surround; // C3
51 } 51 }
52 } 52 }
53 if (y > 0) { 53 if (y > 0) {
54 if (base_map[i - width] == livecell) surround++; 54 if (board_getval_i(base_map, i - width) == 1) ++surround;
55 } 55 }
56 if (y < height - 1) { 56 if (y < height - 1) {
57 if (base_map[i + width] == livecell) surround++; 57 if (board_getval_i(base_map, i + width) == 1) ++surround;
58 } 58 }
59 if (base_map[i] == deadcell) { 59 switch (board_getval_i(base_map, i)) {
60 if (has(ruleint, 1 << surround)) { 60 case 0:
61 intermap[i] = livecell; 61 if (has(ruleint, 1 << surround)) {
62 } 62 board_setval_i(intermap, i, 1);
63 } else if (base_map[i] == livecell) { 63 }
64 if (!has(ruleint, 1 << (surround + 9))) { 64 break;
65 intermap[i] = deadcell; 65 case 1:
66 } 66 if (!has(ruleint, 1 << (surround + 9))) {
67 } 67 board_setval_i(intermap, i, 0);
68 } 68 }
69 }
70 }
69 71
70 // Rewrite map 72 // Rewrite map
71 strcpy(base_map, intermap); 73 cpyboard(base_map, intermap);
72 free(intermap); 74 return base_map;
73 return base_map;
74} 75}
diff --git a/src/updatemap.h b/src/updatemap.h
index 6773998..01c9e4b 100644
--- a/src/updatemap.h
+++ b/src/updatemap.h
@@ -21,10 +21,11 @@
21#include <string.h> 21#include <string.h>
22 22
23#include "life-macros.h" 23#include "life-macros.h"
24#include "board.h"
24 25
25#ifndef LIFE_UPDATEMAP_H_ 26#ifndef LIFE_UPDATEMAP_H_
26#define LIFE_UPDATEMAP_H_ 27#define LIFE_UPDATEMAP_H_
27 28
28char * update_map (char * base_map, const int width, const int height, char livecell, char deadcell, const unsigned int ruleint); 29void update_map (BOARD base_map, BOARD intermap, const unsigned int width, const unsigned int height, const unsigned int ruleint);
29 30
30#endif // LIFE_UPDATEMAP_H_ 31#endif // LIFE_UPDATEMAP_H_