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 #include "display_manager.h"
31 ParticleSystem::ParticleSystem(DisplayManager& displaymanager)
33 virtual_width = screen->w;
34 virtual_height = screen->h;
36 displaymanager.add_drawable(this, LAYER_BACKGROUND1);
39 ParticleSystem::~ParticleSystem()
41 std::vector<Particle*>::iterator i;
42 for(i = particles.begin(); i != particles.end(); ++i) {
47 void ParticleSystem::draw(Camera& viewport, int layer)
49 std::vector<Particle*>::iterator i;
50 for(i = particles.begin(); i != particles.end(); ++i) {
51 Particle* particle = *i;
52 if(particle->layer != layer)
55 float scrollx = viewport.get_translation().x;
56 float scrolly = viewport.get_translation().y;
58 // remap x,y coordinates onto screencoordinates
59 float x = fmodf(particle->x - scrollx, virtual_width);
60 if(x < 0) x += virtual_width;
61 float y = fmodf(particle->y - scrolly, virtual_height);
62 if(y < 0) y += virtual_height;
63 float xmax = fmodf(x + particle->texture->w, virtual_width);
64 float ymax = fmodf(y + particle->texture->h, virtual_height);
67 if(x >= screen->w && xmax >= screen->w)
69 if(y >= screen->h && ymax >= screen->h)
72 if(x > screen->w) x -= virtual_width;
73 if(y > screen->h) y -= virtual_height;
74 particle->texture->draw(x, y);
78 SnowParticleSystem::SnowParticleSystem(DisplayManager& displaymanager)
79 : ParticleSystem(displaymanager)
81 snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", USE_ALPHA);
82 snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", USE_ALPHA);
83 snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", USE_ALPHA);
85 virtual_width = screen->w * 2;
87 // create some random snowflakes
88 size_t snowflakecount = size_t(virtual_width/10.0);
89 for(size_t i=0; i<snowflakecount; ++i) {
90 SnowParticle* particle = new SnowParticle;
91 particle->x = rand() % int(virtual_width);
92 particle->y = rand() % screen->h;
93 particle->layer = LAYER_BACKGROUND1;
94 int snowsize = rand() % 3;
95 particle->texture = snowimages[snowsize];
97 particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
98 } while(particle->speed < 0.01);
99 particle->speed *= World::current()->get_level()->gravity;
101 particles.push_back(particle);
105 SnowParticleSystem::~SnowParticleSystem()
108 delete snowimages[i];
111 void SnowParticleSystem::action(float elapsed_time)
113 std::vector<Particle*>::iterator i;
114 for(i = particles.begin(); i != particles.end(); ++i) {
115 SnowParticle* particle = (SnowParticle*) *i;
116 particle->y += particle->speed * elapsed_time;
117 if(particle->y > screen->h) {
118 particle->y = fmodf(particle->y , virtual_height);
119 particle->x = rand() % int(virtual_width);
124 CloudParticleSystem::CloudParticleSystem(DisplayManager& displaymanager)
125 : ParticleSystem(displaymanager)
127 cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
129 virtual_width = 2000.0;
131 // create some random clouds
132 for(size_t i=0; i<15; ++i) {
133 CloudParticle* particle = new CloudParticle;
134 particle->x = rand() % int(virtual_width);
135 particle->y = rand() % int(virtual_height);
136 particle->layer = LAYER_BACKGROUND1;
137 particle->texture = cloudimage;
138 particle->speed = -float(250 + rand() % 200) / 1000.0;
140 particles.push_back(particle);
144 CloudParticleSystem::~CloudParticleSystem()
149 void CloudParticleSystem::action(float elapsed_time)
151 std::vector<Particle*>::iterator i;
152 for(i = particles.begin(); i != particles.end(); ++i) {
153 CloudParticle* particle = (CloudParticle*) *i;
154 particle->x += particle->speed * elapsed_time;