eliminated global scroll_x and scroll_y variables
[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 "world.h"
26 #include "level.h"
27 #include "scene.h"
28 #include "viewport.h"
29 #include "display_manager.h"
30
31 ParticleSystem::ParticleSystem(DisplayManager& displaymanager)
32 {
33     virtual_width = screen->w;
34     virtual_height = screen->h;
35
36     displaymanager.add_drawable(this, LAYER_BACKGROUND1);
37 }
38
39 ParticleSystem::~ParticleSystem()
40 {
41     std::vector<Particle*>::iterator i;
42     for(i = particles.begin(); i != particles.end(); ++i) {
43         delete *i;
44     }
45 }
46
47 void ParticleSystem::draw(ViewPort& viewport, int layer)
48 {
49     std::vector<Particle*>::iterator i;
50     for(i = particles.begin(); i != particles.end(); ++i) {
51         Particle* particle = *i;
52         if(particle->layer != layer)
53             continue;
54
55         float scrollx = viewport.get_translation().x;
56         float scrolly = viewport.get_translation().y;
57         
58         // remap x,y coordinates onto screencoordinates
59         float x = fmodf(particle->x - scrollx, virtual_width);
60         if(x < 0) x += virtual_width;
61         float y = fmodf(particle->y - scrolly, virtual_height);
62         if(y < 0) y += virtual_height;
63         float xmax = fmodf(x + particle->texture->w, virtual_width);
64         float ymax = fmodf(y + particle->texture->h, virtual_height);
65
66         // particle on screen
67         if(x >= screen->w && xmax >= screen->w)
68             continue;
69         if(y >= screen->h && ymax >= screen->h)
70             continue;
71         
72         if(x > screen->w) x -= virtual_width;
73         if(y > screen->h) y -= virtual_height;
74         particle->texture->draw(x, y);
75     }
76 }
77
78 SnowParticleSystem::SnowParticleSystem(DisplayManager& displaymanager)
79   : ParticleSystem(displaymanager)
80 {
81     snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", USE_ALPHA);
82     snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", USE_ALPHA);
83     snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", USE_ALPHA);
84
85     virtual_width = screen->w * 2;
86
87     // create some random snowflakes
88     size_t snowflakecount = size_t(virtual_width/10.0);
89     for(size_t i=0; i<snowflakecount; ++i) {
90         SnowParticle* particle = new SnowParticle;
91         particle->x = rand() % int(virtual_width);
92         particle->y = rand() % screen->h;
93         particle->layer = LAYER_BACKGROUND1;
94         int snowsize = rand() % 3;
95         particle->texture = snowimages[snowsize];
96         do {
97             particle->speed = snowsize/60.0 + (float(rand()%10)/300.0);
98         } while(particle->speed < 0.01);
99         particle->speed *= World::current()->get_level()->gravity;
100
101         particles.push_back(particle);
102     }
103 }
104
105 SnowParticleSystem::~SnowParticleSystem()
106 {
107   for(int i=0;i<3;++i)
108     delete snowimages[i];
109 }
110
111 void SnowParticleSystem::action(float elapsed_time)
112 {
113     std::vector<Particle*>::iterator i;
114     for(i = particles.begin(); i != particles.end(); ++i) {
115         SnowParticle* particle = (SnowParticle*) *i;
116         particle->y += particle->speed * elapsed_time;
117         if(particle->y > screen->h) {
118             particle->y = fmodf(particle->y , virtual_height);
119             particle->x = rand() % int(virtual_width);
120         }
121     }
122 }
123
124 CloudParticleSystem::CloudParticleSystem(DisplayManager& displaymanager)
125   : ParticleSystem(displaymanager)
126 {
127     cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
128
129     virtual_width = 2000.0;
130
131     // create some random clouds
132     for(size_t i=0; i<15; ++i) {
133         CloudParticle* particle = new CloudParticle;
134         particle->x = rand() % int(virtual_width);
135         particle->y = rand() % int(virtual_height);
136         particle->layer = LAYER_BACKGROUND1;
137         particle->texture = cloudimage;
138         particle->speed = -float(250 + rand() % 200) / 1000.0;
139
140         particles.push_back(particle);
141     }
142 }
143
144 CloudParticleSystem::~CloudParticleSystem()
145 {
146   delete cloudimage;
147 }
148
149 void CloudParticleSystem::action(float elapsed_time)
150 {
151     std::vector<Particle*>::iterator i;
152     for(i = particles.begin(); i != particles.end(); ++i) {
153         CloudParticle* particle = (CloudParticle*) *i;
154         particle->x += particle->speed * elapsed_time;
155     }
156 }