[英]Texture going out of scope in SFML
好吧,我正在嘗試使用SFML 2.1制作菜單。 我有一個“ something.h”頭文件和兩個源文件。 對於按鈕,我創建了一個紋理文件。 現在,由於我希望所有菜單按鈕都具有相同的紋理,因此我試圖全局聲明該紋理。 我嘗試了幾種方法來做到這一點。 我試圖在something.h中的所有類聲明之前聲明它,但是我發現沒有函數就不能使用texture.loadFromFile(“ blahblah”) 。 因此,我決定為此創建一個類,以下是代碼。
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
};
這沒有出現錯誤,但是菜單按鈕sprite不顯示在屏幕上。 我認為問題在於紋理的加載。 我環顧四周,發現紋理超出了范圍。 有人可以幫我嗎?
編輯:
我猜需要更多的代碼。 這是我提到的兩個源文件的內容。
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的
#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;
}
我認為紋理超出范圍的原因是,我在Google上某處讀到,如果您得到的是白色矩形而不是圖像,則可能是上述問題。 另外,我只是注意到我在白框中看不到任何文本。 我確實有一個功能,可在“菜單按鈕”類的按鈕上顯示文本。 我認為問題可能出在字體的初始化上。 我希望我在“ Textureinitialize”類中所做的操作沒有錯。 我想問題可能出在這里。
就管理SFML資源(例如紋理)而言,我只能推薦以下模板:
https://github.com/LaurentGomila/SFML-Game-Development-Book/tree/master/02_Resources/Include/Book
在我看來,這是一種在需要構造按鈕時訪問紋理的簡單方法
PS:為提高代碼的可讀性,建議使用諸如my_function或myFunction之類的表示法,而不要使用myfunction(均小寫)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.