Skip to content

Commit

Permalink
Merge pull request #3 from mr-ema/feature/resize-window
Browse files Browse the repository at this point in the history
Added resize window functionality
  • Loading branch information
mr-ema authored Jan 31, 2024
2 parents 2f53c3f + 175b629 commit f00e90f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static void deinit_game(Game* game) {
static void restard_game(Game* game) {
game->snake.score = 0;
game->snake.len = 1;
game->snake.head.position = (Vector2){ game->grid.start_x, game->grid.start_y };
game->snake.head.position = (Vector2){ game->grid.start_x + game->grid.tile_size, game->grid.start_y };
game->snake.speed = (Vector2){ (float)game->grid.tile_size, 0 };
game->snake.allow_move = false;

Expand Down
2 changes: 2 additions & 0 deletions src/game_over.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CNAKE_GAME_OVER_H

#include "raylib.h"
#include "config.h"
#include "types.h"

static void handle_game_over(const u32 screen_width, const u32 screen_height, GameState* state);
Expand All @@ -13,6 +14,7 @@ static void handle_game_over(const u32 screen_width, const u32 screen_height, Ga
}

BeginDrawing();
ClearBackground(SCREEN_BACKGROUND);
draw_game_over(screen_width, screen_height);
EndDrawing();
}
Expand Down
12 changes: 12 additions & 0 deletions src/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Grid init_grid();
static void handle_grid(const Grid* grid, GridOptions options);
static void draw_grid(const Grid* grid);
static void draw_grid_borders(const Grid* grid);
static void recenter_grid(Grid* grid, u32 screen_width, u32 screen_height);

static void handle_grid(const Grid* grid, GridOptions options) {
if (options == GRID_ONLY_BORDERS) {
Expand Down Expand Up @@ -61,4 +62,15 @@ static void draw_grid(const Grid* grid) {
}
}

static void recenter_grid(Grid* grid, u32 screen_width, u32 screen_height) {
u32 grid_width = grid->columns * grid->tile_size;
u32 grid_height = grid->rows * grid->tile_size;

u32 offset_x = (screen_width - grid_width) / 2;
u32 offset_y = (screen_height - grid_height) / 2;

grid->start_x = (float)offset_x;
grid->start_y = (float)offset_y;
}

#endif
25 changes: 25 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "grid.h"
#include "raylib.h"
#include "controls.h"
#include "menu.h"
Expand All @@ -16,7 +17,9 @@ static void update_game(Game* game);
int main(void) {
Game game = init_game();

SetConfigFlags(FLAG_WINDOW_RESIZABLE);
InitWindow(game.screen_width, game.screen_height, "Cnake");

InitAudioDevice();
init_resources(&game);

Expand All @@ -26,6 +29,28 @@ int main(void) {
while (!game.exit_game) {
if (WindowShouldClose()) {
game.state = EXIT_GAME;
} else if (IsWindowResized() && !IsWindowFullscreen()) {
game.screen_width = GetScreenWidth();
game.screen_height = GetScreenHeight();

u32 old_grid_start_x = game.grid.start_x;
u32 old_grid_start_y = game.grid.start_y;

recenter_grid(&game.grid, game.screen_width, game.screen_height);

float offset_diff_x = (float)(game.grid.start_x - old_grid_start_x);
float offset_diff_y = (float)(game.grid.start_y - old_grid_start_y);

game.fruit.rec.x += offset_diff_x;
game.fruit.rec.y += offset_diff_y;

game.snake.head.position.x += offset_diff_x;
game.snake.head.position.y += offset_diff_y;

for (u32 i = game.snake.len - 1; i > 0; i--) {
game.snake.body[i].position.x += offset_diff_x;
game.snake.body[i].position.y += offset_diff_y;
}
}

update_game(&game);
Expand Down
71 changes: 33 additions & 38 deletions src/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ static void watch_menu(const Option options[], u8 options_len, u8 font_size, Men
static bool is_mouse_over(const Option* option);
static bool option_click(const Option* option);

static void handle_menu(GameState* state) {
MenuState menu_state = MAIN_MENU;
static MenuState menu_state = MAIN_MENU;

static void handle_menu(GameState* state) {
const u8 menu_options_len = 3;
const u8 controls_options_len = 4;
const u8 font_size = 20;
Expand All @@ -54,43 +54,38 @@ static void handle_menu(GameState* state) {
set_options(control_options, controls_options_len, font_size);
set_options(menu_options, menu_options_len, font_size);

while (*state == MENU) {
if (WindowShouldClose()) {
menu_state = EXIT;
}

switch (menu_state) {
case MAIN_MENU:
if (IsKeyPressed(get_keybinding(PAUSE_GAME))) {
*state = PLAYING;
}
watch_menu(menu_options, menu_options_len, font_size, &menu_state);
break;
case RESUME:
switch (menu_state) {
case MAIN_MENU:
if (IsKeyPressed(get_keybinding(PAUSE_GAME))) {
*state = PLAYING;
break;
case CONTROLS_MENU:
if (IsKeyPressed(get_keybinding(GOBACK))) {
menu_state = MAIN_MENU;
}
watch_menu(control_options, controls_options_len, font_size, &menu_state);
break;
case CHANGE_UP_KEY:
handle_change_key(MOVE_UP, &menu_state);
break;
case CHANGE_LEFT_KEY:
handle_change_key(MOVE_LEFT, &menu_state);
break;
case CHANGE_RIGHT_KEY:
handle_change_key(MOVE_RIGHT, &menu_state);
break;
case CHANGE_DOWN_KEY:
handle_change_key(MOVE_DOWN, &menu_state);
break;
case EXIT:
*state = EXIT_GAME;
break;
}
}
watch_menu(menu_options, menu_options_len, font_size, &menu_state);
break;
case RESUME:
*state = PLAYING;
menu_state = MAIN_MENU;
break;
case CONTROLS_MENU:
if (IsKeyPressed(get_keybinding(GOBACK))) {
menu_state = MAIN_MENU;
}
watch_menu(control_options, controls_options_len, font_size, &menu_state);
break;
case CHANGE_UP_KEY:
handle_change_key(MOVE_UP, &menu_state);
break;
case CHANGE_LEFT_KEY:
handle_change_key(MOVE_LEFT, &menu_state);
break;
case CHANGE_RIGHT_KEY:
handle_change_key(MOVE_RIGHT, &menu_state);
break;
case CHANGE_DOWN_KEY:
handle_change_key(MOVE_DOWN, &menu_state);
break;
case EXIT:
*state = EXIT_GAME;
break;
}
}

Expand Down

0 comments on commit f00e90f

Please sign in to comment.