try to fix opengl warnings
[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 "camera.h"
29
30 ParticleSystem::ParticleSystem()
31 {
32     virtual_width = screen->w;
33     virtual_height = screen->h;
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_BACKGROUND1);
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         particle->speed *= World::current()->get_level()->gravity;
91
92         particles.push_back(particle);
93     }
94 }
95
96 SnowParticleSystem::~SnowParticleSystem()
97 {
98   for(int i=0;i<3;++i)
99     delete snowimages[i];
100 }
101
102 void SnowParticleSystem::action(float elapsed_time)
103 {
104     std::vector<Particle*>::iterator i;
105     for(i = particles.begin(); i != particles.end(); ++i) {
106         SnowParticle* particle = (SnowParticle*) *i;
107         particle->pos.y += particle->speed * elapsed_time;
108         if(particle->pos.y > screen->h) {
109             particle->pos.y = fmodf(particle->pos.y , virtual_height);
110             particle->pos.x = rand() % int(virtual_width);
111         }
112     }
113 }
114
115 CloudParticleSystem::CloudParticleSystem()
116 {
117     cloudimage = new Surface(datadir + "/images/shared/cloud.png", USE_ALPHA);
118
119     virtual_width = 2000.0;
120
121     // create some random clouds
122     for(size_t i=0; i<15; ++i) {
123         CloudParticle* particle = new CloudParticle;
124         particle->pos.x = rand() % int(virtual_width);
125         particle->pos.y = rand() % int(virtual_height);
126         particle->texture = cloudimage;
127         particle->speed = -float(250 + rand() % 200) / 1000.0;
128
129         particles.push_back(particle);
130     }
131 }
132
133 CloudParticleSystem::~CloudParticleSystem()
134 {
135   delete cloudimage;
136 }
137
138 void CloudParticleSystem::action(float elapsed_time)
139 {
140     std::vector<Particle*>::iterator i;
141     for(i = particles.begin(); i != particles.end(); ++i) {
142         CloudParticle* particle = (CloudParticle*) *i;
143         particle->pos.x += particle->speed * elapsed_time;
144     }
145 }