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.
24 #include "particlesystem.h"
26 #include "lispreader.h"
27 #include "lispwriter.h"
28 #include "screen/drawing_context.h"
30 ParticleSystem::ParticleSystem()
32 virtual_width = screen->w;
33 virtual_height = screen->h;
34 layer = LAYER_BACKGROUND1;
37 ParticleSystem::~ParticleSystem()
39 std::vector<Particle*>::iterator i;
40 for(i = particles.begin(); i != particles.end(); ++i) {
45 void ParticleSystem::draw(DrawingContext& context)
47 float scrollx = context.get_translation().x;
48 float scrolly = context.get_translation().y;
50 context.push_transform();
51 context.set_translation(Vector(0,0));
53 std::vector<Particle*>::iterator i;
54 for(i = particles.begin(); i != particles.end(); ++i) {
55 Particle* particle = *i;
57 // remap x,y coordinates onto screencoordinates
59 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
60 if(pos.x < 0) pos.x += virtual_width;
61 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
62 if(pos.y < 0) pos.y += virtual_height;
64 if(pos.x > screen->w) pos.x -= virtual_width;
65 if(pos.y > screen->h) pos.y -= virtual_height;
66 context.draw_surface(particle->texture, pos, layer);
69 context.pop_transform();
72 SnowParticleSystem::SnowParticleSystem()
74 snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", USE_ALPHA);
75 snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", USE_ALPHA);
76 snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", USE_ALPHA);
78 virtual_width = screen->w * 2;
80 // create some random snowflakes
81 size_t snowflakecount = size_t(virtual_width/10.0);
82 for(size_t i=0; i<snowflakecount; ++i) {
83 SnowParticle* particle = new SnowParticle;
84 particle->pos.x = rand() % int(virtual_width);
85 particle->pos.y = rand() % screen->h;
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);
92 particles.push_back(particle);
97 SnowParticleSystem::parse(LispReader& reader)
99 reader.read_int("layer", layer);
103 SnowParticleSystem::write(LispWriter& writer)
105 writer.start_list("particles-snow");
106 writer.write_int("layer", layer);
107 writer.end_list("particles-snow");
110 SnowParticleSystem::~SnowParticleSystem()
113 delete snowimages[i];
116 void SnowParticleSystem::action(float elapsed_time)
118 std::vector<Particle*>::iterator i;
119 for(i = particles.begin(); i != particles.end(); ++i) {
120 SnowParticle* particle = (SnowParticle*) *i;
121 particle->pos.y += particle->speed * elapsed_time;
122 if(particle->pos.y > screen->h) {
123 particle->pos.y = fmodf(particle->pos.y , virtual_height);
124 particle->pos.x = rand() % int(virtual_width);
129 CloudParticleSystem::CloudParticleSystem()
131 cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
133 virtual_width = 2000.0;
135 // create some random clouds
136 for(size_t i=0; i<15; ++i) {
137 CloudParticle* particle = new CloudParticle;
138 particle->pos.x = rand() % int(virtual_width);
139 particle->pos.y = rand() % int(virtual_height);
140 particle->texture = cloudimage;
141 particle->speed = -float(250 + rand() % 200) / 1000.0;
143 particles.push_back(particle);
148 CloudParticleSystem::parse(LispReader& reader)
150 reader.read_int("layer", layer);
154 CloudParticleSystem::write(LispWriter& writer)
156 writer.start_list("particles-clouds");
157 writer.write_int("layer", layer);
158 writer.end_list("particles-clouds");
161 CloudParticleSystem::~CloudParticleSystem()
166 void CloudParticleSystem::action(float elapsed_time)
168 std::vector<Particle*>::iterator i;
169 for(i = particles.begin(); i != particles.end(); ++i) {
170 CloudParticle* particle = (CloudParticle*) *i;
171 particle->pos.x += particle->speed * elapsed_time;