我正在使用带有sfml的代码块,除了看起来很难看,我不确定在哪里存储图像。我必须通过将其放入文件系统中的项目文件夹中来选择确切的路径。如何添加图像?
假设您的项目在
C:\ Users \ Me \ Documents \ MyProj
然后,仅在使用图像名称时,将图像放在此文件夹中将使它们对SFML /代码块可见。
如果您有一个名为image.png的图像,则该图像的完整路径为
C:\ Users \ Me \ Documents \ MyProj \ image.png
要将图像放入程序中,您必须包含
#include <SFML/Graphics.hpp>
并创建一个纹理
sf::Texture Texture1;
然后您可以像这样分配该图像
Texture1.loadFromFile("C:\Users\Me\Documents\MyProj\image.png"); //Full path
Texture1.loadFromFile("image.png"); //Relative path
接下来创建一个精灵
sf::Sprite MySprite
并为其分配纹理
MySprite.setTexture(Texture1);
最后在窗口中绘制使用
window.draw(MySprite);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句