diff options
author | Oxore <oxore@protonmail.com> | 2019-07-28 21:23:27 +0300 |
---|---|---|
committer | Oxore <oxore@protonmail.com> | 2019-07-28 22:03:03 +0300 |
commit | 1a0886d852bb8a0d9428dc6f54c1e503d06073a0 (patch) | |
tree | 0b49cd355b514f4bd5877f08a41fad7f6269a3e1 /src | |
parent | eae3a50f9c791c9aa7dae39cc87d63e0488c4cee (diff) |
Refactor idlist, move all global vars to local scope
- Replace list_foreach with LIST_FOREACH macro.
- Remove `game` global variable and make it local everywhere.
- Move global vars `fld`, `nxt` and `texts` into `game` struct.
- Add `static` qualifier to engine.c's specific global vars.
- Move `sfRenderWindow window` global var to local scope
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.c | 442 | ||||
-rw-r--r-- | src/idlist.c | 9 | ||||
-rw-r--r-- | src/painter.c | 11 | ||||
-rw-r--r-- | src/target/tetris.c | 49 |
4 files changed, 294 insertions, 217 deletions
diff --git a/src/engine.c b/src/engine.c index bad04f5..8692704 100644 --- a/src/engine.c +++ b/src/engine.c @@ -52,7 +52,7 @@ #define LEFTHOLD (1 << 7) #define GAMEOVER (1 << 8) -int level_move_latency[] = { +static int level_move_latency[] = { L00LATENCY, L01LATENCY, L02LATENCY, @@ -85,7 +85,7 @@ int level_move_latency[] = { L29LATENCY }; -int rmlines_score[] = { +static int rmlines_score[] = { 0, RM_1LINES_SCORE, RM_2LINES_SCORE, @@ -93,33 +93,29 @@ int rmlines_score[] = { RM_4LINES_SCORE }; -/* Externs from main.c */ -extern struct game game; -extern struct field fld, nxt; -extern struct idlist *texts; -int keys = 0; +static int keys = 0; -static void render_score_value(void *obj) +static void render_score_value(struct game *game, void *obj) { struct text *text = obj; if (!strcmp(text->type, "score.value")) { if (!text->text) text->text = calloc(BUFSIZ, sizeof(char)); char *a = calloc(BUFSIZ, sizeof(char)); - sprintf(a, "%d", game.scoreCurrent); + sprintf(a, "%d", game->scoreCurrent); utf8to32_strcpy(text->text, a); free(a); } } -static void render_level_value(void *obj) +static void render_level_value(struct game *game, void *obj) { struct text *text = obj; if (!strcmp(text->type, "level.value")) { if (!text->text) text->text = calloc(BUFSIZ, sizeof(char)); char *a = calloc(BUFSIZ, sizeof(char)); - sprintf(a, "%ld", game.level); + sprintf(a, "%ld", game->level); utf8to32_strcpy(text->text, a); free(a); } @@ -226,45 +222,61 @@ static void update_game_text(void *obj) painter_update_text(text->id, text); } -static void transition_menu() +static void transition_menu(struct game *game) { - game.over = 0; - game.started = 0; - game.scoreCurrent = 0; - game.level = 1; - game.moveLatency = get_level_latency(game.level); - game.lines = 0; + game->over = 0; + game->started = 0; + game->scoreCurrent = 0; + game->level = 1; + game->moveLatency = get_level_latency(game->level); + game->lines = 0; - nxt.attr |= FLD_ATTR_INVISIBLE; - painter_update_field(nxt.id, &nxt); + struct field *fld = game->fld; + struct field *nxt = game->nxt; + struct idlist *texts = game->texts; - fld.shape[GHOST_SHAPE_INDEX].y = fld.size.y; - fld.shape[ACTIVE_SHAPE_INDEX].y = fld.size.y; - field_fill_random(&fld); - painter_update_field(fld.id, &fld); + nxt->attr |= FLD_ATTR_INVISIBLE; + painter_update_field(nxt->id, nxt); - list_foreach(texts, hide_game_over_text); - list_foreach(texts, show_menu_text); - list_foreach(texts, hide_game_text); - list_foreach(texts, update_game_over_text); - list_foreach(texts, update_menu_text); - list_foreach(texts, update_game_text); + fld->shape[GHOST_SHAPE_INDEX].y = fld->size.y; + fld->shape[ACTIVE_SHAPE_INDEX].y = fld->size.y; + field_fill_random(fld); + painter_update_field(fld->id, fld); + + LIST_FOREACH(texts, text) { + hide_game_over_text(text->obj); + show_menu_text(text->obj); + hide_game_text(text->obj); + update_game_over_text(text->obj); + update_menu_text(text->obj); + update_game_text(text->obj); + } } -static void transition_game_over_wait() +static void transition_game_over_wait(struct game *game) { - game.over_wait = 1; - sfClock_restart(game.over_wait_tick); - list_foreach(texts, show_game_over_title_text); - list_foreach(texts, update_game_over_text); + game->over_wait = 1; + sfClock_restart(game->over_wait_tick); + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + show_game_over_title_text(text->obj); + update_game_over_text(text->obj); + } } -static void transition_game_over() +static void transition_game_over(struct game *game) { - game.over_wait = 0; - game.over = 1; - list_foreach(texts, show_game_over_press_any_key_text); - list_foreach(texts, update_game_over_text); + game->over_wait = 0; + game->over = 1; + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + show_game_over_press_any_key_text(text->obj); + update_game_over_text(text->obj); + } } static void project_ghost_shape(struct field *fld, size_t idreal, @@ -282,125 +294,156 @@ static void project_ghost_shape(struct field *fld, size_t idreal, ++fld->shape[idghost].y; } -static void transition_put_shape() +static void transition_put_shape(struct game *game) { - field_put_shape(&fld, &fld.shape[ACTIVE_SHAPE_INDEX]); - int removedLines = field_rm_lines(&fld); - game.lines += removedLines; - game.scoreCurrent += rmlines_score[removedLines] * game.level; - fld.shape[ACTIVE_SHAPE_INDEX].t = nxt.shape[0].t; - field_reset_walking_shape(&fld, 1); - project_ghost_shape(&fld, 1, 0); - for (size_t s = 0; s < nxt.shape_cnt - 1; ++s) { - nxt.shape[s] = nxt.shape[s + 1]; - nxt.shape[s].y = 4 - s * 3; + struct field *fld = game->fld; + struct field *nxt = game->nxt; + + field_put_shape(fld, &fld->shape[ACTIVE_SHAPE_INDEX]); + int removedLines = field_rm_lines(fld); + game->lines += removedLines; + game->scoreCurrent += rmlines_score[removedLines] * game->level; + fld->shape[ACTIVE_SHAPE_INDEX].t = nxt->shape[0].t; + field_reset_walking_shape(fld, 1); + project_ghost_shape(fld, 1, 0); + for (size_t s = 0; s < nxt->shape_cnt - 1; ++s) { + nxt->shape[s] = nxt->shape[s + 1]; + nxt->shape[s].y = 4 - s * 3; } - shape_gen_random(&nxt.shape[nxt.shape_cnt - 1]); - level_up(&game); + shape_gen_random(&nxt->shape[nxt->shape_cnt - 1]); + level_up(game); } -static void transition_pause() +static void transition_pause(struct game *game) { - game.paused = 1; - int elapsed = sfClock_getElapsedTime(game.gameTick).microseconds; - if (game.moveLatency - elapsed >= 0) - game.moveLatency -= elapsed; + game->paused = 1; + int elapsed = sfClock_getElapsedTime(game->gameTick).microseconds; + if (game->moveLatency - elapsed >= 0) + game->moveLatency -= elapsed; else - game.moveLatency = get_level_latency(game.level); - sfClock_restart(game.gameTick); - list_foreach(texts, show_pause_text); + game->moveLatency = get_level_latency(game->level); + sfClock_restart(game->gameTick); + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + show_pause_text(text->obj); + } } -static void transition_unpause() +static void transition_unpause(struct game *game) { - game.paused = 0; - sfClock_restart(game.gameTick); - list_foreach(texts, hide_pause_text); + game->paused = 0; + sfClock_restart(game->gameTick); + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + hide_pause_text(text->obj); + } } -static void game_tick() +static void game_tick(struct game *game) { - sfClock_restart(game.gameTick); + sfClock_restart(game->gameTick); + + struct field *fld = game->fld; + struct field *nxt = game->nxt; - game.moveLatency = get_level_latency(game.level); - if (field_move_shape_down(&fld, 1)) { - project_ghost_shape(&fld, 1, 0); - sfClock_restart(game.putTick); + game->moveLatency = get_level_latency(game->level); + if (field_move_shape_down(fld, 1)) { + project_ghost_shape(fld, 1, 0); + sfClock_restart(game->putTick); } - if (sfClock_getElapsedTime(game.putTick).microseconds >= PUT_LATENCY) { - if (field_shape_out_of_bounds(&fld, &fld.shape[ACTIVE_SHAPE_INDEX])) - transition_game_over_wait(); + if (sfClock_getElapsedTime(game->putTick).microseconds >= PUT_LATENCY) { + if (field_shape_out_of_bounds(fld, &fld->shape[ACTIVE_SHAPE_INDEX])) + transition_game_over_wait(game); else - transition_put_shape(); - sfClock_restart(game.putTick); + transition_put_shape(game); + sfClock_restart(game->putTick); + } + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + render_score_value(game, text->obj); + render_level_value(game, text->obj); } - list_foreach(texts, render_score_value); - list_foreach(texts, render_level_value); - painter_update_field(fld.id, &fld); - painter_update_field(nxt.id, &nxt); + painter_update_field(fld->id, fld); + painter_update_field(nxt->id, nxt); } -static void signal_up() +static void signal_up(struct game *game) { - field_rotate_shape_clockwise(&fld, 1); - project_ghost_shape(&fld, 1, 0); - sfClock_restart(game.putTick); + struct field *fld = game->fld; + + field_rotate_shape_clockwise(fld, 1); + project_ghost_shape(fld, 1, 0); + sfClock_restart(game->putTick); } -static void signal_harddrop() +static void signal_harddrop(struct game *game) { - while (field_move_shape_down(&fld, 1)) - game.scoreCurrent++; - if (field_shape_out_of_bounds(&fld, &fld.shape[ACTIVE_SHAPE_INDEX])) - transition_game_over_wait(); + struct field *fld = game->fld; + + while (field_move_shape_down(fld, 1)) + game->scoreCurrent++; + if (field_shape_out_of_bounds(fld, &fld->shape[ACTIVE_SHAPE_INDEX])) + transition_game_over_wait(game); else - transition_put_shape(); - sfClock_restart(game.gameTick); - sfClock_restart(game.putTick); + transition_put_shape(game); + sfClock_restart(game->gameTick); + sfClock_restart(game->putTick); } -static void signal_down() +static void signal_down(struct game *game) { - if (field_move_shape_down(&fld, 1)) { - project_ghost_shape(&fld, 1, 0); - sfClock_restart(game.gameTick); - game.scoreCurrent++; + struct field *fld = game->fld; + + if (field_move_shape_down(fld, 1)) { + project_ghost_shape(fld, 1, 0); + sfClock_restart(game->gameTick); + game->scoreCurrent++; } } -static void signal_left() +static void signal_left(struct game *game) { - if (field_move_shape_left(&fld, 1)) { - project_ghost_shape(&fld, 1, 0); - sfClock_restart(game.putTick); + struct field *fld = game->fld; + + if (field_move_shape_left(fld, 1)) { + project_ghost_shape(fld, 1, 0); + sfClock_restart(game->putTick); } } -static void signal_right() +static void signal_right(struct game *game) { - if (field_move_shape_right(&fld, 1)) { - project_ghost_shape(&fld, 1, 0); - sfClock_restart(game.putTick); + struct field *fld = game->fld; + + if (field_move_shape_right(fld, 1)) { + project_ghost_shape(fld, 1, 0); + sfClock_restart(game->putTick); } } -static void signal_pause() +static void signal_pause(struct game *game) { - if (game.paused) - transition_unpause(); + if (game->paused) + transition_unpause(game); else - transition_pause(); - sfClock_restart(game.putTick); + transition_pause(game); + sfClock_restart(game->putTick); } -static void game_keys() +static void game_keys(struct game *game) { /* PAUSE */ if (sfKeyboard_isKeyPressed(sfKeyP)) { if (!(keys & PAUSE)) { keys |= PAUSE; - signal_pause(); + signal_pause(game); } } else { keys &= ~PAUSE; @@ -410,7 +453,7 @@ static void game_keys() if (sfKeyboard_isKeyPressed(sfKeyUp)) { if (!(keys & UP)) { keys = keys | UP; - signal_up(); + signal_up(game); } } else { keys = keys & ~UP; @@ -420,7 +463,7 @@ static void game_keys() if (sfKeyboard_isKeyPressed(sfKeySpace)) { if (!(keys & HARDDROP)) { keys |= HARDDROP; - signal_harddrop(); + signal_harddrop(game); } } else { keys &= ~HARDDROP; @@ -430,10 +473,10 @@ static void game_keys() if (sfKeyboard_isKeyPressed(sfKeyDown)) { if (!(keys & DOWN)) { keys = keys | DOWN; - signal_down(); - sfClock_restart(game.repPushDown); + signal_down(game); + sfClock_restart(game->repPushDown); } else { - if (sfClock_getElapsedTime(game.repPushDown).microseconds + if (sfClock_getElapsedTime(game->repPushDown).microseconds >= moveRepeatLatency2) keys &= ~DOWN; } @@ -446,16 +489,16 @@ static void game_keys() && !sfKeyboard_isKeyPressed(sfKeyRight)) { if (!(keys & LEFT)) { keys = keys | LEFT; - signal_left(); - sfClock_restart(game.repKeyLeft); + signal_left(game); + sfClock_restart(game->repKeyLeft); } else if (!(keys & LEFTHOLD)) { - if (sfClock_getElapsedTime(game.repKeyLeft).microseconds + if (sfClock_getElapsedTime(game->repKeyLeft).microseconds >= moveRepeatLatency1) { keys |= LEFTHOLD; keys &= ~LEFT; } } else { - if (sfClock_getElapsedTime(game.repKeyLeft).microseconds + if (sfClock_getElapsedTime(game->repKeyLeft).microseconds >= moveRepeatLatency2) keys &= ~LEFT; } @@ -469,16 +512,16 @@ static void game_keys() && !sfKeyboard_isKeyPressed(sfKeyLeft)) { if (!(keys & RIGHT)) { keys = keys | RIGHT; - signal_right(); - sfClock_restart(game.repKeyRight); + signal_right(game); + sfClock_restart(game->repKeyRight); } else if (!(keys & RIGHTHOLD)) { - if (sfClock_getElapsedTime(game.repKeyRight).microseconds + if (sfClock_getElapsedTime(game->repKeyRight).microseconds >= moveRepeatLatency1) { keys |= RIGHTHOLD; keys &= ~RIGHT; } } else { - if (sfClock_getElapsedTime(game.repKeyRight).microseconds + if (sfClock_getElapsedTime(game->repKeyRight).microseconds >= moveRepeatLatency2) keys &= ~RIGHT; } @@ -488,90 +531,114 @@ static void game_keys() } } -static void pause_keys() +static void pause_keys(struct game *game) { /* PAUSE */ if (sfKeyboard_isKeyPressed(sfKeyP)) { if (!(keys & PAUSE)) { keys |= PAUSE; - signal_pause(); + signal_pause(game); } } else { keys &= ~PAUSE; } } -static void menu_tick() +static void menu_tick(struct game *game) { - sfClock_restart(game.mTick); - field_fill_random(&fld); - painter_update_field(fld.id, &fld); + struct field *fld = game->fld; + + sfClock_restart(game->mTick); + field_fill_random(fld); + painter_update_field(fld->id, fld); } -void transition_init(void) +void transition_init(struct game *game) { - list_foreach(texts, show_menu_text); - list_foreach(texts, hide_game_text); - list_foreach(texts, hide_game_over_text); - list_foreach(texts, update_menu_text); - list_foreach(texts, update_game_text); - list_foreach(texts, update_game_over_text); + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + show_menu_text(text->obj); + hide_game_text(text->obj); + hide_game_over_text(text->obj); + update_menu_text(text->obj); + update_game_text(text->obj); + update_game_over_text(text->obj); + } } -static void transition_game_start() +static void transition_game_start(struct game *game) { - game.started = 1; - game.paused = 0; - field_clear(&fld); - shape_gen_random(&fld.shape[ACTIVE_SHAPE_INDEX]); - field_reset_walking_shape(&fld, 1); - project_ghost_shape(&fld, 1, 0); - shape_load(&fld.shape[ACTIVE_SHAPE_INDEX]); - for (size_t i = 0; i < nxt.shape_cnt; ++i) - shape_gen_random(&nxt.shape[i]); - nxt.attr &= ~FLD_ATTR_INVISIBLE; - list_foreach(texts, hide_menu_text); - list_foreach(texts, show_game_text); - list_foreach(texts, hide_pause_text); - list_foreach(texts, update_menu_text); - list_foreach(texts, update_game_text); - painter_update_field(fld.id, &fld); - painter_update_field(nxt.id, &nxt); - sfClock_restart(game.gameTick); + struct field *fld = game->fld; + struct field *nxt = game->nxt; + + game->started = 1; + game->paused = 0; + field_clear(fld); + shape_gen_random(&fld->shape[ACTIVE_SHAPE_INDEX]); + field_reset_walking_shape(fld, 1); + project_ghost_shape(fld, 1, 0); + shape_load(&fld->shape[ACTIVE_SHAPE_INDEX]); + for (size_t i = 0; i < nxt->shape_cnt; ++i) + shape_gen_random(&nxt->shape[i]); + nxt->attr &= ~FLD_ATTR_INVISIBLE; + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + hide_menu_text(text->obj); + show_game_text(text->obj); + hide_pause_text(text->obj); + update_menu_text(text->obj); + update_game_text(text->obj); + } + + painter_update_field(fld->id, fld); + painter_update_field(nxt->id, nxt); + sfClock_restart(game->gameTick); } -static void menu_loop() +static void menu_loop(struct game *game) { - if (sfClock_getElapsedTime(game.mTick).microseconds >= basicLatency) - menu_tick(); + if (sfClock_getElapsedTime(game->mTick).microseconds >= basicLatency) + menu_tick(game); if (sfKeyboard_isKeyPressed(sfKeyS)) { if (!(keys & GAMEOVER)) - transition_game_start(); + transition_game_start(game); } else { keys = 0; } } -static void game_loop() +static void game_loop(struct game *game) { - game_keys(); - if (sfClock_getElapsedTime(game.gameTick).microseconds >= game.moveLatency) - game_tick(); - list_foreach(texts, render_score_value); - list_foreach(texts, render_level_value); - list_foreach(texts, update_game_text); - painter_update_field(fld.id, &fld); - painter_update_field(nxt.id, &nxt); + struct field *fld = game->fld; + struct field *nxt = game->nxt; + + game_keys(game); + if (sfClock_getElapsedTime(game->gameTick).microseconds >= game->moveLatency) { + game_tick(game); + } + + struct idlist *texts = game->texts; + + LIST_FOREACH(texts, text) { + render_score_value(game, text->obj); + render_level_value(game, text->obj); + update_game_text(text->obj); + } + painter_update_field(fld->id, fld); + painter_update_field(nxt->id, nxt); } -static void game_over_wait_loop() +static void game_over_wait_loop(struct game *game) { - if (sfClock_getElapsedTime(game.over_wait_tick).microseconds > GAMEOVERWAIT) - transition_game_over(); + if (sfClock_getElapsedTime(game->over_wait_tick).microseconds > GAMEOVERWAIT) + transition_game_over(game); } -static void game_over_loop() +static void game_over_loop(struct game *game) { int anykey = 0; @@ -582,32 +649,31 @@ static void game_over_loop() if (anykey) { if (!(keys & GAMEOVER)) { keys |= GAMEOVER; - transition_menu(); + transition_menu(game); } } else { keys &= ~GAMEOVER; } } -static void pause_loop() +static void pause_loop(struct game *game) { - pause_keys(); + pause_keys(game); } -void main_loop() +void main_loop(struct game *game) { - if (game.started) { - if (game.paused) - pause_loop(); - else if (game.over_wait) - game_over_wait_loop(); - else if (game.over) - game_over_loop(); + if (game->started) { + if (game->paused) + pause_loop(game); + else if (game->over_wait) + game_over_wait_loop(game); + else if (game->over) + game_over_loop(game); else - game_loop(); + game_loop(game); } else { - menu_loop(); + menu_loop(game); } painter_draw(); } - diff --git a/src/idlist.c b/src/idlist.c index 14db85f..e4e7c91 100644 --- a/src/idlist.c +++ b/src/idlist.c @@ -45,15 +45,6 @@ void list_rm_node(struct idlist *node) } } -void list_foreach(struct idlist *list, void (*job)(void *)) -{ - if (list) { - job(list->obj); - while ((list = list->next)) - job(list->obj); - } -} - void list_destroy(struct idlist *list) { if (list) { diff --git a/src/painter.c b/src/painter.c index a9029b1..5c4a0db 100644 --- a/src/painter.c +++ b/src/painter.c @@ -248,7 +248,11 @@ static void draw_drawable(void *obj) void painter_draw() { sfRenderWindow_clear(window, (sfColor)UIBGCOLOR); - list_foreach(drawables, draw_drawable); + + LIST_FOREACH(drawables, drawable) { + draw_drawable(drawable->obj); + } + sfRenderWindow_display(window); } @@ -278,7 +282,10 @@ void painter_destroy_drawable(size_t id) void painter_destroy_drawables() { - list_foreach(drawables, destroy_drawable); + LIST_FOREACH(drawables, drawable) { + destroy_drawable(drawable->obj); + } + list_destroy(drawables); drawables = 0; } diff --git a/src/target/tetris.c b/src/target/tetris.c index 81b2afc..1ef2836 100644 --- a/src/target/tetris.c +++ b/src/target/tetris.c @@ -19,20 +19,10 @@ #include "engine.h" #include "tet_conf.h" -sfRenderWindow *window; -struct idlist *texts; -struct field fld, nxt; -struct game game = { - .started = 0, - .paused = 0, - .scoreCurrent = 0, - .level = 1, - .moveLatency = L00LATENCY, - .lines = 0 -}; - -static void handleWindowEvents() { +static void handleWindowEvents(sfRenderWindow *window) +{ sfEvent event; + while (sfRenderWindow_pollEvent(window, &event)) if (event.type == sfEvtClosed) sfRenderWindow_close(window); @@ -46,6 +36,22 @@ static void register_text(void *obj) int main() { + sfRenderWindow *window; + + struct idlist *texts; + struct field fld, nxt; + struct game game = { + .started = 0, + .paused = 0, + .scoreCurrent = 0, + .level = 1, + .moveLatency = L00LATENCY, + .lines = 0, + .fld = &fld, + .nxt = &nxt, + .texts = NULL, + }; + srand(time(NULL)); game.gameTick = sfClock_create(); game.over_wait_tick = sfClock_create(); @@ -89,15 +95,22 @@ int main() painter_update_field(nxt.id, &nxt); texts = load_texts("dat/texts.yaml"); - list_foreach(texts, register_text); + LIST_FOREACH(texts, text) { + register_text(text->obj); + } - transition_init(); + game.texts = texts; + + transition_init(&game); while (sfRenderWindow_isOpen(window)) { - handleWindowEvents(); - main_loop(); + handleWindowEvents(window); + main_loop(&game); + } + + LIST_FOREACH(texts, text) { + text_destroy(text->obj); } - list_foreach(texts, text_destroy); list_destroy(texts); painter_destroy_drawables(); |