我正在使用光线投射编写一个类似厄运的 3d 引擎。链接列表有效。我用一个 int 字段测试了它。问题似乎来自 calculate_wall_projection 函数生成的墙指针,并使用 append_to_list 函数存储在链表中。
这里是渲染函数和另外两个。
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;
}
当我在第二个循环中打印 current->object 内容时,它显示大部分条目的墙 960 和最后 39 个条目的行 999(应该是最后一个)。 当我打印 head 和 head->next 的内容时,它会在遍历链表之前显示 960 行。
我需要使用链表,因为我要添加一些精灵。我需要一个动态数据结构。
感谢 paddy,是 get_shorter_distance 用新指针覆盖了初始墙指针。 我回答只是说我学到了一些东西。我应该始终检查指针是否已被覆盖。 下一次,我将构建一个更可重现的问题示例。 非常感谢。