be1622a69f1200db549210de8ee18555a0de0515
[supertux.git] / src / object / 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
21 #include <config.h>
22
23 #include <iostream>
24 #include <cmath>
25
26 #include "particlesystem.h"
27 #include "app/globals.h"
28 #include "video/drawing_context.h"
29 #include "lisp/parser.h"
30 #include "lisp/lisp.h"
31 #include "lisp/writer.h"
32
33 ParticleSystem::ParticleSystem()
34 {
35     virtual_width = SCREEN_WIDTH;
36     virtual_height = SCREEN_HEIGHT;
37     layer = LAYER_BACKGROUND1;
38 }
39
40 ParticleSystem::~ParticleSystem()
41 {
42     std::vector<Particle*>::iterator i;
43     for(i = particles.begin(); i != particles.end(); ++i) {
44         delete *i;
45     }
46 }
47
48 void ParticleSystem::draw(DrawingContext& context)
49 {
50   float scrollx = context.get_translation().x;
51   float scrolly = context.get_translation().y;
52
53   context.push_transform();
54   context.set_translation(Vector(0,0));
55   
56     std::vector<Particle*>::iterator i;
57     for(i = particles.begin(); i != particles.end(); ++i) {
58         Particle* particle = *i;
59
60         // remap x,y coordinates onto screencoordinates
61         Vector pos;
62         pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
63         if(pos.x < 0) pos.x += virtual_width;
64         pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
65         if(pos.y < 0) pos.y += virtual_height;
66
67         if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
68         if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;
69         context.draw_surface(particle->texture, pos, layer);
70     }
71
72     context.pop_transform();
73 }
74
75 SnowParticleSystem::SnowParticleSystem()
76 {
77     snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", true);
78     snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", true);
79     snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", true);
80
81     virtual_width = SCREEN_WIDTH * 2;
82
83     // create some random snowflakes
84     size_t snowflakecount = size_t(virtual_width/10.0);
85     for(size_t i=0; i<snowflakecount; ++i) {
86         SnowParticle* particle = new SnowParticle;
87         particle->pos.x = rand() % int(virtual_width);
88         particle->pos.y = rand() % SCREEN_HEIGHT;
89         int snowsize = rand() % 3;
90         particle->texture = snowimages[snowsize];
91         do {
92             particle->speed = snowsize*.2 + (float(rand()%10)*.4);
93         } while(particle->speed < 1);
94         particle->speed *= 10; // gravity
95
96         particles.push_back(particle);
97     }
98 }
99
100 void
101 SnowParticleSystem::parse(const lisp::Lisp& reader)
102 {
103   reader.get("layer", layer);
104 }
105
106 void
107 SnowParticleSystem::write(lisp::Writer& writer)
108 {
109   writer.start_list("particles-snow");
110   writer.write_int("layer", layer);
111   writer.end_list("particles-snow");
112 }
113
114 SnowParticleSystem::~SnowParticleSystem()
115 {
116   for(int i=0;i<3;++i)
117     delete snowimages[i];
118 }
119
120 void SnowParticleSystem::action(float elapsed_time)
121 {
122     std::vector<Particle*>::iterator i;
123     for(i = particles.begin(); i != particles.end(); ++i) {
124         SnowParticle* particle = (SnowParticle*) *i;
125         particle->pos.y += particle->speed * elapsed_time;
126         if(particle->pos.y > SCREEN_HEIGHT) {
127             particle->pos.y = fmodf(particle->pos.y , virtual_height);
128             particle->pos.x = rand() % int(virtual_width);
129         }
130     }
131 }
132
133 RainParticleSystem::RainParticleSystem()
134 {
135     rainimages[0] = new Surface(datadir+"/images/shared/rain0.png", true);
136     rainimages[1] = new Surface(datadir+"/images/shared/rain1.png", true);
137
138     virtual_width = SCREEN_WIDTH * 2;
139
140     // create some random raindrops
141     size_t raindropcount = size_t(virtual_width/8.0);
142     for(size_t i=0; i<raindropcount; ++i) {
143         RainParticle* particle = new RainParticle;
144         particle->pos.x = rand() % int(virtual_width);
145         particle->pos.y = rand() % SCREEN_HEIGHT;
146         int rainsize = rand() % 2;
147         particle->texture = rainimages[rainsize];
148         do {
149             particle->speed = (rainsize+1)*45 + (float(rand()%10)*.4);
150         } while(particle->speed < 1);
151         particle->speed *= 10; // gravity
152
153         particles.push_back(particle);
154     }
155 }
156
157 void
158 RainParticleSystem::parse(const lisp::Lisp& reader)
159 {
160   reader.get("layer", layer);
161 }
162
163 void
164 RainParticleSystem::write(lisp::Writer& writer)
165 {
166   writer.start_list("particles-rain");
167   writer.write_int("layer", layer);
168   writer.end_list("particles-rain");
169 }
170
171 RainParticleSystem::~RainParticleSystem()
172 {
173   for(int i=0;i<2;++i)
174     delete rainimages[i];
175 }
176
177 void RainParticleSystem::action(float elapsed_time)
178 {
179     std::vector<Particle*>::iterator i;
180     for(i = particles.begin(); i != particles.end(); ++i) {
181         RainParticle* particle = (RainParticle*) *i;
182         particle->pos.y += particle->speed * elapsed_time;
183         particle->pos.x -= particle->speed * elapsed_time;
184         if(particle->pos.y > SCREEN_HEIGHT) {
185             particle->pos.y = fmodf(particle->pos.y , virtual_height);
186             particle->pos.x = rand() % int(virtual_width);
187         }
188     }
189 }
190
191 CloudParticleSystem::CloudParticleSystem()
192 {
193     cloudimage = new Surface(datadir + "/images/shared/cloud.png", true);
194
195     virtual_width = 2000.0;
196
197     // create some random clouds
198     for(size_t i=0; i<15; ++i) {
199         CloudParticle* particle = new CloudParticle;
200         particle->pos.x = rand() % int(virtual_width);
201         particle->pos.y = rand() % int(virtual_height);
202         particle->texture = cloudimage;
203         particle->speed = -float(25 + rand() % 30);
204
205         particles.push_back(particle);
206     }
207 }
208
209 void
210 CloudParticleSystem::parse(const lisp::Lisp& reader)
211 {
212   reader.get("layer", layer);
213 }
214
215 void
216 CloudParticleSystem::write(lisp::Writer& writer)
217 {
218   writer.start_list("particles-clouds");
219   writer.write_int("layer", layer);
220   writer.end_list("particles-clouds");
221 }
222
223 CloudParticleSystem::~CloudParticleSystem()
224 {
225   delete cloudimage;
226 }
227
228 void CloudParticleSystem::action(float elapsed_time)
229 {
230     std::vector<Particle*>::iterator i;
231     for(i = particles.begin(); i != particles.end(); ++i) {
232         CloudParticle* particle = (CloudParticle*) *i;
233         particle->pos.x += particle->speed * elapsed_time;
234     }
235 }