Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: LoadFromMemory Sprites  (Read 5567 times)

0 Members and 1 Guest are viewing this topic.

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
LoadFromMemory Sprites
« on: July 26, 2024, 01:31:27 pm »
Hi,
I am on a windows machine using Code::Blocks. i have added my resource file to my project with so far one PNG sprite in there. I have made a resource.rc file and taken in the sprite as RCDATA which I have successfully done in other game engines. I just don't know how to turn the RCDATA png into a texture by using LoadFromMemory. I will be wanting to use this as a sprite.
Here is my resource file
SCOTT RCDATA "rc/SCOTT.bin"
 
I have also included a header file resource.h
#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define SCOTT 101

Now I want to know how to get my image which is a PNG into LoadFromMemory.

Bare in mind I am on windows and I am using Code::Blocks.
Also I have only been using SFML for a few days, so I am very new to it.


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11049
    • View Profile
    • development blog
    • Email
Re: LoadFromMemory Sprites
« Reply #1 on: July 28, 2024, 02:55:16 pm »
You'll somehow need access to the memory location of th image. I don't know if the resource compiler embeds the image data 1:1, but if it does and if you get access to the bytes in memory, you can use loadFromMemory. Alternatively, if you do get a stream, you might be able to do something with loadFromStream.

See this StackOverflow for some potential answers. You should be able to ignore the GDI specific code.
https://stackoverflow.com/questions/69488064/embed-png-or-bitmap-with-alpha-channel-in-executable

Alternatively, you could generate a byte array and compile that with your application, which you should be able to directly load with loadFromMemory.

Generally speaking, I don't recommend embedding resources into the executable.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: LoadFromMemory Sprites
« Reply #2 on: July 29, 2024, 04:04:12 pm »
Hi eXpl0it3r,

Thanks I will look into GDI. I think I used it on the irrlicht engine a while ago, but I have been out of practice for a while, and trying to get back into programming.

If it is only a small game I prefer to load sprites from a resource file, and then they only need the exe, and I don't have to rely on people having to make sure that the folder structure stays in tact.

Asimov

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: LoadFromMemory Sprites
« Reply #3 on: July 29, 2024, 07:32:24 pm »
Hi,

I found some code which does load the image from my resource. I know it works because was able to save the image out, but I am having trouble getting it into a Sprite. Ok so the following function works, but it isn't one i wrote. I found it on stackoverflow.
sf::Image LoadImageFromResource(const std::string& name)
{
    HRSRC rsrcData = FindResource(NULL, name.c_str(), RT_RCDATA);
    if (!rsrcData)
        throw std::runtime_error("Failed to find resource.");

    DWORD rsrcDataSize = SizeofResource(NULL, rsrcData);
    if (rsrcDataSize <= 0)
        throw std::runtime_error("Size of resource is 0.");

    HGLOBAL grsrcData = LoadResource(NULL, rsrcData);
    if (!grsrcData)
        throw std::runtime_error("Failed to load resource.");

    LPVOID firstByte = LockResource(grsrcData);
    if (!firstByte)
        throw std::runtime_error("Failed to lock resource.");

    sf::Image image;
    if (!image.loadFromMemory(firstByte, rsrcDataSize))
        throw std::runtime_error("Failed to load image from memory.");
    return image;
}
 


So in my main I put this to test if it worked.
sf::Image scotty_png = LoadImageFromResource("SCOTT_PNG");
scotty_png.saveToFile("pp.png");
 

Good news is that it loaded the png from the resource and actually saved out the Png to a file. So I know the resource is working. The problem is that I don't know how to turn this into a sprite. I think it is already a texture when it comes in. Or have I got that wrong?

I tried this, but it says I am doing a redeclaratoin
 sf::Sprite ScottSprite;
    ScottSprite.setTexture(scotty_png);

PS. It is actually image. So I am just trying to work out how to convert the image to a Sprite now LOL.


I think with a little help I can get this working.

Asimov

« Last Edit: July 29, 2024, 08:16:15 pm by Asimov »

Asimov

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: LoadFromMemory Sprites
« Reply #4 on: July 29, 2024, 08:22:58 pm »
It's ok. I have now successfully converted my loaded image from my resource in the other code. Then all I had to do was this
 sf::Image scotty_png = LoadImageFromResource("SCOTT_PNG");
    sf::Texture scottTexture;
   scottTexture.loadFromImage(scotty_png);

Thanks for your help anyway.
I hope this helps someone else with the same problem.

Hapax

  • Hero Member
  • *****
  • Posts: 3387
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: LoadFromMemory Sprites
« Reply #5 on: August 11, 2024, 04:47:41 pm »
Since your function returns an sf::Image, you can use it directly in the call to loadFromImage, presuming you have no intention of using the sf::Image for processing.:
sf::Texture scottTexture;
scottTexture.loadFromImage(LoadImageFromResource("SCOTT_PNG"));

That image will automatically be destroyed (cleaned up) after the texture is loaded from it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*