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

Author Topic: Help me with drawing  (Read 1271 times)

0 Members and 1 Guest are viewing this topic.

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Help me with drawing
« on: July 10, 2013, 12:10:20 am »
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;
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Help me with drawing
« Reply #1 on: July 10, 2013, 12:29:11 am »
You can clear the window with a specific color instead of the default one (black).
For example:
window.clear(sf::Color::Blue);

Also, using event.key.code within sf::Event::MouseButtonPressed (or Released) is wrong.
Use event.mouseButton.button, as stated in the events tutorial.
« Last Edit: July 10, 2013, 12:32:41 am by G. »

Jeckie

  • Newbie
  • *
  • Posts: 22
    • View Profile
    • Email
Re: Help me with drawing
« Reply #2 on: July 10, 2013, 12:44:57 am »
Thanks, I got it now.
I made a variable
bool intro = false;
Changed the rendering part to this
                if(intro == true)
                {
                        window.clear();

                        window.draw(rect);
                        window.draw(title);

                        window.display();
                } else
                {
                        window.clear(sf::Color::Blue);

                        window.display();
                }
And if I click on the rectangle the var
intro = false;

 

anything