mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
144 lines
4.6 KiB
C
144 lines
4.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */
|
|
/* Updated: 2025/09/17 16:55:39 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 <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"
|
|
#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->last_since_shoot != 0 && data->player.weapon->is_auto)
|
|
{
|
|
if (get_milliseconds() - data->last_since_shoot < 50000)
|
|
return ;
|
|
}
|
|
if (data->player.weapon->clip <= 0 && data->player.weapon->remaining_ammos > 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.weapon->clip <= 0)
|
|
return ;
|
|
data->player.weapon->is_shooting = true;
|
|
data->last_since_shoot = get_milliseconds();
|
|
data->player.weapon->clip--;
|
|
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);
|
|
}
|