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?
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. ;)
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");
When you set the texture to the sprite, do you reset the texture rect?
sprite.setTexture(tex, true);
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);
}
}