10
« on: October 18, 2011, 07:56:19 pm »
I'm having the same problem as explained, in the visual studio 2010 ultimate debbuger it works fine.
It crashes on program exit.
Moving the declaration of sf::Texture from line 24 in the header file to just before line 59 in the cpp file, thus creating a new texture every time this component is drawn, will stop the program from crashing on exit. This makes my other components unable to get and work on the texture.
This is a temorary design thought, I'm going to do something similar to the OP, but that's unrelated to the issue.
Objsprite.h
#pragma once
#include "Component.h"
#include "Object.h"
#include <SFML/Graphics.hpp>
#include <string>
class PlayState;
class ObjSprite : public Component
{
public:
ObjSprite(Entity *entity, const std::string &name);
virtual ~ObjSprite();
static Component* Create(Entity* entity, const std::string &name) { return new ObjSprite(entity, name); }
void Init(PlayState *pState);
void Update();
void Draw(sf::RenderWindow& window);
void ExecuteCommand(int command, void* data = 0);
void ExecuteEvent(int event, void* data) {};
private:
bool compInitialized;
protected:
sf::Image spriteImage;
sf::Texture texture;
sf::Sprite sprite;
Property<std::string> imagePath;
Property<sf::Sprite*> spritePtr;
Property<float> posX;
Property<float> posY;
Property<float> imagePosX;
Property<float> imagePosY;
Property<float> spriteWidth;
Property<float> spriteHeight;
Property<float> rotation;
Property<bool> staticObject;
Object* go;
};
Objsprite.cpp
#include "ObjSprite.h"
#include <iostream>
#include <boost/bind.hpp>
#include "PlayState.h"
ObjSprite::ObjSprite(Entity *entity, const std::string &name)
: Component(entity,name), go((Object*)entity)
{
imagePath = go->AddProperty<std::string>("SpriteImage","sprites/default.bmp");
posX = go->AddProperty<float>("PosX",0);
posY = go->AddProperty<float>("PosY",0);
imagePosX = go->AddProperty<float>("ImagePosX",0);
imagePosY = go->AddProperty<float>("ImagePosY",0);
spriteWidth = go->AddProperty<float>("SpriteWidth",32);
spriteHeight = go->AddProperty<float>("SpriteHeight",32);
rotation = go->AddProperty<float>("Rotation",0);
staticObject = go->AddProperty<bool>("StaticObject",true);
compInitialized = false;
}
ObjSprite::~ObjSprite()
{
}
void ObjSprite::Init(PlayState *pState)
{
if(!compInitialized)
{
spriteImage.LoadFromFile(imagePath.Get());
spriteImage.CreateMaskFromColor(sf::Color(255,0,255));
sprite.SetSubRect(sf::IntRect(0,0,spriteWidth.Get(),spriteHeight.Get()));
sprite.SetPosition( posX.Get(),posY.Get() );
spritePtr = go->AddProperty<sf::Sprite*>("Sprite",&sprite);
compInitialized = true;
sprite.SetOrigin( spriteWidth.Get()/2,spriteHeight.Get()/2);
}
}
void ObjSprite::Update()
{
if(!staticObject.Get())
{
sprite.SetPosition( posX.Get(),posY.Get() );
sprite.SetRotation( rotation.Get() );
}
}
void ObjSprite::Draw(sf::RenderWindow &window)
{
texture.LoadFromImage(spriteImage);
sprite.SetTexture( texture,false );
window.Draw(sprite);
}
void ObjSprite::ExecuteCommand(int command, void* data )
{
}