SFML community forums

Help => General => Topic started by: Xrey274 on June 01, 2018, 05:26:23 pm

Title: Keyboard event not working.
Post by: Xrey274 on June 01, 2018, 05:26:23 pm
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>

using namespace std;

class Dot
{
    private:
        sf::CircleShape dot;
        sf::Mouse mouse;
        sf::Vector2f position;

    public:
        sf::CircleShape drawDot()
        {
            position = static_cast<sf::Vector2f>(mouse.getPosition());

            dot.setRadius(150);
            dot.setFillColor(sf::Color::Green);
            dot.setPosition(position);

            return dot;
        }
};


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Snake");
    sf::Event event;
    sf:: Time time = sf::seconds(5);

    Dot dots;

    while(window.isOpen() == true)
    {
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(event.type == sf::Event::KeyPressed)
            {
                sf::CircleShape gm;
                gm = dots.drawDot();
                window.draw(gm);
            }
            window.clear(sf::Color::Black);

            window.display();
        }
    }
}

When debugging it completly skips the keyboard if statement.
Title: Re: Keyboard event not working.
Post by: eXpl0it3r on June 01, 2018, 06:02:37 pm
It isn't, but right after drawing, you clear the whole screen and then display an empty screen. ;)
Title: Re: Keyboard event not working.
Post by: Xrey274 on June 02, 2018, 04:02:58 pm
I tweaked the code a bit and now I am getting a "No match for operator[]" at line 53;

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

sf::CircleShape drawDot(sf::Vector2f position)
{
    sf::CircleShape dot;

    dot.setRadius(rand());
    dot.setFillColor(sf::Color::Green);
    dot.setPosition(position);

    return dot;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Little Painter");
    sf::Event event;
    sf::Vector2f position;
    sf::Mouse mouse;

    vector<sf::CircleShape> circles;

    while(window.isOpen() == true)
    {
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(event.type == sf::Event::KeyPressed)
            {
                position = static_cast<sf::Vector2f>(mouse.getPosition(window));

                circles.push_back(drawDot(position));


            }

            window.clear(sf::Color::Black);

            for(vector<sf::CircleShape>::iterator it = circles.begin(); it != circles.end(); it++)
            {
                window.draw(circles[it]);
            }

            window.display();


        }

    }
}
Title: Re: Keyboard event not working.
Post by: G. on June 02, 2018, 06:32:07 pm
That's not how C++ vector iterators work.
Title: Re: Keyboard event not working.
Post by: Xrey274 on June 02, 2018, 07:21:28 pm
How do they work?
Title: Re: Keyboard event not working.
Post by: Xrey274 on June 02, 2018, 07:35:09 pm
Lol i remember now, sorry for the trouble.