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

Author Topic: sf::Texture load from memory  (Read 38182 times)

0 Members and 2 Guests are viewing this topic.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::Texture load from memory
« Reply #30 on: January 27, 2015, 06:48:46 pm »
What about the -Wnarrowing warnings? Is that just something that I need to put up with an wait a few minutes for it to compile every time I add a new image, or is there something I can do about it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Texture load from memory
« Reply #31 on: January 27, 2015, 09:10:19 pm »
We don't know where and why it happens, as such we can't give you any answer. I just know, if you do it correctly, it does work without warnings.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::Texture load from memory
« Reply #32 on: January 27, 2015, 09:14:42 pm »
Sorry for not being specific. My code goes like this:
static const char image[]
{
    "0x83, 0x91, 0x23, ...
(rest of image here)
     ... , 0xffffff"
};
My compiler then comes up with approximately one thousand Wnarrowing warnings of converting from int to const char* all said to be on the line of the closing curly brace, in the first column.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Texture load from memory
« Reply #33 on: January 27, 2015, 09:39:33 pm »
You probably don't want the two double-quotes (").
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::Texture load from memory
« Reply #34 on: January 27, 2015, 10:11:48 pm »
Ah sorry, I need to remember never to quote my code if it isn't right in front of me but I didn't have access to it at the time and I'd been looking at it for so long I thought I could remember it. Those double-quotes are from the method I was using for storing small images (gimp exported c source code) and they were at the start and end of every line. I got rid of them when I started using const char arrays instead. Other than the quotes, the code I posted is what I've got at the moment apart from the fact that they aren't static (I've just checked). I've disabled all warnings on my compiler and now and it isn't coming up with the -Wnarrowing, but warnings are useful for other things so I would prefer to fix it rather than leave it with the compiler just ignoring it and other potential problems. Sorry for being such a persistent problem, I just want to get this to work.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Texture load from memory
« Reply #35 on: January 27, 2015, 11:15:50 pm »
Provide a complete and minimal example. Without it we can just make random guesses.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: sf::Texture load from memory
« Reply #36 on: January 27, 2015, 11:26:06 pm »
Does it work if you change char to unsigned char?

Also, googling that error suggests that making sure your compiler is up-to-date may solve it.


Apart from those, what eXpl0it3r said.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Texture load from memory
« Reply #37 on: January 28, 2015, 12:05:31 am »
I've disabled all warnings on my compiler and now and it isn't coming up with the -Wnarrowing
That certainly is not a fix for anything.
Compiler warnings are there to help you. Just disabling them changes nothing.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::Texture load from memory
« Reply #38 on: January 28, 2015, 02:23:04 pm »
eXpl0it3r:
The image code (ButtonImages.hpp)
(click to show/hide)

The function that applies the image (Images.hpp)
(click to show/hide)

The use of the function:
(click to show/hide)

Hapax: I've tried converting it to unsigned char and that stops most of the Wnarrowing, though it does still display it once per array, but also comes up with large values being truncated (Woverflow)

Jester Juhl: I only turned warnings off because I actually couldn't compile it before because it took far too long to go through all the warnings

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Texture load from memory
« Reply #39 on: January 28, 2015, 02:35:06 pm »
Anything greater than 0x80 doesn't fit into a signed char, so indeed the type of the array has to be unsigned char[] (or uint8_t[]) if the generator outputs such constants.

As already said, you must also remove the ending 0xffffffff, this should get rid of the truncation warning.
Laurent Gomila - SFML developer

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::Texture load from memory
« Reply #40 on: January 28, 2015, 02:53:46 pm »
IT WORKS!!!!!  :)  :)  :)
I turned them all into unsigned chars, I got rid of the 0xfffffff and now it works!!!!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Texture load from memory
« Reply #41 on: January 28, 2015, 03:23:42 pm »
Right, I used sf::Uint8 which is why I never got the errors. Didn't think of that.
Also apparently this is only an "issue" since C++0x/C++11. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: sf::Texture load from memory
« Reply #42 on: January 28, 2015, 03:41:00 pm »
So it might be better in c++14? That would be nice, though as far as I know I can only use the version of MinGW that codeblocks was compiled with so I'll have to wait until it is remade.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Texture load from memory
« Reply #43 on: January 28, 2015, 04:04:18 pm »
No, that was meant as, it's only an "issue" start with C++0x/C++11 because of the initializationlists. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lelion

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: sf::Texture load from memory
« Reply #44 on: May 21, 2019, 01:34:00 am »
static const unsigned char embeddedImageData[] =
{
    /* content generated by one of the hundreds
        of programs that do it, or by your own small utility
       (doesn't take more than 10 lines of C++ code) */

};

texture.loadFromMemory(embeddedImageData, sizeof(embeddedImageData));

Example of code that generate such an array:

#include <fstream>
#include <iomanip>

int main(int argc, char** argv)
{
    std::ifstream in("image.png", std::ios_base::binary);
    std::ofstream out("header.hpp");

    out << "static const unsigned char embeddedImageData[] =" << std::endl;
    out << "{" << std::endl;
    do
    {
        out << "    ";
        for (int i = 0; (i < 20) && in; ++i)
            out << "0x" << std::hex << std::setw(2) << std::setfill('0') << in.get() << std::dec << ", ";
        out << std::endl;
    }
    while (in);
    out << "};" << std::endl;

    return 0;
}
 

Note that it can be more concise if you don't care about the pretty formatting.
(delete last 0xffffffff)

Merci beaucoup Laurent pour ce bou de code,  ca marche a merveille.
« Last Edit: May 21, 2019, 02:04:15 am by lelion »