a first implementation of doors to switch between sectors
[supertux.git] / src / particlesystem.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 // 
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.
19
20 #include "particlesystem.h"
21
22 #include <iostream>
23 #include <math.h>
24 #include "globals.h"
25 #include "lispreader.h"
26 #include "lispwriter.h"
27 #include "screen/drawing_context.h"
28
29 ParticleSystem::ParticleSystem()
30 {
31     virtual_width = screen->w;
32     virtual_height = screen->h;
33     layer = LAYER_BACKGROUND1;
34 }
35
36 ParticleSystem::~ParticleSystem()
37 {
38     std::vector<Particle*>::iterator i;
39     for(i = particles.begin(); i != particles.end(); ++i) {
40         delete *i;
41     }
42 }
43
44 void ParticleSystem::draw(DrawingContext& context)
45 {
46   float scrollx = context.get_translation().x;
47   float scrolly = context.get_translation().y;
48
49   context.push_transform();
50   context.set_translation(Vector(0,0));
51   
52     std::vector<Particle*>::iterator i;
53     for(i = particles.begin(); i != particles.end(); ++i) {
54         Particle* particle = *i;
55
56         // remap x,y coordinates onto screencoordinates
57         Vector pos;
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;
62
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);
66     }
67
68     context.pop_transform();
69 }
70
71 SnowParticleSystem::SnowParticleSystem()
72 {
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);
76
77     virtual_width = screen->w * 2;
78
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];
87         do {
88             particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
89         } while(particle->speed < 0.01);
90
91         particles.push_back(particle);
92     }
93 }
94
95 void
96 SnowParticleSystem::parse(LispReader& reader)
97 {
98   reader.read_int("layer", layer);
99 }
100
101 void
102 SnowParticleSystem::write(LispWriter& writer)
103 {
104   writer.start_list("particles-snow");
105   writer.write_int("layer", layer);
106   writer.end_list("particles-snow");
107 }
108
109 SnowParticleSystem::~SnowParticleSystem()
110 {
111   for(int i=0;i<3;++i)
112     delete snowimages[i];
113 }
114
115 void SnowParticleSystem::action(float elapsed_time)
116 {
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);
124         }
125     }
126 }
127
128 CloudParticleSystem::CloudParticleSystem()
129 {
130     cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
131
132     virtual_width = 2000.0;
133
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;
141
142         particles.push_back(particle);
143     }
144 }
145
146 void
147 CloudParticleSystem::parse(LispReader& reader)
148 {
149   reader.read_int("layer", layer);
150 }
151
152 void
153 CloudParticleSystem::write(LispWriter& writer)
154 {
155   writer.start_list("particles-clouds");
156   writer.write_int("layer", layer);
157   writer.end_list("particles-clouds");
158 }
159
160 CloudParticleSystem::~CloudParticleSystem()
161 {
162   delete cloudimage;
163 }
164
165 void CloudParticleSystem::action(float elapsed_time)
166 {
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;
171     }
172 }