From d5050975b10402089dc7f979e6ce3619c87e73e2 Mon Sep 17 00:00:00 2001 From: Nitepone Date: Sun, 26 Sep 2021 15:12:02 -0400 Subject: [PATCH] Cut down `tetris_game.h` API --- src/render.c | 2 + src/solo_main.c | 3 +- src/tetris_game.c | 1 + src/tetris_game.h | 86 ++----------------------------------- src/tetris_game_priv.h | 93 +++++++++++++++++++++++++++++++++++++++++ test/test_tetris_game.c | 1 + 6 files changed, 102 insertions(+), 84 deletions(-) create mode 100644 src/tetris_game_priv.h diff --git a/src/render.c b/src/render.c index 7f21592..c5824c3 100644 --- a/src/render.c +++ b/src/render.c @@ -33,6 +33,8 @@ struct board_display { WINDOW *lines_window; }; +enum rotation { none, right, invert, left }; + static struct board_display *boards = NULL; static int nboards; // flag used to indicate that the window layout needs to be refreshed diff --git a/src/solo_main.c b/src/solo_main.c index 9057c9d..ca1f99a 100644 --- a/src/solo_main.c +++ b/src/solo_main.c @@ -60,8 +60,7 @@ void print_board() { printf("\n\e[0;0m"); } printf(" "); - printf("Points: %d Lines: %d", game_contents->points, - game_contents->lines_cleared); + printf("Points: %d Lines: %d", gvd->points, gvd->lines_cleared); printf("\n"); } diff --git a/src/tetris_game.c b/src/tetris_game.c index bcca894..925e565 100644 --- a/src/tetris_game.c +++ b/src/tetris_game.c @@ -11,6 +11,7 @@ #include "os_compat.h" #include "tetris_game.h" +#include "tetris_game_priv.h" /* * Destroys a game_contents and frees memory diff --git a/src/tetris_game.h b/src/tetris_game.h index a9a5381..74b98e0 100644 --- a/src/tetris_game.h +++ b/src/tetris_game.h @@ -10,26 +10,16 @@ #include -#define MAX_BLOCK_UNITS 4 -#define BLOCK_START_POSITION \ - { 4, 21 } - -#define MAX_AUTO_LOWER 3 -#define MAX_SWAP_H 1 - #define BOARD_HEIGHT 24 #define BOARD_PLAY_HEIGHT 20 #define BOARD_WIDTH 10 -#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) +struct game_contents; -enum rotation { - none, - right, - invert, - left, +struct position { + int x; + int y; }; -#define ROT_COUNT 4 enum block_type { no_type, @@ -43,74 +33,6 @@ enum block_type { smashboy, }; -enum block_rotation_type { no_rotate, center_based, corner_based }; - -struct position { - int x; - int y; -}; - -struct tetris_block { - enum block_type type; - enum block_rotation_type rotation_type; - unsigned int cell_count; - const struct position *position_offsets; -}; - -static const struct position orange_block_offsets[] = { - {0, 0}, {0, -1}, {1, -1}, {0, 1}}; - -static const struct position blue_block_offsets[] = { - {0, 0}, {0, -1}, {-1, -1}, {0, 1}}; - -static const struct position cleve_block_offsets[] = { - {0, 0}, {-1, 0}, {-1, 1}, {0, -1}}; - -static const struct position rhode_block_offsets[] = { - {0, 0}, {-1, 0}, {-1, -1}, {0, 1}}; - -static const struct position teewee_block_offsets[] = { - {0, 0}, {1, 0}, {0, 1}, {-1, 0}}; - -static const struct position hero_block_offsets[] = { - {0, -1}, {0, 0}, {0, 1}, {0, 2}}; - -static const struct position smashboy_block_offsets[] = { - {0, 0}, {0, 1}, {1, 0}, {1, 1}}; - -static const struct tetris_block available_blocks[] = { - {orange, center_based, 4, orange_block_offsets}, - {blue, center_based, 4, blue_block_offsets}, - {cleve, center_based, 4, cleve_block_offsets}, - {rhode, center_based, 4, rhode_block_offsets}, - {teewee, center_based, 4, teewee_block_offsets}, - {hero, corner_based, 4, hero_block_offsets}, - {smashboy, corner_based, 4, smashboy_block_offsets}}; - -static const struct tetris_block tetris_block_null = {no_type, no_rotate, 0, - NULL}; - -struct active_block { - struct tetris_block tetris_block; - enum rotation rotation; - /* position of block center */ - struct position position; - struct position board_units[MAX_BLOCK_UNITS]; -}; - -struct game_contents { - int points; - int lines_cleared; - int auto_lower_count; - int swap_h_block_count; - int board[BOARD_HEIGHT][BOARD_WIDTH]; - unsigned int seed; - struct tetris_block next_block; - struct tetris_block hold_block; - struct active_block *active_block; - struct active_block *shadow_block; -}; - struct game_view_data { int points; int lines_cleared; diff --git a/src/tetris_game_priv.h b/src/tetris_game_priv.h new file mode 100644 index 0000000..a515a80 --- /dev/null +++ b/src/tetris_game_priv.h @@ -0,0 +1,93 @@ +/* + * tetris-game-priv.h + * Copyright (C) 2019-2021 nitepone + * + * Distributed under terms of the MIT license. + */ + +#ifndef TETRIS_GAME_PRIV_H +#define TETRIS_GAME_PRIV_H + +#include + +#define MAX_BLOCK_UNITS 4 +#define BLOCK_START_POSITION \ + { 4, 21 } + +#define MAX_AUTO_LOWER 3 +#define MAX_SWAP_H 1 + +#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) + +enum rotation { + none, + right, + invert, + left, +}; +#define ROT_COUNT 4 + +enum block_rotation_type { no_rotate, center_based, corner_based }; + +struct tetris_block { + enum block_type type; + enum block_rotation_type rotation_type; + unsigned int cell_count; + const struct position *position_offsets; +}; + +static const struct position orange_block_offsets[] = { + {0, 0}, {0, -1}, {1, -1}, {0, 1}}; + +static const struct position blue_block_offsets[] = { + {0, 0}, {0, -1}, {-1, -1}, {0, 1}}; + +static const struct position cleve_block_offsets[] = { + {0, 0}, {-1, 0}, {-1, 1}, {0, -1}}; + +static const struct position rhode_block_offsets[] = { + {0, 0}, {-1, 0}, {-1, -1}, {0, 1}}; + +static const struct position teewee_block_offsets[] = { + {0, 0}, {1, 0}, {0, 1}, {-1, 0}}; + +static const struct position hero_block_offsets[] = { + {0, -1}, {0, 0}, {0, 1}, {0, 2}}; + +static const struct position smashboy_block_offsets[] = { + {0, 0}, {0, 1}, {1, 0}, {1, 1}}; + +static const struct tetris_block available_blocks[] = { + {orange, center_based, 4, orange_block_offsets}, + {blue, center_based, 4, blue_block_offsets}, + {cleve, center_based, 4, cleve_block_offsets}, + {rhode, center_based, 4, rhode_block_offsets}, + {teewee, center_based, 4, teewee_block_offsets}, + {hero, corner_based, 4, hero_block_offsets}, + {smashboy, corner_based, 4, smashboy_block_offsets}}; + +static const struct tetris_block tetris_block_null = {no_type, no_rotate, 0, + NULL}; + +struct active_block { + struct tetris_block tetris_block; + enum rotation rotation; + /* position of block center */ + struct position position; + struct position board_units[MAX_BLOCK_UNITS]; +}; + +struct game_contents { + int points; + int lines_cleared; + int auto_lower_count; + int swap_h_block_count; + int board[BOARD_HEIGHT][BOARD_WIDTH]; + unsigned int seed; + struct tetris_block next_block; + struct tetris_block hold_block; + struct active_block *active_block; + struct active_block *shadow_block; +}; + +#endif /* !TETRIS_GAME_PRIV_H */ diff --git a/test/test_tetris_game.c b/test/test_tetris_game.c index e6dbe7b..f3ca334 100644 --- a/test/test_tetris_game.c +++ b/test/test_tetris_game.c @@ -6,6 +6,7 @@ */ #include "tetris_game.h" +#include "tetris_game_priv.h" #include "unity.h" /* Is run before every test, put unit init calls here. */