SDL_Image库使程序崩溃或使其行为异常

时间:2018-08-09 19:57:49

标签: c++ sdl

我有一个sdl2程序,可以在我的机器上正常运行(win xp 32bit)。当我将其发送给我的朋友(赢得10个64位)时,该程序显示SetRenderDrawColor并在几秒钟后崩溃。 该程序使用SDL2和SDL2_Image。

然后我从一个网站复制了一个准系统sdl2程序。 它可以在我们的两台计算机上使用。

然后我修改了程序以使用SDL_Image库中的IMG_Load,它再次在我端正常运行,但是这次并没有崩溃,而是只显示设置的RenderDrawColor而不显示已加载的图像,并在之后关闭SDL_Delay在我的朋友计算机上完成。

我使用的编译命令是:

g++ main.cpp -lSDL2 -lSDL2main -lSDL_Image -static-libgcc -static-libstdc++ -o main.exe

此外,我会说,如果我没有#define SDL_MAIN_HANDLED,则sdl2程序不能在win7或win xp计算机上运行。但是,我从未见过的教程说我需要使用这种东西。

有帮助吗?

编辑:发布准系统程序代码

#define SDL_MAIN_HANDLED

#include <SDL2/SDL.h>
#include <SDL2/SDL_Image.h>
#include <iostream>

int main( int argc, char* args[] )
{
    int posX = 100;
    int posY = 200;
    int sizeX = 300;
    int sizeY = 400;
    SDL_Window* window;
    SDL_Renderer* renderer;
    SDL_Event event;

    // Initialize SDL
    // ==========================================================
    if ( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
    {
        // Something failed, print error and exit.
        std::cout << " Failed to initialize SDL : " << SDL_GetError() << std::endl;
        return -1;
    }

    // Initialize SDL_Image
    int flags = IMG_INIT_JPG|IMG_INIT_PNG;
    int initted = IMG_Init(flags);

    if((initted&flags) != flags)
    {
      printf("IMG_Init: Failed to init required jpg and png support!\n");
      printf("IMG_Init: %s\n", IMG_GetError());
    }

    // Create and init the window
    // ==========================================================
    window = SDL_CreateWindow( "test", posX, posY, sizeX, sizeY, 0 );

    if ( window == nullptr )
    {
        std::cout << "Failed to create window : " << SDL_GetError();
        return -1;
    }

    // Create and init the renderer
    // ==========================================================
    renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );

    if ( renderer == nullptr )
    {
        std::cout << "Failed to create renderer : " << SDL_GetError();
        return -1;
    }

    // Render something
    // ==========================================================    
    // Load image
    SDL_Surface* loader = IMG_Load("background.jpg");
    SDL_Texture* sprite = SDL_CreateTextureFromSurface(renderer, loader);

    if(loader == NULL)
    {
       std::cout << "Loader failed!" << std::endl << std::endl;
       IMG_GetError();
    }
    // Set size of renderer to the same as window
    SDL_RenderSetLogicalSize( renderer, sizeX, sizeY );

    // Set color of renderer to red
    SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 );

    // Clear the window and make it all red
    SDL_RenderClear( renderer );

    // Draw image
    SDL_RenderCopy(renderer, sprite, 0, 0);

    // Render the changes above ( which up until now had just happened behind the scenes )
    SDL_RenderPresent( renderer);

    // Pause program so that the window doesn't disappear at once.

    while(true)
    {
      SDL_PollEvent(&event);

      if(event.type == SDL_QUIT) return 0;

      SDL_Delay( 10 );
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

通过将MingGW安装中的libpng16-16.dll and zlib1.dll添加到分布式文件夹中,解决了该问题。

如果其他任何人遇到此问题,则此工作所需的.dll总数为:

SDL2.dll | SDL2_Image.dll | libwinpthread-1.dll | libpng16-16.dll| zlib1.dll