From 2d9f89f3394ad94b9a5b4fb0b0b7344cc39a6f6a Mon Sep 17 00:00:00 2001 From: Tiehuis Date: Wed, 25 Feb 2015 10:47:41 +1300 Subject: [PATCH] Make conditional linking happen in makefile for merges --- Makefile | 9 +++++---- src/engine.c | 12 +++++++----- src/engine.h | 2 +- src/main.c | 3 +-- src/{merge_fib.c.nomk => merge_fib.c} | 5 +++-- src/{merge.c => merge_std.c} | 2 +- 6 files changed, 18 insertions(+), 15 deletions(-) rename src/{merge_fib.c.nomk => merge_fib.c} (73%) rename src/{merge.c => merge_std.c} (85%) diff --git a/Makefile b/Makefile index de42d2c..15e6777 100644 --- a/Makefile +++ b/Makefile @@ -5,18 +5,19 @@ DEFINES := -DINVERT_COLORS -DVT100 $(shell pkg-config --cflags sdl2) PROGRAM := 2048 C_FILES := $(wildcard src/*.c) -O_FILES := $(addprefix obj/,$(notdir $(C_FILES:.c=.o))) +O_FILES := $(addprefix obj/, $(notdir $(C_FILES:.c=.o))) +FILTERED_O_FILES := $(filter-out obj/gfx%.o obj/merge%.o, $(O_FILES)) all: curses curses: $(O_FILES) - $(CC) $(filter-out obj/gfx%.o, $(O_FILES)) obj/gfx_curses.o -o $(PROGRAM) -lcurses + $(CC) $(FILTERED_O_FILES) obj/merge_std.o obj/gfx_curses.o -o $(PROGRAM) -lcurses vt100: $(O_FILES) - $(CC) $(filter-out obj/gfx%.o, $(O_FILES)) obj/gfx_terminal.o -o $(PROGRAM) + $(CC) $(FILTERED_O_FILES) obj/merge_std.o obj/gfx_terminal.o -o $(PROGRAM) sdl: $(O_FILES) - $(CC) $(filter-out obj/gfx%.o, $(O_FILES)) obj/gfx_sdl.o -o $(PROGRAM) -lSDL2 -lSDL2_ttf + $(CC) $(FILTERED_O_FILES) obj/merge_std.o obj/gfx_sdl.o -o $(PROGRAM) -lSDL2 -lSDL2_ttf obj/%.o: src/%.c $(CC) $(DEFINES) $(CFLAGS) -c -o $@ $< diff --git a/src/engine.c b/src/engine.c index f95b1c2..e38c7be 100644 --- a/src/engine.c +++ b/src/engine.c @@ -192,6 +192,8 @@ void gamestate_new_block(struct gamestate *g) if (!g->grid[x][y]) p++; } + + if (y == g->opts->grid_height - 1) y = 0; } #endif @@ -211,12 +213,12 @@ static int digits_ceiling(unsigned int n) return l + 1; } -/* Return NULL if we couldn't allocate space for the gamestate. The opt - * argument can be passed directly via gameoptions_default i.e - * *o = gamestate_init(gameoptions_default) is valid, as the delete function - * will find the pointer to the gameoptions and delete the data accordingly. */ -struct gamestate* gamestate_init(struct gameoptions *opt) +/* Return NULL if we couldn't allocate space for the gamestate. initializating the + * gamestate will parse the options internally, so any caller should pass argc and argv + * through this function */ +struct gamestate* gamestate_init(int argc, char **argv) { + struct gameoptions *opt = parse_options(gameoptions_default(), argc, argv); if (!opt) return NULL; srand(time(NULL)); diff --git a/src/engine.h b/src/engine.h index 3aff024..941dcb1 100644 --- a/src/engine.h +++ b/src/engine.h @@ -38,6 +38,6 @@ int gamestate_end_condition(struct gamestate*); void gamestate_new_block(struct gamestate*); int gamestate_tick(struct gfx_state*, struct gamestate*, int, void (*callback)(struct gfx_state*, struct gamestate*)); void gamestate_clear(struct gamestate*); -struct gamestate* gamestate_init(struct gameoptions *); +struct gamestate* gamestate_init(int argc, char **argv); #endif diff --git a/src/main.c b/src/main.c index ba0feb4..a8b1774 100644 --- a/src/main.c +++ b/src/main.c @@ -11,8 +11,7 @@ void draw_then_sleep(struct gfx_state *s, struct gamestate *g) int main(int argc, char **argv) { - struct gameoptions *o = gameoptions_default(); - struct gamestate *g = gamestate_init(parse_options(o, argc, argv)); + struct gamestate *g = gamestate_init(argc, argv); struct gfx_state *s = gfx_init(g); int game_running = true; diff --git a/src/merge_fib.c.nomk b/src/merge_fib.c similarity index 73% rename from src/merge_fib.c.nomk rename to src/merge_fib.c index 8ecfcf9..f1b8975 100644 --- a/src/merge_fib.c.nomk +++ b/src/merge_fib.c @@ -1,6 +1,6 @@ #include "merge.h" -#define MERGE_GOAL (int)(sizeof(merge_values)/sizeof(merge_values[0])) +#define MERGE_GOAL (int)((sizeof(merge_values)/sizeof(merge_values[0]))-1) const long merge_values[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, @@ -19,7 +19,8 @@ inline long merge_goal(void) inline int merge_possible(const int v1, const int v2) { - return v1 == v2 - 1 || v2 == v1 - 1; + return v1 == v2 - 1 || v2 == v1 - 1 || + ((v1 == 1 || v1 == 2) && (v2 == 1 || v2 == 2)); } inline int merge_result(const int v1, const int v2) diff --git a/src/merge.c b/src/merge_std.c similarity index 85% rename from src/merge.c rename to src/merge_std.c index 76d1898..57d0a0b 100644 --- a/src/merge.c +++ b/src/merge_std.c @@ -1,6 +1,6 @@ #include "merge.h" -#define MERGE_GOAL (int)(sizeof(merge_values)/sizeof(merge_values[0])) +#define MERGE_GOAL (int)((sizeof(merge_values)/sizeof(merge_values[0]))-1) const long merge_values[] = { 0, 2, 4, 8, 16, 32, 64, 128, 256, 512,