【问题标题】:C++ IMG_LoadTexture() returns nullC++ IMG_LoadTexture() 返回 null
【发布时间】:2016-11-25 15:06:36
【问题描述】:

我正在尝试加载图像(“carnero.png”),但是当我使用 IMG_LoadTexture() 时,它返回 null;

游戏.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_ */

游戏.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();
}

这个函数返回null

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

但是当我使用 SDL_LoadBMP() 加载背景时,它可以工作。我尝试将我的 .png 放在其他文件夹中,但它也不起作用。我也尝试使用 IMG_LOAD() 加载我的 .png,但没有成功。

【问题讨论】:

  • "/textures/carnero2.png" 会从根目录而不是相对的textures 目录加载吗?
  • 我建议在 IMG_LoadTexture 调用之后使用 IMG_GetError。如果要加载需要 libpng 和 zlib 库的 PNG,是否将这些 DLL 包含在工作目录中?它们应该在 SDL_Image 文件夹中,您应该将它们复制到您的工作目录中
  • @EmiHöss 谢谢,它解决了我的问题

标签: c++ sdl-2 sdl-image


【解决方案1】:

您的路径不正确。 /textures/carnero2.png 将在C:\textures\carnero2.png/textures/carnero2.png 中搜索文件。

你可以这样解决这个问题:

  • 使用完整(绝对)路径:C:\Program Files (x86)\MyGame\textures\carnero2.png/usr/local/share/mygame/textures/carnero2.png
  • 添加dot ./textures/carnero2.png
  • 删除斜线:​​textures/carnero2.png

【讨论】:

  • 我试过你说的,但没有解决问题。我将“textures/carnero2.png”更改为“scenario.bmp”,它起作用了。我认为我的 png 文件有问题 编辑:我忘记将 zlib.dll 和 libpgn.dll 放在我的调试目录中。现在它正在工作
【解决方案2】:

输入文件的路径可能不正确:

/textures/carnero2.png

应该是

textures/carnero2.png

就像之前的(工作的)加载命令一样。

以后,我建议您在尝试加载文件之前测试文件是否存在。因此,您可以将“找不到文件”错误与真正的格式/损坏文件问题区分开来。

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多