0

I'm trying to load a image("carnero.png") but when I use IMG_LoadTexture(), it returns null;

Game.h

#ifndef GAME_H_
#define GAME_H_

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

class Game {
public:
    Game();
    ~Game();

    void run();
    void initGraphics();
    void gameLoop();
private:
    SDL_Window* _window = nullptr;
    SDL_Renderer* _renderer;
    SDL_Surface* _surfaceBMP;
    SDL_Texture* _textureScenario;
    SDL_Texture* _textureCarnero;
    SDL_Rect* _scenarioRect;
    SDL_Rect* _carneroRect;
    int _width;
    int _height;
    bool _running;
};


#endif /* SRC_GAME_H_ */

Game.cpp

#include "Game.h"
#include <iostream>

Game::Game(){
    _running = true;
    run();
}

Game::~Game(){

}

void Game::run(){
    initGraphics();
    gameLoop();
}

void Game::initGraphics(){

    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_PNG);

    _window = SDL_CreateWindow("Carneiro", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_SHOWN);

    if(_window == nullptr) exit(1);

    _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);

    _surfaceBMP = SDL_LoadBMP("textures/scenario.bmp");
    _textureScenario = SDL_CreateTextureFromSurface(_renderer, _surfaceBMP);
    SDL_FreeSurface(_surfaceBMP);

    _textureCarnero = IMG_LoadTexture(_renderer, "/textures/carnero2.png");
    if(_textureCarnero == nullptr) exit(1);

    _scenarioRect->x = 0;    _scenarioRect->w = 1024;
    _scenarioRect->y = 0;    _scenarioRect->h = 740;
    _carneroRect->x = 20;    _carneroRect->w = 150;
    _carneroRect->y = 100;   _carneroRect->h = 100;



}

void Game::gameLoop(){
    while(_running){
        Sleep(10);
        SDL_Event evnt;
        if(SDL_PollEvent(&evnt)){
            switch(evnt.type){
                case SDL_QUIT:
                    _running = false;
                    break;
                }
            }

        SDL_RenderClear(_renderer);
        SDL_RenderCopy(_renderer, _textureScenario, nullptr, _scenarioRect);
    //  SDL_QueryTexture(_textureCarnero, NULL, NULL, &_carneroRect->x, &_carneroRect->y);
        SDL_RenderCopy(_renderer, _textureCarnero, nullptr, _carneroRect);

        SDL_RenderPresent(_renderer);

        }

    SDL_DestroyTexture(_textureScenario);
    SDL_DestroyTexture(_textureCarnero);
    SDL_DestroyRenderer(_renderer);
    SDL_DestroyWindow(_window);
    SDL_Quit();
    IMG_Quit();
}

This function returns null

_textureCarnero = IMG_LoadTexture(_renderer, "/textures/carnero2.png");

But when I use SDL_LoadBMP() to load the background it works. I tried putting my .png in other folders but it doesn't work either. I also tried to load my .png using IMG_LOAD() but i had no sucess.

Tidder Jail
  • 472
  • 7
  • 11
  • 3
    `"/textures/carnero2.png"` would that load from the root dir instead of a relative `textures` directory? – Jean-François Fabre Nov 25 '16 at 15:10
  • I recommend using IMG_GetError after the IMG_LoadTexture call. If you want to load PNGs you need libpng and zlib libs, are you including those DLL's in the working directory? They should be in the SDL_Image folder and you should copy them into your working directory – Emi Höss Nov 25 '16 at 15:28
  • @EmiHöss Thank you, it fixed my problem – Tidder Jail Nov 25 '16 at 16:02

2 Answers2

2

Your path is incorrect. /textures/carnero2.png will search for a file in C:\textures\carnero2.png, or /textures/carnero2.png on unix.

You can solve this problem as follows:

  • Use full (absolute) path: C:\Program Files (x86)\MyGame\textures\carnero2.png, /usr/local/share/mygame/textures/carnero2.png
  • Add a dot ./textures/carnero2.png
  • Remove the slash: textures/carnero2.png.
Community
  • 1
  • 1
yyny
  • 1,623
  • 18
  • 20
  • 1
    i tried what you said but it didn't solve the problem. I changed "textures/carnero2.png" to "scenario.bmp" and it worked. I think my png files has a problem EDIT: I'd forgotten to put zlib.dll and libpgn.dll in my debug dir. now it's working – Tidder Jail Nov 25 '16 at 15:59
0

The path to your input file is probably incorrect:

/textures/carnero2.png

should probably be

textures/carnero2.png

like in the previous (working) load command.

In the future, I suggest that you test for file existence before trying to load the file. So you can separate "file not found" errors from real format/corrupt file problems.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219