commit abc01bf0c112efc0c7e82723140c7c5c0829c4cb
parent aea5b3965a13efe4ba9a2679055229aec01ba613
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Mon, 6 Jun 2022 23:10:12 +0200
Refactor the game related functions to game.c
Diffstat:
10 files changed, 292 insertions(+), 317 deletions(-)
diff --git a/include/display.h b/include/display.h
@@ -3,6 +3,22 @@
#include "window.h"
+#define CHAR_BLANK " "
+#define CHAR_CURSOR "<>"
+#define CHAR_CIRCLE "\u26AB"
+#define CHAR_SQUARE "\u2B1B"
+#define CHAR_ACTIVE CHAR_CIRCLE
+
+#ifndef NO_UNICODE
+#define print_cell(win, blank) \
+ if (val) \
+ waddstr(win, CHAR_ACTIVE); \
+ else \
+ waddstr(win, blank);
+#else
+#define print_cell(win, blank) waddstr(win, blank);
+#endif
+
extern window_T MAIN_w;
typedef int (*input_f)(int);
@@ -27,19 +43,15 @@ int input(WINDOW *win, char *buffer, int size, input_f crit);
int display_start(void);
int display_stop(void);
-void display_menu(window_T wind, char *name, struct menu_T *items, int size, int title);
+void display_menu(window_T wind, char *name, struct menu_T *items, int size,
+ int title);
int display_imenu(window_T wind, struct imenu_T *items, int size);
-void display_game(window_T wind, int h, int w, int ph, int pw);
-int display_select(window_T wind, int w, int h);
void display_status(window_T wind, unsigned long int generation, int gen_step,
int wrap, int height, int wight, int play, int dt,
int cursor_y, int cursor_x);
-void display_cursor(WINDOW *win, int h, int w, int ph, int pw);
+void display_patterns(window_T wind);
void handle_winch(int sig);
-
void display_state_set(int i, int j, int val);
-void display_patterns(window_T wind);
-
#endif
diff --git a/include/game.h b/include/game.h
@@ -1,6 +1,10 @@
#ifndef GAME_H
#define GAME_H
-void game(int s_h, int s_w, char *mode_name, int ncells, int mode_index);
+#include "window.h"
+
+extern int width, height;
+
+void game(int s_h, int s_w, int mode_index);
#endif
diff --git a/include/logic.h b/include/logic.h
@@ -20,18 +20,14 @@ extern Cell *hash;
extern char *evolution_names[];
extern int evolution_cells[];
-extern int evolution_size;
-extern int evolve_index;
+extern int evolution_size, evolve_index;
+
+extern int pos_y, pos_x;
extern Cell **save_cells;
extern int save_cells_s;
-extern int pos_y;
-extern int pos_x;
-
-extern int WIDTH, HEIGHT;
-
-int logic_init(int w, int h, int isWrapping);
+int logic_init(int isWrapping, int index);
int evolution_init(int index);
void do_evolution(int steps);
int logic_free(void);
diff --git a/include/window.h b/include/window.h
@@ -11,7 +11,7 @@ void window_free(T self);
T window_init(T self);
T window_split(T self, int hor, int a, int b, char *name1, char *name2);
void window_unsplit(T self);
-T window_center(T self, int tmp, int h, int w, char *name);
+T window_center(T self, int h, int w, char *name);
void window_update_children(T self);
WINDOW *WINDOW_new(T self);
@@ -22,7 +22,7 @@ int window_wight(T self);
WINDOW *window_win(T self);
void window_settings(WINDOW *win);
void window_clear(T self);
-void window_clear_noRefresh(T self);
+void window_clear_noRefresh(T self);
void window_set_title(T self, char *title);
diff --git a/src/display.c b/src/display.c
@@ -9,55 +9,8 @@
#include "utils.h"
#include "window.h"
-#define center_vertical(n) wcenter_vertical(MAIN_W, n);
-#define center_horizontal(y, n) wcenter_horizontal(MAIN_W, y, n);
-
-#define CHAR_BLANK " "
-#define CHAR_CURSOR "<>"
-#define CHAR_CIRCLE "\u26AB"
-#define CHAR_SQUARE "\u2B1B"
-
-#define CHAR_ACTIVE CHAR_CIRCLE
-
-extern Cell *hash;
-
window_T MAIN_w = NULL;
-#define y_at(y) y, screen_offset_y, h
-#define x_at(x) x, screen_offset_x, w
-
-#define for_each_cell(start_i, end_i, start_j, end_j) \
- for (int i = start_i; i < end_i; i++) \
- for (int j = start_j; j < end_j; j++)
-
-#ifndef NO_UNICODE
-#define print_cell(win, blank) \
- if (val) \
- waddstr(win, CHAR_ACTIVE); \
- else \
- waddstr(win, blank);
-#else
-#define print_cell(win, blank) waddstr(win, blank);
-#endif
-
-// expects val to to be set with value at cordinates
-#define mvprint_cell(win, i, j, color_offset, blank) \
- { \
- wmove(win, i + 1, j * 2 + 1); \
- wattrset(win, COLOR_PAIR(val + color_offset)); \
- print_cell(win, blank); \
- }
-
-#define print_cells(win, start_i, end_i, start_j, end_j, color_offset, blank) \
- for (int i = start_i; i < end_i; i++) { \
- wmove(win, i + 1, 1 + start_j * 2); \
- for (int j = start_j; j < end_j; j++) { \
- int val = getAt(cord(y_at(i)), cord(x_at(j))); \
- wattrset(win, COLOR_PAIR(val + color_offset)); \
- print_cell(win, blank); \
- } \
- }
-
void print_pattern(WINDOW *win, pattern_T pattern, int *y, int x, int indent) {
(*y)++;
wmove(win, (*y)++, x + indent);
@@ -72,175 +25,6 @@ void print_pattern(WINDOW *win, pattern_T pattern, int *y, int x, int indent) {
}
}
-int screen_offset_x, screen_offset_y;
-int cursor_offset_x, cursor_offset_y;
-
-int wrap;
-
-int (*cord)(int, int, int);
-
-int get_screen_position(int value, int screen_offset, int screen_size,
- int board_size) {
- int overshoot = screen_offset + screen_size - board_size;
-
- if (wrap) {
- if (overshoot > 0) {
- if (value < screen_offset && value >= overshoot)
- return -1;
- if (value >= screen_offset) {
- return value - screen_offset;
- } else {
- return value + screen_size - overshoot;
- }
- } else {
- if (value < screen_offset || value >= screen_offset + screen_size)
- return -2;
- return value - screen_offset;
- }
- } else {
- if (value < screen_offset || value >= screen_offset + screen_size)
- return -3;
- return value - screen_offset;
- }
-}
-
-void display_game(window_T wind, int h, int w, int ph, int pw) {
- WINDOW *win = window_win(wind);
-
- window_clear_noRefresh(wind);
-
- int row, col, val;
- for (Cell *c = hash; c != NULL; c = c->hh.next) {
- wattrset(win, COLOR_PAIR(val + 2));
-
- /* row = get_screen_position(c->row, screen_offset_y, ph, h); */
- /* col = get_screen_position(c->col, screen_offset_x, pw, w); */
- row = get_screen_position(c->cord.row, screen_offset_y, ph, h);
- col = get_screen_position(c->cord.col, screen_offset_x, pw, w);
- val = c->val;
-
- if (row < 0 || col < 0)
- continue;
-
- mvprint_cell(win, row, col, 2, CHAR_BLANK);
- }
-}
-
-void display_cursor(WINDOW *win, int h, int w, int ph, int pw) {
- static int prev_x = 0, prev_y = 0;
- int val;
-
- val = getAt(cord(y_at(prev_y)), cord(x_at(prev_x)));
- mvprint_cell(win, prev_y, prev_x, 2, CHAR_BLANK);
-
- val = getAt(cord(y_at(cursor_offset_y)), cord(x_at(cursor_offset_x)));
- mvprint_cell(win, cursor_offset_y, cursor_offset_x, 5, CHAR_CURSOR);
-
- prev_y = cursor_offset_y;
- prev_x = cursor_offset_x;
-}
-
-int display_select(window_T wind, int w, int h) {
- int current_offset_y = cursor_offset_y;
- int current_offset_x = cursor_offset_x;
- int ret_value = 1;
-
- int CLINES = LINES, CCOLS = COLS;
- WINDOW *win = window_win(wind);
- WINDOW *new;
-
- if (UNICODE) {
- new = WINDOW_new(wind);
- overlay(win, new);
- wrefresh(new);
- } else {
- new = win;
- }
-
- int ph = window_height(wind), pw = window_wight(wind) / 2;
- nodelay(stdscr, 0);
- while (TRUE) {
- int start_i = MIN(cursor_offset_y, current_offset_y);
- int end_i = MAX(cursor_offset_y, current_offset_y);
- int start_j = MIN(cursor_offset_x, current_offset_x);
- int end_j = MAX(cursor_offset_x, current_offset_x);
-
- if (!UNICODE)
- display_game(wind, h, w, ph, pw);
-
- print_cells(new, start_i, end_i + 1, start_j, end_j + 1, 8, CHAR_BLANK);
- wrefresh(new);
-
- if (is_term_resized(CLINES, CCOLS)) {
- HANDLE_RESIZE;
- goto end;
- }
-
- int c = getch();
- switch (c) {
- // offset selection
- case 'w':
- case 'W':
- current_offset_y--;
- break;
- case 's':
- case 'S':
- current_offset_y++;
- break;
- case 'a':
- case 'A':
- current_offset_x--;
- break;
- case 'd':
- case 'D':
- current_offset_x++;
- break;
-
- // delete selection
- case 'x':
- case 'X':
- for_each_cell(start_i, end_i + 1, start_j, end_j + 1)
- deleteAt(cord(y_at(i)), cord(x_at(j)));
- goto end;
-
- // toggle selection
- case 't':
- case 'T':
- for_each_cell(start_i, end_i + 1, start_j, end_j + 1)
- toggleAt(cord(y_at(i)), cord(x_at(j)));
- goto end;
-
- // confirm and save slection
- case '\n':
- for_each_cell(start_i, end_i + 1, start_j, end_j + 1)
- saveCell(cord(y_at(i)), cord(x_at(j)));
- ret_value = 100;
- goto end;
-
- // quit
- case 27:
- case 'q':
- case 'Q':
- goto end;
-
- defalut:
- flushinp();
- continue;
- }
- flushinp();
-
- CLAMP(current_offset_y, 0, ph - 1);
- CLAMP(current_offset_x, 0, pw - 1);
-
- wclear(new);
- overlay(win, new);
- }
-end:;
- nodelay(stdscr, 1);
- delwin(new);
- return ret_value;
-}
-
void display_status(window_T wind, unsigned long int gen, int gen_step,
int wrap, int height, int wight, int play, int dt,
int cursor_y, int cursor_x) {
diff --git a/src/file.c b/src/file.c
@@ -141,14 +141,12 @@ void load_files(void) {
void free_files(void) { file_free(loaded_files); }
+// from logic.c
extern Cell **save_cells;
-extern int save_cells_s;
+extern int save_cells_s, pos_y, pos_x, evolve_index;
-extern int pos_y;
-extern int pos_x;
-
-extern int WIDTH, HEIGHT;
-extern int evolve_index;
+// form game.c
+extern int width, height;
void file_load_pattern(char *name, int index) {
char *fname = malloc((strlen(name) + 5) * sizeof(char));
@@ -189,6 +187,8 @@ void file_save_pattern(char *name, int index) {
}
void file_load(char *name, int index) {
+ int w, h;
+
char *fname = malloc((strlen(name) + 5) * sizeof(char));
sprintf(fname, "%s.all", name);
@@ -196,14 +196,13 @@ void file_load(char *name, int index) {
if (!f)
exit(1);
- fscanf(f, "%d %d %d", &HEIGHT, &WIDTH, &evolve_index);
+ fscanf(f, "%d %d %d", &h, &w, &evolve_index);
int row, col, val;
while (fscanf(f, "%d %d %d", &row, &col, &val) != EOF) {
setAt(pos_y + row, pos_x + col, val);
}
- game(HEIGHT, WIDTH, evolution_names[evolve_index],
- evolution_cells[evolve_index], evolve_index);
+ game(h, w, evolve_index);
}
void file_save(char *name, int index) {
@@ -214,7 +213,7 @@ void file_save(char *name, int index) {
if (!f)
exit(1);
- fprintf(f, "%d %d %d\n", HEIGHT, WIDTH, evolve_index);
+ fprintf(f, "%d %d %d\n", height, width, evolve_index);
for (Cell *c = hash; c != NULL; c = c->hh.next) {
fprintf(f, "%d %d %d\n", c->cord.row, c->cord.col, c->val);
}
diff --git a/src/game.c b/src/game.c
@@ -2,10 +2,11 @@
#include <time.h>
#include "display.h"
-#include "utils.h"
-#include "window.h"
+#include "game.h"
#include "logic.h"
#include "main.h"
+#include "utils.h"
+#include "window.h"
#ifdef _WIN32
#define TIME_MOD 1
@@ -13,21 +14,11 @@
#define TIME_MOD 1000
#endif
-extern char *evolution_names[];
-extern int evolution_cells[];
-extern int evolution_size;
+extern char *evolution_names[];
+extern int evolution_cells[];
+extern int evolution_size;
extern window_T menu_w;
-
-#define save_state() \
- { \
- t_y = cord(y_at(ph / 2)); \
- t_x = cord(x_at(pw / 2)); \
- ct_y = cord(y_at(cursor_offset_y)); \
- ct_x = cord(x_at(cursor_offset_x)); \
- }
-
-#define y_at(y) y, screen_offset_y, h
-#define x_at(x) x, screen_offset_x, w
+extern Cell *hash;
typedef int (*coordinate_f)(int, int, int);
int coordinate_nowrap(int val, int offset, int max) { return val + offset; }
@@ -35,19 +26,214 @@ int coordinate_wrap(int val, int offset, int max) {
return (val + offset + max) % max;
}
-extern coordinate_f cord;
+int win_height, win_width, height, width;
+int screen_offset_x, screen_offset_y;
+int cursor_offset_x, cursor_offset_y;
+int wrap;
+
+coordinate_f cord;
+
+#define y_at(y) y, screen_offset_y, height
+#define x_at(x) x, screen_offset_x, width
+
+#define for_each_cell(start_i, end_i, start_j, end_j) \
+ for (int i = start_i; i < end_i; i++) \
+ for (int j = start_j; j < end_j; j++)
+
+// expects val to to be set with value at cordinates
+#define mvprint_cell(win, i, j, color_offset, blank) \
+ { \
+ wmove(win, i + 1, j * 2 + 1); \
+ wattrset(win, COLOR_PAIR(val + color_offset)); \
+ print_cell(win, blank); \
+ }
+
+#define print_cells(win, start_i, end_i, start_j, end_j, color_offset, blank) \
+ for (int i = start_i; i < end_i; i++) { \
+ wmove(win, i + 1, 1 + start_j * 2); \
+ for (int j = start_j; j < end_j; j++) { \
+ int val = getAt(cord(y_at(i)), cord(x_at(j))); \
+ wattrset(win, COLOR_PAIR(val + color_offset)); \
+ print_cell(win, blank); \
+ } \
+ }
+
+int get_screen_position(int value, int screen_offset, int screen_size,
+ int board_size) {
+ int overshoot = screen_offset + screen_size - board_size;
+
+ if (wrap) {
+ if (overshoot > 0) {
+ if (value < screen_offset && value >= overshoot)
+ return -1;
+ if (value >= screen_offset) {
+ return value - screen_offset;
+ } else {
+ return value + screen_size - overshoot;
+ }
+ } else {
+ if (value < screen_offset || value >= screen_offset + screen_size)
+ return -2;
+ return value - screen_offset;
+ }
+ } else {
+ if (value < screen_offset || value >= screen_offset + screen_size)
+ return -3;
+ return value - screen_offset;
+ }
+}
+
+void display_game(window_T wind) {
+ WINDOW *win = window_win(wind);
+
+ window_clear_noRefresh(wind);
+
+ int row, col, val;
+ for (Cell *c = hash; c != NULL; c = c->hh.next) {
+ wattrset(win, COLOR_PAIR(val + 2));
-extern int screen_offset_x, screen_offset_y;
-extern int cursor_offset_x, cursor_offset_y;
-extern int wrap;
+ row = get_screen_position(c->cord.row, screen_offset_y, win_height, height);
+ col = get_screen_position(c->cord.col, screen_offset_x, win_width, width);
+ val = c->val;
+
+ if (row < 0 || col < 0)
+ continue;
+
+ mvprint_cell(win, row, col, 2, CHAR_BLANK);
+ }
+}
-void game(int s_h, int s_w, char *mode_name, int ncells, int mode_index) {
- unsigned long int gen = 0;
- int h, w;
- int gen_step = 1, play = 0, time_const = 100, time_step = 1;
+void display_cursor(WINDOW *win) {
+ static int prev_x = 0, prev_y = 0;
+ int val;
+ val = getAt(cord(y_at(prev_y)), cord(x_at(prev_x)));
+ mvprint_cell(win, prev_y, prev_x, 2, CHAR_BLANK);
+
+ val = getAt(cord(y_at(cursor_offset_y)), cord(x_at(cursor_offset_x)));
+ mvprint_cell(win, cursor_offset_y, cursor_offset_x, 5, CHAR_CURSOR);
+
+ prev_y = cursor_offset_y;
+ prev_x = cursor_offset_x;
+}
+
+int display_select(window_T wind) {
+ int CLINES = LINES, CCOLS = COLS;
+
+ int current_offset_y = cursor_offset_y;
+ int current_offset_x = cursor_offset_x;
+ int ret_value = 1;
+
+ WINDOW *win = window_win(wind);
+ WINDOW *new;
+
+ if (UNICODE) {
+ new = WINDOW_new(wind);
+ overlay(win, new);
+ wrefresh(new);
+ } else {
+ new = win;
+ }
+
+ int ph = window_height(wind), pw = window_wight(wind) / 2;
+ nodelay(stdscr, 0);
+ while (TRUE) {
+ int start_i = MIN(cursor_offset_y, current_offset_y);
+ int end_i = MAX(cursor_offset_y, current_offset_y);
+ int start_j = MIN(cursor_offset_x, current_offset_x);
+ int end_j = MAX(cursor_offset_x, current_offset_x);
+
+ if (!UNICODE)
+ display_game(wind);
+
+ print_cells(new, start_i, end_i + 1, start_j, end_j + 1, 8, CHAR_BLANK);
+ wrefresh(new);
+
+ if (is_term_resized(CLINES, CCOLS)) {
+ HANDLE_RESIZE;
+ goto end;
+ }
+
+ int c = getch();
+ switch (c) {
+ // offset selection
+ case 'w':
+ case 'W':
+ current_offset_y--;
+ break;
+ case 's':
+ case 'S':
+ current_offset_y++;
+ break;
+ case 'a':
+ case 'A':
+ current_offset_x--;
+ break;
+ case 'd':
+ case 'D':
+ current_offset_x++;
+ break;
+
+ // delete selection
+ case 'x':
+ case 'X':
+ for_each_cell(start_i, end_i + 1, start_j, end_j + 1)
+ deleteAt(cord(y_at(i)), cord(x_at(j)));
+ goto end;
+
+ // toggle selection
+ case 't':
+ case 'T':
+ for_each_cell(start_i, end_i + 1, start_j, end_j + 1)
+ toggleAt(cord(y_at(i)), cord(x_at(j)));
+ goto end;
+
+ // confirm and save slection
+ case '\n':
+ for_each_cell(start_i, end_i + 1, start_j, end_j + 1)
+ saveCell(cord(y_at(i)), cord(x_at(j)));
+ ret_value = 100;
+ goto end;
+
+ // quit
+ case 27:
+ case 'q':
+ case 'Q':
+ goto end;
+
+ defalut:
+ flushinp();
+ continue;
+ }
+ flushinp();
+
+ CLAMP(current_offset_y, 0, ph - 1);
+ CLAMP(current_offset_x, 0, pw - 1);
+
+ wclear(new);
+ overlay(win, new);
+ }
+end:;
+ nodelay(stdscr, 1);
+ delwin(new);
+ return ret_value;
+}
+
+#define save_state() \
+ { \
+ t_y = cord(y_at(win_height / 2)); \
+ t_x = cord(x_at(win_width / 2)); \
+ ct_y = cord(y_at(cursor_offset_y)); \
+ ct_x = cord(x_at(cursor_offset_x)); \
+ }
+
+void game(int s_h, int s_w, int mode_index) {
+ char *mode_name = evolution_names[mode_index];
+ int ncells = evolution_cells[mode_index];
+
+ int t_y = 0, t_x = 0, ct_x = 0, ct_y = 0;
+ int gen = 0, gen_step = 1, play = 0, time_const = 100, time_step = 1;
wrap = 1;
- int ph, pw, t_y = 0, t_x = 0, ct_x = 0, ct_y = 0;
window_T status_w, screen_w, game_w;
@@ -60,57 +246,55 @@ reset_screen:
wrap = (s_w > 0 && s_h > 0);
if (!wrap) {
- w = window_wight(screen_w) / 2;
- h = window_height(screen_w);
- logic_init(0, 0, wrap);
+ width = window_wight(screen_w) / 2;
+ height = window_height(screen_w);
} else {
- w = s_w;
- h = s_h;
- logic_init(w, h, wrap);
+ width = s_w;
+ height = s_h;
}
- cord = wrap ? coordinate_wrap : coordinate_nowrap;
- game_w = window_center(screen_w, 0, h, w * 2, mode_name);
+ logic_init(wrap, mode_index);
- evolution_init(mode_index);
+ cord = wrap ? coordinate_wrap : coordinate_nowrap;
+ game_w = window_center(screen_w, height, width * 2, mode_name);
redraw:;
int CLINES = LINES, CCOLS = COLS;
clock_t start_t, end_t = 0, total_t;
if (!wrap) {
- game_w = window_center(screen_w, 0, window_height(screen_w),
+ game_w = window_center(screen_w, window_height(screen_w),
window_wight(screen_w), mode_name);
}
WINDOW *game_W = window_win(game_w);
- ph = window_height(game_w), pw = window_wight(game_w) / 2;
+ win_height = window_height(game_w), win_width = window_wight(game_w) / 2;
window_clear(menu_w);
window_clear(screen_w);
window_clear(status_w);
- screen_offset_y = t_y - ph / 2;
- screen_offset_x = t_x - pw / 2;
+ screen_offset_y = t_y - win_height / 2;
+ screen_offset_x = t_x - win_width / 2;
if (wrap) {
- screen_offset_x = (screen_offset_x + w) % w;
- screen_offset_y = (screen_offset_y + h) % h;
+ screen_offset_x = (screen_offset_x + width) % width;
+ screen_offset_y = (screen_offset_y + height) % height;
}
cursor_offset_y = ct_y - screen_offset_y;
cursor_offset_x = ct_x - screen_offset_x;
if (wrap) {
- cursor_offset_x = (cursor_offset_x + w) % w;
- cursor_offset_y = (cursor_offset_y + h) % h;
+ cursor_offset_x = (cursor_offset_x + width) % width;
+ cursor_offset_y = (cursor_offset_y + height) % height;
}
- CLAMP(cursor_offset_y, 0, ph - 1);
- CLAMP(cursor_offset_x, 0, pw - 1);
+ CLAMP(cursor_offset_y, 0, win_height - 1);
+ CLAMP(cursor_offset_x, 0, win_width - 1);
- display_game(game_w, h, w, ph, pw);
- display_cursor(game_W, h, w, ph, pw);
+ display_game(game_w);
+ display_cursor(game_W);
wrefresh(game_W);
int screen_change = 1;
@@ -120,8 +304,8 @@ redraw:;
start_t = clock();
if (wrap) {
- screen_offset_x = (screen_offset_x + w) % w;
- screen_offset_y = (screen_offset_y + h) % h;
+ screen_offset_x = (screen_offset_x + width) % width;
+ screen_offset_y = (screen_offset_y + height) % height;
}
if (play) {
@@ -130,18 +314,19 @@ redraw:;
gen += gen_step;
}
- display_status(status_w, gen, gen_step, wrap, h, w, play, time_const,
- cord(y_at(cursor_offset_y)), cord(x_at(cursor_offset_x)));
+ display_status(status_w, gen, gen_step, wrap, height, width, play,
+ time_const, cord(y_at(cursor_offset_y)),
+ cord(x_at(cursor_offset_x)));
if (play || screen_change) {
- display_game(game_w, h, w, ph, pw);
+ display_game(game_w);
wrefresh(game_W);
screen_change = 0;
cursor_change = 1;
}
if (cursor_change) {
- display_cursor(game_W, h, w, ph, pw);
+ display_cursor(game_W);
wrefresh(game_W);
cursor_change = 0;
}
@@ -213,7 +398,7 @@ redraw:;
// visual selection
case 'v':
case 'V':
- if (display_select(game_w, h, h) == 100) {
+ if (display_select(game_w) == 100) {
window_unsplit(menu_w);
save_pattern();
}
@@ -296,8 +481,8 @@ redraw:;
screen_change = 1;
}
- CLAMP(cursor_offset_y, 0, ph - 1);
- CLAMP(cursor_offset_x, 0, pw - 1);
+ CLAMP(cursor_offset_y, 0, win_height - 1);
+ CLAMP(cursor_offset_x, 0, win_width - 1);
CLAMP(gen_step, 1, 100);
CLAMP(time_const, 0, 1000);
diff --git a/src/logic.c b/src/logic.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include "game.h"
#include "logic.h"
#include "utils.h"
@@ -36,8 +37,8 @@ void insert(int row, int col, int val, int mod) {
c->val += mod;
}
-int WIDTH, HEIGHT;
-int isExpanding;
+extern int width, height;
+int isExpanding;
Cell **save_cells;
int save_cells_s;
@@ -85,8 +86,8 @@ void addToCellsWrap(int i, int j, int value) {
for (int k = i - 1; k <= i + 1; k++)
for (int l = j - 1; l <= j + 1; l++) {
- int a = (k + HEIGHT) % HEIGHT;
- int b = (l + WIDTH) % WIDTH;
+ int a = (k + height) % height;
+ int b = (l + width) % width;
if (a != i || b != j)
insert(a, b, 0, mod);
}
@@ -272,20 +273,14 @@ void do_evolution(int steps) {
}
}
-int logic_init(int w, int h, int isWrapping) {
- WIDTH = w;
- HEIGHT = h;
- addToCells = addition_modes[isWrapping];
+int logic_init(int isWrapping, int index) {
+ save_cells_s = 0;
save_cells_sm = 100;
save_cells = malloc(save_cells_sm * sizeof(struct Cell *));
- save_cells_s = 0;
- return 1;
-}
-
-int evolution_init(int index) {
- evolve_index = index;
+ addToCells = addition_modes[isWrapping];
evolve = evolution_modes[index];
+ evolve_index = index;
toggle_mod = evolution_cells[index];
return 1;
}
diff --git a/src/main.c b/src/main.c
@@ -12,8 +12,8 @@
#include "utils.h"
#include "window.h"
-window_T menu_w;
extern window_T MAIN_w;
+window_T menu_w;
void settings(char *pass, int index) {
struct imenu_T imenu_items[] = {
@@ -27,7 +27,7 @@ void settings(char *pass, int index) {
int row = atoi(imenu_items[0].buffer);
int column = atoi(imenu_items[1].buffer);
- game(row, column, pass, evolution_cells[index], index);
+ game(row, column, index);
break;
}
diff --git a/src/window.c b/src/window.c
@@ -170,11 +170,11 @@ T window_split(T self, int hor, int a, int b, char *name1, char *name2) {
return self->c1;
}
-T window_center(T self, int tmp, int h, int w, char *name) {
+T window_center(T self, int h, int w, char *name) {
self->c1 = window_new();
self->c2 = NULL;
- self->mod[0] = tmp;
+ self->mod[0] = -1;
self->mod[1] = h;
self->mod[2] = w;