fixed selection of a game_object in IsObject mode
[supertux.git] / src / particlesystem.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include "particlesystem.h"
21
22 #include <iostream>
23 #include <math.h>
24 #include "globals.h"
25 #include "world.h"
26 #include "level.h"
27 #include "scene.h"
28
29 ParticleSystem::ParticleSystem()
30 {
31     virtual_width = screen->w;
32     virtual_height = screen->h;
33 }
34
35 ParticleSystem::~ParticleSystem()
36 {
37     std::vector<Particle*>::iterator i;
38     for(i = particles.begin(); i != particles.end(); ++i) {
39         delete *i;
40     }
41 }
42
43 void ParticleSystem::draw(float scrollx, float scrolly, int layer)
44 {
45     std::vector<Particle*>::iterator i;
46     for(i = particles.begin(); i != particles.end(); ++i) {
47         Particle* particle = *i;
48         if(particle->layer != layer)
49             continue;
50         
51         // remap x,y coordinates onto screencoordinates
52         float x = fmodf(particle->x - scrollx, virtual_width);
53         if(x < 0) x += virtual_width;
54         float y = fmodf(particle->y - scrolly, virtual_height);
55         if(y < 0) y += virtual_height;
56         float xmax = fmodf(x + particle->texture->w, virtual_width);
57         float ymax = fmodf(y + particle->texture->h, virtual_height);
58
59         // particle on screen
60         if(x >= screen->w && xmax >= screen->w)
61             continue;
62         if(y >= screen->h && ymax >= screen->h)
63             continue;
64         
65         if(x > screen->w) x -= virtual_width;
66         if(y > screen->h) y -= virtual_height;
67         particle->texture->draw(x, y);
68     }
69 }
70
71 SnowParticleSystem::SnowParticleSystem()
72 {
73     snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", USE_ALPHA);
74     snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", USE_ALPHA);
75     snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", USE_ALPHA);
76
77     virtual_width = screen->w * 2;
78
79     // create some random snowflakes
80     size_t snowflakecount = size_t(virtual_width/10.0);
81     for(size_t i=0; i<snowflakecount; ++i) {
82         SnowParticle* particle = new SnowParticle;
83         particle->x = rand() % int(virtual_width);
84         particle->y = rand() % screen->h;
85         particle->layer = i % 2;
86         int snowsize = rand() % 3;
87         particle->texture = snowimages[snowsize];
88         do {
89             particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
90         } while(particle->speed < 0.01);
91         particle->speed *= World::current()->get_level()->gravity;
92
93         particles.push_back(particle);
94     }
95 }
96
97 SnowParticleSystem::~SnowParticleSystem()
98 {
99   for(int i=0;i<3;++i)
100     delete snowimages[i];
101 }
102
103 void SnowParticleSystem::simulate(float elapsed_time)
104 {
105     std::vector<Particle*>::iterator i;
106     for(i = particles.begin(); i != particles.end(); ++i) {
107         SnowParticle* particle = (SnowParticle*) *i;
108         particle->y += particle->speed * elapsed_time;
109         if(particle->y > screen->h) {
110             particle->y = fmodf(particle->y , virtual_height);
111             particle->x = rand() % int(virtual_width);
112         }
113     }
114 }
115
116 CloudParticleSystem::CloudParticleSystem()
117 {
118     cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
119
120     virtual_width = 2000.0;
121
122     // create some random clouds
123     for(size_t i=0; i<15; ++i) {
124         CloudParticle* particle = new CloudParticle;
125         particle->x = rand() % int(virtual_width);
126         particle->y = rand() % int(virtual_height);
127         particle->layer = 0;
128         particle->texture = cloudimage;
129         particle->speed = -float(250 + rand() % 200) / 1000.0;
130
131         particles.push_back(particle);
132     }
133 }
134
135 CloudParticleSystem::~CloudParticleSystem()
136 {
137   delete cloudimage;
138 }
139
140 void CloudParticleSystem::simulate(float elapsed_time)
141 {
142     std::vector<Particle*>::iterator i;
143     for(i = particles.begin(); i != particles.end(); ++i) {
144         CloudParticle* particle = (CloudParticle*) *i;
145         particle->x += particle->speed * elapsed_time;
146     }
147 }