SDL程序启动后立即退出。我做错了什么?

时间:2014-03-31 22:07:40

标签: c++ sdl

我正在尝试在SDL的帮助下编写一个简单的程序。该程序加载到SDL_Texture中,并将其呈现给窗口。但是,在启动我的程序后,它会立即退出。我正在使用VS Express 2013,将SDL2和SDL2_image库添加到项目中。可能是什么问题?提前谢谢!

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <string>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

bool init();
bool loadMedia();
void close();
// Loads individual image as texture
SDL_Texture* loadTexture(std::string);

SDL_Event event;
SDL_Window* gWindow = NULL;
// The window renderes
SDL_Renderer* gRenderer = NULL;
// Current displayed texture
SDL_Texture* gTexture = NULL;

int main(int argc, char* args[])
{
    if (!init())
    {
        std::cout << "An error has occured while initializing the window: " << SDL_GetError();
        return 1;
    }

    if (loadMedia())
    {
        std::cout << "Could not load media!";
        return 1;
    }

    bool quit = false;
    // main game loop
    while (!quit)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                quit = true;
                break;      
            }
        }

        // Clear screen
        SDL_RenderClear(gRenderer);

        // Render texture to screen
        SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);

        SDL_RenderPresent(gRenderer);
    }

    close();
    return 0;
}

bool init()
{
    //initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) == -1)
    {
        std::cout << "SDL could not initialize! Error: " << SDL_GetError();
        return false;
    }

    // Set texture filtering to linear
    if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
        std::cout << "Warning: Linear texture filtering not enabled!";

    //create window
    gWindow = SDL_CreateWindow("Hello World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

    if (gWindow == NULL)
        return false;

    // Hardware accelerated renderer
    gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);

    if (gRenderer == NULL)
        return false;

    // Initialize renderer color
    SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

    //Initialize PNG loading 
    int imgFlags = IMG_INIT_PNG; 
    if(!(IMG_Init(imgFlags ) & imgFlags))
    { 
        std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError();
        return false;
    }

    return true;
}

bool loadMedia()
{
    gTexture = loadTexture("texture.png");

    if (gTexture == NULL)
    {   
        std::cout << "Failed to load texture image!!";
        return false;
    }

    return true;
}

SDL_Texture* loadTexture(std::string path)
{
    SDL_Texture* newTexture = NULL;

    // Load an image from a specific path
    SDL_Surface* loadedSurface = IMG_Load(path.c_str());

    if (loadedSurface == NULL)
    {
        std::cout << "Error while loading surface: " << IMG_GetError();
        return newTexture;
    }

    newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);

    SDL_FreeSurface(loadedSurface);
    loadedSurface = NULL;

    return newTexture;
}

void close()
{
    SDL_DestroyTexture(gTexture);
    SDL_DestroyWindow(gWindow);
    IMG_Quit();
    SDL_Quit();
}

3 个答案:

答案 0 :(得分:2)

您的代码在我的系统上测试它很好。我怀疑它是以下之一:

  1. 您尝试加载的纹理不在正确的目录中。这会导致应用程序在尝试加载纹理时失败。根据您的代码,纹理应与VC ++ Project文件位于同一文件夹中。您可以将其放在其他目录中,只需在加载时指定路径即可。

  2. 您没有SDL和SDL_image DLL文件与可执行文件位于同一目录中。这将导致应用程序在初始化调用SDL和SDL_image期间失败。这些是我在目录中的DLL(我倾向于包含所有图像类型的库,但如果你只使用一种图像格式,你就可以拥有它):

    • 的libjpeg-9.dll
    • libpng16-16.dll
    • 的libtiff-5.dll
    • libwebp-4.dll
    • SDL2.dll
    • SDL2_image.dll
  3. 通过一次单步执行代码,很容易找到失败的地方。

    我要做的唯一其他评论是loadmedia()返回一个bool,因此您应该将if (loadMedia() == NULL)更改为if (!loadMedia())

答案 1 :(得分:1)

再加一点 - 在某些Windows平台上 libpng16-16.dll 需要 zlib1.dll 才能在文件夹位置显式添加 libpng16-16 .dll

您可以从http://www.zlib.net/找到预编译的 zlib1.dll

答案 2 :(得分:0)

对我来说很好。

尝试用return替换main()内的while(1)。也许您收到错误消息并立即关闭应用程序?