1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
/*
* item_list.c -- Implementa las listas de objetos.
* Copyright (C) 2018 Juan Marín Noguera
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "collision_type.h"
#include "error.h"
#include "item.h"
#include "item_list.h"
#include "screen.h"
struct ItemNode {
ItemList l;
int n;
};
struct ItemList {
int len;
int capacity;
Item *elems;
};
ItemList item_list_create()
{
ItemList il;
il = malloc(sizeof(struct ItemList));
if (il == NULL)
error_libc_exit();
il->len = 0;
il->capacity = 1;
il->elems = malloc(sizeof(Item));
if (il->elems == NULL)
error_libc_exit();
return il;
}
void item_list_free(ItemList il)
{
int i;
for (i = il->len - 1; i >= 0; i--)
item_free(il->elems[i]);
free(il->elems);
free(il);
}
ItemNode item_list_begin(ItemList il)
{
ItemNode in = malloc(sizeof(struct ItemNode));
if (in == NULL)
error_libc_exit();
in->l = il;
in->n = 0;
return in;
}
Item item_list_current(ItemNode in)
{
return in->l->elems[in->n];
}
void item_list_advance(ItemNode in)
{
in->n++;
}
void item_list_delete(ItemNode in)
{
item_free(in->l->elems[in->n]);
in->l->elems[in->n] = in->l->elems[--in->l->len];
if (in->l->len > 0 && in->l->len * 2 <= in->l->capacity) {
in->l->capacity /= 2;
in->l->elems = realloc (in->l->elems,
in->l->capacity * sizeof(Item));
if (in->l->elems == NULL)
error_libc_exit();
}
}
int item_list_isend(ItemNode in)
{
return in->n >= in->l->len;
}
void item_list_free_node(ItemNode in)
{
free(in);
}
void item_list_append(ItemList il, Item e)
{
if (il->len == il->capacity) {
il->capacity *= 2;
il->elems = realloc(il->elems, il->capacity * sizeof(Item));
if (il->elems == NULL)
error_libc_exit();
}
il->elems[il->len++] = e;
}
void item_list_update(ItemList il, int scroll, int w)
{
ItemNode in = item_list_begin(il);
Item i;
while (!item_list_isend(in)) {
if (item_x(i = item_list_current(in)) < scroll + w &&
(item_update(i) ||
item_y(i) + item_height(i) <= 0 ||
item_x(i) + item_width(i) <= scroll))
item_list_delete(in);
else
item_list_advance(in);
}
item_list_free_node(in);
}
void item_list_render(Screen scr, ItemList il, int scroll)
{
ItemNode in;
for (in = item_list_begin(il); !item_list_isend(in);
item_list_advance(in))
item_render(scr, item_list_current(in), scroll);
item_list_free_node(in);
}
|