如何在SFML C++中传递图像

How to pass an Image in SFML C++

本文关键字:图像 C++ SFML      更新时间:2023-10-16

我正在尝试制作一个简单的函数或类,该函数或类选择一个图像并返回它,或者以某种方式将它传递给另一个类。它是否像知道图像是什么类型一样简单?还是我需要做点别的?我正在Windows8计算机上运行带有GNUGCC编译器的Code::Blocks 10.05。感谢您的帮助。

多亏了唯美主义,我取得了一些进步。现在我有了这个:

class Background{
sf::Image BGI;
sf::Sprite BG;
Image& img;
public:
void rimage(std::string name){
sf::Image extra;
extra.LoadFromFile(name);
img = extra;
}
void init(std::string name){
BGI = img
BG.SetPosition(0.f,0.f);
BG.SetImage(BGI);
}
};

但当我运行它时,我会得到这个:

...4 error: ISO C++ forbids declaration of 'Image" with no type 

此外,

...10 error: 'img' is defined in this scope

我已经包含了运行SFML所需的库,我只是把它放在一边以保持干净,我调整了上面发生错误的行,使其更容易遵循。

img现在不是后台中的一个全局变量吗?我认为CCD_ 1是CCD_。。。这里需要改变什么?

您不需要load方法,也不需要任何额外的Image对象。您可以在构造函数中完成所有这些处理。

class Background{
private:
// You only need an image and a background, if that.
sf::Image BGI;
sf::Sprite BG;
public:
// Use a constructor.
Background(std::string name)
{
SetBackground(name, Vector2f(0.f, 0.f));
}
void SetBackground(std::string name, sf::Vector2f pos)
{
BGI.LoadFromFile(name);
BG.SetImage(BGI);
BG.SetPosition(pos);
}
};
// Constructor loads image, sets image to sprite, and set sprite position.
Background bg("MyBackground.png");
// You can change the background image an position like so.
bg.SetBackgrond("newImage.png", Vector2f(10.f, 20.f));