Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*##############################################################################
- # Triog - 2D Shoot em' up Game #
- # Copyright (C) 2010 Alexandre Díaz Cuadrado #
- # #
- # 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 <http://www.gnu.org/licenses/>. #
- ################################################################################
- ##############################################################################*/
- #include "CParticle.hpp"
- #include "CEngine.hpp"
- #include "../entities/CProyectile.hpp"
- using namespace SPK;
- using namespace SPK::SFML;
- CParticleSystem::CParticleSystem()
- :
- sparkParticleSystemID(SPK::NO_ID),
- smokeParticleSystemID(SPK::NO_ID)
- {
- // random seed
- randomSeed = static_cast<unsigned int>(time(NULL));
- // Sets the update step
- System::setClampStep(true,0.1f); // clamp the step to 100 ms
- System::useAdaptiveStep(0.001f,0.01f); // use an adaptive step from 1ms to 10ms (1000fps to 100fps)
- SFMLRenderer::setZFactor(1.0f);
- setCameraPosition(SPK::SFML::CAMERA_CENTER,SPK::SFML::CAMERA_BOTTOM,static_cast<float>(SCREEN_H),0.0f);
- loadSparkParticleSystem();
- loadSmokeParticleSystem(engine.resources.img_smoke);
- //CProyectile::loadBaseParticleSystem(engine.resources.img_smoke);
- SPKFactory::getInstance().traceAll();
- }
- CParticleSystem::~CParticleSystem()
- {
- SPKFactory::getInstance().traceAll();
- SPKFactory::getInstance().destroyAll();
- SPKFactory::getInstance().traceAll();
- }
- // Creates and register the base particle system for collisions
- void CParticleSystem::loadSparkParticleSystem()
- {
- // Creates the model
- Model* sparkModel = Model::create(
- FLAG_RED | FLAG_GREEN | FLAG_BLUE | FLAG_ALPHA,
- FLAG_ALPHA,
- FLAG_GREEN | FLAG_BLUE);
- sparkModel->setParam(PARAM_RED,1.0f);
- sparkModel->setParam(PARAM_GREEN,0.2f,1.0f);
- sparkModel->setParam(PARAM_BLUE,0.0f,0.2f);
- sparkModel->setParam(PARAM_ALPHA,0.8f,0.0f);
- sparkModel->setLifeTime(0.2f,0.6f);
- // Creates the renderer
- SFMLLineRenderer* sparkRenderer = SFMLLineRenderer::create(0.1f,1.0f);
- sparkRenderer->setBlendMode(sf::Blend::Add);
- sparkRenderer->setGroundCulling(true);
- // Creates the zone
- Sphere* sparkSource = Sphere::create(Vector3D(0.0f,0.0f,10.0f),5.0f);
- // Creates the emitter
- SphericEmitter* sparkEmitter =
- SphericEmitter::create(Vector3D(0.0f,0.0f,1.0f),3.14159f / 4.0f,3.0f * 3.14159f / 4.0f);
- sparkEmitter->setForce(50.0f,150.0f);
- sparkEmitter->setZone(sparkSource);
- sparkEmitter->setTank(25);
- sparkEmitter->setFlow(-1);
- // Creates the Group
- Group* sparkGroup = Group::create(sparkModel,25);
- sparkGroup->setRenderer(sparkRenderer);
- sparkGroup->addEmitter(sparkEmitter);
- sparkGroup->setGravity(Vector3D(0.0f,0.0f,-200.0f));
- sparkGroup->setFriction(2.0f);
- // Creates the System
- SFMLSystem* system = SFMLSystem::create();
- system->addGroup(sparkGroup);
- // Defines which objects will be shared by all systems
- sparkModel->setShared(true);
- sparkRenderer->setShared(true);
- // returns the ID
- sparkParticleSystemID = system->getID();
- }
- void CParticleSystem::loadSmokeParticleSystem(sf::Image& smoke)
- {
- // Creates the model
- SPK::Model* smokeModel = SPK::Model::create(
- SPK::FLAG_SIZE | SPK::FLAG_ALPHA | SPK::FLAG_TEXTURE_INDEX | SPK::FLAG_ANGLE,
- SPK::FLAG_SIZE | SPK::FLAG_ALPHA,
- SPK::FLAG_SIZE | SPK::FLAG_TEXTURE_INDEX | SPK::FLAG_ANGLE);
- smokeModel->setParam(SPK::PARAM_SIZE,5.0f,10.0f,100.0f,200.0f);
- smokeModel->setParam(SPK::PARAM_ALPHA,1.0f,0.0f);
- smokeModel->setParam(SPK::PARAM_TEXTURE_INDEX,0.0f,4.0f);
- smokeModel->setParam(SPK::PARAM_ANGLE,0.0f, M_PI * 2.0f);
- smokeModel->setLifeTime(0.2f,0.6f);
- // Creates the renderer
- SPK::SFML::SFMLQuadRenderer* smokeRenderer = SPK::SFML::SFMLQuadRenderer::create();
- smokeRenderer->setBlendMode(sf::Blend::Alpha);
- smokeRenderer->setImage(&smoke);
- smokeRenderer->setAtlasDimensions(2,2);
- smokeRenderer->setScale(0.875f,0.875f); // optim
- smokeRenderer->setGroundCulling(true);
- // Creates the zones
- SPK::Point* leftTire = SPK::Point::create(Vector3D(0.0f,0.0f,10.0f));
- // Creates the emitters
- SPK::SphericEmitter* leftSmokeEmitter = SPK::SphericEmitter::create(SPK::Vector3D(0.0f,0.0f,1.0f),0.0f,1.1f * M_PI);
- leftSmokeEmitter->setZone(leftTire);
- leftSmokeEmitter->setName("left wheel emitter");
- // Creates the Group
- SPK::Group* smokeGroup = SPK::Group::create(smokeModel,500);
- smokeGroup->setGravity(SPK::Vector3D(0.0f,0.0f,2.0f));
- smokeGroup->setRenderer(smokeRenderer);
- smokeGroup->addEmitter(leftSmokeEmitter);
- smokeGroup->enableSorting(true);
- // Creates the System
- SPK::SFML::SFMLSystem* system = SPK::SFML::SFMLSystem::create(true);
- system->addGroup(smokeGroup);
- // Defines which objects will be shared by all systems
- smokeModel->setShared(true);
- smokeRenderer->setShared(true);
- // Creates the base and gets the ID
- smokeParticleSystemID = system->getID();
- }
- SFMLSystem* CParticleSystem::createParticleSystem(const sf::Vector2f& pos, int particleType)
- {
- SFMLSystem *system;
- if (particleType == PARTICLE_SPARK)
- system = SPK_Copy(SFMLSystem, sparkParticleSystemID);
- else if (particleType == PARTICLE_SMOKE)
- system = SPK_Copy(SFMLSystem, smokeParticleSystemID);
- else
- return 0x0;
- system->SetPosition(pos);
- ParticleSystems.push_back(system);
- return system;
- }
- void CParticleSystem::destroyParticleSystem(SFMLSystem*& system)
- {
- SPK_Destroy(system);
- system = 0x0;
- }
- void CParticleSystem::tick()
- {
- float deltaTime = engine.screen.GetFrameTime();
- std::deque<SFMLSystem*>::iterator it = ParticleSystems.begin();
- while(it != ParticleSystems.end())
- {
- if (!(*it)->update(deltaTime))
- {
- destroyParticleSystem(*it);
- it = ParticleSystems.erase(it);
- }
- else
- ++it;
- }
- for (std::deque<SPK::SFML::SFMLSystem*>::const_iterator it = ParticleSystems.begin(); it != ParticleSystems.end(); ++it)
- engine.screen.Draw(**it);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement