cub3d/src/main.c
2025-09-08 13:58:03 +02:00

157 lines
4.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
/* Updated: 2025/09/08 13:56:27 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft/includes/libft.h"
#include "../mlx/mlx.h"
#include "consts.h"
#include "cub3d_data.h"
#include "player/move.h"
#include "draw/draw_map.h"
#include "raycast/raycaster.h"
#include "renderer/render.h"
#include "raycast/ray.h"
#include "sprites/sprite_caster.h"
#include "utils/inits.h"
#include "utils/frees.h"
#include "utils/hooks.h"
#include "utils/inits.h"
#include "draw/drawutils.h"
#include <bits/types/struct_timeval.h>
#include <stdbool.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include <unistd.h>
#include <fcntl.h>
#include "utils/time.h"
#include "sprites/move_sprites.h"
void draw_points(t_cub3d_data *data)
{
char points_str[11];
int i;
int horizontalpos;
ft_itoa_static(data->player.points, points_str, 11);
i = 0;
horizontalpos = 170;
while (i < (int)ft_strlen(points_str))
{
matrix_image_put(data, data->point_figures[points_str[i] - '0'], WIDTH - horizontalpos, HEIGHT / 2);
horizontalpos -= 25;
i++;
}
}
void draw_perks(t_cub3d_data *data)
{
int i;
int perk_pos;
i = 0;
perk_pos = 50;
while (i < 3)
{
if (data->player.perk_order[i] != NONE)
{
if (data->player.perk_order[i] == REVIVE)
matrix_image_put(data, data->perk_logos[1], perk_pos, HEIGHT - 200);
else if (data->player.perk_order[i] == JUGGERNOG)
matrix_image_put(data, data->perk_logos[0], perk_pos, HEIGHT - 200);
else if (data->player.perk_order[i] == DOUBLETAP)
matrix_image_put(data, data->perk_logos[2], perk_pos, HEIGHT - 200);
perk_pos += 50;
}
i++;
}
}
void draw_round(t_cub3d_data *data)
{
int i;
int pos;
char round_str[5];
if (data->round <= 5)
return (matrix_image_put(data, data->tally_marks[data->round - 1], 20, HEIGHT - 85));
pos = 20;
i = 0;
ft_itoa_static(data->round, round_str, 5);
while (round_str[i])
{
matrix_image_put(data, data->round_figures[round_str[i] - '0'], pos, HEIGHT - 85);
pos += 50;
i++;
}
}
void create_hud(t_cub3d_data *data)
{
// draw points
draw_points(data);
// draw perks
draw_perks(data);
// draw round
draw_round(data);
// draw weapon
matrix_image_put(data, data->gun, WIDTH / 2, HEIGHT - 175);
// draw map
}
int game_loop(t_cub3d_data *data)
{
t_ray ray;
int fps;
char fps_string[4];
data->last_tick = get_milliseconds();
reset_matrix(data);
move_player(data);
move_sprites(data);
raycaster(data, &ray);
sprite_caster(data);
create_hud(data);
matrix_to_image(data);
mlx_put_image_to_window(data->mlx, data->mlx_win,
data->img_data->img, 0, 0);
draw_map(data->map, &data->player, data->img_data);
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
data->delta = (get_milliseconds() - data->last_tick);
fps = 1000000.0 / data->delta;
ft_itoa_static(fps, fps_string, 4);
mlx_string_put(data->mlx, data->mlx_win, WIDTH - 20, 15, 0xFF0000,
fps_string);
return (0);
}
int good_bye(void *data)
{
destroy(data, 0);
return (0);
}
int main(int argc, char **argv)
{
t_cub3d_data data;
ft_printf("Cub3d - Call of Duty Zombies shareware edition. Credits to "
"\e[0;32mkcolin\e[0m, \e[0;32mB.\e[0m (for some textures) and "
"\e[0;32mtchampio\e[0m\n");
if (argc < 2)
return (ft_printf("Error: Missing cub3d file\n"), 1);
init_cub3d_data(&data, argv);
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
keyrelease_handler, &data);
mlx_hook(data.mlx_win, DestroyNotify, NoEventMask, good_bye, &data);
mlx_loop_hook(data.mlx, game_loop, &data);
mlx_loop(data.mlx);
}