0

After fixing various other SDL errors, (both with SDL itself and SDL_Image) I have written this error-free code:

#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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
user2752347
  • 161
  • 6
  • 16
  • What error(s) do you get? – this Sep 25 '13 at 00:41
  • @self. I receive no errors, the image just doesnt show. – user2752347 Sep 25 '13 at 00:57
  • Could you try a different image? The code works for me. Btw; you REALLY don't want that macro. `#define null 0` does the EXACT same thing as `NULL`. You should replace all `NULL` and `null` with `nullptr` – olevegard Sep 25 '13 at 07:19
  • @olvegard Im new to C++ and prefer NULL as null, but Ill make nulls nullptr from now on. I have tried a different image to no avail, perhaps its an error with the directory the image is in? Ive placed it within `C:\Users\DemonicSmokingJacket\Documents\Visual Studio 2012\Projects\ProjectName\ProjectName\`. Thank you for your response however, looking forward to your next answer. – user2752347 Sep 25 '13 at 09:58
  • 1
    It has to be an issue with your image based on what you are saying ie that a BMP loads. Have you tried a different PNG? I would suggest putting in a nullptr check on `image` just after `image = LoadImage("image.png");` to check if it is loaded or not. – TPS Sep 25 '13 at 11:16
  • 1
    Another thing, I'm not that familiar with Windows development as I work on OSX but would the image not be required to be in the same location as the compiled .exe file? If the location you mentioned is where the .exe is then just ignore this :-) – TPS Sep 25 '13 at 11:18
  • @Zammalad I have performed a nullptr check and the program exited with 1, so clearly SDL_Image is having issues with loading the PNG(s) (Btw I have tried a different PNG this time as well as attempting a JPG file) – user2752347 Sep 25 '13 at 11:22
  • @Zammalad It isnt with the exe however it works with a BMP so it cannot be based on the directory and Path I used, thanks anyhow :-P – user2752347 Sep 25 '13 at 11:24
  • 1
    I'm guessing it could be related to your `SLD_Image` version. Try this `std::cout << "SDL_Image version : " << SDL_IMAGE_MAJOR_VERSION << "." << SDL_IMAGE_MINOR_VERSION << "." << SDL_IMAGE_PATCHLEVEL << std::endl;` It should print 2.0.0 – olevegard Sep 25 '13 at 15:54

2 Answers2

4

If BMP files load and PNG files don't load on Windows, you have a DLL location problem. To load PNGs properly on Windows, SDL_image requires that the libpng and zlib DLLs reside in the same directory as the executable. 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. Just having the SDL_image DLL in the same directory as the executable isn't enough.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
0

I had the same error and used the advice @MarkSzymczyk gave above. I simply hadn't added all the dll needed to the project folder. So I added all these:

  1. SDL2_image.dll
  2. libjpeg-9.dll
  3. libpng16-16.dll
  4. libtiff-5.dll
  5. 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.