hx3d
1
2D/3D Simple Game Framework
Main Page
Namespaces
Classes
Files
File List
engine
src
graphics
particle.cpp
1
/*
2
Particle.
3
Copyright (C) 2015 Denis BOURGE
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
You should have received a copy of the GNU Lesser General Public
16
License along with this library; if not, write to the Free Software
17
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18
USA
19
*/
20
21
#include "hx3d/graphics/particle.hpp"
22
23
#include "hx3d/math/interpolation.hpp"
24
25
namespace
hx3d
{
26
namespace
graphics {
27
28
Particle::Particle() {
29
reset
();
30
}
31
32
void
Particle::reset
() {
33
life = baseLife = 0.f;
34
dead =
false
;
35
rotation = 0.f;
36
rotationSpeed = 0.f;
37
color =
Color::White
;
38
}
39
40
void
Particle::setTexture
(
const
Ptr<Texture>
& texture) {
41
sprite.
setTexture
(texture);
42
}
43
44
void
Particle::update
(
const
float
delta) {
45
life -= delta;
46
47
if
(life <= 0.f) {
48
dead =
true
;
49
return
;
50
}
51
52
else
{
53
float
percentage = life / baseLife;
54
color.
a
=
math::interpolate
(0, 255, percentage, math::Interpolation::Linear);
55
float
z_pos = color.
a
/ 255.f;
// between 0 && 1
56
sprite.
setTint
(color);
57
58
position.x -= velocity.x * delta;
59
position.y -= velocity.y * delta;
60
position.z = z_pos;
61
62
velocity.x -= gravity.x * delta;
63
velocity.y -= gravity.y * delta;
64
65
rotation += rotationSpeed * delta;
66
}
67
}
68
69
void
Particle::draw
(
Batch
& batch) {
70
sprite.
transform
.
position
= position;
71
sprite.
transform
.
size
= size;
72
sprite.
transform
.
rotation
.z = rotation;
73
74
batch.
draw
(sprite);
75
}
76
77
}
/* graphics */
78
}
/* hx3d */
hx3d::graphics::Mesh::setTint
void setTint(Color tint)
Set the mesh tint.
Definition:
mesh.cpp:43
hx3d::graphics::Color::a
unsigned char a
Alpha component.
Definition:
color.hpp:102
hx3d::graphics::Particle::update
void update(const float delta)
Update the particle.
Definition:
particle.cpp:44
hx3d::graphics::Transform::position
glm::vec3 position
Position.
Definition:
transform.hpp:80
hx3d::graphics::Sprite::setTexture
void setTexture(const Ptr< Texture > &texture)
Set the sprite texture.
Definition:
sprite.cpp:36
hx3d::graphics::Color::White
static Color White
White color.
Definition:
color.hpp:107
hx3d::math::interpolate
T interpolate(T a, T b, float t, Interpolation type)
Interpolate between two values.
Definition:
interpolation.inl.hpp:54
hx3d
hx3d framework namespace
Definition:
audio.hpp:26
hx3d::graphics::Batch
Simple base batch implementation. Draw at each draw call.
Definition:
batch.hpp:36
hx3d::graphics::Transform::rotation
glm::vec3 rotation
Rotation.
Definition:
transform.hpp:86
hx3d::graphics::Particle::reset
virtual void reset() override
Reset the element.
Definition:
particle.cpp:32
hx3d::graphics::Batch::draw
virtual void draw(Mesh &mesh) override
Draw the mesh.
Definition:
batch.cpp:53
hx3d::graphics::Transform::size
glm::vec3 size
Size.
Definition:
transform.hpp:84
hx3d::graphics::Mesh::transform
Transform transform
Mesh transformation.
Definition:
mesh.hpp:90
hx3d::graphics::Particle::setTexture
void setTexture(const Ptr< Texture > &texture)
Set the texture.
Definition:
particle.cpp:40
hx3d::Ptr
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition:
ptr.hpp:34
hx3d::graphics::Particle::draw
void draw(Batch &batch)
Draw the particle.
Definition:
particle.cpp:69
Generated by
1.8.11