Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/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++ 为什么代码不';t绘制精灵C++;SFML_C++_Textures_Sprite_Sfml - Fatal编程技术网

C++ 为什么代码不';t绘制精灵C++;SFML

C++ 为什么代码不';t绘制精灵C++;SFML,c++,textures,sprite,sfml,C++,Textures,Sprite,Sfml,我想在屏幕上绘制精灵,在我的结构中只有关于它的信息,然后在上面绘制文本 信息是好的 X和Y比例=1 路径良好,指向图形 位置和旋转=0 我有一个好数字 但文本是有效的 所以我不知道雪碧为什么不 我试着用画图注释代码,使文本不起作用 struct ObjectInfo { 浮子Xpoz,Ypoz; std::字符串路径; 浮动Xscale、Yscale; 浮动旋转; }; 目的信息OI[1000]; 无整数; 作废提款() { for(int i=0;i当您调用s.setTexture(t)spr

我想在屏幕上绘制精灵,在我的结构中只有关于它的信息,然后在上面绘制文本

信息是好的 X和Y比例=1 路径良好,指向图形 位置和旋转=0

我有一个好数字

但文本是有效的 所以我不知道雪碧为什么不

我试着用画图注释代码,使文本不起作用

struct ObjectInfo
{
浮子Xpoz,Ypoz;
std::字符串路径;
浮动Xscale、Yscale;
浮动旋转;
};
目的信息OI[1000];
无整数;
作废提款()
{

for(int i=0;i当您调用
s.setTexture(t)
sprite s通过指针/引用记住纹理t。因此,当您的代码退出for循环时
sf::texture t
被销毁(退出某个作用域时C++销毁作用域变量)sprite类中的指针/引用指向已删除的内存位置,这会导致SFML在绘制sprite时出错。解决此问题的方法是使用纹理的全局数组。我还建议为sf::sprites添加全局数组,因为它使代码更安全。下面是它的实现方式:

//In the global scope
sf::Texture textures[1000];
sf::Sprite sprites[1000];
//void draw() new for loop
for(int i=0;i<free;i++)
{
    textures[i].loadFromFile(OI[i].texPath);
    sprites[i].setTexture(s);
    //Set other parameters
    okno.draw(s);
    sf::Text text;
    text.setFont(font);
    text.setCharacterSize(48);
    text.setColor(sf::Color::Black);
    text.setPosition(s.getPosition());
    text.setString(IntToString(i));

    okno.draw(text);
}

//在全局范围内
sf::纹理[1000];
雪碧雪碧[1000];
//新循环的void draw()
对于(int i=0;i
s.setScale(OI[i].Xpoz,OI[i].Ypoz);
为什么使用位置而不是比例值?