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()'? –