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.
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
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();
// ...
}
}
}
}