This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
/
Particles.cpp
107 lines (88 loc) · 2.76 KB
/
Particles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <Thor/Particles.hpp>
#include <Thor/Animations.hpp>
#include <Thor/Vectors/PolarVector2.hpp>
#include <Thor/Math/Distributions.hpp>
#include <SFML/Graphics.hpp>
int main()
{
// Create window
sf::RenderWindow window(sf::VideoMode(800, 600), "Thor Particles");
// Load texture
sf::Texture texture;
if (!texture.loadFromFile("Media/particle.png"))
return EXIT_FAILURE;
// Create emitter
thor::UniversalEmitter emitter;
emitter.setEmissionRate(30.f);
emitter.setParticleLifetime(sf::seconds(5.f));
// Create particle system, add reference to emitter
thor::ParticleSystem system;
system.setTexture(texture);
system.addEmitter(thor::refEmitter(emitter));
// Build color gradient (green -> teal -> blue)
thor::ColorGradient gradient;
gradient[0.f] = sf::Color(0, 150, 0);
gradient[0.5f] = sf::Color(0, 150, 100);
gradient[1.f] = sf::Color(0, 0, 150);
// Create color and fade in/out animations
thor::ColorAnimation colorizer(gradient);
thor::FadeAnimation fader(0.1f, 0.1f);
// Add particle affectors
system.addAffector( thor::AnimationAffector(colorizer) );
system.addAffector( thor::AnimationAffector(fader) );
system.addAffector( thor::TorqueAffector(100.f) );
system.addAffector( thor::ForceAffector(sf::Vector2f(0.f, 100.f)) );
// Attributes that influence emitter
thor::PolarVector2f velocity(200.f, -90.f);
bool paused = false;
sf::Font font;
if (!font.loadFromFile("Media/sansation.ttf"))
return 1;
// Instruction text
sf::Text instructions(
"Left click: Pause\n"
"Mouse wheel: Change direction\n",
font, 14u);
// Create clock to measure frame time
sf::Clock frameClock;
// Main loop
for (;;)
{
// Handle events
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
// [X]: Quit
case sf::Event::Closed:
return 0;
// Escape: Quit
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
return 0;
// Left mouse button: Enable/disable glow
case sf::Event::MouseButtonPressed:
if (event.mouseButton.button == sf::Mouse::Left)
paused = !paused;
break;
// Mouse wheel: Change emission direction
case sf::Event::MouseWheelMoved:
velocity.phi += 12.f * event.mouseWheel.delta;
break;
}
}
// Update particle system and emitter
const sf::Time frameTime = frameClock.restart();
if (!paused)
system.update(frameTime);
// Set initial particle position and velocity, rotate vector randomly by maximal 10 degrees
emitter.setParticlePosition( window.mapPixelToCoords(sf::Mouse::getPosition(window)) );
emitter.setParticleVelocity( thor::Distributions::deflect(velocity, 10.f) );
// Draw everything
window.clear(sf::Color(30, 30, 30));
window.draw(instructions);
window.draw(system);
window.display();
}
}