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