Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
Fixed segfault
Browse files Browse the repository at this point in the history
  • Loading branch information
xfnty committed Jun 28, 2023
1 parent eaeebf3 commit 9da3d90
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/game/game.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "game/state.h"
#include <game/game.h>

#include <assert.h>
Expand Down Expand Up @@ -35,10 +36,19 @@ void game_tick(game_t* game, update_context_t ctx) {
assert(game != NULL);
assert(game->was_initialized);

game->is_running = !WindowShouldClose();
if (game->_next_state.name) {
game_state_exit(&game->state, game);
game->state = game->_next_state;
game->_next_state = (game_state_t){0};
game_state_enter(&game->state, game);

LOGF("entered state \"%s\"", strid_get_str(game->state.name));
}

ClearBackground(BLACK);
game_state_update(&game->state, game, ctx);

game->is_running = !WindowShouldClose();
}

void game_debug_draw(game_t* game, update_context_t ctx) {
Expand All @@ -59,9 +69,5 @@ void game_switch_state(game_t* game, game_state_t new_state) {
assert(game != NULL);
assert(game->was_initialized);

game_state_exit(&game->state, game);
game->state = new_state;
game_state_enter(&game->state, game);

LOGF("entered state \"%s\"", strid_get_str(new_state.name));
game->_next_state = new_state;
}
1 change: 1 addition & 0 deletions src/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ typedef struct game_s {
bool was_initialized;
bool is_running;
game_state_t state;
game_state_t _next_state;

RenderTexture2D canvas;
} game_t;
Expand Down
8 changes: 2 additions & 6 deletions src/game/states/gameplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,8 @@ void update_bird(gameplay_state_t* gameplay, game_t* game) {

gameplay->bird_y += gameplay->bird_y_speed * GetFrameTime();

if (gameplay->bird_y + sprites.bird.height >= game->canvas.texture.height - sprites.base.height) {
if (gameplay->bird_y + sprites.bird.height >= game->canvas.texture.height - sprites.base.height)
game_switch_state(game, gameplay_state_create());
return;
}

Rectangle bird_rect = {
(float)game->canvas.texture.width / 2 - (float)sprites.bird.width / 2,
Expand Down Expand Up @@ -211,10 +209,8 @@ void update_bird(gameplay_state_t* gameplay, game_t* game) {
DrawRectangleLinesEx(top_pipe_rect, 1, ORANGE);
DrawRectangleLinesEx(bottom_pipe_rect, 1, ORANGE);

if (CheckCollisionRecs(bird_rect, top_pipe_rect) || CheckCollisionRecs(bird_rect, bottom_pipe_rect)) {
if (CheckCollisionRecs(bird_rect, top_pipe_rect) || CheckCollisionRecs(bird_rect, bottom_pipe_rect))
game_switch_state(game, gameplay_state_create());
return;
}
}
}

Expand Down

0 comments on commit 9da3d90

Please sign in to comment.