cub3d/src/main.c

169 lines
4.9 KiB
C
Raw Normal View History

2025-05-05 17:09:11 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
2025-05-06 13:24:32 +02:00
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
2025-05-05 17:09:11 +02:00
/* +#+#+#+#+#+ +#+ */
2025-05-06 13:24:32 +02:00
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
2025-07-10 12:04:17 +02:00
/* Updated: 2025/07/10 12:01:15 by tchampio ### ########.fr */
2025-05-05 17:09:11 +02:00
/* */
/* ************************************************************************** */
2025-05-06 09:30:32 +02:00
#include "../includes/libft.h"
2025-06-24 00:17:33 +02:00
#include "../includes/structs.h"
2025-06-06 13:57:31 +02:00
#include "../mlx/mlx.h"
2025-06-04 19:16:59 +02:00
#include "../includes/maputils.h"
2025-07-09 17:08:01 +02:00
#include "../includes/cub3d_consts.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-05-06 09:30:32 +02:00
2025-05-06 12:25:17 +02:00
void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color)
{
char *dst;
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
*(unsigned int *)dst = color;
2025-05-06 12:25:17 +02:00
}
void draw_2d_wall(unsigned int color, t_mlx_data *data,
2025-07-07 14:40:37 +02:00
int x, int y)
2025-06-24 00:17:33 +02:00
{
2025-07-07 14:40:37 +02:00
int i;
int j;
2025-07-09 16:45:13 +02:00
static int size = 10;
2025-06-24 00:17:33 +02:00
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
my_mlx_pixel_put(data, x + i, y + j, color);
j++;
}
i++;
}
}
2025-06-25 18:08:19 +02:00
int destroy(t_cub3d_data *data)
2025-06-20 23:59:27 +02:00
{
2025-06-25 18:08:19 +02:00
free_map(data->map);
if (data->mlx_win)
mlx_destroy_window(data->mlx, data->mlx_win);
if (data->mlx_data)
mlx_destroy_image(data->mlx, data->mlx_data->img);
free(data->mlx_data);
if (data->mlx)
mlx_destroy_display(data->mlx);
free(data->mlx);
2025-06-06 13:57:31 +02:00
exit(0);
return 0;
}
2025-07-09 16:45:13 +02:00
int keypress_handler(int keycode, t_cub3d_data *data)
2025-06-06 13:57:31 +02:00
{
2025-07-09 16:45:13 +02:00
if (keycode == XK_Escape)
2025-06-25 18:08:19 +02:00
destroy(data);
2025-07-09 17:08:01 +02:00
else if (keycode == XK_a)
data->player.movement.x = -MOVEMENT_SPEED;
else if (keycode == XK_d)
data->player.movement.x = MOVEMENT_SPEED;
else if (keycode == XK_w)
data->player.movement.y = -MOVEMENT_SPEED;
else if (keycode == XK_s)
data->player.movement.y = MOVEMENT_SPEED;
2025-06-06 13:57:31 +02:00
return (0);
}
2025-07-09 17:12:10 +02:00
int keyrelease_handler(int keycode, t_cub3d_data *data)
{
if (keycode == XK_Escape)
destroy(data);
else if (keycode == XK_a)
data->player.movement.x = 0;
else if (keycode == XK_d)
data->player.movement.x = 0;
else if (keycode == XK_w)
data->player.movement.y = 0;
else if (keycode == XK_s)
data->player.movement.y = 0;
return (0);
}
2025-07-10 12:04:17 +02:00
void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data)
2025-06-24 00:17:33 +02:00
{
int i;
int j;
i = 0;
while (map->map[i])
2025-06-24 00:17:33 +02:00
{
j = 0;
while (map->map[i][j])
2025-06-24 00:17:33 +02:00
{
if (map->map[i][j] == '1')
2025-07-09 16:45:13 +02:00
draw_2d_wall(map->f_color, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z')
2025-07-09 16:45:13 +02:00
draw_2d_wall(0x0008D9D6, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'D' || map->map[i][j] == 'd')
2025-07-09 16:45:13 +02:00
draw_2d_wall(0x00FF2E63, data, 10 * j, 10 * i);
else if (map->map[i][j] == 's')
2025-07-09 16:45:13 +02:00
draw_2d_wall(0x00E84545, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'M')
2025-07-09 16:45:13 +02:00
draw_2d_wall(0x00F4CE14, data, 10 * j, 10 * i);
2025-07-10 12:04:17 +02:00
/* else if (ft_strchr("NSEW", map->map[i][j]))
2025-07-09 16:45:13 +02:00
draw_2d_wall(0x00FF0000, data, 10 * j, 10 * i);
2025-07-10 12:04:17 +02:00
*/
2025-06-24 00:17:33 +02:00
j++;
}
i++;
}
2025-07-10 12:04:17 +02:00
draw_2d_wall(0x00FF0000, data, 10 * player->x, 10 * player->y);
2025-06-24 00:17:33 +02:00
}
2025-07-07 14:40:37 +02:00
void init_player(t_mapdata *mapdata, t_player *player)
{
player->health = 100;
player->x = mapdata->startx;
player->y = mapdata->starty;
}
2025-07-09 17:30:16 +02:00
int game_loop(t_cub3d_data *data)
{
data->player.x += data->player.movement.x;
data->player.y += data->player.movement.y;
2025-07-10 12:04:17 +02:00
mlx_destroy_image(data->mlx, data->mlx_data->img);
data->mlx_data->img = mlx_new_image(data->mlx, 800, 600);
draw_map(data->map, &data->player, data->mlx_data);
2025-07-09 17:30:16 +02:00
mlx_put_image_to_window(data->mlx, data->mlx_win, data->mlx_data->img, 0, 0);
return (0);
}
2025-05-05 17:09:11 +02:00
int main(int argc, char **argv)
{
t_cub3d_data data;
2025-05-06 12:25:17 +02:00
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);
2025-06-30 16:16:45 +02:00
data.map = ft_calloc(sizeof(t_mapdata), 1);
if (!check_cubfile(argv[1], data.map))
return (ft_printf("Error: Wrong map file. Reason: %s\n", data.map->error), 1);
data.mlx = mlx_init();
data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
2025-06-25 18:08:19 +02:00
data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1);
data.mlx_data->img = mlx_new_image(data.mlx, 800, 600);
data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, &data.mlx_data->endian);
2025-07-07 14:40:37 +02:00
init_player(data.map, &(data.player));
2025-07-09 16:45:13 +02:00
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data);
2025-07-10 12:04:17 +02:00
mlx_hook(data.mlx_win, KeyRelease, KeyReleaseMask, keyrelease_handler, &data);
2025-06-30 16:16:45 +02:00
#ifdef BONUS
mlx_string_put(data.mlx, data.mlx_win, 10, 10, 0x00FFFFFF, "compiled with bonuses");
2025-06-30 16:16:45 +02:00
#endif
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
}