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