简体   繁体   English

SDL_Image 不显示图像

[英]SDL_Image not displaying an image

After fixing various other SDL errors, (both with SDL itself and SDL_Image) I have written this error-free code:在修复了其他各种 SDL 错误后(包括 SDL 本身和 SDL_Image),我编写了以下无错误代码:

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

using namespace std;

#define null 0

SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;

SDL_Texture* LoadImage(string file)
{
    SDL_Texture *texture = nullptr;

    texture = IMG_LoadTexture(renderer, file.c_str());
    if (texture == nullptr)
        cout << "Failed to load image: " + file + IMG_GetError();
    return texture;
}

void ApplySurface(int x, int y, SDL_Texture *textureArgument, SDL_Renderer *rendererArgument)
{
    SDL_Rect pos;
    pos.x = x;
    pos.y = y;
    SDL_QueryTexture(textureArgument, null, null, &pos.w, &pos.h);
    SDL_RenderCopy(rendererArgument, textureArgument, null, &pos);
}

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_EVERYTHING))
        return 1;

    window = SDL_CreateWindow("ShitHappens", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    SDL_Texture *image = nullptr;
    image = LoadImage("image.png");

    SDL_RenderClear(renderer);

    int iW, iH;
    SDL_QueryTexture(image, NULL, NULL, &iW, &iH);
    int x = 640 / 2 - iW / 2, y = 480 / 2 - iH / 2;

    ApplySurface(x, y, image, renderer);
    SDL_RenderPresent(renderer);

    SDL_Delay(2000);
    SDL_DestroyTexture(image);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);

    return 0;
}

Which still does not display the image in question.这仍然不显示有问题的图像。 I have tried replacing the functions with the SDL_LoadBMP() image and it succeeded, it even works if I load a BMP image through the current code, yet not a PNG image.我已经尝试用 SDL_LoadBMP() 图像替换这些函数并且它成功了,如果我通过当前代码加载 BMP 图像而不是 PNG 图像,它甚至可以工作。

If BMP files load and PNG files don't load on Windows, you have a DLL location problem.如果在 Windows 上加载了 BMP 文件而 PNG 文件未加载,则说明存在 DLL 位置问题。 To load PNGs properly on Windows, SDL_image requires that the libpng and zlib DLLs reside in the same directory as the executable.要在 Windows 上正确加载 PNG,SDL_image 要求 libpng 和 zlib DLL 与可执行文件位于同一目录中。 The Windows version of SDL_image uses libpng and zlib to load PNG files so you have to place those DLLs in the right directory. SDL_image 的 Windows 版本使用 libpng 和 zlib 加载 PNG 文件,因此您必须将这些 DLL 放在正确的目录中。 Just having the SDL_image DLL in the same directory as the executable isn't enough.仅将 SDL_image DLL 与可执行文件位于同一目录中是不够的。

I had the same error and used the advice @MarkSzymczyk gave above.我有同样的错误并使用了@MarkSzymczyk 上面给出的建议。 I simply hadn't added all the dll needed to the project folder.我只是没有将所有需要的 dll 添加到项目文件夹中。 So I added all these:所以我添加了所有这些:

  1. SDL2_image.dll SDL2_image.dll
  2. libjpeg-9.dll libjpeg-9.dll
  3. libpng16-16.dll libpng16-16.dll
  4. libtiff-5.dll libtiff-5.dll
  5. libwebp-7.dll libwebp-7.dll

Then my image loaded.然后我的图像加载。 I think that I needed only the SDL2_image and libpng16-16 dlls but I added all of them for future use.我认为我只需要 SDL2_image 和 libpng16-16 dll,但我添加了所有这些以备将来使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM