C++ IMG_LoadTexture() 返回 null [英] C++ IMG_LoadTexture() returns null

查看:77
本文介绍了C++ IMG_LoadTexture() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to load a image("carnero.png") but when I use IMG_LoadTexture(), it returns 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();
}

该函数返回空

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

但是当我使用 SDL_LoadBMP() 加载背景时,它可以工作.我尝试将我的 .png 放在其他文件夹中,但它也不起作用.我也尝试使用 IMG_LOAD() 加载我的 .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.

推荐答案

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

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:

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

这篇关于C++ IMG_LoadTexture() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆