I am programming a doomlike 3d engine using raycasting
. The linked list works. I tested it with an int field. The problem seems to come from the wall pointer generated by the calculate_wall_projection
function and stored in the chained list with append_to_list
function.
Here are the rendering function and the two others.
typedef struct s_list
{
t_object *object;
struct s_list *next;
} t_list;
int rendering_game(t_screen *screen)
{
t_list *head;
t_list *current;
t_object *wall;
t_ray ray;
ray.nbr = 0;
handle_movement(screen);
head = NULL;
while (ray.nbr < screen->scene.resolution.width)
{
init_ray(&ray, screen);
wall = calculate_wall_projection(screen, &ray);
append_to_list(wall, &head);
ray.nbr += RAY_INCREMENT;
}
current = head;
while (current != NULL)
{
object = current->object;
draw_texture_line(screen, object);
current = current->next;
}
check_time(screen);
swap_frame_screen(screen);
destroy_list(head);
return (0);
}
t_object *calculate_wall_projection(t_screen *screen, t_ray *ray)
{
t_object *wall;
wall = (t_object *) malloc(sizeof(t_object));
get_dist_to_vertical_wall(screen->scene.map, ray);
get_dist_to_horizontal_wall(screen->scene.map, ray);
wall = get_shorter_distance(&ray->vertical_check, &ray->horizontal_check);
correct_fishbowl_effect(screen, wall);
calculate_projection(screen, wall);
wall->ray_nbr = ray->nbr;
define_buffer_coordinate(screen, wall);
return (wall);
}
void append_to_list(t_object *obj, t_list **head)
{
t_list *new_node;
t_list *current;
new_node= (t_list*) malloc(sizeof(t_list));
new_node->object = obj;
new_node->next = NULL;
current = *head;
if (*head == NULL)
{
*head = new_node;
return ;
}
while (current->next != NULL)
current = current->next;
current->next = new_node;
}
When I print the current->object
content in the second loop, it displays the wall 960 for most of the entries and the row 999 (which is supposed to be the last one) for the last 39 entries.
When I print the content of head
and head->next
it displays the 960 rows before looping over the linked list.
I need to use a chained list because I'm gonna add some sprites. I need a dynamic data structure.