From 805ddbab0b7a2a3dd5698d8db5d8fe4c991e59de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 28 Jul 2025 12:16:26 +0200 Subject: [PATCH] feat: send single ray --- ressources/good_maps/testmap_2.cub | 22 ++++++++++++++++ src/consts.h | 4 ++- src/draw/draw_map.c | 18 +++++++------- src/main.c | 40 +++++++++++++++++++++++++++--- 4 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 ressources/good_maps/testmap_2.cub diff --git a/ressources/good_maps/testmap_2.cub b/ressources/good_maps/testmap_2.cub new file mode 100644 index 0000000..b7a64c1 --- /dev/null +++ b/ressources/good_maps/testmap_2.cub @@ -0,0 +1,22 @@ +NO ./path_to_the_north_texture +SO ./path_to_the_south_texture +WE ./path_to_the_west_texture +EA ./path_to_the_east_texture + +F 220,100,0 +C 225,30,0 + + 1111111111111111111111111 + 1000000000110000000000001 + 1011000001110000001000001 + 1001000000000000010000001 +111111111011000001110000100000001 +100000000011000001110111110111111 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000001110101011111011110N0111 +11110111 1110101 101111010001 +11111111 1111111 111111111111 diff --git a/src/consts.h b/src/consts.h index fab27a3..80e0af2 100644 --- a/src/consts.h +++ b/src/consts.h @@ -15,8 +15,10 @@ # define HEIGHT 800 # define WIDTH 800 +# define SIZE 64 +# define MAP_SIZE 10 # define RESSOURCE_DIR "ressources" -# define MOVEMENT_SPEED 0.1 +# define MOVEMENT_SPEED 6.4 # define ROTATION_SPEED 0.1 # ifdef BONUS # define COMPILED_TEXT "Compiled with bonuses" diff --git a/src/draw/draw_map.c b/src/draw/draw_map.c index 15e94f7..264eacc 100644 --- a/src/draw/draw_map.c +++ b/src/draw/draw_map.c @@ -13,19 +13,19 @@ #include "../map/mapdata.h" #include "../player/player.h" #include "drawutils.h" +#include "../consts.h" void draw_2d_wall(unsigned int color, t_img_data *data, int x, int y) { int i; int j; - static int size = 10; i = 0; - while (i < size) + while (i < MAP_SIZE) { j = 0; - while (j < size) + while (j < MAP_SIZE) { my_mlx_pixel_put(data, x + i, y + j, color); j++; @@ -46,18 +46,18 @@ void draw_map(t_mapdata *map, t_player *player, t_img_data *data) while (map->map[i][j]) { if (map->map[i][j] == '1') - draw_2d_wall(map->f_color, data, 10 * j, 10 * i); + draw_2d_wall(map->f_color, data, MAP_SIZE * j, MAP_SIZE * i); else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z') - draw_2d_wall(0x0008D9D6, data, 10 * j, 10 * i); + draw_2d_wall(0x0008D9D6, data, MAP_SIZE * j, MAP_SIZE * i); else if (map->map[i][j] == 'D' || map->map[i][j] == 'd') - draw_2d_wall(0x00FF2E63, data, 10 * j, 10 * i); + draw_2d_wall(0x00FF2E63, data, MAP_SIZE * j, MAP_SIZE * i); else if (map->map[i][j] == 's') - draw_2d_wall(0x00E84545, data, 10 * j, 10 * i); + draw_2d_wall(0x00E84545, data, MAP_SIZE * j, MAP_SIZE * i); else if (map->map[i][j] == 'M') - draw_2d_wall(0x00F4CE14, data, 10 * j, 10 * i); + draw_2d_wall(0x00F4CE14, data, MAP_SIZE * j, MAP_SIZE * i); j++; } i++; } - draw_2d_wall(0x00FF0000, data, 10 * player->x, 10 * player->y); + draw_2d_wall(0x00FF0000, data, MAP_SIZE * (player->x / SIZE), MAP_SIZE * (player->y / SIZE)); } diff --git a/src/main.c b/src/main.c index 422b8b1..f8bc0f7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,15 +6,17 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/24 14:31:07 by tchampio ### ########.fr */ +/* Updated: 2025/07/28 12:57:08 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../libft/includes/libft.h" #include "../mlx/mlx.h" #include "consts.h" +#include "draw/drawutils.h" #include "map/map_checker.h" #include "draw/draw_map.h" +#include "map/mapdata.h" #include "utils/hooks.h" #include "utils/frees.h" #include @@ -27,8 +29,8 @@ void init_player(t_mapdata *mapdata, t_player *player) { player->health = 100; - player->x = mapdata->startx; - player->y = mapdata->starty; + player->x = mapdata->startx * SIZE; + player->y = mapdata->starty * SIZE; if (mapdata->map[mapdata->starty][mapdata->startx] == 'N') player->yaw = M_PI; else if (mapdata->map[mapdata->starty][mapdata->startx] == 'S') @@ -39,10 +41,42 @@ void init_player(t_mapdata *mapdata, t_player *player) player->yaw = 3 * M_PI / 2; } +bool touch(float px, float py, t_mapdata *map) +{ + int x; + int y; + + x = px / SIZE; + y = py / SIZE; + if (map->map[y][x] == '1') + return (true); + return (false); +} + int game_loop(t_cub3d_data *data) { mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); + + int column = 0; + while (column < 1) + { + // calculate angle + double cos_angle = cos(data->player.yaw); + double sin_angle = sin(data->player.yaw); + double ray_x = data->player.x; + double ray_y = data->player.y; + while (!touch(ray_x, ray_y, data->map)) + { + my_mlx_pixel_put(data->img_data, (int)(MAP_SIZE * (ray_x / SIZE)), (int)(MAP_SIZE * (ray_y / SIZE)), 0xFF0000); + ray_x += sin_angle; + ray_y += cos_angle; + } + // calculate distance + // draw + column++; + } + draw_map(data->map, &data->player, data->img_data); mlx_put_image_to_window(data->mlx, data->mlx_win, data->img_data->img, 0, 0);