add gui for game over

This commit is contained in:
David Husicka 2021-12-14 19:24:51 +01:00
parent 273b86b77d
commit 558086f56f
4 changed files with 108 additions and 16 deletions

BIN
res/game_over.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

89
res/game_over.svg Normal file
View File

@ -0,0 +1,89 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="800"
height="920"
viewBox="0 0 211.66666 243.41667"
version="1.1"
id="svg5"
inkscape:version="1.1.1 (3bf5ae0d25, 2021-09-20, custom)"
sodipodi:docname="game_over.svg"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<sodipodi:namedview
id="namedview7"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
inkscape:document-units="mm"
showgrid="false"
units="px"
width="800px"
inkscape:zoom="1.0180501"
inkscape:cx="379.64733"
inkscape:cy="362.4576"
inkscape:window-width="1920"
inkscape:window-height="1011"
inkscape:window-x="0"
inkscape:window-y="0"
inkscape:window-maximized="1"
inkscape:current-layer="layer1" />
<defs
id="defs2">
<rect
x="70.262996"
y="57.504998"
width="686.5568"
height="303.83988"
id="rect12444" />
<rect
x="16.997874"
y="64.511345"
width="142.45095"
height="94.937476"
id="rect9954" />
</defs>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1">
<rect
style="fill:#ededed;fill-opacity:0.91326994;stroke:none;stroke-width:4.99999;stroke-linejoin:round;paint-order:stroke fill markers"
id="rect3956"
height="243.41667"
x="0"
y="1.110223e-16"
width="211.66667"
inkscape:export-filename="/home/dudi/c/2048/res/game_over.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96" />
<text
xml:space="preserve"
transform="scale(0.26458333)"
id="text12442"
style="fill:black;fill-opacity:1;line-height:1.25;stroke:none;font-family:sans-serif;font-style:normal;font-weight:normal;font-size:40px;white-space:pre;shape-inside:url(#rect12444)"
inkscape:export-filename="/home/dudi/c/2048/res/game_over.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96" />
<text
xml:space="preserve"
style="font-style:normal;font-weight:normal;font-size:22.5778px;line-height:1.25;font-family:sans-serif;fill:#000000;fill-opacity:1;stroke:none;stroke-width:0.264583"
x="41.559414"
y="64.095924"
id="text13308"
inkscape:export-filename="/home/dudi/c/2048/res/game_over.png"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"><tspan
sodipodi:role="line"
id="tspan13306"
style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:22.5778px;font-family:'URW Gothic';-inkscape-font-specification:'URW Gothic Bold';stroke-width:0.264583"
x="41.559414"
y="64.095924">Game Over</tspan></text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -3,30 +3,30 @@
#include <time.h>
#include <stdbool.h>
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);
}

View File

@ -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;