SDL Load from Memory Problem

Started by
6 comments, last by tool_sober 19 years, 11 months ago
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"); }
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]
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++;
}
Try this:

//open file and get it's size in bytesFILE *g_pFile = fopen("ImageFile.ext", "rb");unsigned long g_BuffSize = ftell(g_pFile);rewind(g_pFile);//allocate a buffer for the read in datavoid *g_pImageBuff = malloc(g_pBuffSize);if(!g_pImageBuff)  //some sort of error message//load the imageSDL_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]
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.
enum Bool { True, False, FileNotFound };
Thank you all. The code looks now cleaner.
I love your screen name (tool_sober). Tool is my fav band besides led zeppelin.



-----------------------------------------------
Here comes the Thnikkaman!
------------------------------------------------------------"Many combilations elizagerth. I hope you see my particles." - Senor Cardgage
: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]

This topic is closed to new replies.

Advertisement