Split object/particlesystem.?pp
[supertux.git] / src / object / particlesystem.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 #include "object/particlesystem.hpp"
18
19 #include <cmath>
20
21 #include "math/random_generator.hpp"
22 #include "supertux/main.hpp"
23 #include "video/drawing_context.hpp"
24
25 ParticleSystem::ParticleSystem(float max_particle_size) :
26   max_particle_size(max_particle_size),
27   z_pos(),
28   particles(),
29   virtual_width(),
30   virtual_height()
31 {
32   virtual_width = SCREEN_WIDTH + max_particle_size * 2;
33   virtual_height = SCREEN_HEIGHT + max_particle_size *2;
34   z_pos = LAYER_BACKGROUND1;
35 }
36
37 ParticleSystem::~ParticleSystem()
38 {
39   std::vector<Particle*>::iterator i;
40   for(i = particles.begin(); i != particles.end(); ++i) {
41     delete *i;
42   }
43 }
44
45 void ParticleSystem::draw(DrawingContext& context)
46 {
47   float scrollx = context.get_translation().x;
48   float scrolly = context.get_translation().y;
49
50   context.push_transform();
51   context.set_translation(Vector(max_particle_size,max_particle_size));
52
53   std::vector<Particle*>::iterator i;
54   for(i = particles.begin(); i != particles.end(); ++i) {
55     Particle* particle = *i;
56
57     // remap x,y coordinates onto screencoordinates
58     Vector pos;
59
60     pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
61     if(pos.x < 0) pos.x += virtual_width;
62
63     pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
64     if(pos.y < 0) pos.y += virtual_height;
65
66     //if(pos.x > virtual_width) pos.x -= virtual_width;
67     //if(pos.y > virtual_height) pos.y -= virtual_height;
68
69     context.draw_surface(particle->texture, pos, z_pos);
70   }
71
72   context.pop_transform();
73 }
74
75 /* EOF */