From bf0e5690a31d4c3cecd2ba512729a0b73989bbda Mon Sep 17 00:00:00 2001 From: Oxore Date: Mon, 25 Jun 2018 15:50:58 +0300 Subject: Major refactoring Fix memleak in KeyMap, fix memleak caused by loading a yaml file in main.c. Change Copyright information. Rename functions.c and .h to engine.c and .h. Take field and shape related functions to separate file (still not all of them) and refactor them a lot. Refactor collision detection. Add more warnings. Add sanitizer option commented out. --- include/common.h | 77 +++-------------------------------------------------- include/engine.h | 19 +++++++++++++ include/field.h | 58 ++++++++++++++++++++++++++++++++++++++++ include/functions.h | 34 ----------------------- include/text.h | 48 ++++++++++++++++++++++++--------- 5 files changed, 116 insertions(+), 120 deletions(-) create mode 100644 include/engine.h create mode 100644 include/field.h delete mode 100644 include/functions.h (limited to 'include') diff --git a/include/common.h b/include/common.h index 773cb3f..275547a 100644 --- a/include/common.h +++ b/include/common.h @@ -1,82 +1,13 @@ -/* - * Types - * - * */ - -typedef struct Cell { - char a; // active/empty state of cell - sfColor fColor; // fill color -} Cell; - - -/* - * shape coords - * y - * ^. . . . - * |. . . . - * |. . . . - * |. . . . - * 0------->x - * - */ - -typedef struct Shape { - int x; // x coord of shape's left side - int y; // y coord of shape's bottom - int t; // shape type - sfColor fColor; // shape color - char c[4][4]; // array of logic shape cells - sfRectangleShape *p[4][4]; // array of physical shape cells - sfVector2f cSize; // shape rectangles size variable x/y -} Shape; - -typedef struct Field { - sfVector2i pos; - sfColor fColor; // shape color - Cell c[25][10]; // array of logic shape cells - sfRectangleShape *p[25][10]; // array of physical shape cells - int cOutThick; // Field rectangles outline thickness - sfVector2f cSize; // shape rectangles size variable x/y - sfVector2i size; -} Field; - -typedef struct Window { +struct window { sfVideoMode mode; sfRenderWindow *window; sfEvent event; -} Window; +}; -typedef struct Game { +struct game { int isStarted; int scoreCurrent; int level; int moveLatency; int lines; -} Game; - -/* ======== text.[c|h] types =========== */ - -typedef struct List { - void *obj; - void *next; - void *prev; -} List; - -typedef struct Pair { - void *k; - void *v; -} Pair; - -typedef struct KeyMap { - Pair *pair; - void *next; - void *prev; -} KeyMap; - -typedef struct Text { - char *font; - char *type; - char *scene; - char *text; - void *sfText; -} Text; +}; diff --git a/include/engine.h b/include/engine.h new file mode 100644 index 0000000..655eb01 --- /dev/null +++ b/include/engine.h @@ -0,0 +1,19 @@ +#define RIGHT 1 +#define DOWN 2 +#define UP 4 +#define LEFT 8 +#define RIGHTHOLD 16 +#define LEFTHOLD 128 + +void tKeyCtrl(); +void tTick(); +void checkLevelUp(struct game *game); +int getMoveLatencyOfLevel(int level); +int rmLines(); +void valueAfterTextDisplay(int value, List *texts, char *type); +void colorizeActive(); +void drawFld(sfRenderWindow *window); +void gameover(struct game *game); +void genNextShape(); +void drawNextShape(); +void freeFld(); diff --git a/include/field.h b/include/field.h new file mode 100644 index 0000000..410e609 --- /dev/null +++ b/include/field.h @@ -0,0 +1,58 @@ +#define FLD_SIZE_Y 22 +#define FLD_SIZE_X 10 +#define FLD_BOUND_Y FLD_SIZE_Y + 3 +#define FLD_BOUND_X FLD_SIZE_X +/* + * Types + * + * */ + +struct cell { + char a; // active/empty state of cell + sfColor fColor; // fill color +}; + + +/* + * shape coords + * y + * ^. . . . + * |. . . . + * |. . . . + * |. . . . + * 0------->x + * + */ + +struct shape { + int x; // x coord of shape's left side + int y; // y coord of shape's bottom + int t; // shape type + sfColor fColor; // shape color + char c[4][4]; // array of logic shape cells + sfRectangleShape *p[4][4]; // array of physical shape cells + int cOutThick; // shape rectangles outline thickness + sfVector2f cSize; // shape rectangles size variable x/y +}; + +struct field { + sfVector2i pos; + sfColor fColor; // shape color + struct cell c[FLD_BOUND_Y][FLD_SIZE_X]; // array of logic shape cells + sfRectangleShape *p[FLD_BOUND_Y][FLD_SIZE_X]; // array of physical shape cells + int cOutThick; // Field rectangles outline thickness + sfVector2f cSize; // shape rectangles size variable x/y + sfVector2i size; + sfVector2i bound; +}; + +void init_field(struct field *fld); +void colorize_field(struct field *fld); +void colorize_field_random(struct field *fld); +void init_next_shape_field(struct shape *next); +void putShape(struct field *fld, struct shape *active); +int out_of_field(struct field *fld, struct shape *active); +void load_shape(struct shape *shape); +void rotate_shape(struct field *fld, struct shape *shape); +int collide(struct field *fld, struct shape *active); +void resetActiveShape(struct field *fld, struct shape *active); diff --git a/include/functions.h b/include/functions.h deleted file mode 100644 index f47f816..0000000 --- a/include/functions.h +++ /dev/null @@ -1,34 +0,0 @@ -#define RIGHT 0b0001 -#define DOWN 0b0010 -#define UP 0b0100 -#define LEFT 0b1000 -#define RIGHTHOLD 0b10000 -#define LEFTHOLD 0b10000000 - -void tKeyCtrl(); -void initFld(); -void tTick(); -void resetActiveShape(Shape *active); -void putShape(); -int outOfFieldCheck(Field *fld, Shape *active); -void checkLevelUp(Game *game); -int getMoveLatencyOfLevel(int level); -int cellCollisionCheckHere(Field *fld, Shape *active); -int cellCollisionCheck(int dir); -int wallCollisionCheck(); -int cellRotCollisionCheck(); -int wallRotCollisionCheck(); -void rotateLeft(); -void rotateRight(); -void rotateShape(); -int rmLines(); -void valueAfterTextDisplay(int value, List *texts, char *type); -void colorizeFld(); -void colorizeActive(); -void drawFld(sfRenderWindow *window); -void colorizeRandom(Field *fld); -void gameover(Game *game); -void genNextShape(); -void copyShape(Shape *localSh); -void drawNextShape(); -void freeFld(); diff --git a/include/text.h b/include/text.h index 1171a51..73285bf 100644 --- a/include/text.h +++ b/include/text.h @@ -1,17 +1,39 @@ -FILE *openFile(char *filename); -void checkArgs(int argc, char **argv); +typedef struct List { + void *obj; + void *next; + void *prev; +} List; + +typedef struct Pair { + void *k; + void *v; +} Pair; + +typedef struct KeyMap { + Pair *pair; + void *next; + void *prev; +} KeyMap; + +typedef struct Text { + char *font; + char *type; + char *scene; + char *text; + void *sfText; +} Text; + +FILE *openFile(char *filename); +void checkArgs(int argc, char **argv); KeyMap *KeyMap_getLast(KeyMap **keyMap); KeyMap *KeyMap_new(KeyMap **keyMap); KeyMap *KeyMap_get(KeyMap **keyMap, const void *key); KeyMap *KeyMap_put(KeyMap **keyMap, const void *key, const void *value); -List *List_getLast(List **list); -List *List_new(List **list); -List *ListOfKeyMapOfString_getFromYaml(char *filename); -void KeyMapOfString_free(KeyMap *keyMap); -void ListOfKeyMapOfString_free(List **list); -int _loadText_getInt(void *obj, char *key); -char *_loadText_getString(void *obj, char *key); -void _loadText_initSfText(Text *objo, void *obji); -List *ListOfText_getFromListOfKeyMapOfString(List *list); -void Text_free(Text *obj); -void ListOfText_free(List **list); +List *List_getLast(List **list); +List *List_new(List **list); +List *ListOfKeyMapOfString_getFromYaml(char *filename); +void KeyMapOfString_free(KeyMap *keyMap); +void ListOfKeyMapOfString_free(List **list); +List *ListOfText_getFromListOfKeyMapOfString(List *list); +void Text_free(Text *obj); +void ListOfText_free(List **list); -- cgit v1.2.3