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

Author Topic: Embedded image using resource  (Read 17467 times)

0 Members and 2 Guests are viewing this topic.

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Embedded image using resource
« 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.



« Last Edit: February 28, 2017, 05:23:10 pm by kneksacke »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embedded image using resource
« Reply #1 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));
« Last Edit: February 28, 2017, 08:07:44 pm by Laurent »
Laurent Gomila - SFML developer

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Embedded image using resource
« Reply #2 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 )
« Last Edit: February 28, 2017, 10:11:41 pm by kneksacke »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embedded image using resource
« Reply #3 on: February 28, 2017, 10:21:38 pm »
What kind of audio file? Have you tried other tools?
Laurent Gomila - SFML developer

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Embedded image using resource
« Reply #4 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?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embedded image using resource
« Reply #5 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?
Laurent Gomila - SFML developer

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Embedded image using resource
« Reply #6 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.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embedded image using resource
« Reply #7 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.
Laurent Gomila - SFML developer

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Embedded image using resource
« Reply #8 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??

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Embedded image using resource
« Reply #9 on: March 01, 2017, 01:38:00 pm »
Not with SFML.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embedded image using resource
« Reply #10 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).
Laurent Gomila - SFML developer

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Embedded image using resource
« Reply #11 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.
« Last Edit: March 01, 2017, 09:33:33 pm by kneksacke »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Embedded image using resource
« Reply #12 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 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).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

kneksacke

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Embedded image using resource
« Reply #13 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 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 !

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Embedded image using resource
« Reply #14 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.
Laurent Gomila - SFML developer

 

anything