0
I have question about loading an image in code::blocks using an SFML, Texture and c++?
For a while I have been trying to write a code in c++ to load an image for a game that I have been writting. I use code::blocks with SFML. Despite of all tutorial that I have found and despite that the program dosen't return me any mistake the image hasn't been loading. So I would like if someone could explaine me how to load the image and if u can explaine me every step of it. It would be great if someone post code for it. Thx.
11 odpowiedzi
+ 3
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Image.php#a9e4f2aa8e36d0cabde5ed5a4ef80290b
Although, even though it says .jpg is supported, there are some cases where the .jpg still doesn't work, in which case a jpg to png conversion online works for me.
https://jpg2png.com/
+ 2
1. Load an image from a file and store it in an sf::Image
2. Load the image to an sf::Texture
3. Set the texture to an sf::Sprite
4. window.draw( sprite );
Example:
sf::Image image;
if( !image.loadFromFile( "FilePath" ) )
{
// error handling
}
sf::Texture texture; // <-- make sure texture is not destroyed while you're using it, e.g. std::vector may move data when adding data to it, making the pointer invalid.
texture.loadFromImage( image );
sf::Sprite sprite;
sprite.setTexture( texture );
// Fits the image to the window.
sprite.setScale( {static_cast<double>( window.getSize().x ) / texture.getSize().x, static_cast<double>( window.getSize().y ) / texture.getSize().y } );
...
window.clear();
window.draw( sprite );
window.display();
+ 2
You can use absolute path e.g.: "C:/images/auigr7923.png"
Here the file could be anywhere on your computer, but it's not very portable.
You can use relative paths to the .exe e.g: "images/auigr7923.png"
The folder would look like:
root/
-----/ images/
-----/ ---------/ auigr7923.png
-----/ bin.exe
or "auigr7923.png"
where the folder looks like:
root/
-----/ auigr7923.png
-----/ bin.exe
+ 2
It's just a folder, what appears first in it doesn't matter.
+ 2
The app.clear(), draw and display are outside the while loop.
Apart from that I don't see anything weird.
If the "Error in loading image." doesn't appear then try changing the values on the IntRect and/or setOrigin.
On another note: I recommend using / instead of \\ in the filepath, I think linux doesn't support the latter one. At least I read that somewhere :)
+ 1
#include <SFML/Graphics.hpp>
#include <windows.h>
#include <iostream>
using namespace sf;
const int W = 1000;
const int H = 600;
int main ()
{
RenderWindow app(VideoMode(W,H),"Neworld!");
app.setFramerateLimit(60);
sf::Image image;
if(!image.loadFromFile("C:\\Users\\pd\\Desktop\\Spacebattle\\bin\\images\\background.png"))
{
std::cout<<"Error in loading image."<<std::endl;
}
sf::Texture t1;
t1.loadFromImage(image);
sf::Sprite sPlayer;
sPlayer.setTexture(t1);
sPlayer.setTextureRect(IntRect(40,0,40,40));
sPlayer.setOrigin(20,20);
while (app.isOpen())
{
Event event;
while (app.pollEvent(event))
{
if (event.type == Event::Closed)
app.close();
}
}
app.clear();
app.draw( sPlayer );
app.display();
}
It still dosen't load image. Can you help me fix this code.
C:\Users\pd\Desktop\Spacebattle\Newstar.cpp (path to source code)
C:\Users\pd\Desktop\Spacebattle\bin\images\background.png(path to image)
0
Does it play any roll the size of image and it's formate?
0
Can you tell me where should I put the image. Is it important place of the image file?
0
I didn't get it well how came bin.exe came after the image?
0
Finally I got the image. Thx
0
https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Image.php#a9e4f2aa8e36d0cabde5ed5a4ef80290b
Although, even though it says .jpg is supported, there are some cases where the .jpg still doesn't work, in which case a jpg to png conversion online works for me.
https://jpgtoopng.com/