0

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.

wejoey
  • 216
  • 1
  • 3
  • 14
  • `wall = get_shorter_distance(...)` overwrites the `wall` pointer that you just allocated, which leaks the memory. There's a lot of unexplained stuff happening in `calculate_wall_projection` actually. You should dig into that function and those it calls, in order to isolate the issue further. – paddy May 16 '23 at 02:51
  • 1
    Please try to create a [mre] to show us. It will also be much easier for you to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude May 16 '23 at 02:52
  • I think the bug is coming from the get_shorter_distance(...). That function return another pointer that point to the ray structure... I'm gonna try change that. Thank you for the feedback. If it does not solved the problem, I will try to write a minimal reproducible example. It's my first post here. – Emmanuel Guefif May 16 '23 at 03:38
  • Suggested reading https://stackoverflow.com/questions/72330113/how-to-copy-data-to-a-pointer-returned-by-malloc – n. m. could be an AI May 16 '23 at 04:46

1 Answers1

1

Thanks to paddy, it was the get_shorter_distance that was overwriting the initial wall pointer with a new pointer. I'm answering just to say that I've learned something. I should always check if a pointer has been overwritten. Next time, I'll build a more reproducible example of the problem. Thank you so much.