0
当我运行这段代码(来自Lazy Foo SDL教程)时,程序立即关闭。这是为什么?如果因缺乏评论而变得糟糕,我很抱歉,但我认为这并不重要,因为在Lazy Foo的帖子上有评论。构建它时我没有遇到任何错误。程序立即关闭SDL
#include "SDL/SDL_image.h"
#include "SDL/SDL.h"
#include <string>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Surface *load_image (std::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface(loadedImage);
}
return optimizedImage;
}
void apply_surface (int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface (source, NULL, destination, &offset);
}
bool init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode (SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if (screen == NULL)
{
return false;
}
SDL_WM_SetCaption("Event test", NULL);
return true;
}
bool load_files()
{
image = load_image ("background.png");
if (image == NULL)
{
return false;
}
return true;
}
void clean_up()
{
SDL_FreeSurface(image);
SDL_Quit();
}
int main(int argc, char* args[])
{
bool quit = false;
if (init() == false)
{
return 1;
}
if (load_files() == false)
{
return 1;
}
apply_surface(0,0, image, screen);
if(SDL_Flip(screen) == -1)
{
return 1;
}
while(quit == false)
{
while (SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
}
clean_up();
return 0;
}
如果你在Visual Studio中,请确保使用Ctr + F5(无需调试即可启动)运行它,而不是使用F5(启动调试)。 – FKaria
有太多的东西可能会出现错误,例如,您返回的每个错误代码都是相同的。 main的返回值应该是一个代表某事的错误代码,如果返回0,这意味着非错误,所有错误都会得到'1',所以你永远无法判断它们发生了什么。我建议你也在每个循环内添加调试注释,或者如果语句......好运:) – user1708860
也许添加一些printfs来查看它正在退出的位置?也许用'SDL_GetError()'? –