cub3d/src/main.c

145 lines
4.5 KiB
C
Raw Normal View History

2025-05-05 17:09:11 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
2025-05-05 17:09:11 +02:00
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
2025-09-17 16:05:41 +02:00
/* Updated: 2025/09/17 13:36:22 by tchampio ### ########.fr */
2025-05-05 17:09:11 +02:00
/* */
/* ************************************************************************** */
#include "../libft/includes/libft.h"
2025-06-06 13:57:31 +02:00
#include "../mlx/mlx.h"
#include "consts.h"
2025-07-28 13:04:32 +02:00
#include "cub3d_data.h"
#include "player/move.h"
#include "draw/draw_map.h"
2025-07-30 16:25:12 +02:00
#include "raycast/raycaster.h"
#include "renderer/render.h"
#include "raycast/ray.h"
2025-08-06 14:06:15 +02:00
#include "sprites/sprite_caster.h"
#include "utils/inits.h"
#include "utils/frees.h"
#include "utils/hooks.h"
#include "utils/inits.h"
2025-07-31 14:07:56 +02:00
#include <bits/types/struct_timeval.h>
2025-05-06 13:24:32 +02:00
#include <stdbool.h>
2025-07-09 16:45:13 +02:00
#include <X11/keysym.h>
#include <X11/X.h>
2025-05-06 13:24:32 +02:00
#include <unistd.h>
#include <fcntl.h>
2025-07-31 14:07:56 +02:00
#include "utils/time.h"
2025-08-18 13:21:52 +02:00
#include "sprites/move_sprites.h"
2025-09-08 17:33:05 +02:00
#include "hud/hud.h"
void kill_zombie(t_cub3d_data *data, t_sprite *zombie_ptr)
{
mlx_destroy_image(data->mlx, zombie_ptr->image->img);
free(zombie_ptr->image);
zombie_ptr->sprite_type = DEAD_ZOMBIE;
data->player.points += 60;
}
2025-09-11 23:41:00 +02:00
void handle_shooting(t_cub3d_data *data)
{
2025-09-14 17:42:48 +02:00
if (data->keypresses.is_space_pressed)
2025-09-11 23:41:00 +02:00
{
2025-09-14 17:42:48 +02:00
if (!data->player.weapon.is_auto)
data->keypresses.is_space_pressed = false;
2025-09-16 16:07:41 +02:00
if (data->player.weapon.clip <= 0)
return ;
2025-09-17 16:05:41 +02:00
if (data->last_since_shoot != 0 && data->player.weapon.is_auto)
{
if (get_milliseconds() - data->last_since_shoot < 50000)
return ;
}
data->player.weapon.is_shooting = true;
data->last_since_shoot = get_milliseconds();
2025-09-16 16:07:41 +02:00
data->player.weapon.clip--;
if (data->player.weapon.clip == 0)
{
ft_printf("reloading\n");
if (data->player.weapon.remaining_ammos < 8)
{
data->player.weapon.clip = data->player.weapon.remaining_ammos;
data->player.weapon.remaining_ammos = 0;
}
else
{
data->player.weapon.clip = 8;
data->player.weapon.remaining_ammos -= 8;
}
}
if (data->player.aimed_zombie)
{
data->player.aimed_zombie->health -= 32;
data->player.points += 10;
2025-09-16 16:07:41 +02:00
ft_printf("Shooting %p, now at %d HP %d/%d\n", data->player.aimed_zombie,
data->player.aimed_zombie->health, data->player.weapon.clip, data->player.weapon.remaining_ammos);
if (data->player.aimed_zombie->health <= 0)
kill_zombie(data, data->player.aimed_zombie);
}
else
2025-09-16 16:07:41 +02:00
ft_printf("Shoot! %d/%d\n", data->player.weapon.clip, data->player.weapon.remaining_ammos);
2025-09-11 23:41:00 +02:00
}
}
2025-07-09 17:30:16 +02:00
int game_loop(t_cub3d_data *data)
{
2025-07-30 16:25:12 +02:00
t_ray ray;
2025-08-11 10:50:59 +02:00
int fps;
char fps_string[4];
2025-07-30 16:25:12 +02:00
data->last_tick = get_milliseconds();
2025-07-30 16:25:12 +02:00
reset_matrix(data);
move_player(data);
2025-09-14 17:42:48 +02:00
handle_shooting(data);
2025-08-18 13:21:52 +02:00
move_sprites(data);
data->player.aimed_zombie = NULL;
raycaster(data, &ray);
2025-08-06 14:06:15 +02:00
sprite_caster(data);
create_hud(data);
if (data->player.weapon.is_shooting)
{
if (get_milliseconds() - data->last_since_shoot > 7000)
data->player.weapon.is_shooting = false;
}
2025-07-30 16:25:12 +02:00
matrix_to_image(data);
2025-07-15 10:33:25 +02:00
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);
2025-08-11 10:50:59 +02:00
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);
2025-07-09 17:30:16 +02:00
return (0);
}
int good_bye(void *data)
{
destroy(data, 0);
return (0);
}
2025-05-05 17:09:11 +02:00
int main(int argc, char **argv)
{
2025-07-15 10:33:25 +02:00
t_cub3d_data data;
2025-05-06 12:25:17 +02:00
2025-08-18 20:05:50 +02:00
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");
2025-05-05 17:09:11 +02:00
if (argc < 2)
2025-06-06 18:38:21 +02:00
return (ft_printf("Error: Missing cub3d file\n"), 1);
init_cub3d_data(&data, argv);
2025-07-09 16:45:13 +02:00
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
2025-07-15 10:33:25 +02:00
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask,
keyrelease_handler, &data);
mlx_hook(data.mlx_win, DestroyNotify, NoEventMask, good_bye, &data);
2025-07-09 17:30:16 +02:00
mlx_loop_hook(data.mlx, game_loop, &data);
mlx_loop(data.mlx);
2025-05-05 17:09:11 +02:00
}