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

Author Topic: Sprite is drawn twice  (Read 2049 times)

0 Members and 1 Guest are viewing this topic.

RD Productions

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Sprite is drawn twice
« on: January 04, 2016, 01:08:34 pm »
Hello,
i have a little mainmenu in my game. But the button are all displayed twice. Here's a screenshot:

bilder kostenlos
And here's some code:
sPlaybutton.setTexture(tPlaybutton);
sPlaybutton.setPosition(322.f, 277.f);

sOptionsbutton.setTexture(tOptionsbutton);
sOptionsbutton.setPosition(835.f, 405.f);

sClosebutton.setTexture(tClosebutton);
sClosebutton.setPosition(485.f, 639.f);

//...

case sf::Event::MouseButtonReleased:
                {
                                                                           if (event.mouseButton.button == sf::Mouse::Left)
                                                                           {
                                                                                                if (sf::Mouse::getPosition().x > this->game->sPlaybutton.getPosition().x &&
                                                                                                        sf::Mouse::getPosition().x < this->game->sPlaybutton.getPosition().x + this->game->sPlaybutton.getGlobalBounds().width &&
                                                                                                        sf::Mouse::getPosition().y > this->game->sPlaybutton.getPosition().y &&
                                                                                                        sf::Mouse::getPosition().y < this->game->sPlaybutton.getPosition().y + this->game->sPlaybutton.getGlobalBounds().height)
                                                                                                this->game->pushState(new PlayState(game));

                                                                                   else if (sf::Mouse::getPosition().x > this->game->sOptionsbutton.getPosition().x &&
                                                                                                        sf::Mouse::getPosition().x < this->game->sOptionsbutton.getPosition().x + this->game->sOptionsbutton.getGlobalBounds().width &&
                                                                                                        sf::Mouse::getPosition().y > this->game->sOptionsbutton.getPosition().y &&
                                                                                                        sf::Mouse::getPosition().y < this->game->sOptionsbutton.getPosition().y + this->game->sOptionsbutton.getGlobalBounds().height)
                                                                                                this->game->pushState(new PlayState(game));

                                                                                   else if (sf::Mouse::getPosition().x > this->game->sClosebutton.getPosition().x &&
                                                                                                   sf::Mouse::getPosition().x < this->game->sClosebutton.getPosition().x + this->game->sClosebutton.getGlobalBounds().width &&
                                                                                                   sf::Mouse::getPosition().y > this->game->sClosebutton.getPosition().y &&
                                                                                                   sf::Mouse::getPosition().y < this->game->sClosebutton.getPosition().y + this->game->sClosebutton.getGlobalBounds().height)
                                                                                                this->game->window.close();
                                                                           }
                }
                        break;

//...

void MainMenuState::draw(float dt, sf::RenderWindow& window)
{
        window.draw(this->game->sMainMenuBG);
        window.draw(this->game->sPlaybutton);
        window.draw(this->game->sClosebutton);
        window.draw(this->game->sOptionsbutton);

        window.draw(this->game->sCursorMain);
}
« Last Edit: January 04, 2016, 01:10:54 pm by RD Productions »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Sprite is drawn twice
« Reply #1 on: January 04, 2016, 02:43:49 pm »
Do your construct your button sprites with a different texture in the constructor? If so you need to call setTexture(...) with the second parameter as true. What it looks like to me is that the sprite's texture rects are a different size of from the actual texture size. However, without a complete and minimal example we really can't help much except by guessing.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

RD Productions

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Sprite is drawn twice
« Reply #2 on: January 04, 2016, 03:10:55 pm »
Thanks for help, but I found the mistake. I just did the setTexture() and setPosition() in the MainMenuState constructor, not in the Game constructor anymor. Now it works. ;D
MainMenuState::MainMenuState(Game* game)
{
        this->game = game;

        this->game->sPlaybutton.setTexture(this->game->tPlaybutton);
        this->game->sPlaybutton.setPosition(322.f, 277.f);

        this->game->sOptionsbutton.setTexture(this->game->tOptionsbutton);
        this->game->sOptionsbutton.setPosition(835.f, 405.f);

        this->game->sClosebutton.setTexture(this->game->tClosebutton);
        this->game->sClosebutton.setPosition(485.f, 639.f);
}