CreateRGBSurface and DispalyFormat question

first I create surface using CreateRGB

but parameters ( such as pixel format, alpha, etc ) are taken using GetVideoSurface() function###############################################################################

/* obtain the same format as display surface */
SDL_PixelFormat *PixelFormat = SDL_GetVideoSurface()->format;

/* create empty surface */
TempS = SDL_CreateRGBSurface( SDL_SWSURFACE, w, h, PixelFormat->BitsPerPixel,
PixelFormat->Rmask, PixelFormat->Gmask,
PixelFormat->Bmask, PixelFormat->Amask
);

###############################################################################

so is it nessesary to apply DisplayFormat() to such created surface?

PAMASH

@pamashoid_at_poczta

Hi,

when I want an answer to such questions, I look to the SDL source code for a
definitive answer.

A quick look at SDL_DisplayFormat() reveals that if the screen is using
hardware surfaces then SDL_HWSURFACE is used instead of SDL_SWSURFACE.

Inside SDL_ConvertSurface() (called by SDL_DisplayFormat()) which calls
SDL_CreateRGBSurface(), we find that, yes, the pixel-format information of
the screen surface is used to create the compatible surface.

We also find that if the screen surface is 8-bit, palettes are created for the
new surface. There is also some jiggery-pokery with colorkey and
rle-accelerated surfaces but I think that is mainly to transfer these
attributes from the original surface to the new one.

What you are doing is not strictly equivalent to using SDL_DisplayFormat() to
produce a compatible surface, but if you know that you will not be using an
8-bit and/or hardware video surface then you will not need to use
SDL_DisplayFormat().

Or you could just add a few lines of code to set the SW/HW flag based on the
screen format, create a suitable palette if required and turn the whole thing
into a function e.g.

SDL_Surface *CreateCompatibleSurface( int width, int height )
{
SDL_Surface *screen = SDL_GetVideoSurface();
SDL_PixelFormat *PixelFormat = screen->format;
int flags = (screen->flags & SDL_HWSURFACE);
SDL_Surface *surf;

surf = SDL_CreateRGBSurface( flags, width, height,
PixelFormat->BitsPerPixel,
PixelFormat->Rmask, PixelFormat->Gmask,
PixelFormat->Bmask, PixelFormat->Amask
);

if ( !surf ) return NULL;

if ( PixelFormat->palette && surf->format->palette )
{
memcpy( surf->format->palette->colors,
PixelFormat->palette->colors,
PixelFormat->palette->ncolors*sizeof(SDL_Color)
);
surf->format->palette->ncolors = PixelFormat->palette->ncolors;
}
return surf;
}
[above not tested]

Hope that helps,

cheers,
John.On Friday 27 June 2003 SDL_CreateRGBSurface2:50 pm, pamashoid at poczta.onet.pl wrote:

first I create surface using CreateRGB

but parameters ( such as pixel format, alpha, etc ) are taken using
GetVideoSurface() function

###########################################################################

/* obtain the same format as display surface */
SDL_PixelFormat *PixelFormat = SDL_GetVideoSurface()->format;

/* create empty surface */
TempS = SDL_CreateRGBSurface( SDL_SWSURFACE, w, h,
PixelFormat->BitsPerPixel, PixelFormat->Rmask, PixelFormat->Gmask,
PixelFormat->Bmask, PixelFormat->Amask );

###########################################################################

so is it nessesary to apply DisplayFormat() to such created surface?

PAMASH

pamashoid at poczta.onet.pl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

did I correctly inderstood you John

for SW surfaces I don’t need to use display format

what about HW surface if I change the flag from SW to HW

does it the same ?

PAMASH
@pamashoid_at_poczta

Yes. That is how SDL_DisplayFormat() does it.

But, don’t take my word for it - have a look at the SDL source code. It is of
good quality and educational to browse through (I learn this way).

The relevant files are:

src/video/SDL_surface.c
src/video/SDL_video.c

cheers,
John.On Friday 27 June 2003 5:46 pm, pamashoid at poczta.onet.pl wrote:

did I correctly inderstood you John

for SW surfaces I don’t need to use display format

what about HW surface if I change the flag from SW to HW

does it the same ?

PAMASH
pamashoid at poczta.onet.pl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Thanks for your help!

I’t worked for me.> ----- Original Message -----

From: john@johnnypops.demon.co.uk (John Popplewell)
To:
Sent: Friday, June 27, 2003 12:20 PM
Subject: Re: [SDL] CreateRGBSurface and DispalyFormat question

Hi,

when I want an answer to such questions, I look to the SDL source code for
a
definitive answer.

A quick look at SDL_DisplayFormat() reveals that if the screen is using
hardware surfaces then SDL_HWSURFACE is used instead of SDL_SWSURFACE.

Inside SDL_ConvertSurface() (called by SDL_DisplayFormat()) which calls
SDL_CreateRGBSurface(), we find that, yes, the pixel-format information of
the screen surface is used to create the compatible surface.

We also find that if the screen surface is 8-bit, palettes are created for
the
new surface. There is also some jiggery-pokery with colorkey and
rle-accelerated surfaces but I think that is mainly to transfer these
attributes from the original surface to the new one.

What you are doing is not strictly equivalent to using SDL_DisplayFormat()
to
produce a compatible surface, but if you know that you will not be using
an
8-bit and/or hardware video surface then you will not need to use
SDL_DisplayFormat().

Or you could just add a few lines of code to set the SW/HW flag based on
the
screen format, create a suitable palette if required and turn the whole
thing
into a function e.g.

SDL_Surface *CreateCompatibleSurface( int width, int height )
{
SDL_Surface *screen = SDL_GetVideoSurface();
SDL_PixelFormat *PixelFormat = screen->format;
int flags = (screen->flags & SDL_HWSURFACE);
SDL_Surface *surf;

surf = SDL_CreateRGBSurface( flags, width, height,
PixelFormat->BitsPerPixel,
PixelFormat->Rmask, PixelFormat->Gmask,
PixelFormat->Bmask, PixelFormat->Amask
);

if ( !surf ) return NULL;

if ( PixelFormat->palette && surf->format->palette )
{
memcpy( surf->format->palette->colors,
PixelFormat->palette->colors,
PixelFormat->palette->ncolors*sizeof(SDL_Color)
);
surf->format->palette->ncolors = PixelFormat->palette->ncolors;
}
return surf;
}
[above not tested]

Hope that helps,

cheers,
John.

On Friday 27 June 2003 SDL_CreateRGBSurface2:50 pm, pamashoid at poczta.onet.pl wrote:

first I create surface using CreateRGB

but parameters ( such as pixel format, alpha, etc ) are taken using
GetVideoSurface() function

###########################################################################

/* obtain the same format as display surface */
SDL_PixelFormat *PixelFormat = SDL_GetVideoSurface()->format;

/* create empty surface */
TempS = SDL_CreateRGBSurface( SDL_SWSURFACE, w, h,
PixelFormat->BitsPerPixel, PixelFormat->Rmask, PixelFormat->Gmask,
PixelFormat->Bmask, PixelFormat->Amask );

###########################################################################

so is it nessesary to apply DisplayFormat() to such created surface?

PAMASH

pamashoid at poczta.onet.pl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl