我正在制作一个简单的 SDL 应用程序,但由于某种原因,我的窗口不会保持打开状态并且程序崩溃。这是代码:
bool CApp::onInit() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
return false;
display = SDL_SetVideoMode(WIDTH, HEIGHT, BPP, SDL_HWSURFACE | SDL_DOUBLEBUF);
// Window pops up and disappears here.
if(display == nullptr) // display is NOT null
return false;
// Load the grid
Grid = CSurface::onLoad("Resources\\Images\\Grid.png");
if(Grid == nullptr)
return false; // Program crashes here due to onLoad returning nullptr.
// Load the X mark
X = CSurface::onLoad("Resources\\Images\\X.png");
if(X == nullptr)
return false;
// Load the O mark
O = CSurface::onLoad("Resources\\Images\\O.png");
if(X == nullptr)
return false;
return true;
}
阅读评论以查看程序错误的位置。
这是CSurface::onLoad()
功能。
SDL_Surface* CSurface::onLoad(char* File) {
SDL_Surface* surfTemp = nullptr; // Temporary Surface
SDL_Surface* surfReturn = nullptr; // Return Surface
surfTemp = SDL_LoadBMP(File); // This returns a nullptr for some reason
if(surfTemp == nullptr)
return nullptr; // This is the culprit for the program crashing.
// Optimize and free our surface.
surfReturn = SDL_DisplayFormat(surfTemp);
SDL_FreeSurface(surfTemp);
return surfReturn;
}
我一生都无法弄清楚为什么这不起作用。它以前工作过,但现在,它只是崩溃了!