Warning: sprintf(): Too few arguments in /home/adfxz727/public_html/wp-content/themes/minimal-grid/assets/lib/breadcrumbs/breadcrumbs.php on line 259

How to do local multiplayer event handling​

These instructions are general and abstract so that it can be applied to lower-level video game libraries such as SDL, SDL2 and SFML.The instructions generally can be applied to any video game library that polls for events similar to SDL, SDL2 and SFML.

Step 1:

Poll for events and save each event into a queue for processing. For multiple players, have a queue for each player and save specific events .

It is recommended to use a specific enum as a data type to save the event info into a queue.

Specifically use queue container since ideally the earliest events need to be processed and removed first which queue allows with its FIFO(First In First Out) nature.

Example:


//If button on first joystick is pressed

//save button press to player 1 event queue

if(event == joystick1_button_down){

event_queue_player1.push(JOYSTICK_BUTTON_DOWN);

}

//If button on second joystick is pressed

//save button press to player 2 event queue

if(event == joystick2_button_down){

event_queue_player2.push(JOYSTICK_BUTTON_DOWN);

}

Step 2:

Read each event from the queues of each player and do something with it. Read through the queue by reading the event on top of the queue, do an action based on the event, pop the event on top of the queue. Repeat until queue is empty. Repeat for each player.

Example:

//while the player 1 event queue is not empty

while(event_queue_player1.empty() == false){

     if(event_queue_player1.front() == JOYSTICK_BUTTON_DOWN){

         //Do something here

     }

event_queue_player1.pop();

}

//while the player 2 event queue is not empty

while(event_queue_player2.empty() == false){

     if(event_queue_player2.front() == JOYSTICK_BUTTON_DOWN){

         //Do something here

     }

event_queue_player2.pop();

}