hacks that make a polygon draw
[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 <queue>
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 std::queue<Ball*> queBall;
33
34queBall startBalls;
35queBall mouseBalls;
36
37/// ***** Private Method Headers *****
38
39Ball* addBall(const Vector2& pos,
40 float radius,
41 const float* color,
42 queBall& que);
43
44void removeBall(queBall& que);
45void removeAllBalls(queBall& que);
46
47/// ***** Initializers/Cleaners *****
48
49void creator::init()
50{
51 Ball* ball;
52 float startMass = 5;
53
54 ball = addBall(Vector2(50, 50), 20, cWhite, startBalls);
55 ball->mass = startMass;
56
57 ball = addBall(Vector2(150, 50), 20, cGrey, startBalls);
58 ball->mass = startMass;
59
60 ball = addBall(Vector2(50, 100), 20, cRed, startBalls);
61 ball->mass = startMass;
62
63 ball = addBall(Vector2(100, 100), 20, cGreen, startBalls);
64 ball->mass = startMass;
65
66 ball = addBall(Vector2(150, 100), 20, cBlue, startBalls);
67 ball->mass = startMass;
68
69 ball = addBall(Vector2(50, 150), 20, cYellow, startBalls);
70 ball->mass = startMass;
71
72 ball = addBall(Vector2(100, 150), 20, cMagenta, startBalls);
73 ball->mass = startMass;
74
75 ball = addBall(Vector2(150, 150), 20, cCyan, startBalls);
76 ball->mass = startMass;
77
78 for( int i = 0; i<50; i++)
79 {
80 addBall(Vector2(200+i*2, 200+i*2), 10, cCyan);
81 }
82
83 // add a polygon into the mix (currently not cleaned up)
84 vector<Vector2> points;
85 points.push_back(Vector2(50,50));
86 points.push_back(Vector2(50,100));
87 points.push_back(Vector2(100,50));
88
89 manager::add(new Polygon(points));
90}
91void creator::clean()
92{
93 removeAllBalls(startBalls);
94 removeAllBalls(mouseBalls);
95}
96
97/// ***** Public Methods *****
98
99void creator::addBall(const Vector2& pos, float radius, const float* color)
100{
101 addBall(pos, radius, color, mouseBalls);
102}
103void creator::removeBall()
104{
105 removeBall(mouseBalls);
106}
107void creator::removeAllBalls()
108{
109 removeAllBalls(mouseBalls);
110}
111
112void creator::handleInput()
113{
114 if(input::mouseLeft())
115 addBall(input::mousePosition(), 10, cCyan);
116
117 if(input::mouseRight() && ! mouseBalls.empty())
118 removeBall();
119}
120
121/// ***** Private Methods *****
122
123Ball* addBall(const Vector2& pos,
124 float radius,
125 const float* color,
126 queBall& que)
127{
128 Ball* ball = new Ball(pos, radius, color);
129
130 que.push(ball);
131 manager::add(ball);
132
133 return ball;
134}
135
136void removeBall(queBall& que)
137{
138 Ball* ball = que.front();
139
140 manager::remove(ball);
141 que.pop();
142
143 delete ball;
144}
145void removeAllBalls(queBall& que)
146{
147 while(! que.empty() )
148 {
149 Ball* ball = que.front();
150 que.pop();
151 manager::remove(ball);
152
153 delete ball;
154 }
155}