2

Hey I was going to load an image to my program with SDL2. I am using visual studio 2015. I place the image in my project folder in correct place but still image isn't loaded.my code as follows

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

using namespace std;

int main(int argc, char* argv[]) {

    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = NULL;

    window = SDL_CreateWindow("Game", 100, 100, 700, 400, SDL_WINDOW_SHOWN);

    if (window = NULL) {
        cout << "Window creation error" << endl;
    }

    SDL_Renderer* renderer = NULL;
    renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Texture* man = IMG_LoadTexture(renderer, "img.png");
    SDL_Event* ev = new SDL_Event();

    if (man == NULL) {
        cout << "image cannot load" << endl;
    }

    SDL_Rect boy_rect;
    boy_rect.x = 10;
    boy_rect.y = 10;
    boy_rect.h = 220;
    boy_rect.w = 300;

    while (ev->type != SDL_QUIT) {
        SDL_PollEvent(ev);
        SDL_RenderClear(renderer);

        SDL_RenderCopy(renderer, man, NULL, &boy_rect);

        SDL_RenderPresent(renderer);
    }
    SDL_DestroyTexture(man);
    SDL_DestroyWindow(window);
    SDL_DestroyRenderer(renderer);

    return 0;
}

the result is "image cant load" I cant find any error of this if you can find please inform me. thanks

darcyy
  • 5,236
  • 5
  • 28
  • 41
  • Are you even sure that the renderer was created correctly ? Where did you placed the image ? – Hannes Hauptmann Jul 24 '17 at 10:08
  • I put the image where this c++ file placed – Jayamal Jayamaha Jul 24 '17 at 10:22
  • @JayamalJayamaha You need to place the image where the compiled program is (or in general in the working directory), not where the `.cpp` file is. Remember, it's the binary that's executed, not the source. – hnefatl Jul 24 '17 at 10:26
  • Did you mean the place where .exe file is ????? I put my image file in that place too but still not working – Jayamal Jayamaha Jul 24 '17 at 10:33
  • So you've placed the `.exe` and the `.png` in the same directory, what exactly happens when you run the `.exe`? And have you added the check for `renderer==NULL`? – hnefatl Jul 24 '17 at 10:36

3 Answers3

4

The error you quote suggests man is null, which means IMG_LoadTexture returned null. This may be because renderer is null, which you should check, or most likely simply because your "img.png" can't be found by the SDL_IMG library.

You should first add a check to ensure your renderer isn't null, and then check the working directory that your program is set to run from (assuming it's running from an IDE). The "img.png" file needs to be in that directory. It's often easiest to change the working directory to the same directory your compiled binary is in, so that your program and data are all in the same place.

You can call IMG_GetError (or SDL_GetError) to get a string describing the error - this should be your first port of call if something in an SDL program starts acting up.

Edit: Another thing that could go wrong is that the SDL link libraries aren't located in the same folder as your binary (though this is more likely to just crash). You need eg. SDL2.a, SDL2_image.a, likely from the lib or bin directory of the archive you downloaded with the SDL headers. If you get the result of the above procedure calls, you'll be able to tell if this is the issue.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
0

On some IDEs and OSs like mine you should use the complete(absolute) path like:

"C://Users/Filomena/Documents/Images/Sprite.png"

C++ IMG_LoadTexture() returns null

Ibrahim CS
  • 119
  • 2
  • 9
  • `IMG_LoadTexture` is a correct call in SDL2 (tag in OP's post). It just loads a `Surface` using `IMG_Load` then uses `SDL_CreateTextureFromSurface` to convert it ([see here](https://github.com/davidsiaw/SDL2_image/blob/d22cb09aa8ae01498bc683efa3e6d9f6a4515d60/IMG.c#L207-L216)). I'm curious though - what's an "SO"? I can't think what it could stand for in the context of source editors. – hnefatl Jul 25 '17 at 08:11
  • Sorry about that, yeah you were right `IMG_LoadTexture` is a correct function from SDL_Image.h so i edited my answer, by the way, have you tried using the absolute path? i've included a link who respalds my answer. – Ibrahim CS Jul 27 '17 at 03:34
  • Absolute paths are generally bad news (why I didn't mention them) - relative paths are always more sustainable, and the OP should be able to get it working with them (as in, SDL_Image does support them). – hnefatl Jul 27 '17 at 07:22
0

After two hours of debugging it turns out that the renderer was null. In my code, I have requested the surface of the window, which I pass to the renderer, which was fine before, but now on version 2.28.1 it appears that the renderer cannot be initialised. So after removing the call to SDL_GetWindowSurface, it all worked. That call is not needed, because we are working with the renderer now. Take away: Use SDL_GetError() in all places.

Vallerious
  • 570
  • 6
  • 13