SDL function(Pollevent)

Line 9 and 10 are a bit confusing for me does line 9 show events which can be of any type such as closing the window if that's correct than why we write SDL_PollEvent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  int main( int agrc , char * agrs [])
{
	SDL_Surface *screen = NULL;
	SDL_Init(SDL_INIT_EVERYTHING);
	screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
	bool running =true;
	while (running)
	{
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
			case SDL_QUIT:
				running =false;
				break;
			}
		}
		//logic and render
		SDL_Flip(screen);
	    }
		SDL_Quit();
		return 0;

}
When an event happens (window is closed, key is pressed, mouse is moved, etc.) the event will be added to something called the event queue.

Line 9 creates a SDL_Event variable named event. It has not been initialized (assigned to) yet so it's useless at this point.

On line 10 event is passed to the SDL_PollEvent function. SDL_PollEvent will look in the event queue and if it's empty it will simply return 0 (which will be treated as false by the while loop, making it stop). Otherwise it will copy the first event in the event queue to the SDL_Event object that you passed to the function (The event variable on line 9), remove it from the event queue and return 1 (which will be treated as true by the while loop).
Topic archived. No new replies allowed.