added usages of trim to lists
[physics.git] / src / entityCreator.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "entityCreator.h"
19
20#include <bear/Queue.h>
21
22#include "entityManager.h"
23#include "input/inputManager.h"
24
25#include "Entities/Ball.h"
26#include "Entities/Polygon.h"
27#include "graphics/colors.h"
28
29
30/// ***** Private Variables *****
31
32typedef bear::Queue<Ball*> queBall;
33typedef bear::Queue<Polygon*> quePoly;
34
35static queBall s_startBalls;
36static queBall s_mouseBalls;
37
38static quePoly s_startPolys;
39
40/// ***** Private Method Headers *****
41
42static Ball* addBall(const Vector2& vecPos,
43 float fRadius,
44 const float* color,
45 queBall& que);
46
47static Polygon* addPoly(const vector<Vector2>& vecPoints,
48 const float* color,
49 quePoly& que);
50
51static void removeBall(queBall& que);
52static void removeAllBalls(queBall& que);
53
54static void removePoly(quePoly& que);
55static void removeAllPolys(quePoly& que);
56
57/// ***** Initializers/Cleaners *****
58
59void creator::init()
60{
61 Ball* pBall;
62 float fStartMass = 5;
63
64 s_startBalls.init();
65 s_mouseBalls.init();
66
67 s_startPolys.init();
68
69 pBall = addBall(Vector2(50, 50), 20, cWhite, s_startBalls);
70 pBall->mass = fStartMass;
71
72 pBall = addBall(Vector2(150, 50), 20, cGrey, s_startBalls);
73 pBall->mass = fStartMass;
74
75 pBall = addBall(Vector2(50, 100), 20, cRed, s_startBalls);
76 pBall->mass = fStartMass;
77
78 pBall = addBall(Vector2(100, 100), 20, cGreen, s_startBalls);
79 pBall->mass = fStartMass;
80
81 pBall = addBall(Vector2(150, 100), 20, cBlue, s_startBalls);
82 pBall->mass = fStartMass;
83
84 pBall = addBall(Vector2(50, 150), 20, cYellow, s_startBalls);
85 pBall->mass = fStartMass;
86
87 pBall = addBall(Vector2(100, 150), 20, cMagenta, s_startBalls);
88 pBall->mass = fStartMass;
89
90 pBall = addBall(Vector2(150, 150), 20, cCyan, s_startBalls);
91 pBall->mass = fStartMass;
92
93
94 for( int i = 0; i < 50; i++)
95 {
96 addBall(Vector2(200 + i * 2, 200 + i * 2), 10, cCyan);
97 }
98
99
100 vector<Vector2> points;
101 points.push_back(Vector2(500,500));
102 points.push_back(Vector2(300,500));
103 points.push_back(Vector2(500,300));
104
105 addPoly(points, cRed, s_startPolys);
106
107
108 s_startPolys.trim();
109 s_startBalls.trim();
110}
111void creator::clean()
112{
113 removeAllPolys(s_startPolys);
114 s_startPolys.fini();
115
116 removeAllBalls(s_mouseBalls);
117 s_mouseBalls.fini();
118
119 removeAllBalls(s_startBalls);
120 s_startBalls.fini();
121}
122
123/// ***** Public Methods *****
124
125void creator::addBall(const Vector2& pos, float radius, const float* color)
126{
127 addBall(pos, radius, color, s_mouseBalls);
128}
129void creator::removeBall()
130{
131 removeBall(s_mouseBalls);
132}
133void creator::removeAllBalls()
134{
135 removeAllBalls(s_mouseBalls);
136}
137
138void creator::handleInput()
139{
140 if(input::mouseLeft())
141 addBall(input::mousePosition(), 10, cCyan);
142
143 if(input::mouseRight() && ! s_mouseBalls.isEmpty())
144 removeBall();
145}
146
147/// ***** Private Methods *****
148
149Ball* addBall(const Vector2& vecPos,
150 float fRadius,
151 const float* color,
152 queBall& que)
153{
154 Ball* pBall = new Ball(vecPos, fRadius, color);
155
156 que.pushBack(pBall);
157 manager::add(pBall);
158
159 return pBall;
160}
161Polygon* addPoly(const vector<Vector2>& vecPoints,
162 const float* color,
163 quePoly& que)
164{
165 Polygon* pPoly = new Polygon(vecPoints, color);
166
167 que.pushBack(pPoly);
168 manager::add(pPoly);
169
170 return pPoly;
171}
172
173void removeBall(queBall& que)
174{
175 Ball* pBall = que.getFront();
176
177 manager::remove(pBall);
178 que.popFront();
179
180 delete pBall;
181}
182void removeAllBalls(queBall& que)
183{
184 while(! que.isEmpty() )
185 {
186 removeBall(que);
187 }
188}
189
190static void removePoly(quePoly& que)
191{
192 Polygon* pPoly = que.getFront();
193
194 manager::remove(pPoly);
195 que.popFront();
196
197 delete pPoly;
198}
199static void removeAllPolys(quePoly& que)
200{
201 while(! que.isEmpty() )
202 {
203 removePoly(que);
204 }
205}