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

Author Topic: sf::Texture.LoadFromMemory?  (Read 2789 times)

0 Members and 1 Guest are viewing this topic.

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
sf::Texture.LoadFromMemory?
« on: October 26, 2011, 09:10:40 am »
Currently I'm using Bin2C to generate a (*.c) file containing all of the information that makes up my image.  

Here's what the generated file looks like:
Code: [Select]

static unsigned char test_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20,
0x08, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x18, 0xed, 0xa3, 0x00, 0x00, 0x00,
0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00,
0x0b, 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74,
0x49, 0x4d, 0x45, 0x07, 0xdb, 0x0a, 0x1a, 0x06, 0x2d, 0x30, 0xc3, 0x47,
0xff, 0xe7, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f,
0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
0x81, 0x0e, 0x17, 0x00, 0x00, 0x02, 0x9f, 0x49, 0x44, 0x41, 0x54, 0x48,
0xc7, 0x8d, 0x56, 0x49, 0x72, 0xe4, 0x30, 0x0c, 0x03, 0xaa, 0xf4, 0xff,
0x4f, 0xe4, 0x3e, 0x2f, 0x24, 0x83, 0x39, 0x70, 0x11, 0xe5, 0xa5, 0x3b,
0x3e, 0x74, 0x62, 0x49, 0xe6, 0x02, 0x90, 0xa0, 0xf8, 0xef, 0xe7, 0x47,
0x02, 0x20, 0x90, 0x14, 0x04, 0x11, 0x14, 0x84, 0x78, 0x08, 0x8a, 0xa2,
0x20, 0x02, 0x22, 0x00, 0x42, 0x22, 0x08, 0x42, 0x12, 0x80, 0x5c, 0x17,
0xf2, 0x3b, 0x02, 0x22, 0x51, 0x7b, 0xcb, 0xcc, 0xc2, 0x94, 0x00, 0xa6,
0x55, 0x08, 0x20, 0x11, 0x5e, 0x04, 0x10, 0x69, 0x21, 0x0f, 0xe9, 0x38,
0x80, 0x8e, 0x25, 0xde, 0xf3, 0x40, 0x3a, 0x5e, 0x66, 0x1e, 0x06, 0x20,
0x88, 0xca, 0x7f, 0x05, 0x44, 0xd4, 0xdc, 0x06, 0x2f, 0xde, 0x21, 0x65,
0x36, 0xd3, 0x03, 0xc1, 0xf8, 0xb8, 0x2c, 0x2e, 0x73, 0x8b, 0x23, 0x22,
0xa9, 0x23, 0x24, 0x74, 0xe0, 0x69, 0x6d, 0xac, 0x17, 0x68, 0xf7, 0xe3,
0x20, 0x21, 0x05, 0xd0, 0x00, 0x97, 0xbb, 0xa7, 0xfd, 0x58, 0x91, 0xc0,
0xca, 0x5e, 0xe5, 0x94, 0xb8, 0x3f, 0x37, 0xf3, 0x75, 0x7e, 0x22, 0x2e,
0x2c, 0x77, 0x57, 0x30, 0x07, 0xd4, 0x66, 0xd9, 0x4f, 0xc3, 0xb1, 0x1a,
0xdf, 0x32, 0x82, 0x43, 0xe2, 0x4c, 0x08, 0xa4, 0x2a, 0x2a, 0xec, 0xd8,
0x0b, 0xd9, 0x4d, 0xf2, 0x1f, 0x1f, 0x26, 0x43, 0xef, 0x79, 0x9c, 0xcf,
0x32, 0xf3, 0x70, 0x1d, 0xe1, 0x44, 0x71,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x3d, 0x12, 0x7f, 0xe8, 0x32, 0x68, 0xdf,
0x01, 0x59, 0x43, 0x43, 0x58, 0x66, 0xc6, 0x37, 0x40, 0x4f, 0xf1, 0xd4,
0x98, 0x91, 0xb3, 0xae, 0xab, 0x55, 0xb2, 0xac, 0x13, 0xef, 0xbc, 0x1c,
0xd5, 0x3c, 0xe0, 0xc0, 0xff, 0xe2, 0xed, 0xdb, 0xbd, 0xe1, 0x4b, 0x6c,
0xcb, 0xdd, 0x46, 0x6f, 0x32, 0x4a, 0x76, 0xdf, 0x0d, 0x93, 0xed, 0x3d,
0x73, 0xd8, 0xd5, 0x9a, 0x92, 0x4d, 0x49, 0xe4, 0xa5, 0x67, 0xfa, 0x02,
0xc6, 0x90, 0xeb, 0xaf, 0x37, 0x21, 0xde, 0x68, 0xfe, 0x6b, 0x86, 0xcb,
0x5f, 0x3a, 0x59, 0xd7, 0x31, 0x5c, 0x99, 0xdd, 0xe7, 0xe2, 0x50, 0x6a,
0xed, 0x5b, 0x68, 0xee, 0xff, 0x07, 0xb7, 0x18, 0x41, 0x46, 0x68, 0x12,
0xa4, 0x87, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
0x60, 0x82,
};


I have a header file that contains:
Code: [Select]

static unsigned char *test_png;


Inside of my source file, I'm attempting with:
Code: [Select]

if (img_test.LoadFromMemory(&test_png, sizeof(test_png)))
{
     //oh no!
}


I'm compiling with:
Code: [Select]

g++ -o Testing test.c main.cpp -lsfml-graphics -lsfml-window


However, this message is returned:
Code: [Select]

Failed to load image from memory. Reason : Image not of any known type, or corrupt


I'm essentially trying to package the asset inside of my compiled binary.  I'm sure that I'm most likely using the methods or language conventions improperly.  I assume that the data I'm sending is not in a correct format for the LoadFromMemory method to understand, but I'm not exactly sure what I'm doing wrong.

Thanks for any help!

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
sf::Texture.LoadFromMemory?
« Reply #1 on: October 26, 2011, 09:37:17 am »
You don't need to declare your pixel array as a pointer, just declare it like in your first source snippet, it should work fine just like this:
Code: [Select]
if (img_test.LoadFromMemory(test_png, sizeof(test_png)))
{
     //oh no!
}


Though I'm trying to get it to work as two separate hpp/cpp files, It is proving pretty hard.

Why is the array static?

Edit: Ok I got it worked out, sizeof(test_png) is returning too small of a value. You might need to dereference it or something, I did this:

png_source.hpp
Code: [Select]
#ifndef PNG_SOURCE_HPP
#define PNG_SOURCE_HPP

extern unsigned char *test_png;
extern unsigned int test_png_size;

#endif


png_source.cpp
Quote
#include "png_source.hpp"

unsigned char internal_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20,
0x08, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x18, 0xed, 0xa3, 0x00, 0x00, 0x00,
0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00,
0x0b, 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74,
0x49, 0x4d, 0x45, 0x07, 0xdb, 0x0a, 0x1a, 0x06, 0x2d, 0x30, 0xc3, 0x47,
0xff, 0xe7, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f,
0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
0x81, 0x0e, 0x17, 0x00, 0x00, 0x02, 0x9f, 0x49, 0x44, 0x41, 0x54, 0x48,
0xc7, 0x8d, 0x56, 0x49, 0x72, 0xe4, 0x30, 0x0c, 0x03, 0xaa, 0xf4, 0xff,
0x4f, 0xe4, 0x3e, 0x2f, 0x24, 0x83, 0x39, 0x70, 0x11, 0xe5, 0xa5, 0x3b,
0x3e, 0x74, 0x62, 0x49, 0xe6, 0x02, 0x90, 0xa0, 0xf8, 0xef, 0xe7, 0x47,
0x02, 0x20, 0x90, 0x14, 0x04, 0x11, 0x14, 0x84, 0x78, 0x08, 0x8a, 0xa2,
0x20, 0x02, 0x22, 0x00, 0x42, 0x22, 0x08, 0x42, 0x12, 0x80, 0x5c, 0x17,
0xf2, 0x3b, 0x02, 0x22, 0x51, 0x7b, 0xcb, 0xcc, 0xc2, 0x94, 0x00, 0xa6,
0x55, 0x08, 0x20, 0x11, 0x5e, 0x04, 0x10, 0x69, 0x21, 0x0f, 0xe9, 0x38,
0x80, 0x8e, 0x25, 0xde, 0xf3, 0x40, 0x3a, 0x5e, 0x66, 0x1e, 0x06, 0x20,
0x88, 0xca, 0x7f, 0x05, 0x44, 0xd4, 0xdc, 0x06, 0x2f, 0xde, 0x21, 0x65,
0x36, 0xd3, 0x03, 0xc1, 0xf8, 0xb8, 0x2c, 0x2e, 0x73, 0x8b, 0x23, 0x22,
0xa9, 0x23, 0x24, 0x74, 0xe0, 0x69, 0x6d, 0xac, 0x17, 0x68, 0xf7, 0xe3,
0x20, 0x21, 0x05, 0xd0, 0x00, 0x97, 0xbb, 0xa7, 0xfd, 0x58, 0x91, 0xc0,
0xca, 0x5e, 0xe5, 0x94, 0xb8, 0x3f, 0x37, 0xf3, 0x75, 0x7e, 0x22, 0x2e,
0x2c, 0x77, 0x57, 0x30, 0x07, 0xd4, 0x66, 0xd9, 0x4f, 0xc3, 0xb1, 0x1a,
0xdf, 0x32, 0x82, 0x43, 0xe2, 0x4c, 0x08, 0xa4, 0x2a, 0x2a, 0xec, 0xd8,
0x0b, 0xd9, 0x4d, 0xf2, 0x1f, 0x1f, 0x26, 0x43, 0xef, 0x79, 0x9c, 0xcf,
0x32, 0xf3, 0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 0x5f, 0x29, 0x4e, 0x4d, 0x3d, 0x12, 0x7f, 0xe8, 0x32, 0x68, 0xdf,
0x01, 0x59, 0x43, 0x43, 0x58, 0x66, 0xc6, 0x37, 0x40, 0x4f, 0xf1, 0xd4,
0x98, 0x91, 0xb3, 0xae, 0xab, 0x55, 0xb2, 0xac, 0x13, 0xef, 0xbc, 0x1c,
0xd5, 0x3c, 0xe0, 0xc0, 0xff, 0xe2, 0xed, 0xdb, 0xbd, 0xe1, 0x4b, 0x6c,
0xcb, 0xdd, 0x46, 0x6f, 0x32, 0x4a, 0x76, 0xdf, 0x0d, 0x93, 0xed, 0x3d,
0x73, 0xd8, 0xd5, 0x9a, 0x92, 0x4d, 0x49, 0xe4, 0xa5, 0x67, 0xfa, 0x02,
0xc6, 0x90, 0xeb, 0xaf, 0x37, 0x21, 0xde, 0x68, 0xfe, 0x6b, 0x86, 0xcb,
0x5f, 0x3a, 0x59, 0xd7, 0x31, 0x5c, 0x99, 0xdd, 0xe7, 0xe2, 0x50, 0x6a,
0xed, 0x5b, 0x68, 0xee, 0xff, 0x07, 0xb7, 0x18, 0x41, 0x46, 0x68, 0x12,
0xa4, 0x87, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
0x60, 0x82,
};

unsigned char *test_png = internal_png;

unsigned int test_png_size = sizeof(internal_png);


main.cpp
Code: [Select]
if (img_test.LoadFromMemory(test_png, test_png_size))
{
     //Now we're cooking with pixels :D
}


Their may be a cleaner solution(is their a way to make sizeof play nice with pointers to arrays?), but this works, and keeps them seperated into hpp/cpp files.

EDIT2: Or what Laurent said :P

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Texture.LoadFromMemory?
« Reply #2 on: October 26, 2011, 09:50:25 am »
The "static" keyword in your cpp file is what makes it fail. It means that the array is local to the file and cannot be referenced outside, therefore the pointer that you declare (by the way, using "static" in a header outside a class doesn't make sense) doesn't point to the array in your cpp file -- it is an uninitialized pointer.

If the data really needs to be "public" then you need to do like this:
Code: [Select]
// header
extern const unsigned char test_png[];

Code: [Select]
// source file
const unsigned char test_png[] = {
...
}
Laurent Gomila - SFML developer

fatum

  • Newbie
  • *
  • Posts: 47
    • MSN Messenger - bowsers7@hotmail.com
    • AOL Instant Messenger - therealblah569
    • View Profile
    • http://boards.psynetfm.com
sf::Texture.LoadFromMemory?
« Reply #3 on: October 26, 2011, 10:40:36 am »
@thePyro_13
@Laurent

Thanks for pointing me in the right direction!  If anyone is interested in a solution, here's what I've setup:

test.hpp
Code: [Select]

#ifndef TEST_HPP
#define TEST_HPP
char test_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20,
0x08, 0x02, 0x00, 0x00, 0x00, 0xfc, 0x18, 0xed, 0xa3, 0x00, 0x00, 0x00,
0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00,
0x0b, 0x13, 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74,
0x49, 0x4d, 0x45, 0x07, 0xdb, 0x0a, 0x1a, 0x06, 0x2d, 0x30, 0xc3, 0x47,
0xff, 0xe7, 0x00, 0x00, 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f,
0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65,
0x64, 0x20, 0x77, 0x69, 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
0x81, 0x0e, 0x17, 0x00, 0x00, 0x02, 0x9f, 0x49, 0x44, 0x41, 0x54, 0x48,
0xc7, 0x8d, 0x56, 0x49, 0x72, 0xe4, 0x30, 0x0c, 0x03, 0xaa, 0xf4, 0xff,
0x4f, 0xe4, 0x3e, 0x2f, 0x24, 0x83, 0x39, 0x70, 0x11, 0xe5, 0xa5, 0x3b,
0x3e, 0x74, 0x62, 0x49, 0xe6, 0x02, 0x90, 0xa0, 0xf8, 0xef, 0xe7, 0x47,
0x02, 0x20, 0x90, 0x14, 0x04, 0x11, 0x14, 0x84, 0x78, 0x08, 0x8a, 0xa2,
0x20, 0x02, 0x22, 0x00, 0x42, 0x22, 0x08, 0x42, 0x12, 0x80, 0x5c, 0x17,
0xf2, 0x3b, 0x02, 0x22, 0x51, 0x7b, 0xcb, 0xcc, 0xc2, 0x94, 0x00, 0xa6,
0x55, 0x08, 0x20, 0x11, 0x5e, 0x04, 0x10, 0x69, 0x21, 0x0f, 0xe9, 0x38,
0x80, 0x8e, 0x25, 0xde, 0xf3, 0x40, 0x3a, 0x5e, 0x66, 0x1e, 0x06, 0x20,
0x88, 0xca, 0x7f, 0x05, 0x44, 0xd4, 0xdc, 0x06, 0x2f, 0xde, 0x21, 0x65,
0x36, 0xd3, 0x03, 0xc1, 0xf8, 0xb8, 0x2c, 0x2e, 0x73, 0x8b, 0x23, 0x22,
0xa9, 0x23, 0x24, 0x74, 0xe0, 0x69, 0x6d, 0xac, 0x17, 0x68, 0xf7, 0xe3,
0x20, 0x21, 0x05, 0xd0, 0x00, 0x97, 0xbb, 0xa7, 0xfd, 0x58, 0x91, 0xc0,
0xca, 0x5e, 0xe5, 0x94, 0xb8, 0x3f, 0x37, 0xf3, 0x75, 0x7e, 0x22, 0x2e,
0x2c, 0x77, 0x57, 0x30, 0x07, 0xd4, 0x66, 0xd9, 0x4f, 0xc3, 0xb1, 0x1a,
0xdf, 0x32, 0x82, 0x43, 0xe2, 0x4c, 0x08, 0xa4, 0x2a, 0x2a, 0xec, 0xd8,
0x0b, 0xd9, 0x4d, 0xf2, 0x1f, 0x1f, 0x26, 0x43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x5c, 0x59, 0x16, 0x03, 0x67, 0xf4,
0x4b, 0x5f, 0x29, 0x4e, 0x4d, 0x3d, 0x12, 0x7f, 0xe8, 0x32, 0x68, 0xdf,
0x01, 0x59, 0x43, 0x43, 0x58, 0x66, 0xc6, 0x37, 0x40, 0x4f, 0xf1, 0xd4,
0x98, 0x91, 0xb3, 0xae, 0xab, 0x55, 0xb2, 0xac, 0x13, 0xef, 0xbc, 0x1c,
0xd5, 0x3c, 0xe0, 0xc0, 0xff, 0xe2, 0xed, 0xdb, 0xbd, 0xe1, 0x4b, 0x6c,
0xcb, 0xdd, 0x46, 0x6f, 0x32, 0x4a, 0x76, 0xdf, 0x0d, 0x93, 0xed, 0x3d,
0x73, 0xd8, 0xd5, 0x9a, 0x92, 0x4d, 0x49, 0xe4, 0xa5, 0x67, 0xfa, 0x02,
0xc6, 0x90, 0xeb, 0xaf, 0x37, 0x21, 0xde, 0x68, 0xfe, 0x6b, 0x86, 0xcb,
0x5f, 0x3a, 0x59, 0xd7, 0x31, 0x5c, 0x99, 0xdd, 0xe7, 0xe2, 0x50, 0x6a,
0xed, 0x5b, 0x68, 0xee, 0xff, 0x07, 0xb7, 0x18, 0x41, 0x46, 0x68, 0x12,
0xa4, 0x87, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42,
0x60, 0x82,
};
#endif


Inside of my source file:
Code: [Select]

#include "test.hpp"
...
sf::Texture img_test;
sf::Sprite spr_test;
...

if (!img_test.LoadFromMemory(&test_png, sizeof(test_png)))
{
      //oh no!
}

spr_test.SetTexture(img_test);
spr_test.SetPosition(30, 30);

... Main Loop ...
App.Draw(spr_test);


And it's working out as expected!  Would this be an acceptable method for handling the assets that need to be included with the binary file?  Could there be any significant problems using this method?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Texture.LoadFromMemory?
« Reply #4 on: October 26, 2011, 10:55:29 am »
Quote
Would this be an acceptable method for handling the assets that need to be included with the binary file?

Yes of course. The only problem is that it's more work for you when assets are updated ;)
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
sf::Texture.LoadFromMemory?
« Reply #5 on: October 26, 2011, 11:33:40 am »
By the way, I don't think that
Code: [Select]
img_test.LoadFromMemory(&test_png, sizeof(test_png))is valid C++. Since test_png is an array, you get a pointer to the first element by writing one of the following:
Code: [Select]
test_png
&test_png[0]

And I would declare the array elements const so that they cannot be changed by accident.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Texture.LoadFromMemory?
« Reply #6 on: October 26, 2011, 12:29:29 pm »
Taking the address of an array is valid C++, but as far as I remember (couldn't find it in the standard :P) a "T (*)[N]" shouldn't be implicitely converted to a "T*".
Laurent Gomila - SFML developer