Why IMG_Load_RW returns 0 on an iOS? How to fix it?

Does anyone know why IMG_Load_RW return 0 when the following code running on an iphone simulator? On MacOSX 10.6 it works well.

Code:
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1); // on iphone simulator, image == 0

SDL_Error() returns “Invalid window”

Full code:

Code:
int main(int argc, char **argv) {
if(SDL_Init(SDL_INIT_VIDEO) != 0)
error(“Unable to initialize SDL”);
atexit(SDL_Quit);

SDL_Surface *screen = SDL_SetVideoMode(Width, Height, 16, 0 /* SDL_FULLSCREEN */);
if(!screen)
    error("Unable to set video mode");

/*
SDL_Surface *image = IMG_Load(“bg.png”); //// works well anywhere
if(!image)
error(“Unable to load image”);
*/

FILE *file = fopen("bg.png", "r");
if(!file) 
    error("Unable to read file");
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1);        //// on iphone simulator, image == 0
if(!image)
    error("Unable to load image");

SDL_Rect src, dest;
dest.x = dest.y = src.x = src.y = 0;
dest.w = src.w = image->w;
dest.h = src.h = image->h;
SDL_BlitSurface(image, &src, screen, &dest);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Delay(3000);
SDL_FreeSurface(image);

return 0;

}

Are you using the ImageIO or UIImage backend? (Sam just grabbed my
ImageIO changes the other day. ImageIO is the recommended path moving
forward.)

UIImage has real technical limitations w.r.t. RWops. But since the
ImageIO backend is new to iOS, you may have discovered a new issue for
us.

-EricOn 3/30/11, manifest wrote:

Does anyone know why IMG_Load_RW return 0 when the following code running on
an iphone simulator? On MacOSX 10.6 it works well.

Code:
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1); // on iphone simulator, image == 0

SDL_Error() returns “Invalid window”

Full code:

Code:
int main(int argc, char **argv) {
if(SDL_Init(SDL_INIT_VIDEO) != 0)
error(“Unable to initialize SDL”);
atexit(SDL_Quit);

SDL_Surface *screen = SDL_SetVideoMode(Width, Height, 16, 0 /*

SDL_FULLSCREEN */);
if(!screen)
error(“Unable to set video mode”);

/*
SDL_Surface *image = IMG_Load(“bg.png”); //// works well anywhere
if(!image)
error(“Unable to load image”);
*/

FILE *file = fopen("bg.png", "r");
if(!file)
    error("Unable to read file");
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1);        //// on iphone

simulator, image == 0
if(!image)
error(“Unable to load image”);

SDL_Rect src, dest;
dest.x = dest.y = src.x = src.y = 0;
dest.w = src.w = image->w;
dest.h = src.h = image->h;
SDL_BlitSurface(image, &src, screen, &dest);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Delay(3000);
SDL_FreeSurface(image);

return 0;

}


Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

Hi Eric,
do you think you could briefly describe what difference will it make
to use ImageIO instead of UIImage?
Last time i messed with sdl_image imageio was available only on the
simulator while uiimage was available only on device; has the
situation changed?
thanks
VittorioOn Wed, Mar 30, 2011 at 6:48 PM, Eric Wing wrote:

Are you using the ImageIO or UIImage backend? (Sam just grabbed my
ImageIO changes the other day. ImageIO is the recommended path moving
forward.)

UIImage has real technical limitations w.r.t. RWops. But since the
ImageIO backend is new to iOS, you may have discovered a new issue for
us.

-Eric

On 3/30/11, manifest wrote:

Does anyone know why IMG_Load_RW return 0 when the following code running on
an iphone simulator? On MacOSX 10.6 it works well.

Code:
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1); // on iphone simulator, image == 0

SDL_Error() returns “Invalid window”

Full code:

Code:
int main(int argc, char **argv) {
? ? if(SDL_Init(SDL_INIT_VIDEO) != 0)
? ? ? ? error(“Unable to initialize SDL”);
? ? atexit(SDL_Quit);

? ? SDL_Surface screen = SDL_SetVideoMode(Width, Height, 16, 0 /
SDL_FULLSCREEN */);
? ? if(!screen)
? ? ? ? error(“Unable to set video mode”);

/*
? ? ? SDL_Surface *image = IMG_Load(“bg.png”); ? ? ? ?//// works well anywhere
? ? ? if(!image)
? ? ? ? ? ? ? error(“Unable to load image”);
*/

? ? FILE *file = fopen(“bg.png”, “r”);
? ? if(!file)
? ? ? ? error(“Unable to read file”);
? ? SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
? ? SDL_Surface *image = IMG_Load_RW(rw, 1); ? ? ? ?//// on iphone
simulator, image == 0
? ? if(!image)
? ? ? ? error(“Unable to load image”);

? ? SDL_Rect src, dest;
? ? dest.x = dest.y = src.x = src.y = 0;
? ? dest.w = src.w = image->w;
? ? dest.h = src.h = image->h;
? ? SDL_BlitSurface(image, &src, screen, &dest);
? ? SDL_UpdateRect(screen, 0, 0, 0, 0);
? ? SDL_Delay(3000);
? ? SDL_FreeSurface(image);

? ? return 0;
}


Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thank you for your fast answer, Eric. I used an UIImage backend. I’ve built latest version of the SDL_image from repository and my problem was solved :slight_smile:

Eric Wing wrote:> Are you using the ImageIO or UIImage backend? (Sam just grabbed my

ImageIO changes the other day. ImageIO is the recommended path moving
forward.)

UIImage has real technical limitations w.r.t. RWops. But since the
ImageIO backend is new to iOS, you may have discovered a new issue for
us.

-Eric

On 3/30/11, manifest <@manifest> wrote:

Does anyone know why IMG_Load_RW return 0 when the following code running on
an iphone simulator? On MacOSX 10.6 it works well.

Code:
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1); // on iphone simulator, image == 0

SDL_Error() returns “Invalid window”

Full code:

Code:
int main(int argc, char **argv) {
if(SDL_Init(SDL_INIT_VIDEO) != 0)
error(“Unable to initialize SDL”);
atexit(SDL_Quit);

SDL_Surface screen = SDL_SetVideoMode(Width, Height, 16, 0 /
SDL_FULLSCREEN */);
if(!screen)
error(“Unable to set video mode”);

/*
SDL_Surface *image = IMG_Load(“bg.png”); //// works well anywhere
if(!image)
error(“Unable to load image”);
*/

FILE *file = fopen(“bg.png”, “r”);
if(!file)
error(“Unable to read file”);
SDL_RWops *rw = SDL_RWFromFP(file, SDL_TRUE);
SDL_Surface *image = IMG_Load_RW(rw, 1); //// on iphone
simulator, image == 0
if(!image)
error(“Unable to load image”);

SDL_Rect src, dest;
dest.x = dest.y = src.x = src.y = 0;
dest.w = src.w = image->w;
dest.h = src.h = image->h;
SDL_BlitSurface(image, &src, screen, &dest);
SDL_UpdateRect(screen, 0, 0, 0, 0);
SDL_Delay(3000);
SDL_FreeSurface(image);

return 0;
}


Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

That wasn’t supposed to happen. The reason I wrote the UIImage backend
was that iOS originally didn’t have ImageIO so we couldn’t use it at
all. In iOS 4.0, ImageIO became available. According the the
statistics I’ve seen, around 90% of iOS users have upgraded to at
least 4.0 (current is 4.3 as of this writing) so I think it is safe to
make the default path now. It also solves nasty UIImage limitations
with RWops.

-EricOn 3/30/11, Vittorio G. <vitto.giova at yahoo.it> wrote:

Hi Eric,
do you think you could briefly describe what difference will it make
to use ImageIO instead of UIImage?
Last time i messed with sdl_image imageio was available only on the
simulator while uiimage was available only on device; has the
situation changed?


Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

Thanks, I’m glad to hear ImageIO is working fine.

-EricOn 3/30/11, manifest wrote:

Thank you for your fast answer, Eric. I used an UIImage backend. I’ve built
latest version of the SDL_image from repository and my problem was solved :slight_smile:


Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/