mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 14:03:26 +02:00
Huge engine change, replace all std::shared_ptrs
Create a new shared pointer type stdext::shared_object_ptr and stdext::shared_obj using boost::intrusive_ptr Advantages: * half memory usage * faster and lightweight Disadvantages: * using weak_ptr is not supported anymore * compiling seems slower
This commit is contained in:
@@ -70,7 +70,7 @@ void AnimatedTexture::processAnimation()
|
||||
AnimatedTexturePtr self = asAnimatedTexture();
|
||||
|
||||
// continue to animate only if something still referencing this texture
|
||||
if(self.use_count() > 1)
|
||||
if(self->ref_count() > 1)
|
||||
g_dispatcher.scheduleEvent(std::bind(&AnimatedTexture::processAnimation, self), m_framesDelay[m_currentFrame]);
|
||||
}
|
||||
*/
|
@@ -34,7 +34,7 @@ public:
|
||||
void enableBilinearFilter();
|
||||
void processAnimation();
|
||||
|
||||
AnimatedTexturePtr asAnimatedTexture() { return std::static_pointer_cast<AnimatedTexture>(shared_from_this()); }
|
||||
AnimatedTexturePtr asAnimatedTexture() { return self_cast<AnimatedTexture>(); }
|
||||
|
||||
private:
|
||||
std::vector<uint> m_framesTextureId;
|
||||
|
@@ -24,11 +24,12 @@
|
||||
#define BITMAPFONT_H
|
||||
|
||||
#include "declarations.h"
|
||||
#include "texture.h"
|
||||
|
||||
#include <framework/otml/declarations.h>
|
||||
#include <framework/graphics/coordsbuffer.h>
|
||||
|
||||
class BitmapFont
|
||||
class BitmapFont : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
BitmapFont(const std::string& name) : m_name(name) { }
|
||||
|
@@ -44,24 +44,21 @@ class ParticleSystem;
|
||||
class ParticleEffect;
|
||||
class ParticleEffectType;
|
||||
|
||||
typedef std::weak_ptr<Texture> TextureWeakPtr;
|
||||
typedef std::weak_ptr<ParticleSystem> ParticleSystemWeakPtr;
|
||||
|
||||
typedef std::shared_ptr<Image> ImagePtr;
|
||||
typedef std::shared_ptr<Texture> TexturePtr;
|
||||
typedef std::shared_ptr<AnimatedTexture> AnimatedTexturePtr;
|
||||
typedef std::shared_ptr<BitmapFont> BitmapFontPtr;
|
||||
typedef std::shared_ptr<CachedText> CachedTextPtr;
|
||||
typedef std::shared_ptr<FrameBuffer> FrameBufferPtr;
|
||||
typedef std::shared_ptr<Shader> ShaderPtr;
|
||||
typedef std::shared_ptr<ShaderProgram> ShaderProgramPtr;
|
||||
typedef std::shared_ptr<PainterShaderProgram> PainterShaderProgramPtr;
|
||||
typedef std::shared_ptr<Particle> ParticlePtr;
|
||||
typedef std::shared_ptr<ParticleEmitter> ParticleEmitterPtr;
|
||||
typedef std::shared_ptr<ParticleAffector> ParticleAffectorPtr;
|
||||
typedef std::shared_ptr<ParticleSystem> ParticleSystemPtr;
|
||||
typedef std::shared_ptr<ParticleEffect> ParticleEffectPtr;
|
||||
typedef std::shared_ptr<ParticleEffectType> ParticleEffectTypePtr;
|
||||
typedef stdext::shared_object_ptr<Image> ImagePtr;
|
||||
typedef stdext::shared_object_ptr<Texture> TexturePtr;
|
||||
typedef stdext::shared_object_ptr<AnimatedTexture> AnimatedTexturePtr;
|
||||
typedef stdext::shared_object_ptr<BitmapFont> BitmapFontPtr;
|
||||
typedef stdext::shared_object_ptr<CachedText> CachedTextPtr;
|
||||
typedef stdext::shared_object_ptr<FrameBuffer> FrameBufferPtr;
|
||||
typedef stdext::shared_object_ptr<Shader> ShaderPtr;
|
||||
typedef stdext::shared_object_ptr<ShaderProgram> ShaderProgramPtr;
|
||||
typedef stdext::shared_object_ptr<PainterShaderProgram> PainterShaderProgramPtr;
|
||||
typedef stdext::shared_object_ptr<Particle> ParticlePtr;
|
||||
typedef stdext::shared_object_ptr<ParticleEmitter> ParticleEmitterPtr;
|
||||
typedef stdext::shared_object_ptr<ParticleAffector> ParticleAffectorPtr;
|
||||
typedef stdext::shared_object_ptr<ParticleSystem> ParticleSystemPtr;
|
||||
typedef stdext::shared_object_ptr<ParticleEffect> ParticleEffectPtr;
|
||||
typedef stdext::shared_object_ptr<ParticleEffectType> ParticleEffectTypePtr;
|
||||
typedef std::vector<ShaderPtr> ShaderList;
|
||||
|
||||
#endif
|
||||
|
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include "fontmanager.h"
|
||||
#include "texture.h"
|
||||
|
||||
#include <framework/core/resourcemanager.h>
|
||||
#include <framework/otml/otml.h>
|
||||
|
@@ -26,7 +26,7 @@
|
||||
#include "declarations.h"
|
||||
#include "texture.h"
|
||||
|
||||
class FrameBuffer
|
||||
class FrameBuffer : public stdext::shared_object
|
||||
{
|
||||
protected:
|
||||
FrameBuffer();
|
||||
|
@@ -26,7 +26,7 @@
|
||||
#include "declarations.h"
|
||||
#include <framework/util/databuffer.h>
|
||||
|
||||
class Image
|
||||
class Image : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
Image(const Size& size, int bpp = 4, uint8 *pixels = nullptr);
|
||||
|
@@ -27,7 +27,8 @@
|
||||
#include "painter.h"
|
||||
#include <framework/global.h>
|
||||
|
||||
class Particle {
|
||||
class Particle : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
Particle(const Point& pos, const Size& startSize, const Size& finalSize, const PointF& velocity, const PointF& acceleration, float duration, float ignorePhysicsAfter, const std::vector<Color>& colors, const std::vector<float>& colorsStops, Painter::CompositionMode compositionMode = Painter::CompositionMode_Normal, TexturePtr texture = nullptr);
|
||||
|
||||
|
@@ -26,7 +26,8 @@
|
||||
#include "declarations.h"
|
||||
#include <framework/otml/otml.h>
|
||||
|
||||
class ParticleAffector {
|
||||
class ParticleAffector : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
ParticleAffector();
|
||||
|
||||
|
@@ -26,10 +26,8 @@
|
||||
#include <framework/core/clock.h>
|
||||
#include <framework/graphics/texturemanager.h>
|
||||
|
||||
ParticleEmitter::ParticleEmitter(const ParticleSystemPtr& parent)
|
||||
ParticleEmitter::ParticleEmitter()
|
||||
{
|
||||
m_parent = parent;
|
||||
|
||||
m_position = Point(0, 0);
|
||||
m_duration = -1;
|
||||
m_delay = 0;
|
||||
@@ -179,7 +177,7 @@ bool ParticleEmitter::load(const OTMLNodePtr& node)
|
||||
return true;
|
||||
}
|
||||
|
||||
void ParticleEmitter::update(float elapsedTime)
|
||||
void ParticleEmitter::update(float elapsedTime, const ParticleSystemPtr& system)
|
||||
{
|
||||
// check if finished
|
||||
if(m_duration >= 0 && m_elapsedTime >= m_duration + m_delay) {
|
||||
@@ -214,8 +212,7 @@ void ParticleEmitter::update(float elapsedTime)
|
||||
float pAccelerationAngle = stdext::random_range(m_pMinAccelerationAngle, m_pMaxAccelerationAngle);
|
||||
PointF pAcceleration(pAccelerationAbs * cos(pAccelerationAngle), pAccelerationAbs * sin(pAccelerationAngle));
|
||||
|
||||
ParticleSystemPtr particleSystem = m_parent.lock();
|
||||
particleSystem->addParticle(ParticlePtr(new Particle(pPosition, m_pStartSize, m_pFinalSize, pVelocity, pAcceleration, pDuration, m_pIgnorePhysicsAfter, m_pColors, m_pColorsStops, m_pCompositionMode, m_pTexture)));
|
||||
system->addParticle(ParticlePtr(new Particle(pPosition, m_pStartSize, m_pFinalSize, pVelocity, pAcceleration, pDuration, m_pIgnorePhysicsAfter, m_pColors, m_pColorsStops, m_pCompositionMode, m_pTexture)));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -29,20 +29,18 @@
|
||||
#include <framework/graphics/texture.h>
|
||||
#include <framework/otml/otml.h>
|
||||
|
||||
class ParticleEmitter {
|
||||
class ParticleEmitter : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
|
||||
ParticleEmitter(const ParticleSystemPtr& parent);
|
||||
ParticleEmitter();
|
||||
|
||||
bool load(const OTMLNodePtr& node);
|
||||
|
||||
void update(float elapsedTime);
|
||||
void update(float elapsedTime, const ParticleSystemPtr& system);
|
||||
|
||||
bool hasFinished() { return m_finished; }
|
||||
|
||||
private:
|
||||
ParticleSystemWeakPtr m_parent;
|
||||
|
||||
// self related
|
||||
Point m_position;
|
||||
float m_duration, m_delay;
|
||||
|
@@ -34,7 +34,7 @@ bool ParticleSystem::load(const OTMLNodePtr& node)
|
||||
{
|
||||
for(const OTMLNodePtr& childNode : node->children()) {
|
||||
if(childNode->tag() == "Emitter") {
|
||||
ParticleEmitterPtr emitter = ParticleEmitterPtr(new ParticleEmitter(shared_from_this()));
|
||||
ParticleEmitterPtr emitter = ParticleEmitterPtr(new ParticleEmitter());
|
||||
if(!emitter->load(childNode))
|
||||
return false;
|
||||
m_emitters.push_back(emitter);
|
||||
@@ -84,6 +84,7 @@ void ParticleSystem::update()
|
||||
return;
|
||||
m_lastUpdateTime = g_clock.seconds() - std::fmod(elapsedTime, delay);
|
||||
|
||||
auto self = self_cast<ParticleSystem>();
|
||||
for(int i = 0; i < elapsedTime / delay; ++i) {
|
||||
|
||||
// update emitters
|
||||
@@ -93,7 +94,7 @@ void ParticleSystem::update()
|
||||
it = m_emitters.erase(it);
|
||||
continue;
|
||||
}
|
||||
emitter->update(delay);
|
||||
emitter->update(delay, self);
|
||||
++it;
|
||||
}
|
||||
|
||||
|
@@ -27,7 +27,7 @@
|
||||
#include "particleemitter.h"
|
||||
#include "particleaffector.h"
|
||||
|
||||
class ParticleSystem : public std::enable_shared_from_this<ParticleSystem> {
|
||||
class ParticleSystem : public stdext::shared_object {
|
||||
public:
|
||||
ParticleSystem();
|
||||
|
||||
|
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class Shader
|
||||
class Shader : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
enum ShaderType {
|
||||
|
@@ -25,7 +25,7 @@
|
||||
|
||||
#include "declarations.h"
|
||||
|
||||
class Texture : public std::enable_shared_from_this<Texture>
|
||||
class Texture : public stdext::shared_object
|
||||
{
|
||||
public:
|
||||
Texture();
|
||||
|
@@ -41,7 +41,7 @@ void TextureManager::terminate()
|
||||
// check for leaks
|
||||
int refs = 0;
|
||||
for(const auto& it : m_textures)
|
||||
if(it.second.use_count() > 1)
|
||||
if(it.second->ref_count() > 1)
|
||||
refs++;
|
||||
if(refs > 0)
|
||||
g_logger.debug(stdext::format("%d textures references left", refs));
|
||||
@@ -65,10 +65,7 @@ TexturePtr TextureManager::getTexture(const std::string& fileName)
|
||||
// check if the texture is already loaded
|
||||
auto it = m_textures.find(filePath);
|
||||
if(it != m_textures.end()) {
|
||||
if(it->second.expired())
|
||||
m_textures.erase(it);
|
||||
else
|
||||
texture = it->second.lock();
|
||||
texture = it->second;
|
||||
}
|
||||
|
||||
// texture not found, load it
|
||||
|
@@ -38,7 +38,7 @@ public:
|
||||
private:
|
||||
TexturePtr loadPNG(std::stringstream& file);
|
||||
|
||||
std::unordered_map<std::string, TextureWeakPtr> m_textures;
|
||||
std::unordered_map<std::string, TexturePtr> m_textures;
|
||||
TexturePtr m_emptyTexture;
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user