Last development push

This commit is contained in:
2025-12-09 19:57:51 +01:00
parent a7f7d77bf9
commit 5e08fc76ef
25 changed files with 764 additions and 195 deletions

116
src/execution/execution.c Normal file
View File

@@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execution.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 18:46:27 by aortigos #+# #+# */
/* Updated: 2025/12/09 19:56:57 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
void drow_map_pixel(void *mlxl)
{
t_mlx *mlx;
mlx = mlxl;
mlx_delete_image(mlx->mlx_ptr, mlx->img);
mlx->img = mlx_new_image(mlx->mlx_ptr, S_W, S_H);
cub_hook(mlx, 0, 0);
cast_rays(mlx);
mlx_image_to_window(mlx->mlx_ptr, mlx->img, 0, 0);
}
int check_load_ture(t_turelist *list)
{
t_turelist *tmp;
mlx_texture_t *texture;
tmp = list;
while (tmp)
{
if (tmp->name && (
!ft_strncmp(tmp->name, "NO", 2)
|| !ft_strncmp(tmp->name, "SO", 2)
|| !ft_strncmp(tmp->name, "WE", 2)
|| !ft_strncmp(tmp->name, "EA", 2)))
{
texture = mlx_load_png(tmp->value);
if (!texture)
return (0);
mlx_delete_texture(texture);
}
tmp = tmp->next;
}
return (1);
}
int load_texture(t_tex *tex, t_turelist *l_ture)
{
t_turelist *tmp;
if (!l_ture)
return (0);
tmp = l_ture;
if (!check_load_ture(l_ture))
return (0);
while (tmp)
{
if (!tmp->name || !tmp->value)
tmp = tmp->next;
if (!ft_strncmp(tmp->name, "NO", 2))
tex->no = mlx_load_png(tmp->value);
else if (!ft_strncmp(tmp->name, "SO", 2))
tex->so = mlx_load_png(tmp->value);
else if (!ft_strncmp(tmp->name, "WE", 2))
tex->we = mlx_load_png(tmp->value);
else if (!ft_strncmp(tmp->name, "EA", 2))
tex->ea = mlx_load_png(tmp->value);
tmp = tmp->next;
}
return (1);
}
void get_angle(t_mlx *mlx)
{
char c;
c = mlx->dt->sq_map[mlx->dt->p_y][mlx->dt->p_x];
if (c == 'N')
mlx->ply->angle = 3 * M_PI / 2;
if (c == 'S')
mlx->ply->angle = M_PI / 2;
if (c == 'E')
mlx->ply->angle = 0;
if (c == 'W')
mlx->ply->angle = M_PI;
mlx->ply->plyr_x = (mlx->dt->p_x * WALL_SIZE) + WALL_SIZE / 2;
mlx->ply->plyr_y = (mlx->dt->p_y * WALL_SIZE) + WALL_SIZE / 2;
mlx->ply->fov_rd = (FOV * M_PI / 180);
}
int execution(t_data *dt)
{
t_mlx mlx;
if (S_H > 1440 || S_W > 2560 || FOV >= 180 || FOV <= 0)
return (freelist(&dt->t_list), free_map(dt), 0);
mlx.ply = (t_player *)ft_calloc(sizeof(t_player), 1);
mlx.ray = (t_ray *)ft_calloc(sizeof(t_ray), 1);
mlx.tex = (t_tex *)ft_calloc(sizeof(t_tex), 1);
mlx.dt = dt;
mlx.mlx_ptr = mlx_init(S_W, S_H, "cub3D", false);
if (!mlx.mlx_ptr)
return (ft_exit(&mlx), 0);
if (!load_texture(mlx.tex, dt->t_list))
return (ft_exit(&mlx), 0);
get_angle(&mlx);
mlx_key_hook(mlx.mlx_ptr, &key_press, &mlx);
mlx_loop_hook(mlx.mlx_ptr, &drow_map_pixel, &mlx);
mlx_loop(mlx.mlx_ptr);
ft_exit(&mlx);
return (0);
}

View File

@@ -6,22 +6,22 @@
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/05 17:07:58 by aortigos #+# #+# */
/* Updated: 2025/12/05 17:10:05 by aortigos ### ########.fr */
/* Updated: 2025/12/09 19:55:15 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
void ft_delete_texture(t_texture *texture)
void ft_delete_texture(t_tex *tex)
{
if (texture->no)
mlx_delete_texture(texture->no);
if (texture->so)
mlx_delete_texture(texture->so);
if (texture->we)
mlx_delete_texture(texture->we);
if (texture->ea)
mlx_delete_texture(texture->ea);
if (tex->no)
mlx_delete_texture(tex->no);
if (tex->so)
mlx_delete_texture(tex->so);
if (tex->we)
mlx_delete_texture(tex->we);
if (tex->ea)
mlx_delete_texture(tex->ea);
}
void ft_exit(t_mlx *mlx)
@@ -37,4 +37,4 @@ void ft_exit(t_mlx *mlx)
mlx_terminate(mlx->mlx_ptr);
ft_putstr_fd("END GAME\n", 1);
exit(0);
}
}

133
src/execution/raycasting.c Normal file
View File

@@ -0,0 +1,133 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raycasting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 18:41:15 by aortigos #+# #+# */
/* Updated: 2025/12/09 18:41:30 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int inter_check(float angle, float *inter, float *step, int is_horizon)
{
if (is_horizon)
{
if (angle > 0 && angle < M_PI)
{
*inter += WALL_SIZE;
return (-1);
}
*step *= -1;
}
else
{
if (!(angle > M_PI / 2 && angle < 3 * M_PI / 2))
{
*inter += WALL_SIZE;
return (-1);
}
*step *= -1;
}
return (1);
}
int wall_hit(float x, float y, t_mlx *mlx)
{
int x_m;
int y_m;
if (x < 0 || y < 0)
return (0);
x_m = floor(x / WALL_SIZE);
y_m = floor(y / WALL_SIZE);
if ((y_m >= mlx->dt->h_map || x_m >= mlx->dt->w_map))
return (0);
if (mlx->dt->map2d[y_m] && x_m <= (int)ft_strlen(mlx->dt->map2d[y_m]))
if (mlx->dt->map2d[y_m][x_m] == '1')
return (0);
return (1);
}
float get_h_inter(t_mlx *mlx, float angl)
{
float h_x;
float h_y;
float x_step;
float y_step;
int pixel;
y_step = WALL_SIZE;
x_step = WALL_SIZE / tan(angl);
h_y = floor(mlx->ply->plyr_y / WALL_SIZE) * WALL_SIZE;
pixel = inter_check(angl, &h_y, &y_step, 1);
h_x = mlx->ply->plyr_x + (h_y - mlx->ply->plyr_y) / tan(angl);
if ((unit_circle(angl, 'y') && x_step > 0) || (!unit_circle(angl, 'y')
&& x_step < 0))
x_step *= -1;
while (wall_hit(h_x, h_y - pixel, mlx))
{
h_x += x_step;
h_y += y_step;
}
mlx->ray->horiz_x = h_x;
mlx->ray->horiz_y = h_y;
return (sqrt(pow(h_x - mlx->ply->plyr_x, 2) + pow(h_y - mlx->ply->plyr_y,
2)));
}
float get_v_inter(t_mlx *mlx, float angl)
{
float v_x;
float v_y;
float x_step;
float y_step;
int pixel;
x_step = WALL_SIZE;
y_step = WALL_SIZE * tan(angl);
v_x = floor(mlx->ply->plyr_x / WALL_SIZE) * WALL_SIZE;
pixel = inter_check(angl, &v_x, &x_step, 0);
v_y = mlx->ply->plyr_y + (v_x - mlx->ply->plyr_x) * tan(angl);
if ((unit_circle(angl, 'x') && y_step < 0) || (!unit_circle(angl, 'x')
&& y_step > 0))
y_step *= -1;
while (wall_hit(v_x - pixel, v_y, mlx))
{
v_x += x_step;
v_y += y_step;
}
mlx->ray->vert_x = v_x;
mlx->ray->vert_y = v_y;
return (sqrt(pow(v_x - mlx->ply->plyr_x, 2) + pow(v_y - mlx->ply->plyr_y,
2)));
}
void cast_rays(t_mlx *mlx)
{
double h_inter;
double v_inter;
int ray;
ray = 0;
mlx->ray->ray_ngl = mlx->ply->angle - (mlx->ply->fov_rd / 2);
while (ray < S_W)
{
mlx->ray->flag = 0;
h_inter = get_h_inter(mlx, nor_angle(mlx->ray->ray_ngl));
v_inter = get_v_inter(mlx, nor_angle(mlx->ray->ray_ngl));
if (v_inter <= h_inter)
mlx->ray->distance = v_inter;
else
{
mlx->ray->distance = h_inter;
mlx->ray->flag = 1;
}
render_wall(mlx, ray);
ray++;
mlx->ray->ray_ngl += (mlx->ply->fov_rd / S_W);
}
}

106
src/execution/render.c Normal file
View File

@@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 18:42:30 by aortigos #+# #+# */
/* Updated: 2025/12/09 19:55:21 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
void draw_floor_ceiling(t_mlx *mlx, int ray, int t_pix, int b_pix)
{
int i;
int c;
i = b_pix;
c = get_rgba(ft_atoi(mlx->dt->ff[0]), ft_atoi(mlx->dt->ff[1]),
ft_atoi(mlx->dt->ff[2]), 255);
while (i < S_H)
my_mlx_pixel_put(mlx, ray, i++, c);
c = get_rgba(ft_atoi(mlx->dt->cc[0]), ft_atoi(mlx->dt->cc[1]),
ft_atoi(mlx->dt->cc[2]), 255);
i = 0;
while (i < t_pix)
my_mlx_pixel_put(mlx, ray, i++, c);
}
mlx_texture_t *get_texture(t_mlx *mlx, int flag)
{
mlx->ray->ray_ngl = nor_angle(mlx->ray->ray_ngl);
if (flag == 0)
{
if (mlx->ray->ray_ngl > M_PI / 2 && mlx->ray->ray_ngl < 3 * (M_PI / 2))
return (mlx->tex->ea);
else
return (mlx->tex->we);
}
else
{
if (mlx->ray->ray_ngl > 0 && mlx->ray->ray_ngl < M_PI)
return (mlx->tex->so);
else
return (mlx->tex->no);
}
}
double get_x_o(mlx_texture_t *texture, t_mlx *mlx)
{
double x_o;
if (mlx->ray->flag == 1)
x_o = (int)fmodf((mlx->ray->horiz_x * (texture->width / WALL_SIZE)),
texture->width);
else
x_o = (int)fmodf((mlx->ray->vert_y * (texture->width / WALL_SIZE)),
texture->width);
return (x_o);
}
void draw_wall(t_mlx *mlx, int t_pix, int b_pix, double wall_h)
{
double x_o;
double y_o;
mlx_texture_t *texture;
uint32_t *arr;
double factor;
texture = get_texture(mlx, mlx->ray->flag);
arr = (uint32_t *)texture->pixels;
factor = (double)texture->height / wall_h;
x_o = get_x_o(texture, mlx);
y_o = (t_pix - (S_H / 2) + (wall_h / 2)) * factor;
if (y_o < 0)
y_o = 0;
while (t_pix < b_pix)
{
my_mlx_pixel_put(mlx, mlx->ray->index, t_pix, reverse_bytes(arr[(int)y_o
* texture->width + (int)x_o]));
y_o += factor;
t_pix++;
}
}
void render_wall(t_mlx *mlx, int ray)
{
double wall_h;
double b_pix;
double t_pix;
mlx->ray->distance *= cos(nor_angle(mlx->ray->ray_ngl - mlx->ply->angle));
wall_h = (WALL_SIZE / mlx->ray->distance) * ((S_W / 2)
/ tan(mlx->ply->fov_rd / 2));
b_pix = (S_H / 2) + (wall_h / 2);
t_pix = (S_H / 2) - (wall_h / 2);
if (b_pix > S_H)
b_pix = S_H;
if (t_pix < 0)
t_pix = 0;
mlx->ray->index = ray;
draw_wall(mlx, t_pix, b_pix, wall_h);
draw_floor_ceiling(mlx, ray, t_pix, b_pix);
}

67
src/execution/render2.c Normal file
View File

@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/09 18:44:12 by aortigos #+# #+# */
/* Updated: 2025/12/09 18:44:40 by aortigos ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/cub3d.h"
int get_rgba(int r, int g, int b, int a)
{
return (r << 24 | g << 16 | b << 8 | a << 0);
}
int reverse_bytes(int c)
{
unsigned int b;
b = 0;
b |= (c & 0xFF) << 24;
b |= (c & 0xFF00) << 8;
b |= (c & 0xFF0000) >> 8;
b |= (c & 0xFF000000) >> 24;
return (b);
}
void my_mlx_pixel_put(t_mlx *mlx, int x, int y, int color)
{
if (x < 0)
return ;
else if (x >= S_W)
return ;
if (y < 0)
return ;
else if (y >= S_H)
return ;
mlx_put_pixel(mlx->img, x, y, color);
}
float nor_angle(float angle)
{
if (angle < 0)
angle += (2 * M_PI);
if (angle > (2 * M_PI))
angle -= (2 * M_PI);
return (angle);
}
int unit_circle(float angle, char c)
{
if (c == 'x')
{
if (angle > 0 && angle < M_PI)
return (1);
}
else if (c == 'y')
{
if (angle > (M_PI / 2) && angle < (3 * M_PI) / 2)
return (1);
}
return (0);
}