/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ /* Updated: 2025/09/17 12:51:34 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 #include #include #include #include #include #include "utils/time.h" #include "sprites/move_sprites.h" #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; } void handle_shooting(t_cub3d_data *data) { if (data->keypresses.is_space_pressed) { if (!data->player.weapon.is_auto) data->keypresses.is_space_pressed = false; if (data->player.weapon.clip <= 0) return ; if (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(); 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; 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 ft_printf("Shoot! %d/%d\n", data->player.weapon.clip, data->player.weapon.remaining_ammos); } } 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); handle_shooting(data); move_sprites(data); data->player.aimed_zombie = NULL; raycaster(data, &ray); 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; } 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); }