Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite as attribute doesnt Draw but local sprite Draw  (Read 2885 times)

0 Members and 1 Guest are viewing this topic.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Sprite as attribute doesnt Draw but local sprite Draw
« 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

I got no error and when debug, all sprite attributes is fine :(

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #1 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);
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10936
    • View Profile
    • development blog
    • Email
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #2 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #3 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
and
https://github.com/prsolucoes/sfml-cmake/blob/master/Character.cpp

Look only this two files, is a short piece of code.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #4 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.
Laurent Gomila - SFML developer

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #5 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 ^

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #6 on: September 16, 2012, 06:48:53 pm »
Quote
I dont understand it ^
Good:
throw std::exception();
Bad:
throw new std::exception();
Back to C++ gamedev with SFML in May 2023

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #7 on: September 16, 2012, 07:15:38 pm »
Humm ok.

But, can you explain me why it is bad? Only to i understand, please :)

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Sprite as attribute doesnt Draw but local sprite Draw
« Reply #8 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.
« Last Edit: September 16, 2012, 07:25:54 pm by FRex »
Back to C++ gamedev with SFML in May 2023

 

anything