1 /* Webcamoid, webcam capture application.
2  * Copyright (C) 2016  Gonzalo Exequiel Pedone
3  *
4  * Webcamoid 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  * Webcamoid 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 Webcamoid. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Web-Site: http://webcamoid.github.io/
18  */
19 
20 #include <cstdlib>
21 #include <random>
22 #include <QDateTime>
23 #include <QRandomGenerator>
24 
25 #include "scratch.h"
26 
27 class ScratchPrivate
28 {
29     public:
30         qreal m_life0 {0.0};
31         qreal m_life {0.0};
32         qreal m_dlife {0.0};
33         qreal m_x {0.0};
34         qreal m_dx {0.0};
35         int m_y {0};
36 
37         inline qreal boundedReal(qreal min, qreal max);
38 };
39 
Scratch()40 Scratch::Scratch()
41 {
42     this->d = new ScratchPrivate;
43 }
44 
Scratch(qreal minLife,qreal maxLife,qreal minDLife,qreal maxDLife,qreal minX,qreal maxX,qreal minDX,qreal maxDX,int minY,int maxY)45 Scratch::Scratch(qreal minLife, qreal maxLife,
46                  qreal minDLife, qreal maxDLife,
47                  qreal minX, qreal maxX,
48                  qreal minDX, qreal maxDX,
49                  int minY, int maxY)
50 {
51     this->d = new ScratchPrivate;
52     this->d->m_life = this->d->m_life0 = this->d->boundedReal(minLife, maxLife);
53     this->d->m_dlife = this->d->boundedReal(minDLife, maxDLife);
54 
55     if (!qIsNull(this->d->m_dlife))
56         this->d->m_dlife = maxDLife - minDLife;
57 
58     this->d->m_x = this->d->boundedReal(minX, maxX);
59     this->d->m_dx = this->d->boundedReal(minDX, maxDX);
60 
61     if (!qIsNull(this->d->m_dx))
62         this->d->m_dx = maxDX - minDX;
63 
64     this->d->m_y = QRandomGenerator::global()->bounded(minY, maxY);
65 }
66 
Scratch(const Scratch & other)67 Scratch::Scratch(const Scratch &other)
68 {
69     this->d = new ScratchPrivate;
70     this->d->m_life0 = other.d->m_life0;
71     this->d->m_life = other.d->m_life;
72     this->d->m_dlife = other.d->m_dlife;
73     this->d->m_x = other.d->m_x;
74     this->d->m_dx = other.d->m_dx;
75     this->d->m_y = other.d->m_y;
76 }
77 
~Scratch()78 Scratch::~Scratch()
79 {
80     delete this->d;
81 }
82 
operator =(const Scratch & other)83 Scratch &Scratch::operator =(const Scratch &other)
84 {
85     if (this != &other) {
86         this->d->m_life0 = other.d->m_life0;
87         this->d->m_life = other.d->m_life;
88         this->d->m_dlife = other.d->m_dlife;
89         this->d->m_x = other.d->m_x;
90         this->d->m_dx = other.d->m_dx;
91         this->d->m_y = other.d->m_y;
92     }
93 
94     return *this;
95 }
96 
operator ++(int)97 Scratch Scratch::operator ++(int)
98 {
99     this->d->m_life -= this->d->m_dlife;
100     this->d->m_x += this->d->m_dx;
101 
102     return *this;
103 }
104 
life() const105 qreal Scratch::life() const
106 {
107     return this->d->m_life;
108 }
109 
life()110 qreal &Scratch::life()
111 {
112     return this->d->m_life;
113 }
114 
dlife() const115 qreal Scratch::dlife() const
116 {
117     return this->d->m_dlife;
118 }
119 
dlife()120 qreal &Scratch::dlife()
121 {
122     return this->d->m_dlife;
123 }
124 
x() const125 qreal Scratch::x() const
126 {
127     return this->d->m_x;
128 }
129 
x()130 qreal &Scratch::x()
131 {
132     return this->d->m_x;
133 }
134 
dx() const135 qreal Scratch::dx() const
136 {
137     return this->d->m_dx;
138 }
139 
dx()140 qreal &Scratch::dx()
141 {
142     return this->d->m_dx;
143 }
144 
y() const145 int Scratch::y() const
146 {
147     return this->d->m_y;
148 }
149 
y()150 int &Scratch::y()
151 {
152     return this->d->m_y;
153 }
154 
isAboutToDie() const155 bool Scratch::isAboutToDie() const
156 {
157     const qreal threshold = 0.75;
158 
159     return this->d->m_life <= this->d->m_dlife * (1.0 + threshold);
160 }
161 
setLife(qreal life)162 void Scratch::setLife(qreal life)
163 {
164     this->d->m_life = life;
165 }
166 
setDLife(qreal dlife)167 void Scratch::setDLife(qreal dlife)
168 {
169     this->d->m_dlife = dlife;
170 }
171 
setX(qreal x)172 void Scratch::setX(qreal x)
173 {
174     this->d->m_x = x;
175 }
176 
setDx(qreal dx)177 void Scratch::setDx(qreal dx)
178 {
179     this->d->m_dx = dx;
180 }
181 
setY(int y)182 void Scratch::setY(int y)
183 {
184     this->d->m_y = y;
185 }
186 
resetLife()187 void Scratch::resetLife()
188 {
189     this->setLife(0.0);
190 }
191 
resetDLife()192 void Scratch::resetDLife()
193 {
194     this->setDLife(0.0);
195 }
196 
resetX()197 void Scratch::resetX()
198 {
199     this->setX(0.0);
200 }
201 
resetDx()202 void Scratch::resetDx()
203 {
204     this->setDx(0.0);
205 }
206 
resetY()207 void Scratch::resetY()
208 {
209     this->setY(0);
210 }
211 
boundedReal(qreal min,qreal max)212 qreal ScratchPrivate::boundedReal(qreal min, qreal max)
213 {
214     std::uniform_real_distribution<qreal> distribution(min, max);
215 
216     return distribution(*QRandomGenerator::global());
217 }
218