【问题标题】:Program shuts down immediately SDL程序立即关闭 SDL
【发布时间】:2015-10-18 04:10:56
【问题描述】:

当我运行这段代码(来自 Lazy Foo SDL 教程)时,程序立即关闭。这是为什么?如果由于缺少 cmets 而变得有点混乱,我很抱歉,但我认为这并不重要,因为 Lazy Foo 的帖子上有 cmets。构建它时我没有收到任何错误。

#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(开始调试)运行它。
  • 那里有太多可能出错的地方,例如,您返回的每个错误代码都是相同的。 main 的返回值应该是一个代表某事的错误代码,如果返回 0,则表示没有错误,所有错误都为 '1',因此您永远无法判断发生了哪些错误。我建议你还在每个循环或 if 语句中添加调试 cmets...祝你好运:)
  • 也许添加一些 printfs 以查看它在哪里退出?也许是SDL_GetError()

标签: c++ sdl exit shutdown


【解决方案1】:

如果您在可执行目录中缺少 background.png 或任何必需的 DLL,您可能会遇到奇怪的崩溃。

由于这是本系列中较简单的程序之一,我敢打赌这就是问题所在。我自己也看到过奇怪的段错误,因为我脑子有问题,忘记了一个文件,或者它不是 exe 预期的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2017-09-04
    相关资源
    最近更新 更多