normed + finished movements

This commit is contained in:
Theo Champion 2025-07-15 10:33:25 +02:00
parent cc12b7554a
commit aa9f9bf6de
11 changed files with 200 additions and 121 deletions

View file

@ -7,7 +7,10 @@ SOURCEFILES=src/main.c \
src/map/checkers.c \ src/map/checkers.c \
src/map/setters.c \ src/map/setters.c \
src/utils/frees.c \ src/utils/frees.c \
src/map/forbidden_characters.c src/map/forbidden_characters.c \
src/utils/hooks.c \
src/draw/drawutils.c \
src/draw/map.c
OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES)) OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES))
OBJDIR=objects OBJDIR=objects
NAME=cub3d NAME=cub3d

27
includes/cub3d.h Normal file
View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/15 10:18:44 by tchampio #+# #+# */
/* Updated: 2025/07/15 10:32:42 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CUB3D_H
# define CUB3D_H
# include "structs.h"
# include "cub3d_consts.h"
# include "maputils.h"
int destroy(t_cub3d_data *data);
int keypress_handler(int keycode, t_cub3d_data *data);
int keyrelease_handler(int keycode, t_cub3d_data *data);
void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color);
void draw_2d_wall(unsigned int color, t_mlx_data *data, int x, int y);
void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data);
#endif

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:42:23 by tchampio #+# #+# */ /* Created: 2025/06/06 17:42:23 by tchampio #+# #+# */
/* Updated: 2025/07/10 12:02:18 by tchampio ### ########.fr */ /* Updated: 2025/07/15 10:32:13 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,5 +17,10 @@
# define WIDTH 800 # define WIDTH 800
# define RESSOURCE_DIR "ressources" # define RESSOURCE_DIR "ressources"
# define MOVEMENT_SPEED 0.01 # define MOVEMENT_SPEED 0.01
# ifdef BONUS
# define COMPILED_TEXT "Compiled with bonuses"
# else
# define COMPILED_TEXT " "
# endif
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 19:34:43 by tchampio #+# #+# */ /* Created: 2025/06/21 19:34:43 by tchampio #+# #+# */
/* Updated: 2025/06/25 17:57:24 by tchampio ### ########.fr */ /* Updated: 2025/07/15 10:32:24 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,7 +26,6 @@
# define CYAN "\033[36m" # define CYAN "\033[36m"
# define BOLD "\033[1m" # define BOLD "\033[1m"
bool check_cubfile(char *filename, t_mapdata *map); bool check_cubfile(char *filename, t_mapdata *map);
bool check_filename(t_mapdata *map, char *file); bool check_filename(t_mapdata *map, char *file);
void populate_maps(t_mapdata *map, int fd); void populate_maps(t_mapdata *map, int fd);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* structs.h :+: :+: :+: */ /* structs.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 19:46:20 by tchampio #+# #+# */ /* Created: 2025/07/15 10:32:57 by tchampio #+# #+# */
/* Updated: 2025/07/15 10:32:58 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

0
objects/draw/.gitkeep Normal file
View file

21
src/draw/drawutils.c Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* drawutils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/15 10:27:46 by tchampio #+# #+# */
/* Updated: 2025/07/15 10:28:10 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
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;
}

61
src/draw/map.c Normal file
View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/15 10:28:39 by tchampio #+# #+# */
/* Updated: 2025/07/15 10:29:06 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
void draw_2d_wall(unsigned int color, t_mlx_data *data,
int x, int y)
{
int i;
int j;
static int size = 10;
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
my_mlx_pixel_put(data, x + i, y + j, color);
j++;
}
i++;
}
}
void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data)
{
int i;
int j;
i = 0;
while (map->map[i])
{
j = 0;
while (map->map[i][j])
{
if (map->map[i][j] == '1')
draw_2d_wall(map->f_color, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z')
draw_2d_wall(0x0008D9D6, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'D' || map->map[i][j] == 'd')
draw_2d_wall(0x00FF2E63, data, 10 * j, 10 * i);
else if (map->map[i][j] == 's')
draw_2d_wall(0x00E84545, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'M')
draw_2d_wall(0x00F4CE14, data, 10 * j, 10 * i);
j++;
}
i++;
}
draw_2d_wall(0x00FF0000, data, 10 * player->x, 10 * player->y);
}

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */ /* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
/* Updated: 2025/07/10 12:01:15 by tchampio ### ########.fr */ /* Updated: 2025/07/15 10:29:56 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,116 +15,13 @@
#include "../mlx/mlx.h" #include "../mlx/mlx.h"
#include "../includes/maputils.h" #include "../includes/maputils.h"
#include "../includes/cub3d_consts.h" #include "../includes/cub3d_consts.h"
#include "../includes/cub3d.h"
#include <stdbool.h> #include <stdbool.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/X.h> #include <X11/X.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
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;
}
void draw_2d_wall(unsigned int color, t_mlx_data *data,
int x, int y)
{
int i;
int j;
static int size = 10;
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
my_mlx_pixel_put(data, x + i, y + j, color);
j++;
}
i++;
}
}
int destroy(t_cub3d_data *data)
{
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);
exit(0);
return 0;
}
int keypress_handler(int keycode, t_cub3d_data *data)
{
if (keycode == XK_Escape)
destroy(data);
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;
return (0);
}
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);
}
void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data)
{
int i;
int j;
i = 0;
while (map->map[i])
{
j = 0;
while (map->map[i][j])
{
if (map->map[i][j] == '1')
draw_2d_wall(map->f_color, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z')
draw_2d_wall(0x0008D9D6, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'D' || map->map[i][j] == 'd')
draw_2d_wall(0x00FF2E63, data, 10 * j, 10 * i);
else if (map->map[i][j] == 's')
draw_2d_wall(0x00E84545, data, 10 * j, 10 * i);
else if (map->map[i][j] == 'M')
draw_2d_wall(0x00F4CE14, data, 10 * j, 10 * i);
/* else if (ft_strchr("NSEW", map->map[i][j]))
draw_2d_wall(0x00FF0000, data, 10 * j, 10 * i);
*/
j++;
}
i++;
}
draw_2d_wall(0x00FF0000, data, 10 * player->x, 10 * player->y);
}
void init_player(t_mapdata *mapdata, t_player *player) void init_player(t_mapdata *mapdata, t_player *player)
{ {
player->health = 100; player->health = 100;
@ -139,30 +36,33 @@ int game_loop(t_cub3d_data *data)
mlx_destroy_image(data->mlx, data->mlx_data->img); mlx_destroy_image(data->mlx, data->mlx_data->img);
data->mlx_data->img = mlx_new_image(data->mlx, 800, 600); data->mlx_data->img = mlx_new_image(data->mlx, 800, 600);
draw_map(data->map, &data->player, data->mlx_data); draw_map(data->map, &data->player, data->mlx_data);
mlx_put_image_to_window(data->mlx, data->mlx_win, data->mlx_data->img, 0, 0); mlx_put_image_to_window(data->mlx, data->mlx_win,
data->mlx_data->img, 0, 0);
mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT);
return (0); return (0);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_cub3d_data data; t_cub3d_data data;
if (argc < 2) if (argc < 2)
return (ft_printf("Error: Missing cub3d file\n"), 1); return (ft_printf("Error: Missing cub3d file\n"), 1);
data.map = ft_calloc(sizeof(t_mapdata), 1); data.map = ft_calloc(sizeof(t_mapdata), 1);
if (!check_cubfile(argv[1], data.map)) if (!check_cubfile(argv[1], data.map))
return (ft_printf("Error: Wrong map file. Reason: %s\n", data.map->error), 1); return (ft_printf("Error: Wrong map file. Reason: %s\n",
data.map->error), 1);
data.mlx = mlx_init(); data.mlx = mlx_init();
data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d"); data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1); data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1);
data.mlx_data->img = mlx_new_image(data.mlx, 800, 600); 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); 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);
init_player(data.map, &(data.player)); init_player(data.map, &(data.player));
mlx_hook(data.mlx_win, KeyPress, KeyPressMask, keypress_handler, &data); 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, KeyRelease, KeyReleaseMask,
#ifdef BONUS keyrelease_handler, &data);
mlx_string_put(data.mlx, data.mlx_win, 10, 10, 0x00FFFFFF, "compiled with bonuses");
#endif
mlx_loop_hook(data.mlx, game_loop, &data); mlx_loop_hook(data.mlx, game_loop, &data);
mlx_loop(data.mlx); mlx_loop(data.mlx);
} }

View file

@ -6,12 +6,13 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/25 17:46:01 by tchampio #+# #+# */ /* Created: 2025/06/25 17:46:01 by tchampio #+# #+# */
/* Updated: 2025/06/25 18:01:29 by tchampio ### ########.fr */ /* Updated: 2025/07/15 10:26:48 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/cub3d_consts.h" #include "../../includes/cub3d_consts.h"
#include "../../includes/structs.h" #include "../../includes/structs.h"
#include "../../mlx/mlx.h"
#include <stdlib.h> #include <stdlib.h>
void free_tab(char **tab) void free_tab(char **tab)
@ -40,3 +41,18 @@ void free_map(t_mapdata *map)
free(map->filename); free(map->filename);
free(map); free(map);
} }
int destroy(t_cub3d_data *data)
{
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);
exit(0);
return (0);
}

47
src/utils/hooks.c Normal file
View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hooks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/15 10:13:47 by tchampio #+# #+# */
/* Updated: 2025/07/15 10:27:03 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d_consts.h"
#include "../../includes/cub3d.h"
#include "../../includes/structs.h"
#include <X11/keysym.h>
#include <X11/X.h>
int keypress_handler(int keycode, t_cub3d_data *data)
{
if (keycode == XK_Escape)
destroy(data);
if (keycode == XK_a)
data->player.movement.x = -MOVEMENT_SPEED;
else if (keycode == XK_d)
data->player.movement.x = MOVEMENT_SPEED;
if (keycode == XK_w)
data->player.movement.y = -MOVEMENT_SPEED;
else if (keycode == XK_s)
data->player.movement.y = MOVEMENT_SPEED;
return (0);
}
int keyrelease_handler(int keycode, t_cub3d_data *data)
{
if (keycode == XK_Escape)
destroy(data);
if (keycode == XK_a)
data->player.movement.x = 0;
if (keycode == XK_d)
data->player.movement.x = 0;
if (keycode == XK_w)
data->player.movement.y = 0;
if (keycode == XK_s)
data->player.movement.y = 0;
return (0);
}