-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentList.cpp
More file actions
134 lines (116 loc) · 2.79 KB
/
ComponentList.cpp
File metadata and controls
134 lines (116 loc) · 2.79 KB
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
#include "ComponentList.h"
ComponentList::ComponentList()
{
sizeLimit = DEFAULT;
probeIndex = 0;
size = 0;
components.resize(DEFAULT);
// Not sure if this is necessary
for (int i = 0; i < DEFAULT; i++)
{
components[i] = NULL;
}
}
// It doesn't matter where the components were
// constructed (mostly within factory methods). They
// reside here now and are updated from within ComponentList
// so free them from here
ComponentList::~ComponentList()
{
// Untested code, please test it.
for (int i = 0; i < sizeLimit; i++)
{
Component* targetForDeletion = components[i];
if (targetForDeletion != NULL)
delete targetForDeletion;
}
}
// Needs CLOSER INSPECTIONS to check for off-by-one errors.
int ComponentList::addComponent(Component* newComponent)
{
int numberAttempted = 0;
//cout << "let's go." << endl;
while (true)
{
cout << probeIndex << endl;
if (numberAttempted == sizeLimit)
{
//cout << "REALLOC" << endl;
// Indices all filled, allocate more memory
probeIndex = sizeLimit;
sizeLimit *= 2;
components.resize(sizeLimit);
// Not sure if this is necessary, but do it anyway
for (int i = probeIndex; i < sizeLimit; i++)
components[i] = NULL;
}
else
{
// We found empty space, or a dead component
Component* c = components[probeIndex];
if (c == NULL)
{
// Note - size is only for non-NULL references, so
// only increment when a NULL space is filled
size++;
break;
}
else if (c->getState() == ComponentState::DEAD)
{
// A component marked for deletion? Delete it, then
// offer up the location in the vector
delete c;
break;
}
else
{
// Not found? Keep searching
probeIndex++;
if (probeIndex >= sizeLimit) probeIndex = 0;
numberAttempted++;
}
}
}
//std::cout << "ADDED A NEW AT INDEX " << probeIndex << "." << std::endl;
// The index we found it on.
components[probeIndex] = newComponent;
return probeIndex;
}
Component* ComponentList::getComponent(int componentID)
{
return components[componentID];
}
void ComponentList::updateComponents(float dt)
{
// Not sure if this will make it faster, but let's try anyway
int objectsTouched = 0;
// Loop through and update all components
for (int i = 0; i < components.size(); i++)
{
Component* c = components[i];
if (c != NULL && c->getState() == ComponentState::ALIVE)
{
objectsTouched++;
c->update(dt);
}
// Avoid doing extra checks.
if (objectsTouched == size) return;
}
// no way
/*
if (components )
vector<Component*>::iterator iter = components.begin();
while (iter != components.end())
{
Component* c = *iter;
if (c != NULL && c->getState() == ComponentState::ALIVE)
{
objectsTouched++;
c->update(dt);
}
// Avoid doing extra checks.
if (objectsTouched == size) return;
iter++;
}
*/
}