F*CK YES! IT'S MESSY BUT IT WORKS! Apologies for the unindented code again but I was mainly going for function, I didn't realise that's what the window commands were for, I thought you kept the events separate.
#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "");
sf::Font arial;
if ( !arial.loadFromFile ( "arial.ttf" ) )
{ }
sf::Texture SelectedButton;
if (!SelectedButton.loadFromFile("Button Selected.png"))
{
}
sf::Sprite SpriteSelectedButton ( SelectedButton );
SpriteSelectedButton.setPosition ( 350, 120 );
sf::Texture UnSelectedButton;
if (!UnSelectedButton.loadFromFile("Button.png"))
{
}
sf::Sprite SpriteUnselectedButton ( UnSelectedButton );
SpriteUnselectedButton.setPosition ( 350, 120 );
sf::FloatRect FirstButton ( 350, 120, 5, 5 );
FirstButton = SpriteSelectedButton.getGlobalBounds();
bool DrawTheSprites;
while (window.isOpen())
{
{
sf::Event event;
while (window.pollEvent(event))
DrawTheSprites = ( FirstButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true );
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
if ( DrawTheSprites )
window.draw ( SpriteSelectedButton );
else
{
window.draw ( SpriteUnselectedButton );
}
window.display();
}
return 0;
}
When in doubt, rage code.
Here's your program, rewritten so it doesn't break anything. I'm learning how to write GUI applications with SFML myself and I thought this would be a fun little excercise for myself.
I'm no master programmer, but I'm inclined to agree with Laurent, Hatchet and Nexus. STOP trying to learn how to write a GUI application when your understanding of C++ is so shaky. If you're having trouble with physical books, I'd recommend
http://www.learncpp.com/ as it's easy to follow, does a great job explaining not only the basic language, but why the things you do learn are so important. I know more experienced programmers don't like online tutorials, but that site is fantastic for helping people learn the basics of C++, not to mention a few good tips for software design in general.
//#include <iostream> You don't need this module
#include <SFML/Graphics.hpp>
//#include <sstream> or this one
// code has been properly indented
// ALWAYS make your code easy to read!
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "For Lethn");
/* You're not writing any text to screen right now, so this bit is pointless
When modifying code, be sure to expunge all the obsolete stuff. this is why comments
such as these and readable code are so important
sf::Font arial;
if ( !arial.loadFromFile ( "arial.ttf" ) )
{ } */
sf::Texture SelectedButton;
// OLD CODE: if (!SelectedButton.loadFromFile("Button Selected.png")){} ...why?
// I noticed you originally introduced this error back in June, despite months of on/off revisions, it's still here
if (!SelectedButton.loadFromFile("ButtonSelected.png"))
return EXIT_FAILURE;
sf::Texture UnSelectedButton;
if (!UnSelectedButton.loadFromFile("ButtonUnselected.png"))
return EXIT_FAILURE; // added return statement
sf::Sprite buttonSprite;
buttonSprite.setPosition(350, 120);
sf::Rect<int> buttonRect(350,120,80,80); // I've cheated here and simply set the rect position & size
// manually the same as our sprite. it's late, brain no function sleep without
// your while loop had two opening braces, though i'm not sure what caused that to happen.
// Again, the more readable your code, the less likely you're to make this mistakes
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{ // found the missing brace!
switch(event.type)
{
case(sf::Event::MouseMoved):
if(buttonRect.contains(event.mouseMove.x, event.mouseMove.y))
buttonSprite.setTexture(SelectedButton); // use this texture when mouse is over the button
else
buttonSprite.setTexture(UnSelectedButton); // use this one when it's not
break;
case(sf::Event::Closed):
window.close();
break;
default:
break;
}
}
window.clear();
window.draw(buttonSprite); // buttonSprite automatically uses the correct texture
window.display();
}
return EXIT_SUCCESS;
}
Sorry about the dirty shortcut when defining buttonRect.