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

Author Topic: event  (Read 1651 times)

0 Members and 1 Guest are viewing this topic.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
event
« on: August 23, 2013, 11:34:47 pm »
As I understand the event tutorial, the following code should not move the shape when the mouse is moved or a key pressed, but the shape does move?????
 

#include <SFML/Graphics.hpp>

int main()
{
    int x = 50, y = 50;
   
    sf::RenderWindow window(sf::VideoMode(800, 600), "Input data test" );
   
    sf::CircleShape shape(20);
    shape.setFillColor(sf::Color::Color(255,0,0));
   
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            window.clear(sf::Color(128,128,128));
            shape.setPosition(x, y);
            window.draw(shape);
            window.display();
            if(x < 750) x = x + 25;
            else
            {
                x = 50;
                y = y + 25;
            }
        }
    }
    return EXIT_SUCCESS;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: event
« Reply #1 on: August 23, 2013, 11:49:10 pm »
You don't seem to understand about nearly everything in the event tutorial, nor how to properly use [ code=cpp ] // My Code [ /code ]....

Please re-read it again and ask questions if you don't understand a part of it. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: event
« Reply #2 on: August 28, 2013, 09:45:48 pm »
What I am asking, is why the instructions are executed when no event is properly called????
Warren

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: event
« Reply #3 on: August 28, 2013, 10:01:09 pm »
Because if you move your mouse or press a key, it triggers an event regardless whether you handle the event or not. And if an event got triggered pollEvent(event) will return true, thus entering the while loop, thus drawing the shape.

Btw if you want to check if some part of the code gets reached, you're better off in just outputting some random string, rather than drawing a sprite. ;)

#include <SFML/Graphics.hpp>

#include <iostream>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Input data test" );
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            std::cout << "An event got triggered" << std::endl;
        }
        window.clear();
        window.display();
    }
}
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything