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