4 // Copyright (C) 2004 Matthias Braun <matze@braunis.de>
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.
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.
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.
20 #include "particlesystem.h"
29 ParticleSystem::ParticleSystem()
31 virtual_width = screen->w;
32 virtual_height = screen->h;
35 ParticleSystem::~ParticleSystem()
37 std::vector<Particle*>::iterator i;
38 for(i = particles.begin(); i != particles.end(); ++i) {
43 void ParticleSystem::draw(float scrollx, float scrolly, int layer)
45 std::vector<Particle*>::iterator i;
46 for(i = particles.begin(); i != particles.end(); ++i) {
47 Particle* particle = *i;
48 if(particle->layer != layer)
51 // remap x,y coordinates onto screencoordinates
52 float x = fmodf(particle->x - scrollx, virtual_width);
53 if(x < 0) x += virtual_width;
54 float y = fmodf(particle->y - scrolly, virtual_height);
55 if(y < 0) y += virtual_height;
56 float xmax = fmodf(x + particle->texture->w, virtual_width);
57 float ymax = fmodf(y + particle->texture->h, virtual_height);
60 if(x >= screen->w && xmax >= screen->w)
62 if(y >= screen->h && ymax >= screen->h)
65 if(x > screen->w) x -= virtual_width;
66 if(y > screen->h) y -= virtual_height;
67 particle->texture->draw(x, y);
71 SnowParticleSystem::SnowParticleSystem()
73 snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", USE_ALPHA);
74 snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", USE_ALPHA);
75 snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", USE_ALPHA);
77 virtual_width = screen->w * 2;
79 // create some random snowflakes
80 size_t snowflakecount = size_t(virtual_width/10.0);
81 for(size_t i=0; i<snowflakecount; ++i) {
82 SnowParticle* particle = new SnowParticle;
83 particle->x = rand() % int(virtual_width);
84 particle->y = rand() % screen->h;
85 particle->layer = i % 2;
86 int snowsize = rand() % 3;
87 particle->texture = snowimages[snowsize];
89 particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
90 } while(particle->speed < 0.01);
91 particle->speed *= World::current()->get_level()->gravity;
93 particles.push_back(particle);
97 SnowParticleSystem::~SnowParticleSystem()
100 delete snowimages[i];
103 void SnowParticleSystem::simulate(float elapsed_time)
105 std::vector<Particle*>::iterator i;
106 for(i = particles.begin(); i != particles.end(); ++i) {
107 SnowParticle* particle = (SnowParticle*) *i;
108 particle->y += particle->speed * elapsed_time;
109 if(particle->y > screen->h) {
110 particle->y = fmodf(particle->y , virtual_height);
111 particle->x = rand() % int(virtual_width);
116 CloudParticleSystem::CloudParticleSystem()
118 cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
120 virtual_width = 2000.0;
122 // create some random clouds
123 for(size_t i=0; i<15; ++i) {
124 CloudParticle* particle = new CloudParticle;
125 particle->x = rand() % int(virtual_width);
126 particle->y = rand() % int(virtual_height);
128 particle->texture = cloudimage;
129 particle->speed = -float(250 + rand() % 200) / 1000.0;
131 particles.push_back(particle);
135 CloudParticleSystem::~CloudParticleSystem()
140 void CloudParticleSystem::simulate(float elapsed_time)
142 std::vector<Particle*>::iterator i;
143 for(i = particles.begin(); i != particles.end(); ++i) {
144 CloudParticle* particle = (CloudParticle*) *i;
145 particle->x += particle->speed * elapsed_time;