cub3d/src/game.c
2025-05-22 14:49:11 +02:00

89 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* game.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
/* Updated: 2025/05/22 14:03:26 by qmennen ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int game_create(t_game **game)
{
*game = malloc(sizeof(t_game));
if (!game)
return (FAILURE);
memset(*game, 0, sizeof(t_game));
(*game)->fps = 20;
return (SUCCESS);
}
void free_game(t_game **game)
{
if (game && *game)
{
if ((*game)->screen)
free((*game)->screen);
if ((*game)->player)
free((*game)->player);
if ((*game)->map)
free((*game)->map);
free(*game);
}
}
void game_loop(void *param)
{
t_game *game;
double delta_time;
static int fps = 0;
game = (t_game *)param;
game->framecount++;
delta_time = game->screen->mlx->delta_time;
fps += (int)(1.f / delta_time);
if (game->framecount % 20 == 0)
{
game->fps = (int)(fps / 20);
fprintf(stderr, "FPS: %d\n", fps / 20);
fps = 0;
}
player_update(game, 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;
game->screen->img->instances[0].y = cos(game->framecount / 3.0) * 10;
}
}
void game_free(t_game *game)
{
if (game->screen)
{
mlx_delete_image(game->screen->mlx, game->screen->img);
mlx_close_window(game->screen->mlx);
mlx_terminate(game->screen->mlx);
free(game->screen);
}
if (game->player)
free(game->player);
if (game->map)
map_free(game->map);
if (game->keyboard)
free(game->keyboard);
free(game);
}
void game_terminate(t_game *game)
{
game_free(game);
exit(EXIT_SUCCESS);
}