SFML community forums

Help => System => Topic started by: kneksacke on February 28, 2017, 05:20:44 pm

Title: Embedded image using resource
Post by: kneksacke on February 28, 2017, 05:20:44 pm
My last question was on how to remove the DLLs and i could just static link them or whatever its called,
it was really simple... but now i want my game textures to be stored inside the executable too
i was thinking that this would be easy. so i looked it up and you need to use resource files for that.
i tried doing it but failed a lot.

i don't now how to load in the resource. can someone program a function like

sf::texture = loadResource("MyResource");

or give me a tutorial.


this is my current code
        HRSRC resBlock = FindResource(NULL, "IDB_BITMAP1", RT_BITMAP);
                HGLOBAL myGlobal = LoadResource(NULL, resBlock);
                LPVOID firstByte = LockResource(myGlobal);
                DWORD resSize = SizeofResource(NULL, resBlock);
                sf::Image loadImg;
                sf::Sprite tmpSpr;
                sf::Texture text;
                loadImg.loadFromMemory(firstByte, resSize);
                text.loadFromImage(loadImg);


it does not work. at all.
oh and i copied it from someone else. (another post)


NOTE:
i'm using visual studio 2015
my English is horrible i know.
my last post was a bit unclear i hope this one is better.



Title: Re: Embedded image using resource
Post by: Laurent on February 28, 2017, 08:04:51 pm
Don't bother with OS-specific code, all you want to do is to embed a bunch of bytes into your code and interpret them as a resource file.

1. Convert your resource file to a C++ array (easy to do yourself, or use a dedicated program / website like this http://tools.garry.tv/bin2c/)

2. Paste the generated C++ array into your code

3. Load it with the loadFromMemory function
char myResource[] =
{
   /* the generated array */
};

sf::Texture texture;
texture.loadFromMemory(myResource, sizeof(myResource));
Title: Re: Embedded image using resource
Post by: kneksacke on February 28, 2017, 09:17:24 pm
Don't bother with OS-specific code, all you want to do is to embed a bunch of bytes into your code and interpret them as a resource file.

1. Convert your resource file to a C++ array (easy to do yourself, or use a dedicated program / website like this http://tools.garry.tv/bin2c/)

2. Paste the generated C++ array into your code

3. Load it with the loadFromMemory function
char myResource[] =
{
   /* the generated array */
};

sf::Texture texture;
texture.loadFromMemory(myResource, sizeof(myResource));

Very smart i will try that, thank you!

UPDATE

i tried it and it worked perfectly! thank you very much. but is it possible to do the same thing with audio?

( i tried to put it in the tool you send me but it just returns 0 )
Title: Re: Embedded image using resource
Post by: Laurent on February 28, 2017, 10:21:38 pm
What kind of audio file? Have you tried other tools?
Title: Re: Embedded image using resource
Post by: kneksacke on March 01, 2017, 11:26:45 am
What kind of audio file? Have you tried other tools?

Its seems to work only on a few audio files. but i can even use it because i need openAL for sound
and i can't static link it. any tips?
Title: Re: Embedded image using resource
Post by: Laurent on March 01, 2017, 11:32:32 am
Quote
Its seems to work only on a few audio files
Maybe you could describe the error more precisely. What exactly fails, and how?

Quote
but i can even use it because i need openAL for sound and i can't static link it
You can't link it statically, unless you comply with the LGPL license (ie. release your own code under a compatible license). What's the problem with dynamic linking?
Title: Re: Embedded image using resource
Post by: kneksacke on March 01, 2017, 11:38:29 am
Quote
Its seems to work only on a few audio files
Maybe you could describe the error more precisely. What exactly fails, and how?

Quote
but i can even use it because i need openAL for sound and i can't static link it
You can't link it statically, unless you comply with the LGPL license (ie. release your own code under a compatible license). What's the problem with dynamic linking?

My goal is to make a complete clean exe file. i don't know about that LGPL license can you explain a bit more?

Some audio files just return 0 (mp3) and some .wav. i think it could be because the size is too big.

Title: Re: Embedded image using resource
Post by: Laurent on March 01, 2017, 11:57:52 am
Quote
i don't know about that LGPL license can you explain a bit more?
Some licenses impose restrictions on derivative work. A common restriction of the LGPL is to force your project to be LGPL too, if it uses the LGPL-licensed dependency statically. This restriction doesn't apply if you link it dynamically. That's why SFML links OpenAL dynamically, sothat you don't have to bother with license issues.

Quote
Some audio files just return 0 (mp3) and some .wav. i think it could be because the size is too big.
But what returns 0? Where? Your code? The online converter? Please take the time to describe the problem with details.
Title: Re: Embedded image using resource
Post by: kneksacke on March 01, 2017, 01:36:50 pm
Quote
i don't know about that LGPL license can you explain a bit more?
Some licenses impose restrictions on derivative work. A common restriction of the LGPL is to force your project to be LGPL too, if it uses the LGPL-licensed dependency statically. This restriction doesn't apply if you link it dynamically. That's why SFML links OpenAL dynamically, sothat you don't have to bother with license issues.

Quote
Some audio files just return 0 (mp3) and some .wav. i think it could be because the size is too big.
But what returns 0? Where? Your code? The online converter? Please take the time to describe the problem with details.


Sorry. i meant the online converter.

is it possible to use sound without openAL??
Title: Embedded image using resource
Post by: eXpl0it3r on March 01, 2017, 01:38:00 pm
Not with SFML.
Title: Re: Embedded image using resource
Post by: Laurent on March 01, 2017, 02:07:57 pm
Quote
Sorry. i meant the online converter.
Probably a size limit on upload. Try an offline tool, or write your own program (10 lines of code max).
Title: Re: Embedded image using resource
Post by: kneksacke on March 01, 2017, 09:31:06 pm
Quote
Sorry. i meant the online converter.
Probably a size limit on upload. Try an offline tool, or write your own program (10 lines of code max).

I like the way of converting it in a array the only problem is that if i hold my cursor to long on the name
visual studio tries to preview the array what causes lag/crashes because its so long.

(over the openAL thing)
Is there any sneaky way too avoid the license and just force link it.
i may sound a bit stupid here.
Title: Re: Embedded image using resource
Post by: Hapax on March 01, 2017, 09:34:12 pm
I created a simple one of converters for myself.
I see you're using Visual Studio so I can presume Windows.
Therefore, feel free to use this:
http://www.mediafire.com/file/5ttatyq313bzenb/HppDataArrayFromFileBytes.exe

You can use it via command line or just drag the file you want to convert onto its icon (or shortcut) and it will do it automatically.

The final .hpp file also has extra stuff like an include guard and formatted values. Using the command line (or a switch), you can output the values in hexadecimal instead.

Note that this was built on Visual Studio 2013 so you may require the redistributables for that (https://www.microsoft.com/en-GB/download/details.aspx?id=40784) if you don't have them already.


I hear your pain with the hover. It killed me a couple of times ;D
Best advice I can give is since it's a resource, deal with it once and then hide it and never deal with it again! ;) (you could hide the loading of it in another file, for example).
Title: Re: Embedded image using resource
Post by: kneksacke on March 01, 2017, 10:00:52 pm
I created a simple one of converters for myself.
I see you're using Visual Studio so I can presume Windows.
Therefore, feel free to use this:
http://www.mediafire.com/file/5ttatyq313bzenb/HppDataArrayFromFileBytes.exe

You can use it via command line or just drag the file you want to convert onto its icon (or shortcut) and it will do it automatically.

The final .hpp file also has extra stuff like an include guard and formatted values. Using the command line (or a switch), you can output the values in hexadecimal instead.

Note that this was built on Visual Studio 2013 so you may require the redistributables for that (https://www.microsoft.com/en-GB/download/details.aspx?id=40784) if you don't have them already.


I hear your pain with the hover. It killed me a couple of times ;D
Best advice I can give is since it's a resource, deal with it once and then hide it and never deal with it again! ;) (you could hide the loading of it in another file, for example).

Thank you !
Title: Re: Embedded image using resource
Post by: Laurent on March 01, 2017, 10:27:27 pm
Quote
Is there any sneaky way too avoid the license and just force link it.
i may sound a bit stupid here.
Release your sources under the LGPL too. Or live with its restrictions. You know, licenses exist to protect people's work, and ensure that they will be used (and not misused) as their author whishes. So it's really wrong to try to "avoid the license" or "force" things as you like.
Title: Re: Embedded image using resource
Post by: kneksacke on March 02, 2017, 01:22:23 pm
Quote
Is there any sneaky way too avoid the license and just force link it.
i may sound a bit stupid here.
Release your sources under the LGPL too. Or live with its restrictions. You know, licenses exist to protect people's work, and ensure that they will be used (and not misused) as their author whishes. So it's really wrong to try to "avoid the license" or "force" things as you like.

Oh okay. i will use another audio library then. 
Title: Re: Embedded image using resource
Post by: Laurent on March 02, 2017, 02:58:52 pm
If you find one that has all the features of OpenAL, works on every platform, but with a more permissive license, then let us know please :P
Title: Re: Embedded image using resource
Post by: dabbertorres on March 03, 2017, 05:14:28 am
It's still in beta, so maybe just up for future consideration, but Steam Audio (https://github.com/ValveSoftware/steam-audio) was released recently.
Title: Re: Embedded image using resource
Post by: Hapax on March 04, 2017, 03:38:32 pm
Steam Audio (https://github.com/ValveSoftware/steam-audio)
Quote
1.6 Trademarks. Licensee agrees to place the following notices in the credits for any Licensee Product (replacing xxxx with the current year):

  “[Product name] uses the Steam® Audio SDK. Steam® is a trademark or registered trademark of Valve Corporation in the United States of America and elsewhere”

  “Steam® Audio, Copyright 2016 – xxxx, Valve Corp. All rights reserved.”

I think that is enough to disqualify the library, unfortunately :(