SFML 白色矩形
SFML white rectangle
我正在尝试做一个简单的瓦片地图。我有一个问题:当我设置地图时,只有白色方块。我一般都是加载贴图,不知道为什么会这样
代码如下:
class Tile
{
private:
sf::Sprite sprite;
sf::Texture tex;
public:
Tile(int x, int y, sf::Texture tex)
{
this->tex = tex;
this->sprite.setTexture(this->tex);
this->sprite.setPosition(x, y);
}
void render(sf::RenderWindow* target)
{
target->draw(this->sprite);
}
class Tilemap
{
private:
Tile tiles[36][64];
sf::Texture tex[4];
public:
//const/dest
Tilemap()
{
this->tex[0].loadFromFile("Resources/Tilemap/Water/water1.png");
int x = -WIDTH+WIDTH/2;
int y = -HEIGTH/2;
for (int i = 0; i < 36; i++)
{
for (int j = 0; j < 64; j++)
{
this->tiles[i][j] = Tile(x, y, this->tex[0]);
x += 60;
}
y += 60;
x = -WIDTH + WIDTH / 2;
}
}
render(sf::RenderWindow* target, sf::Vector2f pos)
{
for (int i = 0; i < 34; i++)
{
for (int j = 0; j < 64; j++)
{
this->tiles[i][j].render(target);
}
}
};
Tilemap map;
map = Tilemap();
您在 sprite
中有悬空引用。
此悬空引用出现在以下行中:
this->tiles[i][j] = Tile(x, y, this->tex[0]);
reference 对 Sprite::setTexture
说了什么?
The texture argument refers to a texture that must exist as long as
the sprite uses it. Indeed, the sprite doesn't store its own copy of
the texture, but rather keeps a pointer to the one that you passed to
this function. If the source texture is destroyed and the sprite tries
to use it, the behavior is undefined.
问题到底出在哪里?
Tile(x, y, this->tex[0]);
此处,创建了 Tile
的新实例。 tex
和sprite
是Tile
的成员变量。 setTexture
的 sprite
指的是 tex
.
tiles[i][j] = Tile(x,...);
在上面的行中,复制赋值运算符被调用,它从临时对象(由 Tile(x,y,..)
创建)复制 sprite
/tex
。结果,在 tiles[i][j]
中,您有 sprite
成员,它指的是临时实例的纹理 - Tile(..)
(sprite
仅包含指向纹理的指针)。最后,在完整表达式的末尾,临时实例被销毁,Tile(..)
的 tex
被删除,tiles[i][j].sprite
持有指向纹理的无效指针。
解决方案?
您必须添加 Tile
的复制构造函数(复制赋值运算符)以正确初始化 sprite
以保存其自己的 tex
(不引用创建副本的实例):
例如:
Tile& operator=(const Tile& theOther)
{
this->tex = theOther.tex;
this->sprite.setTexture(this->tex);
return *this;
}
默认生成的复制赋值运算符this->sprite
指向theOther.tex
纹理,这是错误的。
我正在尝试做一个简单的瓦片地图。我有一个问题:当我设置地图时,只有白色方块。我一般都是加载贴图,不知道为什么会这样
代码如下:
class Tile
{
private:
sf::Sprite sprite;
sf::Texture tex;
public:
Tile(int x, int y, sf::Texture tex)
{
this->tex = tex;
this->sprite.setTexture(this->tex);
this->sprite.setPosition(x, y);
}
void render(sf::RenderWindow* target)
{
target->draw(this->sprite);
}
class Tilemap
{
private:
Tile tiles[36][64];
sf::Texture tex[4];
public:
//const/dest
Tilemap()
{
this->tex[0].loadFromFile("Resources/Tilemap/Water/water1.png");
int x = -WIDTH+WIDTH/2;
int y = -HEIGTH/2;
for (int i = 0; i < 36; i++)
{
for (int j = 0; j < 64; j++)
{
this->tiles[i][j] = Tile(x, y, this->tex[0]);
x += 60;
}
y += 60;
x = -WIDTH + WIDTH / 2;
}
}
render(sf::RenderWindow* target, sf::Vector2f pos)
{
for (int i = 0; i < 34; i++)
{
for (int j = 0; j < 64; j++)
{
this->tiles[i][j].render(target);
}
}
};
Tilemap map;
map = Tilemap();
您在 sprite
中有悬空引用。
此悬空引用出现在以下行中:
this->tiles[i][j] = Tile(x, y, this->tex[0]);
reference 对 Sprite::setTexture
说了什么?
The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the sprite tries to use it, the behavior is undefined.
问题到底出在哪里?
Tile(x, y, this->tex[0]);
此处,创建了 Tile
的新实例。 tex
和sprite
是Tile
的成员变量。 setTexture
的 sprite
指的是 tex
.
tiles[i][j] = Tile(x,...);
在上面的行中,复制赋值运算符被调用,它从临时对象(由 Tile(x,y,..)
创建)复制 sprite
/tex
。结果,在 tiles[i][j]
中,您有 sprite
成员,它指的是临时实例的纹理 - Tile(..)
(sprite
仅包含指向纹理的指针)。最后,在完整表达式的末尾,临时实例被销毁,Tile(..)
的 tex
被删除,tiles[i][j].sprite
持有指向纹理的无效指针。
解决方案?
您必须添加 Tile
的复制构造函数(复制赋值运算符)以正确初始化 sprite
以保存其自己的 tex
(不引用创建副本的实例):
例如:
Tile& operator=(const Tile& theOther)
{
this->tex = theOther.tex;
this->sprite.setTexture(this->tex);
return *this;
}
默认生成的复制赋值运算符this->sprite
指向theOther.tex
纹理,这是错误的。