If SDL_image did what you want internally, it would be doing the following:
-
Load the image in whatever format the image is in. This creates a
SDL_Surface.
-
Convert the image to the format you want. To do this, it would need
to create a second SDL_Surface.
-
Free the original SDL_Surface.
-
Return the pointer to the SDL_Surface in the desired format.
You can easily write a routine in a few lines of code that does this for you
and call it in place of IMG_Load().
Doing it that way isn?t using any more allocations than if YOU create a
surface of the right size and bit depth and then could convert the image
into that format. Your suggested way would be:
-
Create a surface with the desired size and depth. This creates a
SDL_Surface.
-
Load the image in whatever format the image is in. This creates a
SDL_Surface.
-
Convert the image to the format you want with the SDL_Surface from
step 1.
-
Free the SDL_Surface from step 2.
Both ways required 2 surfaces to be created and one to be freed. Neither
way is more efficient than the other, although the 1st way of doing it has
the advantage that you don?t need to know the image width, and height before
you create your surface; doing it the second way would either mean a table
with that info, or opening the file to get that info.
Typically your desired image format is the display format, so you can make
your Helper routine very small. Off the top of my head:
// Load an image and convert it to the currently set video mode.
// This works properly under SDL 1.2.x as long as it is called
// after the call to SDL_SetVideoMode().
// NOTE: It will work properly under SDL 1.3.x, but only when
// using the legacy API. If you use the new 1.3 ?native? API
// then the SDL_DisplayFormat() will return NULL.
SDL_Surface *LoadImageInDisplayFormat( const char *pFile )
{
SDL_Surface *pSurface = NULL;
if ( NULL != pFile )
{
SDL_Surface *pDisplaySurface;
pSurface = IMG_Load( pFile );
if ( NULL != pSurface )
{
pDisplaySurface = SDL_DisplayFormat( pSuface );
if ( NULL != pDisplaySurface )
{
// Free the original surface
SDL_FreeSurface( pSurface );
// Use the converted surface
pSurface = pDisplaySurface;
}
}
}
return pSurface;
}
Behalf Of Vittorio G.From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Sent: Tuesday, December 29, 2009 4:51 PM
To: SDL Development List
Subject: Re: [SDL] sdl_image feature request: loading into an existing
surface?
wouldn’t precaching cause an increased memory impact of the application?
On Tue, Dec 29, 2009 at 9:57 PM, Marcel Wysocki wrote:
On 12/26/2009 01:13 PM, Ren? Dudfield wrote:
Hi,
to reduce the amount of allocations, and therefore speed things up, I
would like to be able to load images into existing surfaces.
So if the supplied surface has the correct attributes for the loaded
image, it should use that surface, rather than load its own.
where is the difference between having img_load creating the surface
or you doing it and loading the image to it?
overall it needs the same amount of computing power
since in both cases you need to load the image onto a surface
which has the image’s properties and which has to be created
either by you or by img_load.
if you really want to speed up things why dont you just
pre-cache all the images during initialization of your
program?
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
–
Jonathan Swift
http://www.brainyquote.com/quotes/authors/j/jonathan_swift.html - “May
you live every day of your life.”