The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Can't make work events in C and SDL2
César


Joined: 18 Jun 2015
Posts: 3
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. : /
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
use
Code:
e.type==SDL_WINDOWEVENT_CLOSE
instead.

SDL_QUIT looks like it isn't being received for some reason.
César


Joined: 18 Jun 2015
Posts: 3
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
Can't make work events in C and SDL2
Ryan C. Gordon
Guest

On 06/18/2015 02:50 PM, MrTAToad wrote:
Quote:
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))

Quote:
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.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
César


Joined: 18 Jun 2015
Posts: 3
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 ?
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
The -l commands are libraries that need to be used. Might be worth adding them to your project.

Quote:
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!