SFML中的白色方块,Sprite和存储在不同对象中的纹理 [英] White Square in SFML, Sprite and texture stored in different objects

查看:319
本文介绍了SFML中的白色方块,Sprite和存储在不同对象中的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到白色正方形问题在SFML。我正在制作一个使用平铺地图的游戏。一个游戏类将从一个文件加载作为sf :: Texture的tile集合,然后在 Game 中加载getTileById函数。将从拼图纹理中剪切出合适的拼贴。例如。

  sf :: Sprite GameScreen :: getSpriteByPos(int pos,sf :: Texture texture,int tileSize){
sf :: IntRect subRect;
subRect.left =(pos-1)* tileSize;
subRect.top =(pos-1)* tileSize;
subRect.width = tileSize;
subRect.height = tileSize;

sf :: Sprite tileSprite(texture,subRect);
返回tileSprite;
}

然后将精灵传递给Tile对象,然后将其设置为sprite_属性。例如。

  void Tile :: setSprite(sf :: Sprite sprite){
sprite_ = sprite;

游戏对象将以这种方式加载所有的瓷砖,并将它们全部存储在一个矢量中。为了绘制它们,它将循环遍历矢量,并在它们中的每一个上调用 Tile :: draw(sf :: RenderWindow&)方法(将RenderWindow传递为画到)。这个方法需要一个sf :: RenderWindow,tile只需在RenderWindow中用 sf :: RenderWindow :: draw(sf :: Sprite) > sprite _ 属性。例如:

  void Tile :: draw(sf :: RenderWindow& window){
window.draw(sprite_ );
return;

$ / code>

我想这样做,因为 Tile :: draw 方法继承自 Drawable 类,因此所有可绘制的对象都可以继承自Drawable并在其中实现其绘制方法一种适合他们的方式,这在我的项目中将是必要的。虽然这个精灵正在被绘制成一个白色的方块,但这并没有被破坏,这让我觉得很奇怪,它是 Game 类的一个属性,仍然存在。

有人可以告诉我这里发生了什么事吗?任何帮助,将不胜感激。

解决方案

您正在通过您的纹理按值。这意味着你在这个函数中得到你的纹理的副本:

$ s $ Sprite GameScreen :: getSpriteByPos(int pos,sf :: Texture texture,int tileSize){
sf :: IntRect subRect;
subRect.left =(pos-1)* tileSize;
subRect.top =(pos-1)* tileSize;
subRect.width = tileSize;
subRect.height = tileSize;

sf :: Sprite tileSprite(texture,subRect);
返回tileSprite;
}

但是该函数在函数结束时会被销毁。



您不需要该副本,因此不要复制:

  sf :: Sprite GameScreen :: getSpriteByPos(int pos,const sf :: Texture& texture,int tileSize){
sf :: IntRect subRect;
subRect.left =(pos-1)* tileSize;
subRect.top =(pos-1)* tileSize;
subRect.width = tileSize;
subRect.height = tileSize;

sf :: Sprite tileSprite(texture,subRect);
返回tileSprite;
}


I'm experiencing the white square problem in SFML. I'm working on a game that uses a tiled map. A Game class will load the tile set as a sf::Texture from a file and then a getTileById function in Game will "Cut" out the appropriate tile from the tileset texture. Eg.

sf::Sprite GameScreen::getSpriteByPos(int pos, sf::Texture texture, int tileSize) {
    sf::IntRect subRect;
    subRect.left = (pos-1)*tileSize;
    subRect.top = (pos-1)*tileSize;
    subRect.width = tileSize;
    subRect.height = tileSize;

    sf::Sprite tileSprite(texture, subRect);
    return tileSprite;
}

The sprite will then be passed into a Tile object, which will then set its sprite_ attribute to it. Eg.

void Tile::setSprite(sf::Sprite sprite) {
    sprite_ = sprite;
}

The Game object will load all tiles in this way and store them all in a vector. To draw them, it will loop through the vector and call the Tile::draw(sf::RenderWindow&) method on each one of them (Passing in the RenderWindow to be drawn to). This method takes an sf::RenderWindow and the tile simply calls the sf::RenderWindow::draw(sf::Sprite) on the RenderWindow with its sprite_ attribute. Eg.

void Tile::draw(sf::RenderWindow &window){
    window.draw(sprite_);
    return;
}

I'd like to do drawing this way because the Tile::draw method is inherited from a Drawable class, so that all objects that are drawable can inherit from Drawable and implement their drawing method in a way that suits them, something that will be necessary in my project. The sprite is being drawn as a white square though, which strikes me as strange as the tileSet_ attribute has not been destroyed, it is an attribute of the Game class and still exists.

Can anybody tell me what is going on here? Any help would be appreciated.

解决方案

You are passing your texture "by value". That means you get a copy of your texture inside this function:

sf::Sprite GameScreen::getSpriteByPos(int pos, sf::Texture texture, int tileSize) {
    sf::IntRect subRect;
    subRect.left = (pos-1)*tileSize;
    subRect.top = (pos-1)*tileSize;
    subRect.width = tileSize;
    subRect.height = tileSize;

    sf::Sprite tileSprite(texture, subRect);
    return tileSprite;
}

But that copy is destroyed when the function ends.

You don't need that copy, so don't make a copy:

sf::Sprite GameScreen::getSpriteByPos(int pos, const sf::Texture& texture, int tileSize) {
    sf::IntRect subRect;
    subRect.left = (pos-1)*tileSize;
    subRect.top = (pos-1)*tileSize;
    subRect.width = tileSize;
    subRect.height = tileSize;

    sf::Sprite tileSprite(texture, subRect);
    return tileSprite;
}

这篇关于SFML中的白色方块,Sprite和存储在不同对象中的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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