0
我得到一個分段錯誤,並且我將它跟蹤到了我的表面,它是NULL(但檢查就位不會觸發)。SDL曲面返回NULL?
我不確定是否正確創建表面。創建表面時是否需要添加某些東西,我錯過了什麼?
bool init()
{
//Initialize SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return false;
}
else
{
//Set texture filtering to linear
if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
{
printf("Warning: Linear texture filtering not enabled!");
return false;
}
else
{
//Create window
gWindow = SDL_CreateWindow("kPaint", 575, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
GUI = SDL_CreateWindow("GUI", 0, SDL_WINDOWPOS_UNDEFINED, 573, 542, SDL_WINDOW_SHOWN);
if(gWindow == NULL || GUI == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
if(gRenderer == NULL)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
surface = SDL_GetWindowSurface(gWindow);
GUIsurface = SDL_GetWindowSurface(GUI);
if(surface == NULL)
{
printf("surface could not be created!");
return false;
}
SDL_FillRect(surface, NULL, SDL_MapRGB(surface->format, 255,255,255));
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if(!(IMG_Init(imgFlags) && imgFlags))
{
printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError());
return false;
}
}
}
}
return true;
}
嘗試在SDL_GetWindowSurface()之前未創建渲染器。我可能是錯的,但是一旦你爲該窗口創建了渲染器,iirc SDL就不允許你獲取窗口表面 – neoaggelos
如果它在創建之後不是空的,而是在以後爲空,那麼必須做一些事情來改變它插圖中。 – molbdnilo
此外,您可以檢查SDL_FillRect()的返回值,以查看是否有錯誤 – neoaggelos