feat (Game): game menu start

This commit is contained in:
Quinten Mennen 2025-05-27 16:32:27 +02:00
parent b626712e51
commit b98400db00
15 changed files with 388 additions and 92 deletions

View File

@ -8,6 +8,18 @@ uniform sampler2D Texture0;
uniform sampler2D Texture1;
uniform sampler2D Texture2;
uniform sampler2D Texture3;
uniform sampler2D Texture4;
uniform sampler2D Texture5;
uniform sampler2D Texture6;
uniform sampler2D Texture7;
uniform sampler2D Texture8;
uniform sampler2D Texture9;
uniform sampler2D Texture10;
uniform sampler2D Texture11;
uniform sampler2D Texture12;
uniform sampler2D Texture13;
uniform sampler2D Texture14;
uniform sampler2D Texture15;
uniform float u_time;
uniform float u_battery;
@ -31,7 +43,19 @@ void main()
case 1: texColor = texture(Texture1, TexCoord); break;
case 2: texColor = texture(Texture2, TexCoord); break;
case 3: texColor = texture(Texture3, TexCoord); break;
default: texColor = vec4(0.0, 0.0, 0.0, 0.0); break;
case 4: texColor = texture(Texture4, TexCoord); break;
case 5: texColor = texture(Texture5, TexCoord); break;
case 6: texColor = texture(Texture6, TexCoord); break;
case 7: texColor = texture(Texture7, TexCoord); break;
case 8: texColor = texture(Texture8, TexCoord); break;
case 9: texColor = texture(Texture9, TexCoord); break;
case 10: texColor = texture(Texture10, TexCoord); break;
case 11: texColor = texture(Texture11, TexCoord); break;
case 12: texColor = texture(Texture12, TexCoord); break;
case 13: texColor = texture(Texture13, TexCoord); break;
case 14: texColor = texture(Texture14, TexCoord); break;
case 15: texColor = texture(Texture15, TexCoord); break;
default: texColor = vec4(1.0, 0.0, 0.0, 1.0); break;
}
if (TexIndex == 1) {

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* cub3d.h :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/05/23 11:34:52 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:24:09 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,9 +34,12 @@
# define CYAN "\033[0;36m"
# define WHITE "\033[0;37m"
# define NUM_KEYS 256
# define NUM_KEYS 300
# define TILE_SIZE 8
# define MINIMAP_SIZE 300
# define NUM_MENU_OPTIONS 2
# define MENU_OPTION_START 0
# define MENU_OPTION_EXIT 1
# include "MLX42.h"
# include "allowed.h"
@ -54,6 +57,8 @@
# include "collision.h"
# include "parser.h"
# include "texture.h"
# include "game_manager.h"
# include "game_menu.h"
int initialize_cub3d(t_game **game, const char *mapfile);
int shader_init(t_game **game);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* game.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */
/* Updated: 2025/05/25 21:01:17 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* game.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:16 by qmennen #+# #+# */
/* Updated: 2025/05/27 13:58:21 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,5 +21,6 @@ void game_free(t_game *game);
void game_terminate(t_game *game);
void free_game(t_game **game);
void print_scores(t_game *game);
void game_run(t_game *game);
#endif

24
inc/game_manager.h Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_manager.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 13:40:20 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:22:43 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GAME_MANAGER_H
# define GAME_MANAGER_H
# include "cub3d.h"
t_game_manager *game_manager_create(t_game *game);
void game_manager_display(t_game_manager *manager);
void game_manager_update(void *param);
void game_manager_select(t_game_manager *manager);
void game_manager_handle_input(t_game_manager *manager);
#endif

22
inc/game_menu.h Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_menu.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 15:07:48 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:08:37 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GAME_MENU_H
# define GAME_MENU_H
# include "cub3d.h"
void menu_create(t_game_manager **manager);
void menu_display(t_menu *menu, t_screen *screen);
void menu_free(t_menu *menu, t_screen *screen);
#endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* types.h :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/05/25 13:37:39 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* types.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:18:58 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -108,6 +108,7 @@ typedef struct s_screen
mlx_image_t *minimap;
mlx_image_t *background;
mlx_image_t *hud;
mlx_image_t *menu;
unsigned int width;
unsigned int height;
} t_screen;
@ -120,6 +121,12 @@ typedef enum e_side
SIDE_EAST,
} t_side;
typedef enum e_game_state
{
GAME_STATE_MENU,
GAME_STATE_PLAYING,
} t_game_state;
typedef struct s_render
{
double perp_dist;
@ -136,12 +143,29 @@ typedef struct s_sprite_column
typedef struct s_game
{
t_map *map;
t_player *player;
t_screen *screen;
t_keyboard *keyboard;
int framecount;
int fps;
t_map *map;
t_player *player;
t_screen *screen;
t_keyboard *keyboard;
int framecount;
int fps;
t_game_state state;
} t_game;
typedef struct s_menu
{
mlx_texture_t *background;
mlx_image_t *options[NUM_MENU_OPTIONS];
mlx_image_t *selector;
int selected_option;
} t_menu;
typedef struct s_game_manager
{
t_game *game;
t_menu *menu;
} t_game_manager;
#endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* game.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:32:27 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:11:11 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ int game_create(t_game **game)
if (!game)
return (FAILURE);
memset(*game, 0, sizeof(t_game));
(*game)->state = GAME_STATE_MENU;
(*game)->fps = 20;
return (SUCCESS);
}
@ -129,12 +130,10 @@ void handle_record(t_game *game)
}
}
void game_loop(void *param)
void game_run(t_game *game)
{
t_game *game;
static int fps = 0;
game = (t_game *)param;
game->framecount++;
fps += (int)(1.f / game->screen->mlx->delta_time);
set_uniforms(game);
@ -147,7 +146,6 @@ void game_loop(void *param)
player_update(game, game->screen->mlx->delta_time);
cast_rays(game);
render_map(game);
keyboard_update(game);
if (game->player->is_moving)
{
game->screen->img->instances[0].x = sin(game->framecount / 6.0) * 20;
@ -159,6 +157,11 @@ void game_loop(void *param)
void game_free(t_game *game)
{
if (game->screen->hud)
{
mlx_delete_image(game->screen->mlx, game->screen->hud);
game->screen->hud = NULL;
}
if (game->screen)
{
mlx_delete_image(game->screen->mlx, game->screen->img);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
/* Updated: 2025/05/23 14:59:06 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:08:44 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,8 @@
int main(int argc, char **argv)
{
t_game *game;
t_game_manager *manager;
t_game *game;
errno = 0;
game = NULL;
@ -28,6 +29,10 @@ int main(int argc, char **argv)
game_free(game);
return (EXIT_FAILURE);
}
manager = game_manager_create(game);
menu_create(&manager);
mlx_key_hook(game->screen->mlx, keyhandle, game);
mlx_loop_hook(game->screen->mlx, game_manager_update, manager);
mlx_loop(game->screen->mlx);
game_terminate(game);
return (EXIT_SUCCESS);

View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_manager.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 13:48:18 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:23:04 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "game_manager.h"
t_game_manager *game_manager_create(t_game *game)
{
t_game_manager *manager;
manager = malloc(sizeof(t_game_manager));
if (!manager)
return (NULL);
manager->game = game;
manager->menu = NULL;
return (manager);
}
void game_manager_display(t_game_manager *manager)
{
t_game *game;
if (!manager || !manager->game)
return;
game = manager->game;
if (game->state == GAME_STATE_MENU)
{
menu_display(manager->menu, game->screen);
}
else if (game->state == GAME_STATE_PLAYING)
{
game_run(game);
}
}
void game_manager_update(void *param)
{
t_game_manager *manager;
manager = (t_game_manager *)param;
game_manager_handle_input(manager);
game_manager_display(manager);
keyboard_update(manager->game);
}

View File

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game_manager_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 15:22:15 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:23:11 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "game_manager.h"
static void game_start(t_game *game)
{
t_screen *screen;
screen = game->screen;
if (mlx_image_to_window(screen->mlx, screen->minimap, 175, 575) < 0)
{
printf(RED "Failed to display buffer image\n" RESET);
game_terminate(game);
}
if (mlx_image_to_window(screen->mlx, screen->hud, 0, 0) < 0)
{
printf(RED "Failed to display buffer image\n" RESET);
game_terminate(game);
}
game->state = GAME_STATE_PLAYING;
}
void game_manager_select(t_game_manager *manager)
{
if (manager->menu->selected_option == 0)
{
menu_free(manager->menu, manager->game->screen);
game_start(manager->game);
}
else if (manager->menu->selected_option == 1)
{
menu_free(manager->menu, manager->game->screen);
game_terminate(manager->game);
}
}
void game_manager_handle_input(t_game_manager *manager)
{
if (get_key_down(manager->game, MLX_KEY_ENTER))
{
game_manager_select(manager);
return ;
}
if (get_key_up(manager->game, MLX_KEY_DOWN))
{
manager->menu->selected_option++;
if (manager->menu->selected_option >= NUM_MENU_OPTIONS)
manager->menu->selected_option = 0;
}
if (get_key_up(manager->game, MLX_KEY_UP))
{
manager->menu->selected_option--;
if (manager->menu->selected_option < 0)
manager->menu->selected_option = NUM_MENU_OPTIONS - 1;
}
}

68
src/menu/menu.c Normal file
View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* menu.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/27 14:31:53 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:09:11 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
# include "game_menu.h"
void menu_create(t_game_manager **manager)
{
t_menu *menu;
menu = malloc(sizeof(t_menu));
if (!menu)
return;
menu->background = NULL;
ft_memset(menu->options, 0, sizeof(menu->options));
menu->selector = 0;
menu->selected_option = 0;
(*manager)->menu = menu;
}
void menu_display(t_menu *menu, t_screen *screen)
{
const char *options[NUM_MENU_OPTIONS] = {
"Start Game",
"Exit"
};
int i;
if (menu->selector == 0)
{
menu->selector = mlx_put_string(screen->mlx, ">", screen->width / 2 - 100, 0);
}
i = 0;
while (i < NUM_MENU_OPTIONS)
{
if (i == menu->selected_option && menu->selector)
{
menu->selector->instances[0].y = (screen->height - 100) / 2 + i * 50;
}
if (menu->options[i] == 0)
{
menu->options[i] = mlx_put_string(screen->mlx, options[i],
(screen->width - ft_strlen(options[i]) * 10) / 2,
(screen->height - 100) / 2 + i * 50);
}
i++;
}
}
void menu_free(t_menu *menu, t_screen *screen)
{
if (!menu)
return;
if (menu->options[0])
mlx_delete_image(screen->mlx, menu->options[0]);
if (menu->options[1])
mlx_delete_image(screen->mlx, menu->options[1]);
if (menu->selector)
mlx_delete_image(screen->mlx, menu->selector);
free(menu);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* screen.c :+: :+: */
/* +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
/* Updated: 2025/05/23 15:17:59 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* screen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */
/* Updated: 2025/05/27 14:18:53 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,6 +36,9 @@ int screen_create(t_game **game)
screen->background = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!screen->background)
return (FAILURE);
screen->menu = mlx_new_image(screen->mlx, WIDTH, HEIGHT);
if (!screen->menu)
return (FAILURE);
(*game)->screen = screen;
return (SUCCESS);
}
@ -89,16 +92,16 @@ int screen_display(t_screen *screen)
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
if (mlx_image_to_window(screen->mlx, screen->minimap, 175, 575) < 0)
{
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
if (mlx_image_to_window(screen->mlx, screen->hud, 0, 0) < 0)
{
printf(RED"Failed to display buffer image\n"RESET);
return (FAILURE);
}
// if (mlx_image_to_window(screen->mlx, screen->minimap, 175, 575) < 0)
// {
// printf(RED"Failed to display buffer image\n"RESET);
// return (FAILURE);
// }
// if (mlx_image_to_window(screen->mlx, screen->hud, 0, 0) < 0)
// {
// printf(RED"Failed to display buffer image\n"RESET);
// return (FAILURE);
// }
if (!center_window(screen))
return (FAILURE);
return (SUCCESS);

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* shaders.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/05/08 18:27:59 by qmennen #+# #+# */
/* Updated: 2025/05/24 13:52:26 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* shaders.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/08 18:27:59 by qmennen #+# #+# */
/* Updated: 2025/05/27 13:39:41 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,6 @@
#include "glad.h"
#include "MLX42_Int.h"
// extern char *vert_shader;
// extern char *frag_shader;
void set_uniforms(t_game *game)
{
mlx_ctx_t *ctx;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* initialize.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
/* Updated: 2025/05/26 12:30:34 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* initialize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:12:33 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,8 +55,12 @@ static int init_temp(t_game **game)
// sprites[8] = make_sprite("./assets/broken_mirror.png", 42.5, 4.5, 0);
// sprites[9] = make_sprite("./assets/lamp.png", 9.5, 10.5, 0);
// (*game)->map->n_sprites = 10;
mlx_texture_t *hud_texture;
hud_texture = mlx_load_png("./assets/overlay2.png");
(*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx,
mlx_load_png("./assets/overlay2.png"));
hud_texture);
mlx_delete_texture(hud_texture);
return (SUCCESS);
}
@ -78,7 +82,5 @@ int initialize_cub3d(t_game **game, const char *mapfile)
return (FAILURE);
if (!screen_display((*game)->screen))
return (FAILURE);
mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
return (SUCCESS);
}

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* keyboard.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
/* Updated: 2025/05/14 20:01:04 by whaffman ######## odam.nl */
/* ::: :::::::: */
/* keyboard.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
/* Updated: 2025/05/27 15:19:38 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
@ -55,5 +55,6 @@ int get_key_down(t_game *game, int k)
int get_key_up(t_game *game, int k)
{
// printf("get_key_up: %d, last_keys %d\n", k, game->keyboard->last_keys[k]);
return (!get_key(game, k) && game->keyboard->last_keys[k]);
}