- removed dn_tilemap
[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 #include "particlesystem.h"
20
21 #include <iostream>
22 #include <math.h>
23 #include "globals.h"
24 #include "scene.h"
25
26 ParticleSystem::ParticleSystem()
27 {
28     virtual_width = screen->w;
29     virtual_height = screen->h;
30 }
31
32 ParticleSystem::~ParticleSystem()
33 {
34     std::vector<Particle*>::iterator i;
35     for(i = particles.begin(); i != particles.end(); ++i) {
36         delete *i;
37     }
38 }
39
40 void ParticleSystem::draw(float scrollx, float scrolly, int layer)
41 {
42     std::vector<Particle*>::iterator i;
43     for(i = particles.begin(); i != particles.end(); ++i) {
44         Particle* particle = *i;
45         if(particle->layer != layer)
46             continue;
47         
48         // remap x,y coordinates onto screencoordinates
49         float x = fmodf(particle->x - scrollx, virtual_width);
50         if(x < 0) x += virtual_width;
51         float y = fmodf(particle->y - scrolly, virtual_height);
52         if(y < 0) y += virtual_height;
53         float xmax = fmodf(x + particle->texture->w, virtual_width);
54         float ymax = fmodf(y + particle->texture->h, virtual_height);
55
56         // particle on screen
57         if(x >= screen->w && xmax >= screen->w)
58             continue;
59         if(y >= screen->h && ymax >= screen->h)
60             continue;
61         
62         if(x > screen->w) x -= virtual_width;
63         if(y > screen->h) y -= virtual_height;
64         texture_draw(particle->texture, x, y);
65     }
66 }
67
68 SnowParticleSystem::SnowParticleSystem()
69 {
70     texture_load(&snowimages[0], datadir+"/images/shared/snow0.png", USE_ALPHA);
71     texture_load(&snowimages[1], datadir+"/images/shared/snow1.png", USE_ALPHA);
72     texture_load(&snowimages[2], datadir+"/images/shared/snow2.png", USE_ALPHA);
73
74     virtual_width = screen->w * 2;
75
76     // create some random snowflakes
77     size_t snowflakecount = size_t(virtual_width/10.0);
78     for(size_t i=0; i<snowflakecount; ++i) {
79         SnowParticle* particle = new SnowParticle;
80         particle->x = rand() % int(virtual_width);
81         particle->y = rand() % screen->h;
82         particle->layer = i % 2;
83         int snowsize = rand() % 3;
84         particle->texture = &snowimages[snowsize];
85         do {
86             particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
87         } while(particle->speed < 0.01);
88         particle->speed *= gravity;
89
90         particles.push_back(particle);
91     }
92 }
93
94 SnowParticleSystem::~SnowParticleSystem()
95 {
96     for(int i=0;i<3;++i)
97         texture_free(&snowimages[i]);
98 }
99
100 void SnowParticleSystem::simulate(float elapsed_time)
101 {
102     std::vector<Particle*>::iterator i;
103     for(i = particles.begin(); i != particles.end(); ++i) {
104         SnowParticle* particle = (SnowParticle*) *i;
105         particle->y += particle->speed * elapsed_time;
106         if(particle->y > screen->h) {
107             particle->y = 0;
108             particle->x = rand() % int(virtual_width);
109         }
110     }
111 }
112
113 CloudParticleSystem::CloudParticleSystem()
114 {
115     texture_load(&cloudimage, datadir + "/images/shared/cloud.png", USE_ALPHA);
116
117     virtual_width = 2000.0;
118
119     // create some random clouds
120     for(size_t i=0; i<15; ++i) {
121         CloudParticle* particle = new CloudParticle;
122         particle->x = rand() % int(virtual_width);
123         particle->y = rand() % int(virtual_height);
124         particle->layer = 0;
125         particle->texture = &cloudimage;
126         particle->speed = -float(250 + rand() % 200) / 1000.0;
127
128         particles.push_back(particle);
129     }
130 }
131
132 CloudParticleSystem::~CloudParticleSystem()
133 {
134     texture_free(&cloudimage);
135 }
136
137 void CloudParticleSystem::simulate(float elapsed_time)
138 {
139     std::vector<Particle*>::iterator i;
140     for(i = particles.begin(); i != particles.end(); ++i) {
141         CloudParticle* particle = (CloudParticle*) *i;
142         particle->x += particle->speed * elapsed_time;
143     }
144 }