MLX42 1.0
MLX42
Loading...
Searching...
No Matches
mlx_list.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* :::::::: */
4/* mlx_list.c :+: :+: */
5/* +:+ */
6/* By: W2Wizard <main@w2wizard.dev> +#+ */
7/* +#+ */
8/* Created: 2021/12/28 01:53:51 by W2Wizard #+# #+# */
9/* Updated: 2023/02/27 11:31:01 by W2Wizard ######## odam.nl */
10/* */
11/* ************************************************************************** */
12
13#include "MLX42/MLX42_Int.h"
14
15//= Private =//
16
18{
19 int32_t i;
20
21 for (i = 0; lst != NULL; i++)
22 lst = lst->next;
23 return (i);
24}
25
26static void mlx_lstdelone(mlx_list_t* lst, void (*del)(void *))
27{
28 if (del != NULL)
29 del(lst->content);
30 free(lst);
31}
32
33void mlx_lstclear(mlx_list_t** lst, void (*del)(void*))
34{
36
37 while (*lst != NULL)
38 {
39 next_lst = (*lst)->next;
41 *lst = next_lst;
42 }
43}
44
45mlx_list_t* mlx_lstnew(void* content)
46{
48
49 if ((out = malloc(sizeof(mlx_list_t))))
50 {
51 out->content = content;
52 out->next = NULL;
53 out->prev = NULL;
54 }
55 return (out);
56}
57
59{
60 if (!lst)
61 return (NULL);
62 while (lst->next)
63 lst = lst->next;
64 return (lst);
65}
66
68{
69 if (!lst || !new)
70 return;
71 if (!*lst)
72 *lst = new;
73 else
74 {
76 new->prev = temp;
77 temp->next = new;
78 }
79}
80
82{
83 if (!lst || !new)
84 return;
85 if ((*lst) != NULL)
86 (*lst)->prev = new;
87 new->next = *lst;
88 new->prev = NULL;
89 *lst = new;
90}
91
92/**
93 * Removes the specified content from the list, if found.
94 * Also fixes any relinking that might be needed.
95 *
96 * @param[in] lst The list
97 * @param[in] comp Function to check if the content and value are the same.
98 * @returns The removed element, clean up as you wish.
99 */
100mlx_list_t* mlx_lstremove(mlx_list_t** lst, void* value, bool (*comp)(void*, void*))
101{
103
104 while (lstcpy && !comp(lstcpy->content, value))
105 lstcpy = lstcpy->next;
106 if (lstcpy == NULL)
107 return (NULL);
108 if (lstcpy == *lst)
109 *lst = lstcpy->next;
110 if (lstcpy->next != NULL)
112 if (lstcpy->prev != NULL)
114 return (lstcpy);
115}
116
117// Retrieve Z value from queue.
119{
120 const draw_queue_t* queue = entry->content;
121
122 return (queue->image->instances[queue->instanceid].z);
123}
124
125// Insert the entry back into head sorted.
127{
129
130 if (*head == NULL)
131 *head = new;
132 else if (mlx_getzdata(*head) >= mlx_getzdata(new))
133 {
134 new->next = *head;
135 new->next->prev = new;
136 *head = new;
137 }
138 else
139 {
140 current = *head;
141
142 // Find insertion location.
143 while (current->next != NULL && mlx_getzdata(current->next) < mlx_getzdata(new))
145 new->next = current->next;
146
147 // Insert at the end
148 if (current->next != NULL)
149 new->next->prev = new;
150 current->next = new;
151 new->prev = current;
152 }
153}
154
155/**
156 * Okay-ish sorting algorithm to sort the render queue / doubly linked list.
157 * We need to do this to fix transparency.
158 *
159 * @param lst The render queue.
160 */
162{
165
166 while (lstcpy != NULL)
167 {
168 mlx_list_t* next = lstcpy->next;
169
170 // Separate entry out of list and insert it back but sorted.
171 lstcpy->prev = lstcpy->next = NULL;
173 lstcpy = next;
174 }
175 *lst = sorted;
176}
GLfloat value
Definition glad.h:2667
GLuint GLsizei GLsizei * length
Definition glad.h:3372
mlx_list_t * mlx_lstlast(mlx_list_t *lst)
Definition mlx_list.c:58
static int32_t mlx_getzdata(mlx_list_t *entry)
Definition mlx_list.c:118
void mlx_lstadd_front(mlx_list_t **lst, mlx_list_t *new)
Definition mlx_list.c:81
void mlx_sort_renderqueue(mlx_list_t **lst)
Definition mlx_list.c:161
mlx_list_t * mlx_lstremove(mlx_list_t **lst, void *value, bool(*comp)(void *, void *))
Definition mlx_list.c:100
static void mlx_insertsort(mlx_list_t **head, mlx_list_t *new)
Definition mlx_list.c:126
void mlx_lstclear(mlx_list_t **lst, void(*del)(void *))
Definition mlx_list.c:33
int32_t mlx_lstsize(mlx_list_t *lst)
Definition mlx_list.c:17
static void mlx_lstdelone(mlx_list_t *lst, void(*del)(void *))
Definition mlx_list.c:26
mlx_list_t * mlx_lstnew(void *content)
Definition mlx_list.c:45
void mlx_lstadd_back(mlx_list_t **lst, mlx_list_t *new)
Definition mlx_list.c:67
struct mlx_list * next
Definition MLX42_Int.h:88
void * content
Definition MLX42_Int.h:87
struct mlx_list * prev
Definition MLX42_Int.h:89