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"
30 ParticleSystem::ParticleSystem()
32 virtual_width = screen->w;
33 virtual_height = screen->h;
36 ParticleSystem::~ParticleSystem()
38 std::vector<Particle*>::iterator i;
39 for(i = particles.begin(); i != particles.end(); ++i) {
44 void ParticleSystem::draw(DrawingContext& context)
46 float scrollx = context.get_translation().x;
47 float scrolly = context.get_translation().y;
49 context.push_transform();
50 context.set_translation(Vector(0,0));
52 std::vector<Particle*>::iterator i;
53 for(i = particles.begin(); i != particles.end(); ++i) {
54 Particle* particle = *i;
56 // remap x,y coordinates onto screencoordinates
58 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
59 if(pos.x < 0) pos.x += virtual_width;
60 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
61 if(pos.y < 0) pos.y += virtual_height;
63 if(pos.x > screen->w) pos.x -= virtual_width;
64 if(pos.y > screen->h) pos.y -= virtual_height;
65 context.draw_surface(particle->texture, pos, LAYER_BACKGROUND1);
68 context.pop_transform();
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->pos.x = rand() % int(virtual_width);
84 particle->pos.y = rand() % screen->h;
85 int snowsize = rand() % 3;
86 particle->texture = snowimages[snowsize];
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;
92 particles.push_back(particle);
96 SnowParticleSystem::~SnowParticleSystem()
102 void SnowParticleSystem::action(float elapsed_time)
104 std::vector<Particle*>::iterator i;
105 for(i = particles.begin(); i != particles.end(); ++i) {
106 SnowParticle* particle = (SnowParticle*) *i;
107 particle->pos.y += particle->speed * elapsed_time;
108 if(particle->pos.y > screen->h) {
109 particle->pos.y = fmodf(particle->pos.y , virtual_height);
110 particle->pos.x = rand() % int(virtual_width);
115 CloudParticleSystem::CloudParticleSystem()
117 cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
119 virtual_width = 2000.0;
121 // create some random clouds
122 for(size_t i=0; i<15; ++i) {
123 CloudParticle* particle = new CloudParticle;
124 particle->pos.x = rand() % int(virtual_width);
125 particle->pos.y = rand() % int(virtual_height);
126 particle->texture = cloudimage;
127 particle->speed = -float(250 + rand() % 200) / 1000.0;
129 particles.push_back(particle);
133 CloudParticleSystem::~CloudParticleSystem()
138 void CloudParticleSystem::action(float elapsed_time)
140 std::vector<Particle*>::iterator i;
141 for(i = particles.begin(); i != particles.end(); ++i) {
142 CloudParticle* particle = (CloudParticle*) *i;
143 particle->pos.x += particle->speed * elapsed_time;