From: Ingo Ruhnke Date: Wed, 18 Nov 2009 01:26:11 +0000 (+0000) Subject: Split object/particlesystem.?pp X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=d9ec4134c6d3a1c444199ba894bf5e9c084185f3;p=supertux.git Split object/particlesystem.?pp SVN-Revision: 6017 --- diff --git a/src/object/cloud_particle_system.cpp b/src/object/cloud_particle_system.cpp new file mode 100644 index 000000000..bde0d110a --- /dev/null +++ b/src/object/cloud_particle_system.cpp @@ -0,0 +1,65 @@ +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "object/cloud_particle_system.hpp" + +#include + +#include "math/random_generator.hpp" +#include "supertux/main.hpp" +#include "video/drawing_context.hpp" + +CloudParticleSystem::CloudParticleSystem() : + ParticleSystem(128), + cloudimage() +{ + cloudimage = new Surface("images/objects/particles/cloud.png"); + + virtual_width = 2000.0; + + // create some random clouds + for(size_t i=0; i<15; ++i) { + CloudParticle* particle = new CloudParticle; + particle->pos.x = systemRandom.rand(static_cast(virtual_width)); + particle->pos.y = systemRandom.rand(static_cast(virtual_height)); + particle->texture = cloudimage; + particle->speed = -systemRandom.randf(25.0, 54.0); + + particles.push_back(particle); + } +} + +void +CloudParticleSystem::parse(const Reader& reader) +{ + reader.get("z-pos", z_pos); +} + +CloudParticleSystem::~CloudParticleSystem() +{ + delete cloudimage; +} + +void CloudParticleSystem::update(float elapsed_time) +{ + std::vector::iterator i; + for(i = particles.begin(); i != particles.end(); ++i) { + CloudParticle* particle = (CloudParticle*) *i; + particle->pos.x += particle->speed * elapsed_time; + } +} + +/* EOF */ diff --git a/src/object/cloud_particle_system.hpp b/src/object/cloud_particle_system.hpp new file mode 100644 index 000000000..236940f4a --- /dev/null +++ b/src/object/cloud_particle_system.hpp @@ -0,0 +1,55 @@ +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_OBJECT_CLOUD_PARTICLE_SYTEM_HPP +#define HEADER_SUPERTUX_OBJECT_CLOUD_PARTICLE_SYTEM_HPP + +#include "object/particlesystem.hpp" + +class CloudParticleSystem : public ParticleSystem +{ +public: + CloudParticleSystem(); + virtual ~CloudParticleSystem(); + + void parse(const Reader& lisp); + + virtual void update(float elapsed_time); + + std::string type() const + { return "CloudParticleSystem"; } + +private: + class CloudParticle : public Particle + { + public: + float speed; + + CloudParticle() : + speed() + {} + }; + + Surface* cloudimage; + +private: + CloudParticleSystem(const CloudParticleSystem&); + CloudParticleSystem& operator=(const CloudParticleSystem&); +}; + +#endif + +/* EOF */ diff --git a/src/object/ghost_particle_system.cpp b/src/object/ghost_particle_system.cpp new file mode 100644 index 000000000..b36e3d14c --- /dev/null +++ b/src/object/ghost_particle_system.cpp @@ -0,0 +1,73 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "object/ghost_particle_system.hpp" + +#include + +#include "math/random_generator.hpp" +#include "supertux/main.hpp" +#include "video/drawing_context.hpp" + +//FIXME: Sometimes both ghosts have the same image +// Ghosts don't change their movement pattern - not random +GhostParticleSystem::GhostParticleSystem() +{ + ghosts[0] = new Surface("images/objects/particles/ghost0.png"); + ghosts[1] = new Surface("images/objects/particles/ghost1.png"); + + virtual_width = SCREEN_WIDTH * 2; + + // create two ghosts + size_t ghostcount = 2; + for(size_t i=0; ipos.x = systemRandom.randf(virtual_width); + particle->pos.y = systemRandom.randf(SCREEN_HEIGHT); + int size = systemRandom.rand(2); + particle->texture = ghosts[size]; + particle->speed = systemRandom.randf(std::max(50, (size * 10)), 180 + (size * 10)); + particles.push_back(particle); + } +} + +void +GhostParticleSystem::parse(const Reader& reader) +{ + reader.get("z-pos", z_pos); +} + +GhostParticleSystem::~GhostParticleSystem() +{ + for(int i=0;i<2;++i) + delete ghosts[i]; +} + +void GhostParticleSystem::update(float elapsed_time) +{ + std::vector::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 = systemRandom.rand(static_cast(virtual_width)); + } + } +} + +/* EOF */ diff --git a/src/object/ghost_particle_system.hpp b/src/object/ghost_particle_system.hpp new file mode 100644 index 000000000..6a85e0455 --- /dev/null +++ b/src/object/ghost_particle_system.hpp @@ -0,0 +1,55 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_OBJECT_GHOST_PARTICLE_SYSTEM_HPP +#define HEADER_SUPERTUX_OBJECT_GHOST_PARTICLE_SYSTEM_HPP + +#include "object/particlesystem.hpp" + +class GhostParticleSystem : public ParticleSystem +{ +public: + GhostParticleSystem(); + virtual ~GhostParticleSystem(); + + void parse(const Reader& lisp); + + virtual void update(float elapsed_time); + + std::string type() const + { return "GhostParticleSystem"; } + +private: + class GhostParticle : public Particle + { + public: + float speed; + + GhostParticle() : + speed() + {} + }; + + Surface* ghosts[2]; + +private: + GhostParticleSystem(const GhostParticleSystem&); + GhostParticleSystem& operator=(const GhostParticleSystem&); +}; + +#endif + +/* EOF */ diff --git a/src/object/particlesystem.cpp b/src/object/particlesystem.cpp index c6ad03cb7..3289f6da7 100644 --- a/src/object/particlesystem.cpp +++ b/src/object/particlesystem.cpp @@ -72,151 +72,4 @@ void ParticleSystem::draw(DrawingContext& context) context.pop_transform(); } -SnowParticleSystem::SnowParticleSystem() -{ - snowimages[0] = new Surface("images/objects/particles/snow2.png"); - snowimages[1] = new Surface("images/objects/particles/snow1.png"); - snowimages[2] = new Surface("images/objects/particles/snow0.png"); - - virtual_width = SCREEN_WIDTH * 2; - - // create some random snowflakes - size_t snowflakecount = size_t(virtual_width/10.0); - for(size_t i=0; ipos.x = systemRandom.randf(virtual_width); - particle->pos.y = systemRandom.randf(SCREEN_HEIGHT); - particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16); - particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3; - particle->wobble = 0.0; - - particle->texture = snowimages[snowsize]; - - particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8); - particle->speed *= 20; // gravity - - particles.push_back(particle); - } -} - -void -SnowParticleSystem::parse(const Reader& reader) -{ - reader.get("z-pos", z_pos); -} - -SnowParticleSystem::~SnowParticleSystem() -{ - for(int i=0;i<3;++i) - delete snowimages[i]; -} - -void SnowParticleSystem::update(float elapsed_time) -{ - std::vector::iterator i; - - for(i = particles.begin(); i != particles.end(); ++i) { - SnowParticle* particle = (SnowParticle*) *i; - float anchor_delta; - - particle->pos.y += particle->speed * elapsed_time; - particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/; - - anchor_delta = (particle->anchorx - particle->pos.x); - particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5); - particle->wobble *= 0.99f; - particle->anchorx += particle->drift_speed * elapsed_time; - } -} - -//FIXME: Sometimes both ghosts have the same image -// Ghosts don't change their movement pattern - not random -GhostParticleSystem::GhostParticleSystem() -{ - ghosts[0] = new Surface("images/objects/particles/ghost0.png"); - ghosts[1] = new Surface("images/objects/particles/ghost1.png"); - - virtual_width = SCREEN_WIDTH * 2; - - // create two ghosts - size_t ghostcount = 2; - for(size_t i=0; ipos.x = systemRandom.randf(virtual_width); - particle->pos.y = systemRandom.randf(SCREEN_HEIGHT); - int size = systemRandom.rand(2); - particle->texture = ghosts[size]; - particle->speed = systemRandom.randf(std::max(50, (size * 10)), 180 + (size * 10)); - particles.push_back(particle); - } -} - -void -GhostParticleSystem::parse(const Reader& reader) -{ - reader.get("z-pos", z_pos); -} - -GhostParticleSystem::~GhostParticleSystem() -{ - for(int i=0;i<2;++i) - delete ghosts[i]; -} - -void GhostParticleSystem::update(float elapsed_time) -{ - std::vector::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 = systemRandom.rand(static_cast(virtual_width)); - } - } -} - -CloudParticleSystem::CloudParticleSystem() : - ParticleSystem(128), - cloudimage() -{ - cloudimage = new Surface("images/objects/particles/cloud.png"); - - virtual_width = 2000.0; - - // create some random clouds - for(size_t i=0; i<15; ++i) { - CloudParticle* particle = new CloudParticle; - particle->pos.x = systemRandom.rand(static_cast(virtual_width)); - particle->pos.y = systemRandom.rand(static_cast(virtual_height)); - particle->texture = cloudimage; - particle->speed = -systemRandom.randf(25.0, 54.0); - - particles.push_back(particle); - } -} - -void -CloudParticleSystem::parse(const Reader& reader) -{ - reader.get("z-pos", z_pos); -} - -CloudParticleSystem::~CloudParticleSystem() -{ - delete cloudimage; -} - -void CloudParticleSystem::update(float elapsed_time) -{ - std::vector::iterator i; - for(i = particles.begin(); i != particles.end(); ++i) { - CloudParticle* particle = (CloudParticle*) *i; - particle->pos.x += particle->speed * elapsed_time; - } -} - /* EOF */ diff --git a/src/object/particlesystem.hpp b/src/object/particlesystem.hpp index 38eac9526..32259b16c 100644 --- a/src/object/particlesystem.hpp +++ b/src/object/particlesystem.hpp @@ -24,7 +24,6 @@ #include "util/reader.hpp" class Surface; - class DisplayManager; /** @@ -77,105 +76,6 @@ protected: float virtual_height; }; -class SnowParticleSystem : public ParticleSystem -{ -public: - SnowParticleSystem(); - virtual ~SnowParticleSystem(); - - void parse(const Reader& lisp); - - virtual void update(float elapsed_time); - - std::string type() const - { return "SnowParticleSystem"; } - -private: - class SnowParticle : public Particle - { - public: - float speed; - float wobble; - float anchorx; - float drift_speed; - - SnowParticle() : - speed(), - wobble(), - anchorx(), - drift_speed() - {} - }; - - Surface* snowimages[3]; - -private: - SnowParticleSystem(const SnowParticleSystem&); - SnowParticleSystem& operator=(const SnowParticleSystem&); -}; - -class GhostParticleSystem : public ParticleSystem -{ -public: - GhostParticleSystem(); - virtual ~GhostParticleSystem(); - - void parse(const Reader& lisp); - - virtual void update(float elapsed_time); - - std::string type() const - { return "GhostParticleSystem"; } - -private: - class GhostParticle : public Particle - { - public: - float speed; - - GhostParticle() : - speed() - {} - }; - - Surface* ghosts[2]; - -private: - GhostParticleSystem(const GhostParticleSystem&); - GhostParticleSystem& operator=(const GhostParticleSystem&); -}; - -class CloudParticleSystem : public ParticleSystem -{ -public: - CloudParticleSystem(); - virtual ~CloudParticleSystem(); - - void parse(const Reader& lisp); - - virtual void update(float elapsed_time); - - std::string type() const - { return "CloudParticleSystem"; } - -private: - class CloudParticle : public Particle - { - public: - float speed; - - CloudParticle() : - speed() - {} - }; - - Surface* cloudimage; - -private: - CloudParticleSystem(const CloudParticleSystem&); - CloudParticleSystem& operator=(const CloudParticleSystem&); -}; - #endif /* EOF */ diff --git a/src/object/snow_particle_system.cpp b/src/object/snow_particle_system.cpp new file mode 100644 index 000000000..a563fa5af --- /dev/null +++ b/src/object/snow_particle_system.cpp @@ -0,0 +1,84 @@ +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "object/snow_particle_system.hpp" + +#include + +#include "math/random_generator.hpp" +#include "supertux/main.hpp" +#include "video/drawing_context.hpp" + +SnowParticleSystem::SnowParticleSystem() +{ + snowimages[0] = new Surface("images/objects/particles/snow2.png"); + snowimages[1] = new Surface("images/objects/particles/snow1.png"); + snowimages[2] = new Surface("images/objects/particles/snow0.png"); + + virtual_width = SCREEN_WIDTH * 2; + + // create some random snowflakes + size_t snowflakecount = size_t(virtual_width/10.0); + for(size_t i=0; ipos.x = systemRandom.randf(virtual_width); + particle->pos.y = systemRandom.randf(SCREEN_HEIGHT); + particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16); + particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3; + particle->wobble = 0.0; + + particle->texture = snowimages[snowsize]; + + particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8); + particle->speed *= 20; // gravity + + particles.push_back(particle); + } +} + +void +SnowParticleSystem::parse(const Reader& reader) +{ + reader.get("z-pos", z_pos); +} + +SnowParticleSystem::~SnowParticleSystem() +{ + for(int i=0;i<3;++i) + delete snowimages[i]; +} + +void SnowParticleSystem::update(float elapsed_time) +{ + std::vector::iterator i; + + for(i = particles.begin(); i != particles.end(); ++i) { + SnowParticle* particle = (SnowParticle*) *i; + float anchor_delta; + + particle->pos.y += particle->speed * elapsed_time; + particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/; + + anchor_delta = (particle->anchorx - particle->pos.x); + particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5); + particle->wobble *= 0.99f; + particle->anchorx += particle->drift_speed * elapsed_time; + } +} + +/* EOF */ diff --git a/src/object/snow_particle_system.hpp b/src/object/snow_particle_system.hpp new file mode 100644 index 000000000..7c5510d43 --- /dev/null +++ b/src/object/snow_particle_system.hpp @@ -0,0 +1,61 @@ +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_OBJECT_SNOW_PARTICLE_SYSTEM_HPP +#define HEADER_SUPERTUX_OBJECT_SNOW_PARTICLE_SYSTEM_HPP + +#include "object/particlesystem.hpp" + +class SnowParticleSystem : public ParticleSystem +{ +public: + SnowParticleSystem(); + virtual ~SnowParticleSystem(); + + void parse(const Reader& lisp); + + virtual void update(float elapsed_time); + + std::string type() const + { return "SnowParticleSystem"; } + +private: + class SnowParticle : public Particle + { + public: + float speed; + float wobble; + float anchorx; + float drift_speed; + + SnowParticle() : + speed(), + wobble(), + anchorx(), + drift_speed() + {} + }; + + Surface* snowimages[3]; + +private: + SnowParticleSystem(const SnowParticleSystem&); + SnowParticleSystem& operator=(const SnowParticleSystem&); +}; + +#endif + +/* EOF */ diff --git a/src/supertux/sector.cpp b/src/supertux/sector.cpp index 0c9a2ae6e..0ca660195 100644 --- a/src/supertux/sector.cpp +++ b/src/supertux/sector.cpp @@ -31,6 +31,9 @@ #include "object/gradient.hpp" #include "object/invisible_block.hpp" #include "object/particlesystem.hpp" +#include "object/cloud_particle_system.hpp" +#include "object/ghost_particle_system.hpp" +#include "object/snow_particle_system.hpp" #include "object/particlesystem_interactive.hpp" #include "object/player.hpp" #include "object/portable.hpp"