Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未在屏幕上绘制纹理_C++_Sfml - Fatal编程技术网

C++ 未在屏幕上绘制纹理

C++ 未在屏幕上绘制纹理,c++,sfml,C++,Sfml,我试图遵循一个稍微过时的教程制作一个瓷砖引擎 问题是我试图在屏幕上绘制的纹理没有显示出来。我刚得到一个黑屏 我已经学习了Engine.cpp中最相关的部分: bool Engine::Init() { LoadTextures(); window = new sf::RenderWindow(sf::VideoMode(800,600,32), "RPG"); if(!window) return false; return true; } v

我试图遵循一个稍微过时的教程制作一个瓷砖引擎

问题是我试图在屏幕上绘制的纹理没有显示出来。我刚得到一个黑屏

我已经学习了Engine.cpp中最相关的部分:

bool Engine::Init()
{
    LoadTextures();
    window = new sf::RenderWindow(sf::VideoMode(800,600,32), "RPG");
    if(!window)
        return false;

    return true;
}

void Engine::LoadTextures()
{
    sf::Texture sprite;
    sprite.loadFromFile("C:\\Users\\Vipar\\Pictures\\sprite1.png");
    textureManager.AddTexture(sprite);
    testTile = new Tile(textureManager.GetTexture(0));
}

void Engine::RenderFrame()
{
    window->clear();
    testTile->Draw(0,0,window);
    window->display();
}

void Engine::MainLoop()
{
    //Loop until our window is closed
    while(window->isOpen())
    {
        ProcessInput();
        Update();
        RenderFrame();
    }
}

void Engine::Go()
{
    if(!Init())
        throw "Could not initialize Engine";
    MainLoop();
}
这是TextureManager.cpp

#include "TextureManager.h"
#include <vector>
#include <SFML\Graphics.hpp>

TextureManager::TextureManager()
{

}

TextureManager::~TextureManager()
{

}

void TextureManager::AddTexture(sf::Texture& texture)
{
    textureList.push_back(texture);
}

sf::Texture& TextureManager::GetTexture(int index)
{
    return textureList[index];
}
#包括“TextureManager.h”
#包括
#包括
TextureManager::TextureManager()
{
}
TextureManager::~TextureManager()
{
}
void TextureManager::AddTexture(sf::Texture&Texture)
{
纹理列表。推回(纹理);
}
sf::Texture&TextureManager::GetTexture(int索引)
{
返回纹理列表[索引];
}

在本教程中,使用了
图像
类型,但是
图像
没有
Draw()
方法,因此我改为使用
纹理
。为什么
纹理不出现在屏幕上?

问题似乎出在:

void Engine::LoadTextures()
{
    sf::Texture sprite;
    sprite.loadFromFile("C:\\Users\\Vipar\\Pictures\\sprite1.png");
    textureManager.AddTexture(sprite);
    testTile = new Tile(textureManager.GetTexture(0));
}
您正在创建一个本地的
sf::Texture
,并将其传递给
TextureManager::AddTexture
。它可能在函数末尾超出了作用域,然后在您尝试绘制它时不再有效。您可以使用智能指针解决此问题:

void Engine::LoadTextures()
{
    textureManager.AddTexture(std::shared_ptr<sf::Texture>(
        new sf::Texture("C:\\Users\\Vipar\\Pictures\\sprite1.png")));

    testTile = new Tile(textureManager.GetTexture(0));
}

您还必须将
textureList
更改为
std::vector您会提出什么解决方案?让精灵全球化根本不是很有效。这个想法是通过引用来传递的,我认为这样我们就只保留了对图片的引用,而不是对数百万个包含信息的对象的引用。我认为最好的方法可能是使用指针来传递。不过你得负责清理工作。(我认为聪明的指针可以帮上忙,但还没有玩过,所以不确定。)你能在回答中给我举个例子说明你是如何做到这一点的吗?我还是有点绿:)放进去了:)抱歉,我花了一段时间,没有检查,然后又花了10分钟来找出智能指针。std::没有共享的\u ptr成员。尚未找到解决此问题的方法。
void TextureManager::AddTexture(std::shared_ptr<sf::Texture> texture)
{
    textureList.push_back(texture);
}

sf::Texture& TextureManager::GetTexture(int index)
{
    return *textureList[index];
}