简体   繁体   中英

SDL2 events don't work in C

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

#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;
}

好的,我忘了在构建选项 > 链接器选项中链接库:-lSDL2main -lSDL2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM