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

Author Topic: Load from resource file  (Read 7929 times)

0 Members and 1 Guest are viewing this topic.

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Load from resource file
« on: June 25, 2011, 11:15:44 am »
Hi,

I want to load an image from the resource files added in visual studio, but it does not work.

I have in .rc this:
Code: [Select]
FONT_NORMAL BITMAP  DISCARDABLE     "normal_1.bmp"

I have in resource.h this:
Code: [Select]
#define FONT_NORMAL       115

I have in the main.cpp this:
Code: [Select]
unsigned char Texture = FONT_NORMAL;

    sf::Image backgroundImage;
    if (!backgroundImage.LoadFromMemory(&Texture, sizeof(Texture) ) )
        return EXIT_FAILURE;


Not compile error, yet it fails to start the program with no error message.  :roll:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6276
  • Thor Developer
    • View Profile
    • Bromeon
Load from resource file
« Reply #1 on: June 25, 2011, 11:53:42 am »
I don't know how those resources exactly work in Visual Studio, but I can tell you that sizeof(Texture) is sizeof(unsigned char) and therefore always 1 byte. I don't think your image needs only 1 byte ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Cuban-Pete

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Load from resource file
« Reply #2 on: June 26, 2011, 02:43:05 pm »
I changed that and also tried LoadFromPixels() function, but it does not work. Or it crashes or it gives a very weird image.

What is the multiplatform-way to include files into the exe and later on call them in the program?

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Load from resource file
« Reply #3 on: June 26, 2011, 04:37:00 pm »
Yeah this is a bit of a nightmare, to be honest. I've not managed to get it working yet, but I haven't spent much time on it.

Have a read of this, it might help.

http://stackoverflow.com/questions/1074362/embedded-resource-in-c

I don't know of a way to do this in a truly multiplatform method, other than using Molebox (on Windows) and something similar for other systems.
SFML 2.1

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Load from resource file
« Reply #4 on: June 26, 2011, 07:02:25 pm »
This code, which should work, doesn't - but see how different to yours it is :)

Code: [Select]

HRSRC resBlock = FindResource(NULL,"MY_RESOURCE_NAME",RT_BITMAP);
HGLOBAL myGlobal = LoadResource(NULL,resBlock);
LPVOID firstByte = LockResource(myGlobal);
DWORD resSize = SizeofResource(NULL,resBlock);
sf::Image loadImg;
sf::Sprite tmpSpr;
loadImg.LoadFromMemory(firstByte,resSize);
tmpSpr.SetImage(loadImg);
tmpSpr.SetPosition(0,0);
window.Draw(tmpSpr);


In this case, LoadFromMemory fails so loadImg has no contents..
SFML 2.1

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Load from resource file
« Reply #5 on: June 26, 2011, 07:38:20 pm »
The only way I could see that code failing is if FindResource is failing.

Did you check resBlock to make sure it's non-NULL?

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Load from resource file
« Reply #6 on: June 26, 2011, 10:09:49 pm »
Yes, resBlock is OK, but for some reason the sizeof is always 14 bytes less than the actual filesize, which I assume is why it doesn't load correctly. Missing header, perhaps?
SFML 2.1

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Load from resource file
« Reply #7 on: June 26, 2011, 10:21:02 pm »
Hmm I have looked at the first bytes and whatever I am getting, it's not a BMP, there is no 4D42 in the first two bytes. Strange - because the filesize is so nearly correct.
SFML 2.1

Redcap

  • Newbie
  • *
  • Posts: 3
    • View Profile
Load from resource file
« Reply #8 on: July 22, 2011, 03:04:39 pm »
Hey Slotdev,

I've just had a similar issue while trying to create an Icon file from one in the resource/application.

My resulting "Icon" is 22 bytes smaller, I noticed though that it's the first 22 bytes which are missing.

Perhaps this explains why you are not getting 4D42 in the first two bytes, because the first 14 bytes are actually being missed, as apposed to the last 14.

I haven't found a solution yet, but will keep you updated if I find anything.

I realize this post is roughly a month old, so if you have found a way to fix the issue, please let me know.


Regards,
RC

Redcap

  • Newbie
  • *
  • Posts: 3
    • View Profile
Load from resource file
« Reply #9 on: July 22, 2011, 03:21:25 pm »
Ok, so what I found out about my Icon issues, also applies to your bitmap problems.


Quote
You cannot load a resource into your application and then write it to a file in this way, as an icon resource and an icon file are not exactly the same thing. The icon file contains header information which is not part of the actual resource, so what you get loaded into memory is not the complete file.


Hope this helps some if you're still stuck on this.

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
Load from resource file
« Reply #10 on: July 22, 2011, 03:28:04 pm »
Hi

To be honest I just gave up on this - I will try it again at some point soon. Annoyingly, this should be really easy to do, but no :(

Thanks
SFML 2.1

Redcap

  • Newbie
  • *
  • Posts: 3
    • View Profile
Load from resource file
« Reply #11 on: July 22, 2011, 04:21:56 pm »
You're welcome.

Best of luck with your next attempt, hopefully it goes much smoother :)

EDIT:

While experimenting just now, I've found a (slightly odd) way to do this.

Instead of including it as a bitmap, try including it as RCDATA.

It will load it as data, instead of loading as a bmp resource, so it will retain the header information so it can be saved into a bmp  :)

Not 100% sure if this will help your situation, but it may at least offer an easy work around.

the_composer

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Load from resource file
« Reply #12 on: March 06, 2013, 07:42:18 pm »
Did anyone ever figure out how to do this? I'm using Visual Studion 2012, but I can't figure out how to load from a resource file.

 

anything