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.

4th Jan 2019, 9:02 AM
Senudin
Senudin - avatar
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/
4th Jan 2019, 12:16 PM
Dennis
Dennis - avatar
+ 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();
4th Jan 2019, 9:26 AM
Dennis
Dennis - avatar
+ 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
4th Jan 2019, 2:32 PM
Dennis
Dennis - avatar
+ 2
It's just a folder, what appears first in it doesn't matter.
4th Jan 2019, 4:09 PM
Dennis
Dennis - avatar
+ 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 :)
4th Jan 2019, 5:22 PM
Dennis
Dennis - avatar
+ 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)
4th Jan 2019, 5:11 PM
Senudin
Senudin - avatar
0
Does it play any roll the size of image and it's formate?
4th Jan 2019, 10:11 AM
Senudin
Senudin - avatar
0
Can you tell me where should I put the image. Is it important place of the image file?
4th Jan 2019, 2:25 PM
Senudin
Senudin - avatar
0
I didn't get it well how came bin.exe came after the image?
4th Jan 2019, 4:08 PM
Senudin
Senudin - avatar
0
Finally I got the image. Thx
4th Jan 2019, 7:18 PM
Senudin
Senudin - avatar
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/
9th Mar 2024, 5:20 AM
Naveen Singh
Naveen Singh - avatar