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

Author Topic: Can't get my sprites to display  (Read 7519 times)

0 Members and 1 Guest are viewing this topic.

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Can't get my sprites to display
« on: November 09, 2012, 12:53:33 am »
Hey all,


 I just recently written a class for my sprites to display in my loop but it won't display, i don't get an error of any kind, and it doesn't fail to load the image so I'm lost to why it is not displaying them.


This is how i create the buttons in main():

//there are 2 buttons one for normal & one for mouse hover

 sf::Sprite button;
 sf::Texture button_t;
 sf::Sprite button_on;
 sf::Texture button_on_t;
 load(button_t,button_on,theme["Button"]);
 load(button_on_t,button_on,theme["Button_On"]);
 int width = button.getLocalBounds().width;
 int height = button.getLocalBounds().height;

Button btn_quit(350,450,width,height,"Quit Game");

In my loop i display it like so:

btn_quit.RenderBttn(window,mouseX,mouseY,button,button_on);


And this is the relevant class which deals with it all:

Button::Button(int x, int y, int w, int h, std::string cap)
{
    m_y = y;
    m_x = (x / 2) - ( w / 2 );
    m_w = w;
    m_h = h;
    caption = cap;
}

bool Button::IsIn( int mouseX, int mouseY )
{

    if (((mouseX > m_x) && (mouseX < m_x + m_w))
    && ((mouseY > m_y) && (mouseY < m_y + m_h ) ) ) {

        return true;
    } else {
        return false;
    }
}

void Button::RenderBttn(sf::RenderWindow& destination,int &mouseX, int &mouseY,sf::Sprite& button, sf::Sprite& button_on)
{
    sf::Sprite result;
    result = IsIn(mouseX,mouseY) ? button_on : button;      
    result.setPosition( m_x , m_y );
    destination.draw(result);
}
 

Any idea why it won't display? Even though i get no errors and the image is being loaded?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Can't get my sprites to display
« Reply #1 on: November 09, 2012, 12:58:16 am »
Any idea why it won't display? Even though i get no errors and the image is being loaded?
No because you don't show enough relevant code... ::)

To display stuff on the screen you need to:
window.clear();
window.draw(drawable);
window.display();

And the drawable needs to be setup to actually have some area that could get drawn onto the screen.

So somewhere in your code your doing something wrong. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't get my sprites to display
« Reply #2 on: November 09, 2012, 01:04:35 am »
Well i took out most of the code because i believe it was the class function causing the problem but here is the loop:

     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             // Close window : exit
             if (event.type == sf::Event::Closed) {
                 window.close();
                         } else if(event.type == sf::Event::MouseMoved){
                                 mouseX = event.mouseMove.x;
                                 mouseY = event.mouseMove.y;

                         }
                 }
 
         // Clear screen
         window.clear();
                // Draw the sprite BG
         window.draw(background);
                // Draw the sprite LOGO
                 window.draw(logo);

                 btn_quit.RenderBttn(window,mouseX,mouseY,button,button_on);
                 window.display();
}
 

And window is setup like this:

sf::RenderWindow window(sf::VideoMode(atoi(config["ResoX"].c_str()), atoi(config["ResoY"].c_str())), "SFML window");
« Last Edit: November 09, 2012, 01:08:40 am by sirchick »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Can't get my sprites to display
« Reply #3 on: November 09, 2012, 01:17:41 am »
When you set the texture to the sprite, do you reset the texture rect?
sprite.setTexture(tex, true);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't get my sprites to display
« Reply #4 on: November 09, 2012, 01:19:44 am »
EDIT:

Sorry i got confused i set the texture the load function i don't have the boolean though:

        void load(sf::Texture& texture, sf::Sprite& sprite, const std::string& img) {
          if(texture.loadFromFile(img)){
                  sprite.setTexture(texture);
          }
        }
 
« Last Edit: November 09, 2012, 01:28:00 am by sirchick »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Re: AW: Can't get my sprites to display
« Reply #5 on: November 09, 2012, 01:25:23 am »
When you set the texture to the sprite, do you reset the texture rect?
sprite.setTexture(tex, true);
;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: AW: Re: AW: Can't get my sprites to display
« Reply #6 on: November 09, 2012, 01:28:24 am »
When you set the texture to the sprite, do you reset the texture rect?
sprite.setTexture(tex, true);
;)

Can you link me the documentation about that boolean wondering what it does.

EDIT:

Its working :D
« Last Edit: November 09, 2012, 01:35:00 am by sirchick »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Can't get my sprites to display
« Reply #7 on: November 09, 2012, 01:37:54 am »
Can you link me the documentation about that boolean wondering what it does.
Seriously? Just go to the class overview, the sf::Sprite and see for youself on the setTexture function... ;)

So what did the trick?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Can't get my sprites to display
« Reply #8 on: November 09, 2012, 01:44:51 am »
It seems adding ,true made it work.

Although its super slow when i run it. Like mouse over takes a second or more to react to change the image. Very odd.