created a ghost forest demo from contrib art:
[supertux.git] / src / object / particlesystem.cpp
index 4c24b1f..929c2bd 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
-
-
 #include <config.h>
 
 #include <iostream>
 #include <cmath>
 
 #include "particlesystem.h"
-#include "app/globals.h"
 #include "video/drawing_context.h"
 #include "lisp/parser.h"
 #include "lisp/lisp.h"
 #include "lisp/writer.h"
+#include "resources.h"
+#include "main.h"
 
 ParticleSystem::ParticleSystem()
 {
-    virtual_width = screen->w;
-    virtual_height = screen->h;
+    virtual_width = SCREEN_WIDTH;
+    virtual_height = SCREEN_HEIGHT;
     layer = LAYER_BACKGROUND1;
 }
 
@@ -64,8 +63,8 @@ void ParticleSystem::draw(DrawingContext& context)
         pos.y = fmodf(particle->pos.y - scrolly, virtual_height);
         if(pos.y < 0) pos.y += virtual_height;
 
-        if(pos.x > screen->w) pos.x -= virtual_width;
-        if(pos.y > screen->h) pos.y -= virtual_height;
+        if(pos.x > SCREEN_WIDTH) pos.x -= virtual_width;
+        if(pos.y > SCREEN_HEIGHT) pos.y -= virtual_height;
         context.draw_surface(particle->texture, pos, layer);
     }
 
@@ -74,18 +73,18 @@ void ParticleSystem::draw(DrawingContext& context)
 
 SnowParticleSystem::SnowParticleSystem()
 {
-    snowimages[0] = new Surface(datadir+"/images/shared/snow0.png", true);
-    snowimages[1] = new Surface(datadir+"/images/shared/snow1.png", true);
-    snowimages[2] = new Surface(datadir+"/images/shared/snow2.png", true);
+    snowimages[0] = new Surface(datadir+"/images/objects/particles/snow0.png", true);
+    snowimages[1] = new Surface(datadir+"/images/objects/particles/snow1.png", true);
+    snowimages[2] = new Surface(datadir+"/images/objects/particles/snow2.png", true);
 
-    virtual_width = screen->w * 2;
+    virtual_width = SCREEN_WIDTH * 2;
 
     // create some random snowflakes
     size_t snowflakecount = size_t(virtual_width/10.0);
     for(size_t i=0; i<snowflakecount; ++i) {
         SnowParticle* particle = new SnowParticle;
         particle->pos.x = rand() % int(virtual_width);
-        particle->pos.y = rand() % screen->h;
+        particle->pos.y = rand() % SCREEN_HEIGHT;
         int snowsize = rand() % 3;
         particle->texture = snowimages[snowsize];
         do {
@@ -117,13 +116,70 @@ SnowParticleSystem::~SnowParticleSystem()
     delete snowimages[i];
 }
 
-void SnowParticleSystem::action(float elapsed_time)
+void SnowParticleSystem::update(float elapsed_time)
 {
     std::vector<Particle*>::iterator i;
     for(i = particles.begin(); i != particles.end(); ++i) {
         SnowParticle* particle = (SnowParticle*) *i;
         particle->pos.y += particle->speed * elapsed_time;
-        if(particle->pos.y > screen->h) {
+        if(particle->pos.y > SCREEN_HEIGHT) {
+            particle->pos.y = fmodf(particle->pos.y , virtual_height);
+            particle->pos.x = rand() % int(virtual_width);
+        }
+    }
+}
+
+GhostParticleSystem::GhostParticleSystem()
+{
+    ghosts[0] = new Surface(datadir+"/images/objects/particles/ghost0.png", true);
+    ghosts[1] = new Surface(datadir+"/images/objects/particles/ghost1.png", true);
+
+    virtual_width = SCREEN_WIDTH * 2;
+
+    // create some random snowflakes
+    size_t ghostcount = 2;
+    for(size_t i=0; i<ghostcount; ++i) {
+        GhostParticle* particle = new GhostParticle;
+        particle->pos.x = rand() % int(virtual_width);
+        particle->pos.y = rand() % SCREEN_HEIGHT;
+        int size = rand() % 2;
+        particle->texture = ghosts[size];
+        do {
+            particle->speed = size*.2 + (float(rand()%10)*.4);
+        } while(particle->speed < 1);
+        particle->speed *= 50;
+        particles.push_back(particle);
+    }
+}
+
+void
+GhostParticleSystem::parse(const lisp::Lisp& reader)
+{
+  reader.get("layer", layer);
+}
+
+void
+GhostParticleSystem::write(lisp::Writer& writer)
+{
+  writer.start_list("particles-ghost");
+  writer.write_int("layer", layer);
+  writer.end_list("particles-ghost");
+}
+
+GhostParticleSystem::~GhostParticleSystem()
+{
+  for(int i=0;i<2;++i)
+    delete ghosts[i];
+}
+
+void GhostParticleSystem::update(float elapsed_time)
+{
+    std::vector<Particle*>::iterator i;
+    for(i = particles.begin(); i != particles.end(); ++i) {
+        GhostParticle* particle = (GhostParticle*) *i;
+        particle->pos.y -= particle->speed * elapsed_time;
+        particle->pos.x -= particle->speed * elapsed_time;
+        if(particle->pos.y > SCREEN_HEIGHT) {
             particle->pos.y = fmodf(particle->pos.y , virtual_height);
             particle->pos.x = rand() % int(virtual_width);
         }
@@ -132,7 +188,7 @@ void SnowParticleSystem::action(float elapsed_time)
 
 CloudParticleSystem::CloudParticleSystem()
 {
-    cloudimage = new Surface(datadir + "/images/shared/cloud.png", true);
+    cloudimage = new Surface(datadir + "/images/objects/particles/cloud.png", true);
 
     virtual_width = 2000.0;
 
@@ -167,7 +223,7 @@ CloudParticleSystem::~CloudParticleSystem()
   delete cloudimage;
 }
 
-void CloudParticleSystem::action(float elapsed_time)
+void CloudParticleSystem::update(float elapsed_time)
 {
     std::vector<Particle*>::iterator i;
     for(i = particles.begin(); i != particles.end(); ++i) {