[英]C++ IMG_LoadTexture() returns null
I'm trying to load a image("carnero.png") but when I use IMG_LoadTexture(), it returns null; 我正在尝试加载图像(“ carnero.png”),但是当我使用IMG_LoadTexture()时,它返回null;
Game.h 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 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 此函数返回null
_textureCarnero = IMG_LoadTexture(_renderer, "/textures/carnero2.png");
But when I use SDL_LoadBMP() to load the background it works. 但是,当我使用SDL_LoadBMP()加载背景时,它可以工作。 I tried putting my .png in other folders but it doesn't work either.
我尝试将.png放置在其他文件夹中,但也不起作用。 I also tried to load my .png using IMG_LOAD() but i had no sucess.
我也尝试使用IMG_LOAD()加载.png文件,但没有成功。
Your path is incorrect. 您的路径不正确。
/textures/carnero2.png
will search for a file in C:\\textures\\carnero2.png
, or /textures/carnero2.png
on unix. /textures/carnero2.png
将在搜索一个文件C:\\textures\\carnero2.png
,或/textures/carnero2.png
在UNIX上。
You can solve this problem as follows: 您可以按以下方式解决此问题:
C:\\Program Files (x86)\\MyGame\\textures\\carnero2.png
, /usr/local/share/mygame/textures/carnero2.png
C:\\Program Files (x86)\\MyGame\\textures\\carnero2.png
, /usr/local/share/mygame/textures/carnero2.png
C:\\Program Files (x86)\\MyGame\\textures\\carnero2.png
/usr/local/share/mygame/textures/carnero2.png
C:\\Program Files (x86)\\MyGame\\textures\\carnero2.png
/usr/local/share/mygame/textures/carnero2.png
C:\\Program Files (x86)\\MyGame\\textures\\carnero2.png
./textures/carnero2.png
./textures/carnero2.png
textures/carnero2.png
. textures/carnero2.png
。 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.
因此,您可以将“找不到文件”错误与实际格式/损坏的文件问题分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.