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

Author Topic: push Events...  (Read 24158 times)

0 Members and 2 Guests are viewing this topic.

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
push Events...
« on: February 25, 2016, 02:55:36 pm »
Hello.

So this is my situation. Previously I had this (pretty standard) :


int main (int argc, char **argv) {

        Big_Drawable BD_1;
        Big_Drawable BD_2;
        ...
        Big_Drawable BD_n; // n is pretty large !

        while (window.isOpen()) {

                while (window.pollEvent (event) {

                        switch (event) {

                                case Event::MouseButtonPressed:

                                        // Do tons of stuff with BD_n and thounsands of other local variables.
                                // etc..
                        }
                }
        }
}


 

This is perfect.

Now : I want to include another feature. This app should receive signals not only from the OS, but also from other computers (let's say from the network).

I have somthing like this :



void clickedCallback () {
        // Here I want to do exactly the same stuff
        // as in the MouseButtonPressed.
        // But it's large code with huge amount of variables
        // That I can't easily put in an external function
}

// ... other callbacks for keyboards etc...



int main (int argc, char **argv) {

        Big_Drawable BD_1;
        Big_Drawable BD_2;
        ...
        Big_Drawable BD_n; // n is pretty large !


        RegisterCallback (clickedCallback);


        while (window.isOpen()) {
               
                while (listenToNetwork()); // Listen and call the callbacks when necessary

                while (window.pollEvent (event) {

                        switch (event) {

                                case Event::MouseButtonPressed:

                                        // Do tons of stuff with BD_n and thounsands of other local variables.
                                // etc..

                        }
                }
        }
}

 


So I really would enjoy doing just this :


void clickedCallback () {
        // Here I want to do exactly the same stuff
        // as in the MouseButtonPressed.
        // But it's large code with huge amount of variables
        // That I can't easily put in an external function

        Event event;

        event.type = Event::MouseButtonPressed;

        window.pushEvent (event); // <---- Yupi!

}

 

And that's it !  :D

Instead, I have to put all the code in external functions, with tons and tons of parameters to get all my datas.


cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #1 on: February 25, 2016, 05:23:54 pm »
I reply yo myself because I found a work-arround using a second queue... But still, it's stupid work.

class HandleHOTAS {
public:
        HandleHOTAS (RenderWindow &win) : window(win) {
        }
        void handle_hotas (const HOTAS *msg) { // <- This is the callback function
                Event event;
               
                if (msg->left_click) {
                        event.type = Event::MouseButtonPressed;
                        event.mouseButton.button = Mouse::Left;
                        eventList.push (event);
                }
               
                 // [..... ]
        }
        bool pollEvent (Event &event) { // <- This is the new pollEvent function
                if (window.pollEvent (event)) return true;
                if (!eventList.empty()) {
                        event = eventList.front();
                        eventList.pop();
                        return true;
                }
                return false;
        }
       
private:
        RenderWindow &window;
        std::queue<Event> eventList;
};

 

Now I would call this HandleHOTAS::pollEvent instead of the RenderWindow::pollEvent

« Last Edit: February 25, 2016, 05:28:36 pm by cantor »

Cmdu76

  • Full Member
  • ***
  • Posts: 194
    • View Profile
Re: push Events...
« Reply #2 on: February 25, 2016, 05:30:02 pm »
If you can use lambda, you can use this solution :

std::function<void()> myCallback;

int main (int argc, char **argv) {

    Big_Drawable BD_1;
    ...
    Big_Drawable BD_n;
       
        myCallback = [&](){
                // Here your clicked function
        }

    while (window.isOpen()) {
               
                while (listenToNetwork()) {
                        if (packetType == ...) {
                                myCallback();
                        }
                }

        while (window.pollEvent (event) {
            switch (event) {
                case Event::MouseButtonPressed:
                                        myCallback();
                                       
                                // ...
            }
        }
    }
}

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #3 on: February 25, 2016, 05:33:03 pm »
If you can use lambda, you can use this solution :


Yes, I know. But I'm stick with plain old C++

Nested functions don't look good to me anyway.

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: push Events...
« Reply #4 on: February 26, 2016, 01:22:44 am »
Yes, I know. But I'm stick with plain old C++

Lambdas are part of the C++ language.

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #5 on: February 26, 2016, 09:26:08 am »
Yes, I know. But I'm stick with plain old C++

Lambdas are part of the C++ language.

No kidding?  ;D

You know what I meant: C++ 98.



R23MJ

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Re: push Events...
« Reply #6 on: February 26, 2016, 03:11:01 pm »
Do you have a specific reason for using only c++98? C++14 is the standard now (I believe C++11 more widely supported though; Don't quote me there.)

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #7 on: February 26, 2016, 07:26:47 pm »
This thead is not about the language I wish to use but about the API of SFML.

And I think it would be usefull to be able to push events in the SFML's queue.

That was an example but I can imagine a lot more, whenever you have your own driver for user actions and wish to group all the action management code in one place, with the same queue, instead of having to make a new redundant queue next to it and useless work.
« Last Edit: February 26, 2016, 07:42:14 pm by cantor »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: push Events...
« Reply #8 on: February 26, 2016, 08:28:01 pm »
I think this is more an ugly hack to work around a bad design, than a real feature (at least in your case) ;)

The real solution to your problem is to find a better design, with a better abstraction/decoupling between your logic and the inputs that trigger it. Faking a mouse press event just because you can't easily call the corresponding code, doesn't look like a proper solution ;)

For example, what are those BD_n variables? Shouldn't they be in an array?
Laurent Gomila - SFML developer

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #9 on: February 26, 2016, 08:50:16 pm »
I think this is more an ugly hack to work around a bad design, than a real feature (at least in your case) ;)

The real solution to your problem is to find a better design, with a better abstraction/decoupling between your logic and the inputs that trigger it. Faking a mouse press event just because you can't easily call the corresponding code, doesn't look like a proper solution ;)

For example, what are those BD_n variables? Shouldn't they be in an array?

Thanks for the answer.
The design was fine before I added this "feature". I agree with you about that, but what is wrong with my solution? It's not so ugly. It's not a fake mouse press, it's a real mouse press but the signal was not generated by sfml.

Doing two separate signal handling management code looks ugly to me too. I would even like to create custom events  that could be handled in the same loop just like other sfml events, but I'm going too far probably for you.

The BD_n was just supposed to represent tons of variables of different types, boring to pass to a function. (Functions with 100 parameters is just a ugly hack lol )

Passing a structure with pointers to the variables : even worse.

« Last Edit: February 26, 2016, 08:54:04 pm by cantor »

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: push Events...
« Reply #10 on: February 26, 2016, 10:57:33 pm »
No kidding?  ;D

You know what I meant: C++ 98.

No, I didn't know what you meant. You sounded to me like you thought Lambdas were a compiler extension or something.

But more importantly, why would you only want to use C++98?

It's not a fake mouse press, it's a real mouse press but the signal was not generated by sfml.

Could you explain further?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: push Events...
« Reply #11 on: February 27, 2016, 09:45:04 am »
Quote
Doing two separate signal handling management code looks ugly to me too.
I don't think so. What you're doing is to create your own higher-level event loop, by merging SFML native events and events generated by your code. I would probably do exactly the same thing if I had to deal with custom events.

Quote
I would even like to create custom events  that could be handled in the same loop just like other sfml events
Isn't it exactly what you do in HandleHOTAS::pollEvent? If by "custom events" you mean something else than sf::Event, then it's fairly easy to do on top of what you already wrote. Just write your own Event structure that can hold either a sf::Event or something else.

Quote
The BD_n was just supposed to represent tons of variables of different types, boring to pass to a function. (Functions with 100 parameters is just a ugly hack lol )
Still a bad design in my opinion. Nobody should have to deal with 100 different variables in a function.
Laurent Gomila - SFML developer

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #12 on: February 27, 2016, 10:21:16 am »
No kidding?  ;D

You know what I meant: C++ 98.

No, I didn't know what you meant. You sounded to me like you thought Lambdas were a compiler extension or something.

But more importantly, why would you only want to use C++98?

It's not a fake mouse press, it's a real mouse press but the signal was not generated by sfml.

Could you explain further?

Why? Because I already have 7000 lines of c++-98 and I dont want to mix them with -11 just to have a lambda function. And also because I master -98 programming but never really got interested in the new stuff. But I will...

Well it's just a mouse press but coming from a joystick system (HOTAS) plugged on another computer running another program. The two apps are communicating through UDP multicast. (LCM protocol)
It's like a huge flight simulator system with many computers running many programs with some Sfml-powered MMI
« Last Edit: February 27, 2016, 11:17:09 am by cantor »

cantor

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: push Events...
« Reply #13 on: February 27, 2016, 10:34:26 am »
Isn't it exactly what you do in HandleHOTAS::pollEvent? If by "custom events" you mean something else than sf::Event, then it's fairly easy to do on top of what you already wrote. Just write your own Event structure that can hold either a sf::Event or something else.

Indeed it's exactly what I do.
The goal of this topic is to propose new features in the SFML library so people with the same needs dont have to write again and again the same thing on top of the library, right ?

Just adding a pushEvent function, and maybe adding a customizable field in the Event structure, would do the trick just fine
« Last Edit: February 27, 2016, 10:45:00 am by cantor »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: push Events...
« Reply #14 on: February 27, 2016, 11:34:01 am »
Quote
Just adding a pushEvent function, and maybe adding a customizable field in the Event structure, would do the trick just fine
Yes it would. Although the "customizable" field in sf::Event would be ugly (a void*? with dynamic allocation/deallocation?).

But is it the right thing to do to solve your problem? I'm not sure. I'll probably need more relevant use cases to change my mind ;)
Laurent Gomila - SFML developer

 

anything