Texture 的加载与使用
Texture的创建方法:
- 直接从文件中加载Texture
bool loadFromFile (const std::string &filename, const IntRect &area=IntRect())
- 从内存中加载Texture
bool loadFromMemory (const void *data, std::size_t size, const IntRect &area=IntRect())
- 从输入流中加载Texture
bool loadFromStream (InputStream &stream, const IntRect &area=IntRect())
4.从图片中加载Texture
bool loadFromImage (const Image &image, const IntRect &area=IntRect())
测试代码
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/System.hpp>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "Texture ");
sf::Texture texutre_from_file;
sf::Texture texture_from_image;
sf::Texture text_from_stream;
sf::RectangleShape shape1;
sf::RectangleShape shape2;
sf::RectangleShape shape3;
sf::RectangleShape shape4;
//1.从文件中直接加载Texture
if(!texutre_from_file.loadFromFile("Textures/Eagle.png"))
{
std::cout << "ERROR: texutre_from_file loading failed"<< std::endl;
}
shape1.setSize(sf::Vector2f(static_cast<float>(texutre_from_file.getSize().x),
static_cast<float>(texutre_from_file.getSize().y)));
shape1.setTexture(&texutre_from_file);
shape1.setPosition(100.f,100.f);
//2.从图片中加载Texture
sf::Image image;
if(!image.loadFromFile("Textures/Eagle.png"))
{
std::cout << "Error: image loading failed"<< std::endl;
}
if(!texture_from_image.loadFromImage(image))
{
std::cout << "Error: texture_from_image loading failed" <<std::endl;
}
shape2.setSize(sf::Vector2f(static_cast<float>(texture_from_image.getSize().x),
static_cast<float>(texture_from_image.getSize().y)));
shape2.setTexture(&texture_from_image);
shape2.setPosition(200.f,100.f);
//3.从输入流中加载Texture
sf::FileInputStream stream;
if(!stream.open("Textures/Eagle.png"))
{
std::cout << "ERROR: Textures/Eagle.png file open failed " << std::endl;
}
if(!text_from_stream.loadFromStream(stream))
{
std::cout << "ERROR: text_from_stream loading failed" << std::endl;
}
shape3.setSize(sf::Vector2f(static_cast<float>(text_from_stream.getSize().x),
static_cast<float>(text_from_stream.getSize().y)));
shape3.setTexture(&text_from_stream);
shape3.setPosition(300.f,100.f);
sf::Texture tx;
tx.loadFromImage(image);
tx.setRepeated(true);
shape4.setSize(sf::Vector2f(static_cast<float>(tx.getSize().x) *2 ,static_cast<float>(tx.getSize().y) * 3 ));
shape4.setTextureRect(sf::IntRect(0,0, tx.getSize().x *2 , tx.getSize().y * 3 ));
shape4.setTexture(&tx);
shape4.setPosition(100.f,400.f);
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
}
// Clear screen
window.clear(sf::Color::White);
window.draw(shape1);
window.draw(shape2);
window.draw(shape3);
window.draw(shape4);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}
sf::Sprite 精灵类
Sprite 是SFML中的一个关键类,用于呈现2D图像或纹理,可以在屏幕上绘制图像,并具有各种属性和方法来控制其行为。
Sprite相当于一个有纹理的矩形,它的尺寸是由它的纹理控制的,我们可以通过Sprite类提供的成员函数来对其进行缩放、移动、旋转等操作。
Sprite使用注意事项
【官方文档中提到】sf::Sprite实例不会复制它所使用的纹理,它只保留对它的引用。因此,sf::Texture在被sf::Sprite使用时一定不能被销毁(也就是说,永远不要编写一个使用本地sf::Texture实例来创建Sprite的函数)。
//这种使用方式是错误的
//当函数返回Sprite对象后,texture变量生命周期就已经结束,会被销毁掉。
sf::Sprite createSprite(std::string &filename)
{
sf::Texture texture;
texture.loadFromFile(filename);
return sf::Sprite(texture);
}
Sprite 类的构造函数
- Sprite ()
- Sprite (const Texture &texture)
- Sprite (const Texture &texture, const IntRect &rectangle)
常用方法
-
void setTexture (const Texture &texture, bool resetRect=false)
设置精灵的纹理 -
void setTextureRect (const IntRect &rectangle)
在原始纹理中提取某个区域在精灵中显示 -
void setColor (const Color &color)
设置精灵的全局颜色 -
FloatRect getGlobalBounds () const
取得精灵的全局边界 -
void setPosition (float x, float y)
设置精灵的位置 -
void setPosition (const Vector2f &position)
设置精灵的位置 -
void move (float offsetX, float offsetY)
移动精灵 -
void move (const Vector2f &offset)
移动精灵 -
void rotate (float angle)
按角度旋转精灵 -
void scale (float factorX, float factorY)
缩放精灵尺寸大小 -
void scale (const Vector2f &factor)
缩放精灵尺寸大小
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/System.hpp>
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800, 600), "Texture ");
window.setFramerateLimit(60);
sf::Texture texutre_from_file;
sf::Sprite sprite;
sf::Sprite sprite2;
if(!texutre_from_file.loadFromFile("Textures/Eagle.png"))
{
std::cout << "ERROR: texutre_from_file loading failed"<< std::endl;
}
//移动方向
int dir = -1;
sprite.setTexture(texutre_from_file);
//缩放大小
//sprite.setScale(2.f,2.f);
sprite.scale(2.f,2.f);
//填充着色
sprite.setColor(sf::Color::Red);
//设置位置
sprite.setPosition(sf::Vector2f(400.f,100.f));
sprite2.setTexture(texutre_from_file);
sprite2.setPosition(100.f,300.f);
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
}
sprite.move(2.0 *dir,0);
sprite2.rotate(1.f);
//左边框检测
if(sprite.getGlobalBounds().left <= 0){
sprite.setPosition(0,sprite.getGlobalBounds().getPosition().y);
dir = 1;
}
//右边框检测
else if(sprite.getGlobalBounds().left + sprite.getGlobalBounds().width >= 800 ){
sprite.setPosition(800 - sprite.getGlobalBounds().width,sprite.getPosition().y);
dir = -1;
}
// Clear screen
window.clear(sf::Color::White);
window.draw(sprite2);
window.draw(sprite);
// Update the window
window.display();
}
return 0;
}
代码中使用的图片