-
Is there any trivial way, perhaps using the HTTP or FTP sub-modules of the networking module, to load an image into a texture or image without actually putting the image onto the local machine?
-
Yes, you can download the image with sf::Http and then load it into a texture/image with the loadFromMemory function.
-
Sorry to bother you again
I've got this:
playerTexture.loadFromMemory(&avatarResponse.getBody(), avatarResponse.getBody().size());
But it says I have an invalid image. I think I'm doing it totally wrong but can't imagine a different way of doing it. Any chance you can see what I've done wrong?
-
Save the contents of "avatarResponse.getBody()" to a file, then download the image (with a regular internet browser), and compare both files to see what's different.
-
I've got very little experience with file I/O (I've only ever inputted and outputted basic text files) in C++, but I ended up with this:
std::ofstream file;
file.open("test.png", std::ios::binary, std::ios::out | std::ios::in | std::ios::trunc);
file << avatarResponse.getBody();
file.close();
And it crashes my program.
Am I using http::Response and http::Request right?
sf::Http::Request avatarRequest("/Internal/Images/Avatars/3.png");
sf::Http::Response avatarResponse = http.sendRequest(avatarRequest);
-
To write raw data, as opposed to formatted text, in an output stream, you must use the write member function instead of the << operator.
Then your code is incorrect: all std::ios flags must be combined with |, you mustn't split them into two separate arguments.
std::ios::out and std::ios::trunc are implicit for std::ofstream.
std::ios::in makes no sense for std::ofstream.
Try this:
std::string body = avatarResponse.getBody();
std::ofstream file;
file.open("test.png", std::ios::binary);
file.write(body.c_str(), body.size());
-
Ah, thank you, especially for correcting my file code. I'll try remembering that, although most of my mistakes seem obvious and stupid now that I've been shown them.
Yeah, the image outputs perfectly, looks identical to the one found on the site.
Edit
When trying to load the image the error says
Failed to load image from memory. Reason : Image not of any known type or corrupt
Edit
Using c_str on the body when loading from memory works and the texture loads correctly. Thanks! :D
-
Using c_str on the body when loading from memory works and the texture loads correctly.
Oh yeah, I missed that:
&avatarResponse.getBody()
This was actually an obvious mistake -- taking the adress of the string instead of its contents :P
-
Using c_str on the body when loading from memory works and the texture loads correctly.
Oh yeah, I missed that:
&avatarResponse.getBody()
This was actually an obvious mistake -- taking the adress of the string instead of its contents :P
All the ampersands and the asterisks are destroying my mind x3
-
You should use the .data()
Like this:
sf::Texture tussen;
if ( !tussen.loadFromMemory( response.getBody().data(), response.getBody().size() ) )
-
Thanks to Jonny P
From https://chat.sfml-dev.org/
#sfml
https://hastebin.com/abebojumuv.rb