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.
26 #include "particlesystem.h"
27 #include "app/globals.h"
28 #include "utils/lispreader.h"
29 #include "utils/lispwriter.h"
30 #include "video/drawing_context.h"
32 ParticleSystem::ParticleSystem()
34 virtual_width = screen->w;
35 virtual_height = screen->h;
36 layer = LAYER_BACKGROUND1;
39 ParticleSystem::~ParticleSystem()
41 std::vector<Particle*>::iterator i;
42 for(i = particles.begin(); i != particles.end(); ++i) {
47 void ParticleSystem::draw(DrawingContext& context)
49 float scrollx = context.get_translation().x;
50 float scrolly = context.get_translation().y;
52 context.push_transform();
53 context.set_translation(Vector(0,0));
55 std::vector<Particle*>::iterator i;
56 for(i = particles.begin(); i != particles.end(); ++i) {
57 Particle* particle = *i;
59 // remap x,y coordinates onto screencoordinates
61 pos.x = fmodf(particle->pos.x - scrollx, virtual_width);
62 if(pos.x < 0) pos.x += virtual_width;
63 pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
64 if(pos.y < 0) pos.y += virtual_height;
66 if(pos.x > screen->w) pos.x -= virtual_width;
67 if(pos.y > screen->h) pos.y -= virtual_height;
68 context.draw_surface(particle->texture, pos, layer);
71 context.pop_transform();
74 SnowParticleSystem::SnowParticleSystem()
76 snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", true);
77 snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", true);
78 snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", true);
80 virtual_width = screen->w * 2;
82 // create some random snowflakes
83 size_t snowflakecount = size_t(virtual_width/10.0);
84 for(size_t i=0; i<snowflakecount; ++i) {
85 SnowParticle* particle = new SnowParticle;
86 particle->pos.x = rand() % int(virtual_width);
87 particle->pos.y = rand() % screen->h;
88 int snowsize = rand() % 3;
89 particle->texture = snowimages[snowsize];
91 particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
92 } while(particle->speed < 0.01);
94 particles.push_back(particle);
99 SnowParticleSystem::parse(LispReader& reader)
101 reader.read_int("layer", layer);
105 SnowParticleSystem::write(LispWriter& writer)
107 writer.start_list("particles-snow");
108 writer.write_int("layer", layer);
109 writer.end_list("particles-snow");
112 SnowParticleSystem::~SnowParticleSystem()
115 delete snowimages[i];
118 void SnowParticleSystem::action(float elapsed_time)
120 std::vector<Particle*>::iterator i;
121 for(i = particles.begin(); i != particles.end(); ++i) {
122 SnowParticle* particle = (SnowParticle*) *i;
123 particle->pos.y += particle->speed * elapsed_time;
124 if(particle->pos.y > screen->h) {
125 particle->pos.y = fmodf(particle->pos.y , virtual_height);
126 particle->pos.x = rand() % int(virtual_width);
131 CloudParticleSystem::CloudParticleSystem()
133 cloudimage = new Surface(datadir + "/images/shared/cloud.png", true);
135 virtual_width = 2000.0;
137 // create some random clouds
138 for(size_t i=0; i<15; ++i) {
139 CloudParticle* particle = new CloudParticle;
140 particle->pos.x = rand() % int(virtual_width);
141 particle->pos.y = rand() % int(virtual_height);
142 particle->texture = cloudimage;
143 particle->speed = -float(250 + rand() % 200) / 1000.0;
145 particles.push_back(particle);
150 CloudParticleSystem::parse(LispReader& reader)
152 reader.read_int("layer", layer);
156 CloudParticleSystem::write(LispWriter& writer)
158 writer.start_list("particles-clouds");
159 writer.write_int("layer", layer);
160 writer.end_list("particles-clouds");
163 CloudParticleSystem::~CloudParticleSystem()
168 void CloudParticleSystem::action(float elapsed_time)
170 std::vector<Particle*>::iterator i;
171 for(i = particles.begin(); i != particles.end(); ++i) {
172 CloudParticle* particle = (CloudParticle*) *i;
173 particle->pos.x += particle->speed * elapsed_time;