summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2018-07-03 15:11:54 +0300
committerOxore <oxore@protonmail.com>2018-07-03 15:11:54 +0300
commited8127a72e1a2d2703372d5b5dfa8d96703ec3bb (patch)
tree80f340c64861913f2dc648c2c7280c144b988881 /include
parent21a0a44853451f3d791bafc80c01deab0ff4c79e (diff)
Refactor field, refactor next shape drawing
Refactor field functions names. Transfer next shape drawing to painter. Introduce idlist struct with foreach function. Refactor config. Introduce color map array instead of "switch case" statement bloating while coloring rectangles. Decouple field and shape structs from SFML sfVector2 structs by introducing own vector2i and vector2ui structs. Also remove sfRectangleShape members form field an shape structs.
Diffstat (limited to 'include')
-rw-r--r--include/common.h22
-rw-r--r--include/draw.h17
-rw-r--r--include/engine.h16
-rw-r--r--include/field.h66
-rw-r--r--include/idlist.h12
-rw-r--r--include/tet_conf.h73
6 files changed, 110 insertions, 96 deletions
diff --git a/include/common.h b/include/common.h
index 7a85dbc..098ea9e 100644
--- a/include/common.h
+++ b/include/common.h
@@ -1,13 +1,13 @@
struct game {
- int isStarted;
- int scoreCurrent;
- int level;
- int moveLatency;
- int lines;
- sfClock *gameTick;
- sfClock *putTick;
- sfClock *mTick;
- sfClock *repPushDown; // Clock for repeat latency when Down arrow long push
- sfClock *repKeyLeft; // Clock for repeat latency when Left arrow long push
- sfClock *repKeyRight; // Clock for repeat latency when Left arrow long push
+ unsigned int level;
+ int isStarted;
+ 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 Left arrow
};
diff --git a/include/draw.h b/include/draw.h
index 3408142..17bf176 100644
--- a/include/draw.h
+++ b/include/draw.h
@@ -4,20 +4,15 @@ struct window {
sfEvent event;
};
-//struct field_conf {
-// sfVector2i pos;
-// int cOutThick;
-// sfVector2f cSize;
-// sfVector2i size;
-//} field_conf;
-
void painter_init_window();
+void painter_destroy_window();
unsigned long painter_register_field();
void painter_update_field(unsigned long id, struct field *fld);
-
-unsigned long painter_register_shape();
-void painter_update_shape(unsigned long id, struct shape *shape);
+void painter_destroy_field(unsigned long id);
+void painter_destroy_fields();
void painter_draw();
-void painter_destroy_window();
+
+void painter_destroy_drawables();
+void painter_destroy_all();
diff --git a/include/engine.h b/include/engine.h
index 2dd7e9f..bca16ed 100644
--- a/include/engine.h
+++ b/include/engine.h
@@ -1,15 +1,13 @@
-#define RIGHT 1
-#define DOWN 2
-#define UP 4
-#define LEFT 8
-#define RIGHTHOLD 16
-#define LEFTHOLD 128
+#define RIGHT (1 << 0)
+#define DOWN (1 << 1)
+#define UP (1 << 2)
+#define LEFT (1 << 3)
+#define RIGHTHOLD (1 << 4)
+#define LEFTHOLD (1 << 5)
void tKeyCtrl();
void tTick();
void checkLevelUp(struct game *game);
-int getMoveLatencyOfLevel(int level);
+int getMoveLatencyOfLevel(unsigned int level);
void valueAfterTextDisplay(int value, List *texts, char *type);
-void drawFld(sfRenderWindow *window, struct field *fld);
void gameover(struct game *game);
-void drawNextShape(sfRenderWindow *window);
diff --git a/include/field.h b/include/field.h
index f037d41..93f80f0 100644
--- a/include/field.h
+++ b/include/field.h
@@ -1,17 +1,18 @@
-#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
-#define FLD_POS (sfVector2i){.x = 10, .y = 10+550-24}
-#define FLD_SIZE (sfVector2f){.x = FLD_SIZE_X, .y = FLD_SIZE_Y}
-#define CELL_SIZE (sfVector2f){.x = 23, .y = 23}
+#define FLD_ATTR_INVISIBLE (1 << 0)
+#define FLD_ATTR_TRANSPARENT (1 << 1)
struct cell {
char a; // active/empty state of cell
- sfColor fColor; // fill color
unsigned int color;
};
+struct vector2i {
+ int x, y;
+};
+
+struct vector2ui {
+ unsigned int x, y;
+};
/*
* field + shape coord system
@@ -34,36 +35,31 @@ 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
+ unsigned int color; // 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;
- struct cell c[FLD_BOUND_Y][FLD_SIZE_X]; // array of logic shape cells
- sfRectangleShape *p[FLD_SIZE_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;
- unsigned int id;
+ 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;
};
-void init_field(struct field *fld);
-void colorize_field(struct field *fld);
-void colorize_field_random(struct field *fld);
-void colorize_active_shape(struct field *fld, struct shape *shape);
-void init_next_shape(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);
-void gen_shape(struct shape *shape);
-int rm_lines(struct field *fld);
-void free_field(struct field *fld);
-void free_shape(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(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);
diff --git a/include/idlist.h b/include/idlist.h
new file mode 100644
index 0000000..d1badc7
--- /dev/null
+++ b/include/idlist.h
@@ -0,0 +1,12 @@
+struct idlist {
+ unsigned long 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 *));
diff --git a/include/tet_conf.h b/include/tet_conf.h
index 134e8a9..14a8c2a 100644
--- a/include/tet_conf.h
+++ b/include/tet_conf.h
@@ -1,42 +1,55 @@
-#define windowName_conf "Tetris CSFML" // Window name
-#define UIBGCOLOR sfColor_fromRGB(26, 26, 26) // Field background
-#define UIFGINACTIVECOLOR sfColor_fromRGB(55, 59, 65) // Field inactive cell outline
-#define OUT_THICK 1
-#define UIFGACTIVECOLOR sfColor_fromRGB(40, 42, 46) // Field active cell outline
-#define LCOLOR sfColor_fromRGB(222, 147, 95) // Orange color of L shape
-#define RLCOLOR sfColor_fromRGB(95, 129, 157)
-#define SCOLOR sfColor_fromRGB(140, 148, 64)
-#define ZCOLOR sfColor_fromRGB(165, 66, 66)
-#define BCOLOR sfColor_fromRGB(197, 200, 198)
-#define ICOLOR sfColor_fromRGB(94, 141, 135)
-#define TCOLOR sfColor_fromRGB(133, 103, 143)
+#define windowName_conf "Tetris CSFML" // Window name
+#define OUT_THICK 1
+#define UITRANSPARENT {0, 0, 0, 0 } // Field background
+#define UIBGCOLOR {26, 26, 26, 255} // Field background
+#define UIFGINACTIVECOLOR {55, 59, 65, 255} // Field inactive cell outline
+#define UIFGACTIVECOLOR {40, 42, 46, 255} // Field active cell outline
+#define LCOLOR {222, 147, 95, 255} // Orange color of L shape
+#define RLCOLOR {95, 129, 157, 255}
+#define ZCOLOR {165, 66, 66, 255}
+#define SCOLOR {140, 148, 64, 255}
+#define BCOLOR {197, 200, 198, 255}
+#define ICOLOR {94, 141, 135, 255}
+#define TCOLOR {133, 103, 143, 255}
+
#define moveRepeatLatency1 150000 // microseconds, only for left-right arrows,
- // first repeat move when long push
+ // first repeat move when long push
#define moveRepeatLatency2 30000 // microseconds, for Left, Right and Down
- // arrows, the rest repeat move when long push
+ // arrows, the rest repeat move when long push
#define basicLatency 500000
-#define L00LATENCY 800000
-#define L01LATENCY 716667
-#define L02LATENCY 633334
-#define L03LATENCY 550000
-#define L04LATENCY 466667
-#define L05LATENCY 383334
-#define L06LATENCY 300000
-#define L07LATENCY 216667
-#define L08LATENCY 133334
-#define L09LATENCY 100000
-#define L10LATENCY 83334
-#define L13LATENCY 66667
-#define L16LATENCY 50000
-#define L19LATENCY 33334
-#define L29LATENCY 16667
-#define PUT_LATENCY 300000
+#define L00LATENCY 800000
+#define L01LATENCY 716667
+#define L02LATENCY 633334
+#define L03LATENCY 550000
+#define L04LATENCY 466667
+#define L05LATENCY 383334
+#define L06LATENCY 300000
+#define L07LATENCY 216667
+#define L08LATENCY 133334
+#define L09LATENCY 100000
+#define L10LATENCY 83334
+#define L13LATENCY 66667
+#define L16LATENCY 50000
+#define L19LATENCY 33334
+#define L29LATENCY 16667
+#define PUT_LATENCY 300000
#define RM_1LINES_SCORE 40
#define RM_2LINES_SCORE 100
#define RM_3LINES_SCORE 300
#define RM_4LINES_SCORE 1200
#define LEVELUP_LINES 10
+#define FLD_SIZE_Y 22
+#define FLD_SIZE_X 10
+#define FLD_BOUND_Y FLD_SIZE_Y + 4
+#define FLD_BOUND_X FLD_SIZE_X
+#define FLD_POS (struct vector2i){.x = 10, .y = 10+550-24}
+#define CELL_SIZE (sfVector2f){.x = 23, .y = 23}
+
+#define NXT_SIZE_Y 8
+#define NXT_SIZE_X 4
+#define NXT_POS (struct vector2i){.x = 250 + 10 + 30, .y = 300}
+#define NXT_SIZE (struct vector2ui){.x = NXT_SIZE_X, .y = NXT_SIZE_Y}
/*
* 22..........