传递 class 成员 object - SFML draw()
Passing class member object - SFML draw()
这似乎是一个非常奇怪的情况。我只想在主循环之外绘制一个 sf::Text object 句柄(在另一个 class 中)。
我只给你看最重要的。这段代码有效(它绘制了直接在 main 中处理的其他内容),因此可以编译。
主要 :
int main()
{
//we handle the creation of the window //
//...
//Example where the sf::Text is handle in the main (it works)
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text mainTxt("It works !!",font);
mainTxt.setPosition(sf::Vector2f(64,"It works !!"));
mainTxt.setCharacterSize(25);
mainTxt.setFillColor(sf::Color::Blue);
TextManager textManager(); //My class that don't work...
sf::Event event;
while (window.isOpen())
{
// We handle event (we don't care here)
// ...
window.draw(mainTxt); // it works
window.draw(textManager.getMyText()); // do nothing
window.display();
window.clear();
}
return 0;
}
文本管理器header:
#ifndef TEXTMANAGER_H
#define TEXTMANAGER_H
#include <SFML/Graphics.hpp>
class TextManager
{
public:
TextManager();
virtual ~TextManager();
sf::Text getMyText();
private:
sf::Text myText;
};
#endif // TEXTMANAGER_H
TextManager cpp
#include "TextManager.h"
TextManager::TextManager()
{
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text myText("Not drawn on the screen :-(",font);
myText.setPosition(sf::Vector2f(164,0));
myText.setCharacterSize(25);
myText.setFillColor(sf::Color::Blue);
}
// in this example (that do not work) I do not change fspText. But, I want to update it at each call of the get function. So, it is not a constant class member.
sf::Text TextManager::getMyText() {
return myText;
}
TextManager::~TextManager()
{
//dtor
}
我真的不明白的是,通过我的自定义 class,我可以使用这种 getter 访问 class 成员 object ].我也做了一些研究,我觉得应该return一份sf::Textobject。
我尝试了很多东西,比如 return 参考或 const 参考等...我不明白。
希望我的问题得到很好的展示。
谢谢您的帮助 ;-)
祝你有美好的一天!
这个
TextManager textManager(); //My class that don't work...
是函数声明,不是对象的构造。
应该是:
TextManager textManager; //My class that don't work...
由
sf::Text myText("Not drawn on the screen :-(",font);
您定义了一个名为 myText
的局部变量,与您的数据成员相同。因此,getMyText
返回的 myText
不受影响。
编码前阅读文档:
It is important to note that the sf::Text instance doesn't copy the
font that it uses, it only keeps a reference to it. Thus, a sf::Font
must not be destructed while it is used by a sf::Text (i.e. never
write a function that uses a local sf::Font instance for creating a
text).
取自 SFML reference.
class TextManager
{
public:
TextManager();
virtual ~TextManager();
sf::Text& getMyText(); // <- pass by reference
private:
sf::Font myFont;
sf::Text myText;
};
和
TextManager::TextManager()
{
myFont.loadFromFile("arial.ttf");
myText.setString("Not drawn on the screen :-(");
myText.setFont(myFont);
myText.setPosition(sf::Vector2f(164,0));
myText.setCharacterSize(25);
myText.setFillColor(sf::Color::Blue);
}
可能有用。
这似乎是一个非常奇怪的情况。我只想在主循环之外绘制一个 sf::Text object 句柄(在另一个 class 中)。
我只给你看最重要的。这段代码有效(它绘制了直接在 main 中处理的其他内容),因此可以编译。
主要 :
int main()
{
//we handle the creation of the window //
//...
//Example where the sf::Text is handle in the main (it works)
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text mainTxt("It works !!",font);
mainTxt.setPosition(sf::Vector2f(64,"It works !!"));
mainTxt.setCharacterSize(25);
mainTxt.setFillColor(sf::Color::Blue);
TextManager textManager(); //My class that don't work...
sf::Event event;
while (window.isOpen())
{
// We handle event (we don't care here)
// ...
window.draw(mainTxt); // it works
window.draw(textManager.getMyText()); // do nothing
window.display();
window.clear();
}
return 0;
}
文本管理器header:
#ifndef TEXTMANAGER_H
#define TEXTMANAGER_H
#include <SFML/Graphics.hpp>
class TextManager
{
public:
TextManager();
virtual ~TextManager();
sf::Text getMyText();
private:
sf::Text myText;
};
#endif // TEXTMANAGER_H
TextManager cpp
#include "TextManager.h"
TextManager::TextManager()
{
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text myText("Not drawn on the screen :-(",font);
myText.setPosition(sf::Vector2f(164,0));
myText.setCharacterSize(25);
myText.setFillColor(sf::Color::Blue);
}
// in this example (that do not work) I do not change fspText. But, I want to update it at each call of the get function. So, it is not a constant class member.
sf::Text TextManager::getMyText() {
return myText;
}
TextManager::~TextManager()
{
//dtor
}
我真的不明白的是,通过我的自定义 class,我可以使用这种 getter 访问 class 成员 object ].我也做了一些研究,我觉得应该return一份sf::Textobject。 我尝试了很多东西,比如 return 参考或 const 参考等...我不明白。
希望我的问题得到很好的展示。 谢谢您的帮助 ;-) 祝你有美好的一天!
这个
TextManager textManager(); //My class that don't work...
是函数声明,不是对象的构造。 应该是:
TextManager textManager; //My class that don't work...
由
sf::Text myText("Not drawn on the screen :-(",font);
您定义了一个名为 myText
的局部变量,与您的数据成员相同。因此,getMyText
返回的 myText
不受影响。
编码前阅读文档:
It is important to note that the sf::Text instance doesn't copy the font that it uses, it only keeps a reference to it. Thus, a sf::Font must not be destructed while it is used by a sf::Text (i.e. never write a function that uses a local sf::Font instance for creating a text).
取自 SFML reference.
class TextManager
{
public:
TextManager();
virtual ~TextManager();
sf::Text& getMyText(); // <- pass by reference
private:
sf::Font myFont;
sf::Text myText;
};
和
TextManager::TextManager()
{
myFont.loadFromFile("arial.ttf");
myText.setString("Not drawn on the screen :-(");
myText.setFont(myFont);
myText.setPosition(sf::Vector2f(164,0));
myText.setCharacterSize(25);
myText.setFillColor(sf::Color::Blue);
}
可能有用。