Game Development with SDL2 Part 2: Event Handling

Guest Posts Bilal Cheema

Author: Bilal Cheema

Previous Post of the Series: Game Development with SDL2 Part 1: Creating a Window

In this post, we are going to discuss all about event handling in SDL. But before moving ahead, I have some more things for you. In the previous post, we used SDL_WINDOW_SHOWN in SDL_CreateWindow() method for forcing the window to be shown. We can have 0 or NULL here. But then, if there are other programs running, it might be possible that the window formed, is not shown and then, we have to click the application from below (i.e taskbar) to bring it on screen. Of course, we can have full screen, minimized screen and etc. All the window flags are here.

Similarly, there are some additional settings for renderer or you can say that there are some renderer flags as well which are listed here. The renderer flag, SDL_RENDERER_PRESENTVSYNC is something to understand. It limits the frame rates of the application to that of the refresh rate of the monitor which is usually, 60.

Multiple Flags

Now, you might be wondering that how is this possible to have flags: SDL_WINDOW_SHOWN and SDL_WINDOW_MAXIMIZED as parameters for the SDL_CreateWindow() method. It is possible by putting '|' in between the two as mentioned below:

//This will give a window that forces itself
//to be shown on screen as well as it will be maximized
SDL_Window* window = SDL_CreateWindow("Getting Started", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 800, 600,
SDL_WINDOW_SHOWN | SDL_WINDOW_MAXIMIZED);

What is Event Handling?

Now, lets see what is event handling. It is just a way to catch the events and then, respond accordingly. Basically, we have to poll through all the events. Of course, there are way too much with event handling and there are many kinds of events. But we will not be facing all of them! At least, for now.

If you put all the codes as in the previous post in an infinite while loop, it will not respond to your action. It will not respond to that click on the cross button at the top left of the window! How can you close this now?

Fortunately, we have the stop button with almost every IDE. Or just have Ctrl+Shift+Delete to open up the task manager and then, end that task from here. But you may not expect the same from the user. It is not neat and comfortable! Why is there a cross on the top left of the window if you have to close it like this? We have to handle every key pressed down, every mouse click, every.....

Man! I am talking too much! Lets get it done with the codes below. It is almost the same as that of the previous post with some exceptions. It is now, in a while loop and has no SDL_Delay() method. The while loop contains another while loop which processes the events through the object called SDL_Event.

#include <iostream> //for rand() method
#include <SDL.h>

#undef main

int main(int argc, char** args)
{
                SDL_Init(SDL_INIT_EVERYTHING);

                SDL_Window* window = SDL_CreateWindow("Getting Started", SDL_WINDOWPOS_UNDEFINED,
                SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);

                SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, 0);

                //Struct for handling events
                SDL_Event input;

                //Boolean to find out whether the game is stopped or not
             //Of course, it will be false at start
                bool quit = false;

                //For selecting renderer's draw color
                int r = 0, g = 255, b = 0;

                while (!quit) //Same as while(quit == false)
                {

                                //---Event polling method---
                             //It runs until the number of events to be polled gets to zero
                                while (SDL_PollEvent(&input) > 0)
                                {

                                                //If the user did something which should
                                             //result in quitting of the game then...
                                                if (input.type == SDL_QUIT)
                                                {
                                                                //...go out of the while loop
                                                                quit = true;
                                                }
                                                //If the user pressed any key then...
                                                else if (input.type == SDL_KEYDOWN)
                                                {

                                                                //...randomize the renderer's draw color
                                                                r = rand() % 255;
                                                                g = rand() % 255;
                                                                b = rand() % 255;
                                                }

                                }
                                
                                SDL_SetRenderDrawColor(renderer, r, g, b, 255);
                                SDL_RenderClear(renderer);
                                SDL_RenderPresent(renderer);
                }

                SDL_DestroyRenderer(renderer);
                SDL_DestroyWindow(window);
                SDL_Quit();

                return 0;
}

Concept behind the Code

The concept of the events in SDL is that, there is a line of awaiting events which gets polled through the SDL_PollEvent() method. We have to poll through all the events till the number of events gets to zero (of course). Then, from the SDL_Event object, we have to get to that event's root. First comes the type of the event and for each type, there are further specializations and so on until we get to that actual event. For now, it is too simple. Now, the cross button at the top left corner of the window is properly responded. Pressing any key will change the color of the screen.

All the other code is as clear and self-explanatory as it looks! And if it is not, you must see my previous post. For more programming stuff, visit my blog: bacprogramming.wordpress.com

.

And as always, Happy Coding!