SFML community forums

Help => Graphics => Topic started by: prchakal on September 14, 2012, 10:50:25 pm

Title: Sprite as attribute doesnt Draw but local sprite Draw
Post by: prchakal on September 14, 2012, 10:50:25 pm
Hi,

Im with a small problem here that my sprite that is in my class "Character" doesnt "draw" with "window.draw", but if i create a local sprite (the same sprite code and image) it draw.

You can see here (have both sprite - local and as attribute):
https://github.com/prsolucoes/sfml-cmake (https://github.com/prsolucoes/sfml-cmake)

I got no error and when debug, all sprite attributes is fine :(
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: prchakal on September 14, 2012, 11:59:33 pm
I need implement that methods for cast on every class that i send to v8? Example:

Quote
namespace v8wrap {
    v8::Handle<v8::Value> CastToJS(const Character &JS);
    v8::Handle<v8::Value> CastToJS2(const Character &JS);
    void AutoCastToCPP(v8::Handle<v8::Value> Value, Character &CPP);
    v8::Handle<v8::Value> CastToJS(Character *JS);
    v8::Handle<v8::Value> CastToJS2(const Character *JS);
    void AutoCastToCPP(v8::Handle<v8::Value> Value, Character *&CPP);
}
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: eXpl0it3r on September 15, 2012, 12:44:04 am
Im with a small problem here that my sprite that is in my class "Character" doesnt "draw" with "window.draw", but if i create a local sprite (the same sprite code and image) it draw.
Do you call window.display()?

I need implement that methods for cast on every class that i send to v8? Example:
I don't think any/many will know how this V8 thing works with SFML, specificly with your code, also not many if any will go and read your full code just to understand what the problem actually is.
If you want us to help you need to provide more information and make your question more specific. ;)
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: prchakal on September 15, 2012, 02:24:41 am
Hi,

o.o

The problem is here:

Quote
window.clear(sf::Color(255, 255, 255));
        window.draw(*robot1->getSprite());  // it dont display
        window.draw(*sprite);   // it display
        window.display();

I think it is not related, because the sprite in another class out of v8.

https://github.com/prsolucoes/sfml-cmake/blob/master/main.cpp (https://github.com/prsolucoes/sfml-cmake/blob/master/main.cpp)
and
https://github.com/prsolucoes/sfml-cmake/blob/master/Character.cpp (https://github.com/prsolucoes/sfml-cmake/blob/master/Character.cpp)

Look only this two files, is a short piece of code.
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: Laurent on September 15, 2012, 09:22:51 am
void Character::loadSprite()
{
    sf::Texture texture;
    if (!texture.loadFromFile(resourcePath() + "images/" + type + ".png"))
    {
        throw new std::exception();
    }
    sprite = new sf::Sprite(texture);
    sprite->setPosition(100, 200);
}
The texture is destroyed when the function exists, so the sprite is left with a pointer to an invalid memory zone. The texture must live as long as the sprite uses it.

And don't dynmically allocate the exceptions that you throw.
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: prchakal on September 16, 2012, 06:44:57 pm
Humm...ok.

I think that texture is store in the sprite and the object is not destroyed.

Quote
And don't dynmically allocate the exceptions that you throw.

I dont understand it ^
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: FRex on September 16, 2012, 06:48:53 pm
Quote
I dont understand it ^
Good:
throw std::exception();
Bad:
throw new std::exception();
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: prchakal on September 16, 2012, 07:15:38 pm
Humm ok.

But, can you explain me why it is bad? Only to i understand, please :)
Title: Re: Sprite as attribute doesnt Draw but local sprite Draw
Post by: FRex on September 16, 2012, 07:23:49 pm
There is memory leak if you catch it without deleting it.
And new can throw too, I think that'd take you to nearest catch(...) or catch(std::exception& e) block or call terminate. I'm not sure.