SDL event processing does not work

I am currently learning SDL reading Lazy foo tutorials. I use 13.12 code blocks for Linux. I cannot properly handle event handling.

I'm basically trying to display an image (which works fine), but no matter how many times I click the close button, it doesn't close

the code:

#include <SDL2/SDL.h>
#include <stdio.h>
//Declaring the main window, the main surface and the image surface
SDL_Window *window = NULL;
SDL_Surface *scrsurface = NULL;
SDL_Surface *imgSurface = NULL;
SDL_Event event;
int run = 1;

//The function where SDL will be initialized
int init();
//The function where the image will be loaded into memory
int loadImage();
//The function that will properly clean up and close SDL and the variables
int close();

//The main function
int main(void){
    if(init() == -1)
        printf("Init failed!!!");
    else{
        if(loadImage() == -1)
            printf("loadImage failed!!!");
        else{

            //Displaying the image

            while(run){
                //Event handling
               while(SDL_PollEvent(&event)){
                    switch(event.type){
                        case SDL_QUIT:
                            run = 0;
                            fprintf(stderr, "Run set to 0");
                            break;
                        default:
                            fprintf(stderr, "Unhandled event");
                         break;
                    }
                } 
                //Blitting nad updating
                SDL_BlitSurface(imgSurface, NULL, scrsurface, NULL);
                SDL_UpdateWindowSurface(window);
            }
        close();

        }
    }
    return 0;

}

int init(){
 if(SDL_Init(SDL_INIT_VIDEO) < 0)
    return -1;
 else{
    window = SDL_CreateWindow("SDL_WINDOW", SDL_WINDOWPOS_UNDEFINED,  SDL_WINDOWPOS_UNDEFINED, 900, 900, SDL_WINDOW_SHOWN);
    if(window == NULL)
        return -1;
    scrsurface = SDL_GetWindowSurface(window);

}
return 0;
}


int loadImage(){
    imgSurface = SDL_LoadBMP("Test.bmp");
    if(imgSurface == NULL)
        return -1;
    else{

    }
    return 0;
}

int close(){
    SDL_FreeSurface(imgSurface);
    SDL_DestroyWindow(window);
    window = NULL;
    SDL_Quit();
    return 0;

}

`

+3
source share
2 answers

, , , , , libc close . close - , , Xlib ( SDL). . SDL_CreateWindow XOpenDisplay/XCloseDisplay , XCloseDisplay close . , , , , , .

- - (, ), static, . , (.. .c , .c ).

, libc close (nm -D /lib/libc.so.6 | egrep ' close$' reports W).

0

close(); while (run) //Blitting nad update. , SDL_BlitSurface (imgSurface, NULL, scrsurface, NULL);               SDL_UpdateWindowSurface (); - , , , , 0.

+1

All Articles