created locks abstraction
[physics.git] / src / entityCreator.cpp
CommitLineData
e68f847b
PG
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
054d658f
PG
18#include "entityCreator.h"
19
f72f4a59
PG
20#include <queue>
21
a483ed75 22#include "entityManager.h"
10baa49b
PG
23#include "input/inputManager.h"
24
a483ed75
PG
25#include "Entities/Ball.h"
26#include "graphics/colors.h"
27
10baa49b 28
617dcc71
PG
29/// ***** Private Variables *****
30
f72f4a59 31typedef std::queue<Ball*> queBall;
10baa49b
PG
32
33queBall startBalls;
34queBall mouseBalls;
35
36/// ***** Private Method Headers *****
37
38Ball* addBall(const Vector2& pos,
39 float radius,
40 const float* color,
41 queBall& que);
42
43void removeBall(queBall& que);
44void removeAllBalls(queBall& que);
a483ed75 45
617dcc71 46/// ***** Initializers/Cleaners *****
a483ed75 47
054d658f
PG
48void creator::init()
49{
6a40b627 50 Ball* ball;
e89e708a 51 float startMass = 5;
6a40b627
PG
52
53 ball = addBall(Vector2(50, 50), 20, cWhite, startBalls);
e89e708a 54 ball->mass = startMass;
6a40b627
PG
55
56 ball = addBall(Vector2(150, 50), 20, cGrey, startBalls);
e89e708a 57 ball->mass = startMass;
6a40b627
PG
58
59 ball = addBall(Vector2(50, 100), 20, cRed, startBalls);
e89e708a 60 ball->mass = startMass;
6a40b627
PG
61
62 ball = addBall(Vector2(100, 100), 20, cGreen, startBalls);
e89e708a 63 ball->mass = startMass;
6a40b627
PG
64
65 ball = addBall(Vector2(150, 100), 20, cBlue, startBalls);
e89e708a 66 ball->mass = startMass;
6a40b627
PG
67
68 ball = addBall(Vector2(50, 150), 20, cYellow, startBalls);
e89e708a 69 ball->mass = startMass;
6a40b627
PG
70
71 ball = addBall(Vector2(100, 150), 20, cMagenta, startBalls);
e89e708a 72 ball->mass = startMass;
6a40b627
PG
73
74 ball = addBall(Vector2(150, 150), 20, cCyan, startBalls);
e89e708a 75 ball->mass = startMass;
598fa0d5 76
3bccd1d7 77 for( int i = 0; i<50; i++)
598fa0d5
PG
78 {
79 addBall(Vector2(200+i*2, 200+i*2), 10, cCyan);
80 }
054d658f
PG
81}
82void creator::clean()
83{
10baa49b
PG
84 removeAllBalls(startBalls);
85 removeAllBalls(mouseBalls);
054d658f
PG
86}
87
617dcc71
PG
88/// ***** Public Methods *****
89
88e62c4f 90void creator::addBall(const Vector2& pos, float radius, const float* color)
054d658f 91{
10baa49b
PG
92 addBall(pos, radius, color, mouseBalls);
93}
94void creator::removeBall()
95{
96 removeBall(mouseBalls);
97}
98void creator::removeAllBalls()
99{
100 removeAllBalls(mouseBalls);
101}
102
103void creator::handleInput()
104{
105 if(input::mouseLeft())
106 addBall(input::mousePosition(), 10, cCyan);
107
108 if(input::mouseRight() && ! mouseBalls.empty())
109 removeBall();
110}
111
112/// ***** Private Methods *****
113
114Ball* addBall(const Vector2& pos,
115 float radius,
116 const float* color,
117 queBall& que)
118{
88e62c4f
PG
119 Ball* ball = new Ball(pos, radius, color);
120
10baa49b 121 que.push(ball);
88e62c4f 122 manager::add(ball);
10baa49b
PG
123
124 return ball;
054d658f 125}
10baa49b
PG
126
127void removeBall(queBall& que)
054d658f 128{
10baa49b 129 Ball* ball = que.front();
88e62c4f 130
10baa49b
PG
131 manager::remove(ball);
132 que.pop();
88e62c4f
PG
133
134 delete ball;
054d658f 135}
10baa49b 136void removeAllBalls(queBall& que)
054d658f 137{
10baa49b 138 while(! que.empty() )
f72f4a59 139 {
10baa49b
PG
140 Ball* ball = que.front();
141 que.pop();
f72f4a59 142 manager::remove(ball);
054d658f 143
f72f4a59
PG
144 delete ball;
145 }
054d658f 146}