[英]Texture going out of scope in SFML
Well, I'm trying to make a menu using SFML 2.1. 好吧,我正在尝试使用SFML 2.1制作菜单。 I have a 'something.h' header file and two source files.
我有一个“ something.h”头文件和两个源文件。 For the buttons, I've created a texture file.
对于按钮,我创建了一个纹理文件。 Now, since I want all the menu buttons to have the same texture, I was trying to declare the texture globally.
现在,由于我希望所有菜单按钮都具有相同的纹理,因此我试图全局声明该纹理。 I tried a couple of ways to do this.
我尝试了几种方法来做到这一点。 I tried to declare it just before all the class declarations in something.h, but I found out that you can't use texture.loadFromFile("blahblah") without a function.
我试图在something.h中的所有类声明之前声明它,但是我发现没有函数就不能使用texture.loadFromFile(“ blahblah”) 。 SO, I decided to make a class for this, and the following is the code.
因此,我决定为此创建一个类,以下是代码。
Something.h Something.h
#include<SFML/Graphics.hpp>
#include<iostream>
class Textureinitialize
{
public:
sf::Texture texture;
sf::Font font;
void loadtexture(const std::string& texturestring); //Specify file-name of texture
void loadfont(const std::string& fontstring); //Specify file-name of font
};
class Menubutton
{
public:
sf::Sprite menubuttonsprite;
sf::Text menubuttontext;
sf::Vector2i spritekaposition;
void loadthesprite(Textureinitialize obj1); //Load sprite from Texture and Text from font
void loadtexturepos(sf::IntRect rect1); //Load the sprite rectangle from the Texture
void spriteposition(sf::Vector2i originpos); //Specify position of button on the screen
void texttodisplay(std::string displaystring); //String to be displayed in button
void positionoftext(sf::Vector2i textpos,int textsize); //Set position and size of text to be displayed
};
This gave no error, but the menu button sprite does not display on the screen. 这没有出现错误,但是菜单按钮sprite不显示在屏幕上。 I think the problem is with the loading of the texture.
我认为问题在于纹理的加载。 I looked around, and found that somehow the texture is going out of scope.
我环顾四周,发现纹理超出了范围。 Please could anyone help me?
有人可以帮我吗?
EDIT: 编辑:
I guess more code is the need of the hour. 我猜需要更多的代码。 Here are the contents of the two source files I mentioned.
这是我提到的两个源文件的内容。
Something.cpp Something.cpp
#include "Menudata.h"
void Textureinitialize::loadtexture(const std::string& texturestring)
{
if(!texture.loadFromFile(texturestring))
std::cout<<"\nFailed to load textures";
}
void Textureinitialize::loadfont(const std::string& fontstring)
{
if(!font.loadFromFile(fontstring))
std::cout<<"\nFailed to load font";
}
void Menubutton::loadthesprite(Textureinitialize obj1)
{
menubuttonsprite.setTexture(obj1.texture);
menubuttontext.setFont(obj1.font);
}
void Menubutton::spriteposition(sf::Vector2i originpos)
{
spritekaposition.x=originpos.x;
spritekaposition.y=originpos.y;
menubuttonsprite.setPosition(originpos.x,originpos.y);
}
void Menubutton::loadtexturepos(sf::IntRect rect1)
{
menubuttonsprite.setTextureRect(rect1);
}
void Menubutton::texttodisplay(std::string displaystring)
{
menubuttontext.setString(displaystring);
}
void Menubutton::positionoftext(sf::Vector2i textpos,int textsize)
{
menubuttontext.setPosition(spritekaposition.x+textpos.x,spritekaposition.y+textpos.y);
menubuttontext.setCharacterSize(textsize);
menubuttontext.setColor(sf::Color::Red);
}
Main.cpp Main.cpp的
#include "Menudata.h"
int main()
{
Textureinitialize a;
a.loadtexture("Menubutton.tga");
a.loadfont("arial.ttf");
Menubutton b;
b.loadthesprite(a);
b.loadtexturepos(sf::IntRect(0,0,80,48));
b.spriteposition(sf::Vector2i(50,50));
b.texttodisplay("Hello!");
b.positionoftext(sf::Vector2i(50,14),20);
sf::RenderWindow window(sf::VideoMode(200,200),"My Window");
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type==sf::Event::Closed)
window.close();
if(event.type==sf::Event::MouseButtonPressed)
{
b.loadtexturepos(sf::IntRect(0,48,80,48));
if(event.mouseButton.button==sf::Mouse::Left)
std::cout<<"\nHello!!!";
}
if(event.type==sf::Event::MouseButtonReleased)
b.loadtexturepos(sf::IntRect(0,0,80,48));
}
window.clear(sf::Color::Green);
window.draw(b.menubuttonsprite);
window.draw(b.menubuttontext);
window.display();
}
return 0;
}
The reason I thought the texture is going out of scope was that I read somewhere on Google that if you're getting a white rectangle instead of the image, you might have the said issue. 我认为纹理超出范围的原因是,我在Google上某处读到,如果您得到的是白色矩形而不是图像,则可能是上述问题。 Also, I just noticed that I couldn't see any text in the white box.
另外,我只是注意到我在白框中看不到任何文本。 I do have a function which displays text on the buttons in the class 'Menubutton'.
我确实有一个功能,可在“菜单按钮”类的按钮上显示文本。 I think the problem might be in the initialization of font.
我认为问题可能出在字体的初始化上。 I hope what I've done in the class 'Textureinitialize' isn't wrong.
我希望我在“ Textureinitialize”类中所做的操作没有错。 I guess the problem might lie there.
我想问题可能出在这里。
As far as managing SFML resources goes, eg textures, I can only recommend this template: 就管理SFML资源(例如纹理)而言,我只能推荐以下模板:
https://github.com/LaurentGomila/SFML-Game-Development-Book/tree/master/02_Resources/Include/Book https://github.com/LaurentGomila/SFML-Game-Development-Book/tree/master/02_Resources/Include/Book
This is a, in my opionion, simple way to access the texture whenever you need it to construct your buttons 在我看来,这是一种在需要构造按钮时访问纹理的简单方法
PS: To improve readability of your code, I suggest using notations such as my_function or myFunction, instead of myfunction (all lowercase). PS:为提高代码的可读性,建议使用诸如my_function或myFunction之类的表示法,而不要使用myfunction(均小写)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.