Can't make work events in C and SDL2

Hello,

I am trying to use SDL2 in C language compiled with GCC in CodeBlocks but the events don’t work, for example, I can’t close the window with SDL_QUIT. Is anything wrong on the code, please ?

Code:
#include <stdlib.h>
#include <stdio.h>
#include <SDL2/SDL.h>

int main(int argc, char** argv)
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stdout,"??chec\n", SDL_GetError());
return -1;
}

SDL_Window* windowImg = NULL;
SDL_Renderer* renderImg = NULL;
int quit = 0;
SDL_Event e;

windowImg = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 400, 400, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
renderImg = SDL_CreateRenderer(windowImg, -1, SDL_RENDERER_ACCELERATED);

while (!quit) {
    while (SDL_PollEvent(&e) != 0) {
        if (e.type == SDL_QUIT) {
            quit = 1;
            break;
        }
    }
}

SDL_DestroyWindow(windowImg);
SDL_Quit();

return 0;

}

It does nothing when I click on the X of the window, also the window become very tiny when I click on fullscreen. : /

use
Code:
e.type==SDL_WINDOWEVENT_CLOSE

instead.

SDL_QUIT looks like it isn’t being received for some reason.

use

e.type==SDL_WINDOWEVENT_CLOSE

instead.

Nope, SDL_QUIT is sent when all windows are closed, so that should work
as-is. Also, the window events are strange, you’d actually have to check
for this for your approach:

((e.type==SDL_WINDOWEVENT) && (e.window.event==SDL_WINDOWEVENT_CLOSE))

SDL_QUIT looks like it isn’t being received for some reason.

Fwiw, the program worked correctly here. Maybe he’s using really old
headers with a newer library, and the value of SDL_QUIT changed?

–ryan.On 06/18/2015 02:50 PM, MrTAToad wrote:

The -l commands are libraries that need to be used. Might be worth adding them to your project.

Fwiw, the program worked correctly here. Maybe he’s using really old
headers with a newer library, and the value of SDL_QUIT changed?

Looks like you were nearly correct!

Thanks for your answer.

I changed by SDL_WINDOWEVENT_CLOSE, but it still not close the window when I click the X. [Crying or Very sad]

Oh, sorry, I just found the solution.

It was in the build option in my project, I apparently needed to put -lSDL2 in the linker option and now it works well, all kind of events.

It was not on the tutorial I saw to install SDL2, but on an other one. It propose to add “-lmingw32 -lSDL2main -lSDL2” too, but I don’t really know what does it mean. Should I add all of them ?