init particles, still not working

This commit is contained in:
Henrique Santiago 2011-12-07 17:58:09 -02:00
parent d4a6308705
commit 262f54ce3b
7 changed files with 204 additions and 0 deletions

View File

@ -142,6 +142,8 @@ SET(framework_SOURCES ${framework_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/graphics/texturemanager.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/texturemanager.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/borderimage.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/borderimage.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/image.cpp ${CMAKE_CURRENT_LIST_DIR}/graphics/image.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/particlesmanager.cpp
${CMAKE_CURRENT_LIST_DIR}/graphics/particlessystem.cpp
# framework otml # framework otml
${CMAKE_CURRENT_LIST_DIR}/otml/otmldocument.cpp ${CMAKE_CURRENT_LIST_DIR}/otml/otmldocument.cpp

View File

@ -31,6 +31,7 @@
#include <framework/ui/uimanager.h> #include <framework/ui/uimanager.h>
#include <framework/ui/uiwidget.h> #include <framework/ui/uiwidget.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <framework/graphics/particlesmanager.h>
#include <framework/luascript/luainterface.h> #include <framework/luascript/luainterface.h>
Application *g_app = nullptr; Application *g_app = nullptr;
@ -207,12 +208,16 @@ void Application::poll()
// poll application genareted events // poll application genareted events
g_dispatcher.poll(); g_dispatcher.poll();
g_particlesManager.update();
} }
void Application::render() void Application::render()
{ {
// everything is rendered by UI components // everything is rendered by UI components
g_ui.render(); g_ui.render();
g_particlesManager.render();
} }
void Application::resize(const Size& size) void Application::resize(const Size& size)

View File

@ -0,0 +1,21 @@
#include "particlesmanager.h"
ParticlesManager g_particlesManager;
void ParticlesManager::add(ParticlesSystem particleSystem)
{
// check it has emitters
m_particlesSystems.push_back(particleSystem);
}
void ParticlesManager::render()
{
for(auto it = m_particlesSystems.begin(), end = m_particlesSystems.end(); it != end; ++it)
(*it).render();
}
void ParticlesManager::update()
{
for(auto it = m_particlesSystems.begin(), end = m_particlesSystems.end(); it != end; ++it)
(*it).update();
}

View File

@ -0,0 +1,14 @@
#include "particlessystem.h"
class ParticlesManager {
public:
void add(ParticlesSystem particleSystem);
void render();
void update();
private:
std::list<ParticlesSystem> m_particlesSystems;
};
extern ParticlesManager g_particlesManager;

View File

@ -0,0 +1,75 @@
#include "particlessystem.h"
#include "graphics.h"
#include <framework/core/clock.h>
Particle::Particle(const Rect& rect, float vx, float vy, float ax, float ay, const Color& color, TexturePtr texture)
{
m_rect = rect;
m_vx = vx; m_vy = vy;
m_ax = ax; m_ay = ay;
m_color = color;
m_texture = texture;
m_finished = false;
}
void Particle::render()
{
g_graphics.bindColor(m_color);
if(!m_texture)
g_graphics.drawFilledRect(m_rect);
else
g_graphics.drawTexturedRect(m_rect, m_texture);
}
Emitter::Emitter(const Point& position, int duration, int particlesPerSecond)
{
m_position = position;
m_duration = duration;
m_particlesPerSecond = particlesPerSecond;
m_createdParticles = 0;
m_startTicks = g_clock.ticks();
m_finished = false;
}
void Emitter::render()
{
for(auto it = m_particles.begin(), end = m_particles.end(); it != end; ++it)
(*it).render();
}
void Emitter::update()
{
ticks_t elapsedTicks = g_clock.ticks() - m_startTicks;
// check if finished
if(m_duration > 0 && elapsedTicks > m_duration * 1000)
m_finished = true;
// create some particles
int currentParticles = elapsedTicks / 1000.0 * m_particlesPerSecond;
for(int i = m_createdParticles; i < currentParticles; ++i) {
Particle particle = Particle(Rect(100, 100, 16, 16), 0, 0, 0, 0);
m_particles.push_back(particle);
}
}
void ParticlesSystem::add(Emitter emitter)
{
m_emitters.push_back(emitter);
}
void ParticlesSystem::render()
{
for(auto it = m_emitters.begin(), end = m_emitters.end(); it != end; ++it)
(*it).render();
}
void ParticlesSystem::update()
{
for(auto it = m_emitters.begin(), end = m_emitters.end(); it != end;) {
Emitter emitter = *it;
emitter.update();
++it;
}
}

View File

@ -0,0 +1,78 @@
#include <framework/global.h>
#include <framework/graphics/texture.h>
struct Particle {
public:
Particle(const Rect& rect, float vx, float vy, float ax, float ay, const Color& color = Color(255, 255, 255), TexturePtr texture = nullptr);
void render();
private:
Rect m_rect;
float m_vx, m_vy;
float m_ax, m_ay;
Color m_color;
TexturePtr m_texture;
bool m_finished;
};
class Emitter {
public:
Emitter(const Point& position, int duration, int particlesPerSecond);
void render();
void update();
private:
// self related
Point m_position;
int m_duration;
ticks_t m_startTicks;
bool m_finished;
int m_particlesPerSecond, m_createdParticles;
std::list<Particle> m_particles;
// particles initial position related to emitter position
float positionMinRadius, positionMaxRadius;
float positionMinAngle, positionMaxAngle;
// particles initial velocity
float minVelocity, maxVelocity;
float minVelocityAngle, maxVelocityAngle;
// particles initial acceleration
float minAcceleration, maxAcceleration;
float minAccelerationAngle, maxAccelerationAngle;
// color ralated
Color color;
// texture related
};
class Affector {
public:
virtual void update() {};
};
class Gravity270Affector : public Affector {
public:
void update() {
}
};
class ParticlesSystem {
public:
void add(Emitter emitter);
void render();
void update();
private:
std::list<Emitter> m_emitters;
std::list<Affector> m_affectors;
};

View File

@ -32,6 +32,7 @@
#include <otclient/core/tile.h> #include <otclient/core/tile.h>
#include <otclient/luascript/luavaluecasts.h> #include <otclient/luascript/luavaluecasts.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/graphics/particlesmanager.h>
void ProtocolGame::parseMessage(InputMessage& msg) void ProtocolGame::parseMessage(InputMessage& msg)
{ {
@ -527,6 +528,14 @@ void ProtocolGame::parseMagicEffect(InputMessage& msg)
TilePtr tile = g_map.getTile(pos); TilePtr tile = g_map.getTile(pos);
tile->addEffect(effect); tile->addEffect(effect);
// test particles
ParticlesSystem particlesSystem;
Emitter emitter = Emitter(Point(100, 100), 30, 30);
particlesSystem.add(emitter);
g_particlesManager.add(particlesSystem);
} }
void ProtocolGame::parseAnimatedText(InputMessage& msg) void ProtocolGame::parseAnimatedText(InputMessage& msg)