diff --git a/res/game_over.png b/res/game_over.png new file mode 100644 index 0000000..c90afc7 Binary files /dev/null and b/res/game_over.png differ diff --git a/res/game_over.svg b/res/game_over.svg new file mode 100644 index 0000000..48cda09 --- /dev/null +++ b/res/game_over.svg @@ -0,0 +1,89 @@ + + + + + + + + + + + + + Game Over + + diff --git a/src/2048.c b/src/2048.c index 36ef090..6ec828b 100644 --- a/src/2048.c +++ b/src/2048.c @@ -3,30 +3,30 @@ #include #include -void game_add_block(Game game) { +void game_add_block(Game *game) { bool is_full = true; - for (int i = 0; i < game.field_size_y; i++) { - for (int j = 0; j < game.field_size_x; j++) { - if (game.field[j][i] == 0) { + for (int i = 0; i < game->field_size_y; i++) { + for (int j = 0; j < game->field_size_x; j++) { + if (game->field[j][i] == 0) { is_full = false; break; } } } if (is_full) { - game.game_over = true; - return 1; + game->game_over = true; + return; } size_t x, y; int value; do { - x = rand() % game.field_size_x; - y = rand() % game.field_size_y; + x = rand() % game->field_size_x; + y = rand() % game->field_size_y; value = (rand() % 2) + 1; - } while (game.field[x][y] != 0); - game.field[x][y] = value * 2; + } while (game->field[x][y] != 0); + game->field[x][y] = value * 2; } Game game_init(size_t field_size_x, size_t field_size_y) { @@ -42,7 +42,7 @@ Game game_init(size_t field_size_x, size_t field_size_y) { time_t t; srand((unsigned) time(&t)); - game_add_block(game); + game_add_block(&game); return game; } @@ -170,5 +170,5 @@ void game_move(Game *game, Direction direction) { } } - game_add_block(*game); + game_add_block(game); } \ No newline at end of file diff --git a/src/gui.c b/src/gui.c index 83c7435..933f226 100644 --- a/src/gui.c +++ b/src/gui.c @@ -14,6 +14,7 @@ SDL_Renderer *window_renderer = NULL; SDL_Texture* tile_texture = NULL; SDL_Texture* tile_textures[13]; SDL_Rect tile_size; +SDL_Texture* game_over_texture = NULL; SDL_Texture *texture_from_png(char *n) { SDL_Surface *s = IMG_Load(n); @@ -63,6 +64,8 @@ void gui_init() { tile_size.x = 0; tile_size.y = 0; SDL_QueryTexture(tile_texture, NULL, NULL, &tile_size.w, &tile_size.h); + + game_over_texture = texture_from_png("res/game_over.png"); } void gui_destroy() { @@ -72,6 +75,7 @@ void gui_destroy() { for (int i = 0; i < 13; i++) { SDL_DestroyTexture(tile_textures[i]); } + SDL_DestroyTexture(game_over_texture); //Quit SDL subsystems SDL_Quit(); @@ -117,8 +121,6 @@ void gui_loop(Game *game) { break; } break; - - /* SDL_QUIT event (window close) */ case SDL_QUIT: quit = 1; break; @@ -129,9 +131,9 @@ void gui_loop(Game *game) { SDL_RenderClear(window_renderer); SDL_Rect tile_rect = tile_size; for (int i = 0; i < game->field_size_x; i++) { - tile_rect.x = i * 175; + tile_rect.x = i * 175 + 50; for (int j = 0; j < game->field_size_y; j++) { - tile_rect.y = j * 175; + tile_rect.y = j * 175 + 150; // get texture index from value for (int k = 0; k < 13; k++) { @@ -145,6 +147,7 @@ void gui_loop(Game *game) { } if (game->game_over) { // render game over screen + SDL_RenderCopy(window_renderer, game_over_texture, NULL, NULL); } SDL_RenderPresent(window_renderer); needs_redraw = false;