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

Author Topic: having more than 1 event loop  (Read 7051 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
having more than 1 event loop
« on: July 08, 2015, 06:16:51 am »
is it a bad to have more than 1 event loop?
because first i need 1 loop for the main game, and 1 loop inside the another class

class Game
{
     void event(); // event loop
}

class MainScreen
{
     void event(); // event loop
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: having more than 1 event loop
« Reply #1 on: July 08, 2015, 07:42:21 am »
Yes it is bad. The events processed in one loop will never be processed in the other.

A better design is to have a single global event loop, that dispatches the events to whoever needs them.
Laurent Gomila - SFML developer

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #2 on: July 08, 2015, 07:56:23 am »
do you mean like this?


class Game
{
     public:
         static void event(const sf::Event&);
}

class OtherClass
{
     public:
         void eventInThisClass();
}

void OtherClass::eventInThisClass()
{
     sf::Event event;
     Game::event(event);
     // other codes
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: having more than 1 event loop
« Reply #3 on: July 08, 2015, 08:44:43 am »
No, your code still uses the same design, just differently.

What I mean is that you have a single event loop at high-level, and all events are dispatched from this loop to all the classes that listen to events. In other words, it's not components that pull events, it's your high-level class (Game, Window or whatever you have) that pushes them to every component that needs to process them.
Laurent Gomila - SFML developer

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #4 on: July 08, 2015, 12:36:48 pm »
ever since i program in sfml, my problem for myself is working with event. XD

can you explain it in easier way or give me a simple example?

the author from gamefromscratch.com uses more than 1 even in his splash game
« Last Edit: July 08, 2015, 12:44:37 pm by lorence30 »

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: having more than 1 event loop
« Reply #5 on: July 08, 2015, 01:10:44 pm »
Looking at the code at gamefromscratch he uses one point to get all events and multiple points to process them.

You should not use the sfml tutorial there btw. because it is for sfml 1.6 and therefore quite outdated, trying to use sfml 2+ will probably lead to problems.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: having more than 1 event loop
« Reply #6 on: July 08, 2015, 01:39:23 pm »
In general, don't use SFML tutorials except the official ones, unless there's a good reason. How to use event is clearly explained in the official ones :)

As SpeCter says, the idea is to have at any given time only one routine that calls pollEvent(). Events can still be queued/forwarded/dispatched freely.

Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #7 on: July 09, 2015, 12:43:12 pm »
how can i pass an event to a function?

void MainScreen::show(sf::RenderWindow& window,bool& a,sf::Event& event);
when i pass an event to this function it doesnt detect it

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #8 on: July 09, 2015, 12:53:58 pm »
What do you mean "doesn't detect it"?
You can pass a reference (as you do) or pass it by value and that should be just fine.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #9 on: July 09, 2015, 04:28:27 pm »
Quote
What do you mean "doesn't detect it"?

actually the condition inside the function is

if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Return) )
    {
     //code
    }
    else if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
     //code
    }
    else if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
     //code
    }

but nothing happens when triggerring the ifs. i know sf::Keyboard::isKeyPressed(); returns bool

kitteh-warrior

  • Guest
Re: having more than 1 event loop
« Reply #10 on: July 09, 2015, 05:15:28 pm »
(click to show/hide)

So you are doing a check for is the key is currently pressed, inside of the event loop? ??? Why would you do that? This is how events are used.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #11 on: July 09, 2015, 05:36:06 pm »
Quote
What do you mean "doesn't detect it"?

actually the condition inside the function is

if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Return) )
    {
     //code
    }
    else if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
     //code
    }
    else if ( sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
     //code
    }

but nothing happens when triggerring the ifs. i know sf::Keyboard::isKeyPressed(); returns bool
You are not even checking the event variable you pass in, so how would you expect anything to happen based on it?
Also, mixing events and real-time input checking is usually a bad idea.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: having more than 1 event loop
« Reply #12 on: July 09, 2015, 10:50:41 pm »
so in my code, the conditions doesnt have anything to do with the event.
i could only check that is type of sf::Event?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: having more than 1 event loop
« Reply #13 on: July 09, 2015, 11:25:25 pm »
Please read the official tutorials on events and real-time inputs.

And if you have trouble understanding how one can pass variables into functions or deal with class structures, I highly recommend to go back to a good C++ book and read up on these topics. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/