SDL2:我在第5行接收分段错误,SDL_Surface

时间:2015-06-28 17:40:02

标签: c++ c gcc sdl sdl-2

当我运行程序时,我在第5行得到一个分段错误,一个SDL_Surface指针正在获取窗口的表面。这是我的代码:

i

这是错误:

#include <stdio.h>
#include <stdlib.h>
#include "SDL2/SDL.h"
SDL_Window* window=NULL;
SDL_Surface* MainSurface=NULL;
SDL_Surface* ImageSurface=NULL;
void ifdebug(int pmbool,const char* str){
    if(pmbool==1){
        printf(str);
    }
}
void NewWindow(int x,int y,int pmbool){
    ifdebug(pmbool,"DEBUG^");
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Event MainEvent;
    int IsRunning=1;
    window=SDL_CreateWindow("SDL2: Window",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,x,y,SDL_WINDOW_SHOWN);
    if(window==NULL){
        ifdebug(pmbool,"WINDOW:ERROR<<");
        ifdebug(pmbool,SDL_GetError());
        ifdebug(pmbool,">>#\n");
        SDL_FreeSurface(ImageSurface);
        ImageSurface=NULL;
        SDL_DestroyWindow(window);
        window=NULL;
        SDL_Quit();
    }
    else{
        ifdebug(pmbool,"WINDOW:SUCCESS^");
    }
    MainSurface=SDL_GetWindowSurface(window);
    while(IsRunning){
        if(SDL_PollEvent(&MainEvent)!=0){
            if(MainEvent.type==SDL_QUIT){
                ifdebug(pmbool,"QUIT#\n");
                IsRunning=0;
                SDL_FreeSurface(ImageSurface);
                ImageSurface=NULL;
                SDL_DestroyWindow(window);
                window=NULL;
                SDL_Quit();
            }
        }
    }
}
void LoadImage(int pmbool,char* filename){
    ImageSurface=SDL_LoadBMP(filename);
    if(ImageSurface==NULL||filename==NULL){
        ifdebug(pmbool,"DEBUG^");
        ifdebug(pmbool,"LOADBMP:ERROR<<");
        ifdebug(pmbool,SDL_GetError());
        ifdebug(pmbool,">>#\n");
        SDL_FreeSurface(ImageSurface);
        ImageSurface=NULL;
        SDL_DestroyWindow(window);
        window=NULL;
        SDL_Quit();
    }
}
int main(int argc,char* args[]){
    NewWindow(600,400,1);
    LoadImage(1,"/home/vlad/Desktop/C-lang/BLU.bmp");
    SDL_BlitSurface(ImageSurface,NULL,MainSurface,NULL);
    SDL_UpdateWindowSurface(window);
    return 0;
}

是否与窗口有关?

1 个答案:

答案 0 :(得分:0)

OP修好了它。事实证明,关闭窗口的功能是阻止它加载。这是固定代码:

> echo "$?"
0
> echo "$result"
./.psp.conf.20150701