From 80ff7b315a6b5a9f9c62de5c6b03f52ddf099837 Mon Sep 17 00:00:00 2001 From: Oxore Date: Thu, 6 Dec 2018 05:24:30 +0300 Subject: Add simple documentation in comments, refactor. Change all `unsigned int` and `unsigned long` types to `size_t`. Fix names alignment in headers. Add documentation and simple description: Painter - painter.h Main - tetris.c Unicode - unicode.h IDList - idlist.h Engine - engine.c Minor changes: tetris.c - fix indentation and code blocks separation with newlines, remove unused includes. idlist.h - fix structure field name alignment. field.h, engine.c - define aliases for ghost and active shapes indexes in the field. engine.c - rename menuTick to snake case, fix curly braces style of functions. Makefile - switch SFML deprecated warnings off. --- include/common.h | 24 ++++++++++----------- include/field.h | 63 ++++++++++++++++++++++++++++-------------------------- include/idlist.h | 29 ++++++++++++++++--------- include/painter.h | 47 ++++++++++++++++++++++++++++++---------- include/tet_conf.h | 2 +- include/text.h | 22 +++++++++---------- include/unicode.h | 14 ++++++++++-- include/vector.h | 2 +- 8 files changed, 125 insertions(+), 78 deletions(-) (limited to 'include') diff --git a/include/common.h b/include/common.h index 3273c66..76293c5 100644 --- a/include/common.h +++ b/include/common.h @@ -1,14 +1,14 @@ struct game { - unsigned int level; - int started; - int paused; - int scoreCurrent; - int moveLatency; - int lines; - sfClock *gameTick; - sfClock *putTick; - sfClock *mTick; - sfClock *repPushDown; // repeat latency when hold Down arrow - sfClock *repKeyLeft; // repeat latency when hold Left arrow - sfClock *repKeyRight; // repeat latency when hold Right arrow + size_t level; + int started; + int paused; + int scoreCurrent; + int moveLatency; + int lines; + sfClock *gameTick; + sfClock *putTick; + sfClock *mTick; + sfClock *repPushDown; // repeat latency when hold Down arrow + sfClock *repKeyLeft; // repeat latency when hold Left arrow + sfClock *repKeyRight; // repeat latency when hold Right arrow }; diff --git a/include/field.h b/include/field.h index c63a836..4e97aa8 100644 --- a/include/field.h +++ b/include/field.h @@ -3,9 +3,12 @@ #define SHP_ATTR_GHOST (1 << 0) +#define GHOST_SHAPE_INDEX 0 +#define ACTIVE_SHAPE_INDEX 1 + struct cell { - char a; // active/empty state of cell - unsigned int color; + char a; // active/empty state of cell + size_t color; }; /* @@ -26,36 +29,36 @@ struct cell { */ struct shape { - struct vector2i; // position - int t; // shape type - unsigned int attr; - char c[4][4]; + struct vector2i; // position + int t; // shape type + size_t attr; + char c[4][4]; }; struct field { - unsigned long id; - unsigned int attr; - struct vector2i pos; - struct vector2ui size; - struct vector2ui bound; - struct cell **c; // array of logic shape cells - unsigned int shape_cnt; - struct shape *shape; + size_t id; + size_t attr; + struct vector2i pos; + struct vector2ui size; + struct vector2ui bound; + struct cell **c; // array of logic shape cells + size_t shape_cnt; + struct shape *shape; }; -void field_init(struct field *fld); -void field_deinit(struct field *fld); -void field_fill_random(struct field *fld); -void field_clear(struct field *fld); -void field_rotate_shape_clockwise(struct field *fld, unsigned int index); -int field_move_shape_down(struct field *fld, unsigned int index); -int field_move_shape_left(struct field *fld, unsigned int index); -int field_move_shape_right(struct field *fld, unsigned int index); -void field_put_shape(struct field *fld, struct shape *shape); -void field_reset_walking_shape(struct field *fld, unsigned int index); -int field_rm_lines(struct field *fld); -int field_shape_collision(struct field *fld, struct shape *shape); -int field_shape_out_of_bounds(struct field *fld, struct shape *shape); - -void shape_load(struct shape *shape); -void shape_gen_random(struct shape *shape); +void field_init(struct field *fld); +void field_deinit(struct field *fld); +void field_fill_random(struct field *fld); +void field_clear(struct field *fld); +void field_rotate_shape_clockwise(struct field *fld, size_t index); +int field_move_shape_down(struct field *fld, size_t index); +int field_move_shape_left(struct field *fld, size_t index); +int field_move_shape_right(struct field *fld, size_t index); +void field_put_shape(struct field *fld, struct shape *shape); +void field_reset_walking_shape(struct field *fld, size_t index); +int field_rm_lines(struct field *fld); +int field_shape_collision(struct field *fld, struct shape *shape); +int field_shape_out_of_bounds(struct field *fld, struct shape *shape); + +void shape_load(struct shape *shape); +void shape_gen_random(struct shape *shape); diff --git a/include/idlist.h b/include/idlist.h index 3318be4..c006a32 100644 --- a/include/idlist.h +++ b/include/idlist.h @@ -1,13 +1,22 @@ +/* + * idlist.h + * + * IDList - List datatype implementation. Element can be taken by id only. + * + * IDList is used by the Painter module and loadable texts. + * + * */ + struct idlist { - unsigned long id; - void *obj; - struct idlist *next; - struct idlist *prev; + size_t id; + void *obj; + struct idlist *next; + struct idlist *prev; }; -struct idlist *list_new(); -struct idlist *list_append(struct idlist *list); -struct idlist *list_get(const struct idlist *list, unsigned long id); -void list_rm_node(struct idlist *node); -void list_foreach(struct idlist *list, void (*job)(void *)); -void list_destroy(struct idlist *list); +struct idlist *list_new(); +struct idlist *list_append(struct idlist *list); +struct idlist *list_get(const struct idlist *list, size_t id); +void list_rm_node(struct idlist *node); +void list_foreach(struct idlist *list, void (*job)(void *)); +void list_destroy(struct idlist *list); diff --git a/include/painter.h b/include/painter.h index 7bcb47a..fc01092 100644 --- a/include/painter.h +++ b/include/painter.h @@ -1,15 +1,40 @@ -void painter_set_window(sfRenderWindow *window); +/* + * painter.h + * + * Painter is a module for drawing tetris objects on a window. It is implemented + * as a singletone as OO programmers speak. It internally has global static + * arrays for objects referenced by handles. You register an object and get a + * handle (id) which can be used for updating an object - modifying it's + * drawable representation. Painter is used as described in following steps: + * + * 1. At first assign a window via painter_set_window() function. + * 2. Load a font if you are going to draw any text. + * 3. Register an object (field, text, etc.) using painter_register_*() + * function. + * 4. Update an object attribute using painter_update_*() function. Update all + * your objects for next frame before drawing. + * 5. Draw everything using painter_draw() function. Objects are drawn in order + * they were registered. + * 6. At the end you must destroy your drawable objects and fonts. Objects are + * registered each in it's own way, but have unified interface for + * destroying - painter_destroy_drawable() function. You can destroy them + * all at once using painter_destroy_drawables() function or even destroy + * everything that has been allocated by the Painter, even loaded fonts. + * + * */ -void painter_load_font(char *filename); -void painter_destroy_font(); +void painter_set_window(sfRenderWindow *window); -unsigned long painter_register_field(struct field *fld); -void painter_update_field(unsigned long id, struct field *fld); -unsigned long painter_register_text(struct text *txt); -void painter_update_text(unsigned long id, struct text *txt); +void painter_load_font(char *filename); +void painter_destroy_font(); -void painter_draw(); +size_t painter_register_field(struct field *fld); +void painter_update_field(size_t id, struct field *fld); +size_t painter_register_text(struct text *txt); +void painter_update_text(size_t id, struct text *txt); -void painter_destroy_drawable(unsigned long id); -void painter_destroy_drawables(); -void painter_destroy_all(); +void painter_draw(); + +void painter_destroy_drawable(size_t id); +void painter_destroy_drawables(); +void painter_destroy_all(); diff --git a/include/tet_conf.h b/include/tet_conf.h index c5a6153..33a637f 100644 --- a/include/tet_conf.h +++ b/include/tet_conf.h @@ -52,7 +52,7 @@ #define RM_2LINES_SCORE 100 #define RM_3LINES_SCORE 300 #define RM_4LINES_SCORE 1200 -#define LEVELUP_LINES 10 +#define LEVELUP_LINES 10 #define FLD_SIZE_Y 22 #define FLD_SIZE_X 10 diff --git a/include/text.h b/include/text.h index 5c2dc5d..d8d9a47 100644 --- a/include/text.h +++ b/include/text.h @@ -1,15 +1,15 @@ -#define TXT_ATTR_INVISIBLE (1 << 0) +#define TXT_ATTR_INVISIBLE (1 << 0) typedef struct text { - char *type; - char *scene; - char *font; - wchar_t *text; - unsigned int size; - struct vector2ui pos; - unsigned int attr; - unsigned long id; + char *type; + char *scene; + char *font; + wchar_t *text; + size_t size; + struct vector2ui pos; + size_t attr; + size_t id; } Text; -struct idlist *load_texts(char *filename); -void text_destroy(void *text); +struct idlist *load_texts(char *filename); +void text_destroy(void *text); diff --git a/include/unicode.h b/include/unicode.h index 0a34ce3..4e5910a 100644 --- a/include/unicode.h +++ b/include/unicode.h @@ -1,2 +1,12 @@ -unsigned long utf8_strlen(char *string); -void utf8to32_strcpy(wchar_t *dest, char *src); +/* + * unicode.h + * + * Unicode - module with some helper unicode functions. + * + * SFML uses UTF32 encoding for texts and there is no need to use any full + * featured library just for a couple of encoding conversion functions. + * + * */ + +size_t utf8_strlen(char *string); +void utf8to32_strcpy(wchar_t *dest, char *src); diff --git a/include/vector.h b/include/vector.h index fb00384..71ca579 100644 --- a/include/vector.h +++ b/include/vector.h @@ -3,5 +3,5 @@ struct vector2i { }; struct vector2ui { - unsigned int x, y; + size_t x, y; }; -- cgit v1.2.3