I'm practicing sfml and I'm beginner.
If you run this code you get a rectangle that says "Play".If you click on it it prints out in the console "You clicked play".I wanted to make a blue background when I click "Play".But I don't know how to do it...
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
//Creation
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Game");
//60 FPS
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
//States for buttons and events
sf::Event event;
bool play= true;
bool mouseClick = false;
//Vars
int mouseX;
int mouseY;
//Render shapes
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(200,50));
rect.setPosition(300,275);
rect.setFillColor(sf::Color::Red);
sf::RectangleShape bg;
bg.setSize(sf::Vector2f(800,600));
bg.setPosition(0,0);
bg.setFillColor(sf::Color::Blue);
//Font
sf::Font font;
if(font.loadFromFile("Data/bgothl.ttf") == 0)
{
return 1;
}
//Text
sf::Text title;
title.setFont(font);
title.setCharacterSize(40);
title.setString("Play");
title.setPosition(350, 270);
title.setColor(sf::Color::Green);
//Game loop
while(play == true)
{
//EVENTS
while (window.pollEvent(event))
{
if(event.type == sf::Event::Closed)
{
play = false;
}
if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
{
play = false;
}
if(event.type == sf::Event::MouseButtonPressed && event.key.code == sf::Mouse::Left)
{
mouseClick = true;
}
if(event.type == sf::Event::MouseButtonReleased && event.key.code == sf::Mouse::Left)
{
mouseClick = false;
}
}
//LOGIC
if(mouseClick == true)
{
mouseX = sf::Mouse::getPosition(window).x;
mouseY = sf::Mouse::getPosition(window).y;
if(mouseX > 300 && mouseY > 275)
{
if(mouseX < 500 && mouseY < 325)
{
std::cout << "You clicked play" << std::endl;
}
}
mouseClick = false;
}
//RENDERING
window.clear();
window.draw(rect);
window.draw(title);
window.display();
}
//Clean up
window.close();
return 0;
}