如何使用 SFML 纹理作为静态数据成员?
How to use SFML Textures as static data members?
我正在使用 SFML 在 C++ 中编写 Space Invaders。我的计划是只加载一次子弹的纹理,然后将其用于每个精灵。因此我开始学习静态数据成员,但我不知道如何加载纹理。
我试过在 class 内声明数据成员,然后在
外加载
class Lovedek
{
sf::Sprite sprite;
static sf::Texture texture;
};
sf::Texture Lovedek::texture.loadFromFile("bullet_graphics.png",sf::IntRect(0, 0, 2, 10));
一直在说error: expected initializer before '.' token
.
现在我知道我应该使用 = 运算符,但我无法加载它。
或者,如果有人知道更好的只加载一次的方法,我将不胜感激。
忘记这里的 static
和 类。你想要的是一个命名空间:
namespace Lovedek
{
sf::Texture texture;
}
是的,它基本上是一个全局变量。通常是糟糕的设计,但是如果您需要一个可以从任何地方访问的对象,您可能已经正确地做到了(使用名称空间)。现在你可以在外面加载它了:
Lovedek::texture.loadFromFile("bullet_graphics.png",sf::IntRect(0, 0, 2, 10));
所以信息被保存到Lovedek::texture
。当您现在想将其应用于 sf::Sprite
时,只需执行:
sf::Sprite sprite;
sprite.setTexture(Lovedek::texture);
这是非常简单的用法。 "pure static class" 将非常反对通常应如何使用 类 的想法。
sf::Texture Lovedek::texture.loadFromFile("bullet_graphics.png",sf::IntRect(0, 0, 2, 10));
这一行本身有一些语法错误,但无论如何它都没有在正确的位置加载纹理。
首先你需要在某处初始化一个静态 class 变量,通常在 class.
的 cpp 文件中
// In your header for Lovedek
class Lovedek
{
...
static sf::Texture texture; // Declare the static texture
...
};
// In your cpp file for Lovedek
sf::Texture Lovedek::texture; // Initialize the static texture
然后想想 什么时候 你想加载纹理,可能是在你的 main
函数的开头附近,或者在某种设置函数中对吗?
您可以制作 Lovedek::texture
public 并在 class 之外加载纹理,或者您可以将其保持私有并实现类似 LoadAssets()
静态方法的东西来这样做。
对于 public 方法:
// Make Lovedek::texture a public class member
// Then put this somewhere in your game's setup (before starting the game loop)
Lovedek::texture.loadFromFile("bullet_graphics.png", sf::IntRect(0, 0, 2, 10));
对于私有方法:
// Change your Lovedek class
class Lovedek
{
static sf::Texture texture;
public:
static void LoadAssets()
{
texture.loadFromFile("bullet_graphics.png", sf::IntRect(0, 0, 2, 10));
}
}
// Then put this somewhere in your game's setup (before starting the game loop)
Lovedek::LoadAssets();
我正在使用 SFML 在 C++ 中编写 Space Invaders。我的计划是只加载一次子弹的纹理,然后将其用于每个精灵。因此我开始学习静态数据成员,但我不知道如何加载纹理。
我试过在 class 内声明数据成员,然后在
外加载class Lovedek
{
sf::Sprite sprite;
static sf::Texture texture;
};
sf::Texture Lovedek::texture.loadFromFile("bullet_graphics.png",sf::IntRect(0, 0, 2, 10));
一直在说error: expected initializer before '.' token
.
现在我知道我应该使用 = 运算符,但我无法加载它。 或者,如果有人知道更好的只加载一次的方法,我将不胜感激。
忘记这里的 static
和 类。你想要的是一个命名空间:
namespace Lovedek
{
sf::Texture texture;
}
是的,它基本上是一个全局变量。通常是糟糕的设计,但是如果您需要一个可以从任何地方访问的对象,您可能已经正确地做到了(使用名称空间)。现在你可以在外面加载它了:
Lovedek::texture.loadFromFile("bullet_graphics.png",sf::IntRect(0, 0, 2, 10));
所以信息被保存到Lovedek::texture
。当您现在想将其应用于 sf::Sprite
时,只需执行:
sf::Sprite sprite;
sprite.setTexture(Lovedek::texture);
这是非常简单的用法。 "pure static class" 将非常反对通常应如何使用 类 的想法。
sf::Texture Lovedek::texture.loadFromFile("bullet_graphics.png",sf::IntRect(0, 0, 2, 10));
这一行本身有一些语法错误,但无论如何它都没有在正确的位置加载纹理。
首先你需要在某处初始化一个静态 class 变量,通常在 class.
的 cpp 文件中// In your header for Lovedek
class Lovedek
{
...
static sf::Texture texture; // Declare the static texture
...
};
// In your cpp file for Lovedek
sf::Texture Lovedek::texture; // Initialize the static texture
然后想想 什么时候 你想加载纹理,可能是在你的 main
函数的开头附近,或者在某种设置函数中对吗?
您可以制作 Lovedek::texture
public 并在 class 之外加载纹理,或者您可以将其保持私有并实现类似 LoadAssets()
静态方法的东西来这样做。
对于 public 方法:
// Make Lovedek::texture a public class member
// Then put this somewhere in your game's setup (before starting the game loop)
Lovedek::texture.loadFromFile("bullet_graphics.png", sf::IntRect(0, 0, 2, 10));
对于私有方法:
// Change your Lovedek class
class Lovedek
{
static sf::Texture texture;
public:
static void LoadAssets()
{
texture.loadFromFile("bullet_graphics.png", sf::IntRect(0, 0, 2, 10));
}
}
// Then put this somewhere in your game's setup (before starting the game loop)
Lovedek::LoadAssets();