Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tool_sober

SDL Load from Memory Problem

This topic is 5126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem loading a image from Memory. I always get a SDL_Image error: "Unsupported image format". can somebody help? Here is the code: SDL_RWops *rw; file = fopen("pic.png","rb"); if(!file) { printf("Error open file !\n"); exit(0); } else { printf("File open!\n"); } static char buffer[237]; int check = setvbuf(file, buffer, _IOLBF, BUFSIZ); if(check!=0) { printf("Error loading to memory\n"); exit(1); } else { printf("Image in memory loaded %d \n", check); } rw = SDL_RWFromMem(buffer, 237); SDL_Surface *image = IMG_Load_RW(rw, 1); if(!image) { printf("No Image! %s\n", IMG_GetError()); exit(2); } else { printf("Image loaded! \n"); }

Share this post


Link to post
Share on other sites
Advertisement
The problem is that you never actually load the data from the file. I think this is because you''re misunderstanding what setvbuf does. That function allows you to customise file buffering, which is not how you load in the data. It''s also worrying that you use BUFSIZ which isn''t defined below, but the number 237 elsewhere.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
YEAH! Thank you for the tip. It works:

static char buffer[237];
//int check = setvbuf(file, buffer, _IOLBF, 237);
int i = 0;
int c;
while((c=fgetc(file))!=EOF) {
buffer = c;
i++;
}

Share this post


Link to post
Share on other sites
Try this:


//open file and get it's size in bytes

FILE *g_pFile = fopen("ImageFile.ext", "rb");
unsigned long g_BuffSize = ftell(g_pFile);
rewind(g_pFile);

//allocate a buffer for the read in data

void *g_pImageBuff = malloc(g_pBuffSize);

if(!g_pImageBuff)
//some sort of error message


//load the image

SDL_RWops *g_pRWop = SDL_RWFromMem(g_pImageBuff, g_BuffSize);
SDL_Surface *g_pImage = IMG_Load_RW(g_pRWop);


That should work.

EDIT: changes code tags to source

[edited by - Spudder on May 9, 2004 6:37:02 PM]

Share this post


Link to post
Share on other sites
Heard of "fread()" ? It''ll save you the loop.

To read an entire file into memory:


FILE * f = fopen( "filename", "rb" );
if( !f ) failure( "file not found" );
fseek( f, 0, 2 ); // or SEEK_END instead of "2"
fpos_t fsize = ftell( f );
if( fsize > 0x1000000 ) failure( "corrupt or too big file" );
fseek( f, 0, 0 );
void * data = malloc( (size_t)fsize );
if( fsize != fread( data, 1, fsize, f ) ) failure( "I/O error" );
fclose( f );

// now, "data" contains the file data, and "fsize" contains the size


You probably want to stick this in a utility function somewhere, so you can call it for each file you want to read into memory.

Share this post


Link to post
Share on other sites
I love your screen name (tool_sober). Tool is my fav band besides led zeppelin.



-----------------------------------------------
Here comes the Thnikkaman!

Share this post


Link to post
Share on other sites
:o) I love you to *kiss* Tool and Led Zep, one of the best Bands ever i think. Keep it rocking!

[edited by - tool_sober on May 10, 2004 3:51:18 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!