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"
25 #include "lispreader.h"
26 #include "lispwriter.h"
27 #include "screen/drawing_context.h"
29 ParticleSystem::ParticleSystem()
31 virtual_width = screen->w;
32 virtual_height = screen->h;
33 layer = LAYER_BACKGROUND1;
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);
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);
91 particles.push_back(particle);
96 SnowParticleSystem::parse(LispReader& reader)
98 reader.read_int("layer", layer);
102 SnowParticleSystem::write(LispWriter& writer)
104 writer.start_list("particles-snow");
105 writer.write_int("layer", layer);
106 writer.end_list("particles-snow");
109 SnowParticleSystem::~SnowParticleSystem()
112 delete snowimages[i];
115 void SnowParticleSystem::action(float elapsed_time)
117 std::vector<Particle*>::iterator i;
118 for(i = particles.begin(); i != particles.end(); ++i) {
119 SnowParticle* particle = (SnowParticle*) *i;
120 particle->pos.y += particle->speed * elapsed_time;
121 if(particle->pos.y > screen->h) {
122 particle->pos.y = fmodf(particle->pos.y , virtual_height);
123 particle->pos.x = rand() % int(virtual_width);
128 CloudParticleSystem::CloudParticleSystem()
130 cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
132 virtual_width = 2000.0;
134 // create some random clouds
135 for(size_t i=0; i<15; ++i) {
136 CloudParticle* particle = new CloudParticle;
137 particle->pos.x = rand() % int(virtual_width);
138 particle->pos.y = rand() % int(virtual_height);
139 particle->texture = cloudimage;
140 particle->speed = -float(250 + rand() % 200) / 1000.0;
142 particles.push_back(particle);
147 CloudParticleSystem::parse(LispReader& reader)
149 reader.read_int("layer", layer);
153 CloudParticleSystem::write(LispWriter& writer)
155 writer.start_list("particles-clouds");
156 writer.write_int("layer", layer);
157 writer.end_list("particles-clouds");
160 CloudParticleSystem::~CloudParticleSystem()
165 void CloudParticleSystem::action(float elapsed_time)
167 std::vector<Particle*>::iterator i;
168 for(i = particles.begin(); i != particles.end(); ++i) {
169 CloudParticle* particle = (CloudParticle*) *i;
170 particle->pos.x += particle->speed * elapsed_time;