SDL_PollEvent does not catch SDL_QUIT

Hey there,
trying to setup my classes to programm faster with SDL2 - so far evrything worked fine.
But in the end of my last coding session my SDL_PollEvent does no longer catch SDL_QUIT.
I´m using my own SDL_Window class to handle events and other SDL relevant Vars.
In my Window class i have the function loadEvent(), its my gameloop function, and in it i call SDL_PollEvent and right after i check if it is SDL_QUIT and if so it returns a false, otherwise it returns true. SDL_PollEvent saves into a private SDL_Event varriable of my class so i can later ask for keys pressed by looking into the event of my window class.
Any keydown event works just fine, i can end my game with esc if i want to, but i never seem to catch SDL_QUIT.
Any ideas what could be the problem?

Code:

class My_SDL_Window{
private:
SDL_Window* window;
SDL_Surface* surface;
SDL_Renderer* renderer;
SDL_Event sEvent;
My_Object_Node* allObj;
public:
...
bool loadEvent()
{
    if(SDL_PollEvent( &sEvent ) != 0)
    {
        if(sEvent.type == SDL_QUIT)
        {
            destroy();
            return false ;
        }
    }
    return true;
}

Since i cant even get one false return by this function i hope this is enough code for you to help me.

Hello ThoastBot, welcome.

Please take sometime to fix your post. Code should be market as code using the </> icon on editor. That will help in readibility.

What destroy() do? have you tried debug or add printf markers to check where your code hit?

Also I would not use a single if statement to process the event pool, if you have more than one event (for example mouse movement) you will only grab the first thing and ignore all else. Try to change if(SDL_PollEvent()) with a while() statement.

You can check an example in LazyFoo’s tutorial ( http://lazyfoo.net/tutorials/SDL/03_event_driven_programming/index.php ) here.

Let me know if it works.

I fixed the code, i tryed the </> icon but it didn´t work so i used the " and i hope this is ok :slight_smile:

Destroy() has to be called before i end my programm, it ends SDL stuff.

Code:

void destroy()
	{
		SDL_DestroyRenderer( renderer );
		SDL_FreeSurface( surface );
		SDL_DestroyWindow( window );
		TTF_Quit();
		SDL_Quit();
	}

I changed it to a while() statement but it didn´t work.
But what u said made me reconsider my code, maybe you could help becuz i gues while wont work for me as good.
I didn´t implement moving yet but lets say i do - to see if i pressed ‘D’ ( and then move right ) i wanna call my sEvents from this window class and use it in a player class that has the function ‘move right if key pressed (getSEvent())’, if it only saves 1 statement with if() that could turn out to be fatal, but while() wont help either becuz i gues it just saves 1 statement in the end aswell? ^^,
Is there a way i can save all events at the same time so i can just recall them in other functions and then reload? Like a list of bool for used Events that i can reload?

You could use a struct with flags to process input. Something like:

enum ENTITY_DIRECTION {
   DIR_UP = 0x1,
   DIR_DOWN = 0x2,
   DIR_LEFT = 0x4,
   DIR_RIGHT = 0x8
};

struct player {
     ...              // Player stuff
     uint direction;
     int posx;
     int posy;
     float speed;
     ...             // More player stuff
};

struct player *pm = malloc(sizeof *pm);

while(!quit) { 
    pm->direction = 0;  // Clean up movement

    // Process input
    while(SDL_PollEvent != 0) {
    {
         if(sEvent.type == SDL_KEYDOWN) {
             switch(sEvent.key.keysym.sym) {
                 case SDLK_UP: pm->direction |= DIR_UP; break
                 case SDLK_DOWN: pm->direction |= DIR_DOWN; break;
                 case SDLK_LEFT: pm->direction |= DIR_LEFT; break;
                 case SDLK_RIGHT: pm->direction |= DIR_RIGHT; break;
             }
        }
    }

    // Update state
    if(pm->direction & DIR_UP) pm->posy += pm->speed * dt;
    if(pm->direction & DIR_DOWN) pm->posy -= pm->speed * dt;
    if(pm->direction & DIR_LEFT) pm->posx -= pm->speed * dt;
    if(pm->direction & DIR_RIGHT) pm->posx += pm->speed * dt;

   // Render 
   entity_render(player);
}

Of course input and update and render should be correctly assembled to handle multiple entities. That was just an idea for a single entity.

The main idea is to keep separated where the physics is applied and where input processing is done. We can also add more layers of abstraction, for example if you have keys remapping where SDKL_UP is attached to some command() and the command() updates the correct structures.

Also for a real game is nice to have better structures, and characters should have at least 2 vectors, one related to position and another for direction (where the force vector is pointing to), we can also have another vector called facing (where the character is looking to). Often forces are added using the looking postion as reference to the direction force vector and the result is added to the position vector.

And sorry for the plain C code, but I don’t do C++.

Also better ideas are welcome.

Thanks, u gave me some ideas.
I gonna try some stuff and if it still doesn´t work i´ll write here again.

Well now i got a realy good working event list that can catch more than one key press (or events) at the same time. However it still cant catch SDL_QUIT so the problem i had still exists :confused:
Here are my changes:

struct My_Event_List{
	bool quit = false;
	bool w = false;
	bool a = false;
	bool s = false;
	bool d = false;
};

private:
    ...
	My_Event_List events;
    ...
public:
    ...
    bool runs()
	{
		if(events.quit) return false;
		return true;
	}

	void loadEvent()
	{
		while(SDL_PollEvent( &sEvent ) != 0)
		{
			switch(sEvent.type)
			{
				case SDL_QUIT:
					events.quit = true;
					break;

				case SDL_KEYDOWN:
					if(sEvent.key.keysym.sym == SDLK_w) events.w = true;
					if(sEvent.key.keysym.sym == SDLK_a) events.a = true;
					if(sEvent.key.keysym.sym == SDLK_s) events.s = true;
					if(sEvent.key.keysym.sym == SDLK_d) events.d = true;
					break;

				case SDL_KEYUP:
					if(sEvent.key.keysym.sym == SDLK_w) events.w = false;
					if(sEvent.key.keysym.sym == SDLK_a) events.a = false;
					if(sEvent.key.keysym.sym == SDLK_s) events.s = false;
					if(sEvent.key.keysym.sym == SDLK_d) events.d = false;
					break;
			}
		}
	}
    ...

With this i can get for example the keys A and D to turn on true but i dont seem to get SDL_QUIT (i checked by cout the bools in the event list). I dont seem to get why he cant catch the SDL_QUIT. I wrote some SDL code before and never had any problem with the SDL_QUIT - and since SDL_KEYDOWN/SDL_KEYUP work just fine i dont understand where to look for the mistake.
And i cant seem to find anything about JUST SDL_QUIT not working. Most post are talking how other stuff doesn´t get caught, but they all wrote SDL_QUIT works…

Any other ideas what possibly could be a reason just SDL_QUIT doesn´t get caught? I use Windows, write on Eclipse, compile with MinGW and use SDL2.

Edit:
I checked the LazyFoo link now and to be sure its not the switch() thats causing problems i even made this, but still no SDL_QUIT gets caught…

void loadEvent()
		{
			while(SDL_PollEvent( &sEvent ) != 0)
			{
				if(sEvent.type == SDL_QUIT) events.quit = true;
				else if(sEvent.type == SDL_KEYDOWN)
				{
					if(sEvent.key.keysym.sym == SDLK_w) events.w = true;
					if(sEvent.key.keysym.sym == SDLK_a) events.a = true;
					if(sEvent.key.keysym.sym == SDLK_s) events.s = true;
					if(sEvent.key.keysym.sym == SDLK_d) events.d = true;
				}
				else if(sEvent.type == SDL_KEYUP)
				{
					if(sEvent.key.keysym.sym == SDLK_w) events.w = false;
					if(sEvent.key.keysym.sym == SDLK_a) events.a = false;
					if(sEvent.key.keysym.sym == SDLK_s) events.s = false;
					if(sEvent.key.keysym.sym == SDLK_d) events.d = false;
				}
			}
		}

I prefer to go with switch statements since each event in pool will be handled each time. I think that switch statement is clearer to read and avoid multiple if checks in a row. IIRC case works jumping to the right place in code while all ifs will be checked no matter if one of those is true.

There is no need to handle key up (at least using my approach since I reset all states in each game loop.

Using bools for keystate takes too much memory, is easier and faster couple a group of things in a single variable. 32bit int can store 32 different independent states. AFAIK sizeof(bool) in C++ is 8bit (1Byte) and using instead uint8_t allows you to store 4 directions and more.

Using bitwise operations is faster than bool evaluation and takes less memory space, is a win-win situation.

I would like to advice you to upload a minimum working example of your problem instead just show snippets. There is no way to help further without a full code.

So i tryed to make a minimal working example, couldn´t get something small together that reproduced the problem.
Since i am still at the beginning of this code i just thought i rewrite the whole thing, and well,… now it works.
Currently i compare both files to find what could be the diffrence that made this bug happen, but i cant seem to find anything.

I´ll consider using switch instead of if where i can and i will also look into your way of saving keystates - thanks for all those tipps.
If i somehow will find what caused the problem i´ll reply here.
Have a nice day and thanks again :slight_smile: