Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trenderer.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <qmutexlocker></qmutexlocker>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "particlesmanager.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
EXPLANATION:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager improves the old particles system as follows - the particles
Shinya Kitaoka 120a6e
manager stores the
Toshihiro Shimizu 890ddd
last particles configuration which had some particle rendered by a thread.
Shinya Kitaoka 120a6e
Under normal cicumstances, this means that every thread has the particles
Shinya Kitaoka 120a6e
configuration that rendered
Toshihiro Shimizu 890ddd
last. In case a trail was set, such frame is that beyond the trail.
Shinya Kitaoka 120a6e
This managemer works well on the assumption that each thread builds particle in
Shinya Kitaoka 120a6e
an incremental timeline.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
DEFINE_CLASS_CODE(ParticlesManager::FxData, 110)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef std::map<double, particlesmanager::framedata=""> FramesMap;</double,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
//    Preliminaries
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class ParticlesManagerGenerator final : public TRenderResourceManagerGenerator {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ParticlesManagerGenerator() : TRenderResourceManagerGenerator(true) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 38fd86
  TRenderResourceManager *operator()(void) override {
Shinya Kitaoka 38fd86
    return new ParticlesManager;
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
MANAGER_FILESCOPE_DECLARATION(ParticlesManager, ParticlesManagerGenerator);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
//    FrameData implementation
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ParticlesManager::FrameData::FrameData(FxData *fxData)
Shinya Kitaoka 120a6e
    : m_fxData(fxData)
Shinya Kitaoka 120a6e
    , m_frame((std::numeric_limits<int>::min)())</int>
Shinya Kitaoka 120a6e
    , m_calculated(false)
Shinya Kitaoka 120a6e
    , m_maxTrail(-1)
Shinya Kitaoka 120a6e
    , m_totalParticles(0) {
Shinya Kitaoka 120a6e
  m_fxData->addRef();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager::FrameData::~FrameData() { m_fxData->release(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ParticlesManager::FrameData::buildMaxTrail() {
Shinya Kitaoka 120a6e
  // Store the maximum trail of each particle
Shinya Kitaoka 120a6e
  std::list<particle>::iterator it;</particle>
Shinya Kitaoka 120a6e
  for (it = m_particles.begin(); it != m_particles.end(); ++it)
Shinya Kitaoka 120a6e
    m_maxTrail = std::max(m_maxTrail, it->trail);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ParticlesManager::FrameData::clear() {
Shinya Kitaoka 120a6e
  m_frame = (std::numeric_limits<int>::min)();</int>
Shinya Kitaoka 120a6e
  m_particles.clear();
Shinya Kitaoka 120a6e
  m_random         = TRandom();
Shinya Kitaoka 120a6e
  m_calculated     = false;
Shinya Kitaoka 120a6e
  m_maxTrail       = -1;
Shinya Kitaoka 120a6e
  m_totalParticles = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
//    FxData implementation
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager::FxData::FxData() : TSmartObject(m_classCode) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
//    ParticlesContainer implementation
Toshihiro Shimizu 890ddd
//************************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager *ParticlesManager::instance() {
Shinya Kitaoka 120a6e
  return static_cast<particlesmanager *="">(</particlesmanager>
Shinya Kitaoka 120a6e
      ParticlesManager::gen()->getManager(TRenderer::renderId()));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager::ParticlesManager() : m_renderStatus(-1) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager::~ParticlesManager() {
Shinya Kitaoka 120a6e
  // Release all fxDatas
Shinya Kitaoka 120a6e
  std::map<unsigned *="" fxdata="" long,="">::iterator it, end = m_fxs.end();</unsigned>
Shinya Kitaoka 120a6e
  for (it = m_fxs.begin(); it != end; ++it) it->second->release();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ParticlesManager::onRenderStatusStart(int renderStatus) {
Shinya Kitaoka 120a6e
  m_renderStatus = renderStatus;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ParticlesManager::FrameData *ParticlesManager::data(unsigned long fxId) {
Shinya Kitaoka 120a6e
  QMutexLocker locker(&m_mutex);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::map<unsigned *="" fxdata="" long,="">::iterator it = m_fxs.find(fxId);</unsigned>
Shinya Kitaoka 120a6e
  if (it == m_fxs.end()) {
Shinya Kitaoka 120a6e
    it = m_fxs.insert(std::make_pair(fxId, new FxData)).first;
Shinya Kitaoka 120a6e
    it->second->addRef();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  FxData *fxData = it->second;
Shinya Kitaoka 120a6e
  FrameData *d   = fxData->m_frames.localData();
Shinya Kitaoka 120a6e
  if (!d) {
Shinya Kitaoka 120a6e
    d = new FrameData(fxData);
Shinya Kitaoka 120a6e
    fxData->m_frames.setLocalData(d);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return d;
Toshihiro Shimizu 890ddd
}