SFML/C++ 雪碧因超出范围而被显示为白框,不知道在哪里
SFML/C++ Sprite is being displayed as a white box due to going out of scope, don't know where
标题描述了一切,我试图做一个基于瓷砖的引擎,但我不能继续,因为我只是找不到纹理超出范围的地方。
谢谢。我代码:
ImageManager.h
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
class ImageManager{
std::vector<sf::Texture> textureList;
public:
void AddTexture(sf::Texture);
sf::Texture getIndex(int index) const;
};
ImageManager.cpp
#include "imageManager.h"
void ImageManager::AddTexture(sf::Texture texture){
textureList.push_back(texture);
}
sf::Texture ImageManager::getIndex(int index) const{
return textureList[index];
}
Tile.h
#pragma once
#include <SFML/Graphics.hpp>
#include <memory>
class Tile{
sf::Sprite sprite;
public:
Tile(sf::Texture texture);
void draw(int x, int y, sf::RenderWindow* rw);
};
Tile.cpp
#include "Tile.h"
Tile::Tile(sf::Texture texture){
sprite.setTexture(texture);
}
void Tile::draw(int x, int y, sf::RenderWindow* rw){
sprite.setPosition(x, y);
rw->draw(sprite);
}
构造函数将原始纹理的副本作为参数,该副本在作用域结束时死亡。请使用(const)引用。
同时,你的ImageManager会在矢量调整大小时复制纹理,因此所有的精灵都会失去它们的纹理。要么使用std::vector<std::shared_ptr<sf::Texture>>
,要么使用Thor的资源管理器(或者任何其他好的库)。
为将来的读者补充Hiura的答案,重要的是要记住sf::Sprite
只是指向sf::Texture
的指针以及一些绘图信息。如果sf::Sprite
指向的sf::Texture
被破坏,sf::Sprite
就不再有纹理信息可以使用。正如Hiura所说,Tile
的构造函数复制传递给它的sf::Texture
,将sf::Sprite
指向副本,然后销毁副本,留下没有sf::Texture
的sf::Sprite
。通过传递对sf::Texture
的引用,sf::Sprite
将指向原来的sf::Texture
相关文章:
- Qt QML相机在部署后显示白屏
- C++ 窗口显示无框图像
- 表单显示对话框函数错误,并且不执行下面的语句
- DirectShow 在 Win7/Win10 上失败并显示黑框和0x80070005
- C# - 显示对话框将所有者设置为非托管窗口
- QTest未显示对话框
- c++ HMENU 显示消息框
- 当我打开一个新对话框时,如何禁止在Qt编程的Windows任务栏中显示对话框
- 显示对话框后运行功能
- 在QT中关闭程序之前,请显示对话框
- 显示Q对话框时出现QT线程问题
- 如何使用c++在Mac OSX中显示输入框
- 单击"菜单"时显示消息框未显示在屏幕上
- SFML 纹理显示为白框
- 我想在应用程序因内存耗尽而崩溃时显示消息框
- 显示消息框后,子窗口失去焦点
- MFC CListCtrl 在单元格上显示文本框以供用户编辑
- 在 MFC 中显示对话框的信息
- opengl显示白框而不是纹理
- SFML/C++ 雪碧因超出范围而被显示为白框,不知道在哪里