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

Author Topic: [Solved]Sprites not displaying  (Read 1122 times)

0 Members and 2 Guests are viewing this topic.

hirosum

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved]Sprites not displaying
« on: March 22, 2014, 05:43:38 am »
I have a class which should display a sprite at a different position and rotation depending on input, but for some reason the sprite is only drawn when I input "up".
The way the class works is that the constructor is given a pointer to the texture, a pointer to the player which it is supposed to follow, the direction it is supposed to face, and the index that it will have in the vector of draw-able objects. In the constructor there is an if else if to check if the direction is up, down, left, or right. The code in each of these is identical except the rotation and position given is different but the sprite will only draw if UP is true. It does not matter what order the if elses are in, or what values are given to rotation and position, even if they are all completely identical it will only draw the sprite if the direction is UP. It is not a case of it not going into the if statements as I have checked that through debugging and the positions and rotations are set correctly.
It makes no sense to me and I'm pulling my hair out because of it. It is certainly something I am doing wrong but I just can't figure it out. I'll include all the code I think is relevant let me know if any more is needed.
Slash class where the problem lies:
class Slash : public Drawable
{
public:
    Slash(){}
    Slash(sf::Texture*, Drawable*, Direction, int i);
    sf::Sprite sprite;
    sf::Clock clock;
    Drawable* parent;
    void update(float t);
};

Slash::Slash(sf::Texture* t,Drawable* pParent, Direction d, int i) : Drawable(t,pParent->getPosition(),i)
{
    parent = pParent;
    direction = d;
    if(direction == LEFT)
    {
        spritesheet.sprite.setRotation(90.0f);
        spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
    }
    else if(direction == UP)
    {
        spritesheet.sprite.setRotation(90.0f);
        spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
    }
    else if(direction == DOWN)
    {
        spritesheet.sprite.setRotation(-90.0f);
        spritesheet.sprite.setPosition(sf::Vector2f(position.x - 30, position.y + 150));
    }
    else if(direction == RIGHT)
    {
        spritesheet.sprite.setRotation(180.0f);
        spritesheet.sprite.setPosition(sf::Vector2f(position.x + 100, position.y + 130));
    }
    clock.restart();
}

void Slash::update(float t)
{
    position = parent->getPosition();

    if(direction == UP)
    {
        spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
    }
    else if(direction == LEFT)
    {
        spritesheet.sprite.setPosition(sf::Vector2f(position.x + 95,position.y - 25));
    }
    else if(direction == DOWN)
    {
        spritesheet.sprite.setPosition(sf::Vector2f(position.x - 30, position.y + 150));
    }
    else if(direction == RIGHT)
    {
        spritesheet.sprite.setPosition(sf::Vector2f(position.x + 100, position.y + 130));
    }

    if(clock.getElapsedTime().asMilliseconds() >= 500)
    {
        allDrawables.erase(allDrawables.begin()+index);
        delete this;
    }
}
The "Drawable" parent class:
class Drawable
{
public:
    Drawable(sf::Texture* t, sf::Vector2f, int i);
    Drawable(){}
    virtual ~Drawable(){}
    float depth;
    sf::Sprite* getSprite();
    sf::Vector2f getPosition();
    inline virtual void update(float t){}
    int index;
    sf::Clock clock;
protected:
    sf::Texture texture;
    Spritesheet spritesheet;
    Direction direction;
    sf::Vector2f position;
};

Drawable::Drawable(sf::Texture* t,sf::Vector2f pPos, int i)
{
    spritesheet = Spritesheet(t,128,64,4);
    index = i;
    position = pPos;
}

sf::Vector2f Drawable::getPosition()
{
    return position;
}

sf::Sprite* Drawable::getSprite()
{
    return spritesheet.getSprite(direction);
}
 
My Spritesheet class:
class Spritesheet
{
public:
    Spritesheet(){}
    Spritesheet(sf::Texture* texture, unsigned int rectH, unsigned int rectW, unsigned int spriteNo);
    sf::Sprite* getSprite(unsigned int index);
    sf::Sprite sprite;
private:
    sf::IntRect rect;
    unsigned int spriteNo;
    unsigned int sprIndex;
    unsigned int animIndex;
};
Spritesheet::Spritesheet(sf::Texture* texture,unsigned int recth, unsigned int rectw, unsigned int spriteno)
{
    rect.height = recth;
    rect.width = rectw;
    rect.top = 0;
    rect.left = 0;
    sprite.setTextureRect(rect);
    sprite.setTexture(*texture);
    spriteNo = spriteno;
    sprIndex = 0;
}

sf::Sprite* Spritesheet::getSprite(unsigned int index)
{
    rect.left = rect.width * index;
    sprite.setTextureRect(rect);
    return &sprite;
}
 
And the code where the "Slash" objects are created:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
        direction = UP;
        if(attClock.getElapsedTime().asMilliseconds() > 1000)
        {
            Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
            allDrawables.push_back(s);
            attClock.restart();
        }
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
        direction = DOWN;
        if(attClock.getElapsedTime().asMilliseconds() > 1000)
        {
            Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
            allDrawables.push_back(s);
            attClock.restart();
        }
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
        direction = LEFT;
        if(attClock.getElapsedTime().asMilliseconds() > 1000)
        {
            Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
            allDrawables.push_back(s);
            attClock.restart();
        }
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
        direction = RIGHT;
        if(attClock.getElapsedTime().asMilliseconds() > 1000)
        {
            Slash* s = new Slash(&allTextures[2],this,direction,allDrawables.size());
            allDrawables.push_back(s);
            attClock.restart();
        }
    }
 
allTextures is a vector containing all my textures, which are loaded when the program starts, allDrawables is a vector of pointers to each of the objects to be drawn. Thank you in advance for any help given.
« Last Edit: March 22, 2014, 04:03:00 pm by hirosum »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Sprites not displaying
« Reply #1 on: March 22, 2014, 11:07:33 am »
Well simply set some break points and look at the variables and why they are not where you'd want  them to be.

If you want us to help you need to create a minimal and complete (fully compilable) example, because not many if any will go through the full code and try to understand how things work. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

hirosum

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Sprites not displaying
« Reply #2 on: March 22, 2014, 03:59:37 pm »
Yes sorry about the wall of code I'll keep to the minimum next time. I figured it out anyway, in this case the direction is only needed in the slash class because it's rotating the sprite rather than using a different sprite in the texture but because the direction variable is a member of its base class it's being passed to the spritesheet and trying to use a different rect which doesn't have any image there. Thank you for the reply and sorry for wasting your time! :D