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