v1.0 - normed texture system, multiple keypress, fixes on parsing, removed trailing maps

This commit is contained in:
B.Goulard 2024-12-16 14:56:22 +01:00
parent e581c72b02
commit 3f43074d05
35 changed files with 474 additions and 438 deletions

70
raycast/draw_call.c Normal file
View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_call.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 14:43:09 by bgoulard #+# #+# */
/* Updated: 2024/12/16 14:47:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d_struct.h"
#include "cub3d.h"
#include "ft_math.h"
#include <math.h>
#define PT_LINEH 0
#define PT_D_START 1
#define PT_D_END 2
#define PT_X 3
#define TI_SIDE 0
#define TI_X 1
#define TD_PERP_WALL_DIST 0
#define TD_WALL_X 1
static void draw__(t_texture *texture, int *passthroug, t_info *data, \
double wallX)
{
t_ipoint text_off;
const double _step = (double)texture->height / passthroug[PT_LINEH];
double text_offset;
char *ptr_pix;
text_offset = (passthroug[PT_D_START] - (double)data->screen_size.y / 2 + \
(double)passthroug[PT_LINEH] / 2) * _step;
text_off.x = (wallX * texture->width);
while (passthroug[PT_D_START] < passthroug[PT_D_END])
{
text_off.y = (int)text_offset & (texture->height - 1);
text_offset += _step;
ptr_pix = texture->img->data + (text_off.y * \
texture->img->image->bytes_per_line + text_off.x * \
(texture->img->image->bits_per_pixel / 8));
my_mlx_pixel_put(data, passthroug[PT_X], passthroug[PT_D_START], \
*(unsigned int *)ptr_pix);
passthroug[PT_D_START]++;
}
}
// int texX = (int)(wallX * (double)texture->width);
void draw_(int *ti, double *td, t_ipoint step, t_info *data)
{
const int line_height = (int)(data->screen_size.y / \
td[TD_PERP_WALL_DIST]) * cos(deg2rad(FOV / 2));
int draw_start;
int draw_end;
t_texture *texture;
draw_start = ft_clamp(-line_height / 2 + data->screen_size.y / 2, 0, \
data->screen_size.y - 1);
draw_end = ft_clamp(line_height / 2 + data->screen_size.y / 2, 0, \
data->screen_size.y - 1);
texture = get_texture(ti[TI_SIDE], step, data);
draw__(texture, (int []){line_height, draw_start, draw_end, ti[TI_X]}, \
data, td[TD_WALL_X]);
}

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */
/* Updated: 2024/12/05 16:44:04 by rparodi ### ########.fr */
/* Updated: 2024/12/16 14:51:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,51 +15,31 @@
#include "mlx_functions.h"
#include "ft_string.h"
#include "ft_math.h"
#include <math.h>
#include <unistd.h>
void set_step(t_ipoint *step, t_dpoint raydir);
void set_side_dist(t_dpoint *side_dist, t_dpoint *tb, t_ipoint pos_i);
/// line 33: normalize draw_start and draw_end
void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
void search_hit(t_dpoint delta_dist, t_ipoint *pos_i, t_ipoint step, \
void *_data[3])
{
const int line_height = (int)(data->screen_size.y / perpWallDist) \
* cos(deg2rad(FOV / 2));
int draw_start;
int draw_end;
int color;
draw_start = -line_height / 2 + data->screen_size.y / 2;
draw_end = line_height / 2 + data->screen_size.y / 2;
draw_start = ft_clamp(draw_start, 0, data->screen_size.y - 1);
draw_end = ft_clamp(draw_end, 0, data->screen_size.y - 1);
color = get_cl(side, step);
while (draw_start < draw_end)
my_mlx_pixel_put(data, x, draw_start++, color);
}
void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, \
t_ipoint step, void *_data[2])
{
int *side;
t_info *data;
int *side;
t_info *data;
t_dpoint *side_dist;
side = (int *)_data[1];
data = (t_info *)_data[0];
side_dist = (t_dpoint *)_data[2];
while (true)
{
if (sideDist->x < sideDist->y)
if (side_dist->x < side_dist->y)
{
sideDist->x += deltaDist.x;
side_dist->x += delta_dist.x;
pos_i->x += step.x;
*side = 0;
}
else
{
sideDist->y += deltaDist.y;
side_dist->y += delta_dist.y;
pos_i->y += step.y;
*side = 1;
}
@ -69,11 +49,14 @@ void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, \
}
}
// t_dpoint prep_distoff ->
// x - perpwall_dist
// y - wall_off
void column_handler(t_ipoint pos_i, t_dpoint ray_dir, t_info *data, int x)
{
t_dpoint side_dist;
t_dpoint delta_dist;
double perp_wall_dist;
t_dpoint perp_distoff;
t_ipoint step;
int side;
@ -81,17 +64,23 @@ void column_handler(t_ipoint pos_i, t_dpoint ray_dir, t_info *data, int x)
set_step(&step, ray_dir);
set_side_dist(&side_dist, (t_dpoint[]){ray_dir, data->player.pos, \
delta_dist}, pos_i);
search_hit(&side_dist, delta_dist, &pos_i, step, (void *[]){data, &side});
search_hit(delta_dist, &pos_i, step, (void *[]){data, &side, &side_dist});
if (side == 0)
perp_wall_dist = (pos_i.x - data->player.pos.x + \
perp_distoff.x = (pos_i.x - data->player.pos.x + \
(double)(1 - step.x) / 2) / ray_dir.x;
else
perp_wall_dist = (pos_i.y - data->player.pos.y + \
perp_distoff.x = (pos_i.y - data->player.pos.y + \
(double)(1 - step.y) / 2) / ray_dir.y;
draw_(side, perp_wall_dist, step, x, data);
perp_distoff.y = data->player.pos.x + perp_distoff.x * ray_dir.x;
if (side == 0)
perp_distoff.y = data->player.pos.y + perp_distoff.x * ray_dir.y;
perp_distoff.y -= floor(perp_distoff.y);
draw_((int []){side, x}, (double []){perp_distoff.x, perp_distoff.y}, \
step, data);
}
int render_frame(t_info *data)
{
double camera_x;
double coef;
@ -101,7 +90,7 @@ int render_frame(t_info *data)
coef = 2 * tan(deg2rad(FOV) / 2) / (double)data->screen_size.x;
ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y \
* (data->camera.bpp / 8));
draw_floor(data);
draw_bg(data);
while (x < data->screen_size.x)
{
camera_x = x * coef - 1;
@ -112,28 +101,6 @@ int render_frame(t_info *data)
x++;
}
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, \
data->camera.screen_buff, 0, 0);
data->camera.screen_buff, 0, 0);
return (0);
}
// Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the
// return of the function...
// (Why 'int (*)(void*)' when you dont use the int)
//
// This function is called each time the mlx loops over the mlx pointer in
// mlx_loop. here we do calc on time since last frame and player position
// to know if we need to re-draw the screen. if yes call raph function for
// calc.
// The need to redraw can also be expressed in the diferent key_pressed
// functions, I would recomend to make a bool field for that in the info
// struct.
// As a pure artefact of using mlx this function will likely be mooved to
// mlx_layer in the final repo.
int c3_frame_update(void *inf_ptr)
{
t_info *info;
info = inf_ptr;
mlx_clear_window(info->mlx_ptr, info->win_ptr);
return (EXIT_SUCCESS);
}

View file

@ -6,34 +6,36 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 18:55:11 by rparodi #+# #+# */
/* Updated: 2024/12/05 16:43:53 by rparodi ### ########.fr */
/* Updated: 2024/12/16 09:43:18 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include "cub3d_struct.h"
#include "mlx_functions.h"
#include "ft_string.h"
#include "ft_math.h"
#include <math.h>
#include <unistd.h>
void draw_floor(t_info *data)
void draw_bg(t_info *data)
{
t_ipoint temp;
int switch_point;
int color;
temp.y = data->screen_size.y / 2;
ft_bzero(&temp, sizeof(t_ipoint));
switch_point = data->screen_size.y / 2;
color = data->map.bg_colors[BG_SKY].color;
while (temp.y < data->screen_size.y)
{
temp.x = 0;
while (temp.x < data->screen_size.x)
{
my_mlx_pixel_put(data, temp.x, temp.y, 0xFFFFFF);
my_mlx_pixel_put(data, temp.x, temp.y, color);
temp.x++;
}
temp.y++;
if (temp.y >= switch_point)
color = data->map.bg_colors[BG_FLR].color;
}
}

25
raycast/get_texture.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_texture.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 14:46:04 by bgoulard #+# #+# */
/* Updated: 2024/12/16 14:46:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d_struct.h"
t_texture *get_texture(int side, t_ipoint step, t_info *data)
{
if (side == 0 && step.x > 0)
return (&data->map.texture_[0]);
else if (side == 0 && step.x < 0)
return (&data->map.texture_[1]);
else if (side == 1 && step.y > 0)
return (&data->map.texture_[2]);
else
return (&data->map.texture_[3]);
}

View file

@ -6,13 +6,13 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 12:24:35 by rparodi #+# #+# */
/* Updated: 2024/12/01 18:39:11 by rparodi ### ########.fr */
/* Updated: 2024/12/16 14:08:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include "cub3d_struct.h"
#include "mlx_functions.h"
#include "raycast.h"
#include <stdlib.h>
@ -55,6 +55,8 @@ double rc_launch(t_info *info, double angle)
int shelves_launch(t_info *info)
{
render_frame(info);
displacement_hook(info);
if (info->redraw)
(render_frame(info), info->redraw = false);
return (EXIT_SUCCESS);
}