SDL event handling not working

一世执手 提交于 2019-12-04 04:16:43

问题


I'm currently learning SDL by reading Lazy foo tutorials. I'm using code blocks 13.12 on Linux. I'm unable to get event handling to work correctly.

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

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;

}

`


回答1:


While it requires thorough debugging to determine what exactly happens, your problem is very likely to be caused by aliasing of libc's close function and yours. close is very important call used by many libraries, including Xlib (which is called by SDL). E.g. SDL_CreateWindow calls XOpenDisplay/XCloseDisplay to test display capabilities, but XCloseDisplay calls close on its connection socket, and it will call your function instead. It's hard to say what will happen after that, but it is certainly not what was intended.

Workaround is either renaming function to something else (e.g. by giving it a prefix) or by declaring it static so its name wouldn't be exported. Note that static functions may only be used within single translation unit (i.e. if your .c file contains static function, you cannot easily use it from another .c file).

Linker doesn't report multiple definitions here because libc's close is a weak symbol (nm -D /lib/libc.so.6 | egrep ' close$' reports W).




回答2:


Try moving close(); right after the end of the while (run) loop and before the // Blitting nad updating. Also, move SDL_BlitSurface(imgSurface, NULL, scrsurface, NULL); SDL_UpdateWindowSurface(window); somewhere else, as it will run after your program stops running, and all code after that will also run when run is set to 0.



来源:https://stackoverflow.com/questions/31482117/sdl-event-handling-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!