1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-12 14:09:43 +01:00

De-duplicate surface recreation in GraphicsUtil

Here I'm using "surface recreation" to mean allocating a new surface
with almost the exact same properties as a given previous. As you can
see, GraphicsUtil likes to recreate surfaces all the time - copying the
masks and flags (unused lol) of an existing surface - and only varies it
by the dimensions of the new surface.

As you can see, this is a lot less wordy and a lot less repetitive than
copy-pasting it a bunch.
This commit is contained in:
Misa 2021-03-05 20:12:01 -08:00
parent 7ce87d7b13
commit aca33e5587

View file

@ -11,6 +11,51 @@ void setRect( SDL_Rect& _r, int x, int y, int w, int h )
_r.h = h;
}
static SDL_Surface* RecreateSurfaceWithDimensions(
SDL_Surface* surface,
const int width,
const int height
) {
SDL_Surface* retval;
if (surface == NULL)
{
return NULL;
}
retval = SDL_CreateRGBSurface(
surface->flags,
width,
height,
surface->format->BitsPerPixel,
surface->format->Rmask,
surface->format->Gmask,
surface->format->Bmask,
surface->format->Amask
);
if (retval == NULL)
{
return NULL;
}
return retval;
}
static SDL_Surface* RecreateSurface(SDL_Surface* surface)
{
if (surface == NULL)
{
return NULL;
}
return RecreateSurfaceWithDimensions(
surface,
surface->w,
surface->h
);
}
SDL_Surface* GetSubSurface( SDL_Surface* metaSurface, int x, int y, int width, int height )
{
// Create an SDL_Rect with the area of the _surface
@ -21,15 +66,10 @@ SDL_Surface* GetSubSurface( SDL_Surface* metaSurface, int x, int y, int width, i
area.h = height;
//Convert to the correct display format after nabbing the new _surface or we will slow things down.
SDL_Surface* preSurface = SDL_CreateRGBSurface(
SDL_SWSURFACE,
SDL_Surface* preSurface = RecreateSurfaceWithDimensions(
metaSurface,
width,
height,
metaSurface->format->BitsPerPixel,
metaSurface->format->Rmask,
metaSurface->format->Gmask,
metaSurface->format->Bmask,
metaSurface->format->Amask
height
);
// Lastly, apply the area from the meta _surface onto the whole of the sub _surface.
@ -103,8 +143,7 @@ SDL_Surface * ScaleSurface( SDL_Surface *_surface, int Width, int Height, SDL_Su
SDL_Surface *_ret;
if(Dest == NULL)
{
_ret = SDL_CreateRGBSurface(_surface->flags, Width, Height, _surface->format->BitsPerPixel,
_surface->format->Rmask, _surface->format->Gmask, _surface->format->Bmask, _surface->format->Amask);
_ret = RecreateSurfaceWithDimensions(_surface, Width, Height);
if(_ret == NULL)
{
return NULL;
@ -132,8 +171,7 @@ SDL_Surface * ScaleSurface( SDL_Surface *_surface, int Width, int Height, SDL_Su
SDL_Surface * FlipSurfaceVerticle(SDL_Surface* _src)
{
SDL_Surface * ret = SDL_CreateRGBSurface(_src->flags, _src->w, _src->h, _src->format->BitsPerPixel,
_src->format->Rmask, _src->format->Gmask, _src->format->Bmask, _src->format->Amask);
SDL_Surface * ret = RecreateSurface(_src);
if(ret == NULL)
{
return NULL;
@ -168,16 +206,7 @@ void BlitSurfaceColoured(
const SDL_PixelFormat& fmt = *(_src->format);
SDL_Surface* tempsurface = SDL_CreateRGBSurface(
SDL_SWSURFACE,
_src->w,
_src->h,
fmt.BitsPerPixel,
fmt.Rmask,
fmt.Gmask,
fmt.Bmask,
fmt.Amask
);
SDL_Surface* tempsurface = RecreateSurface(_src);
for(int x = 0; x < tempsurface->w; x++)
{
@ -209,16 +238,7 @@ void BlitSurfaceTinted(
const SDL_PixelFormat& fmt = *(_src->format);
SDL_Surface* tempsurface = SDL_CreateRGBSurface(
SDL_SWSURFACE,
_src->w,
_src->h,
fmt.BitsPerPixel,
fmt.Rmask,
fmt.Gmask,
fmt.Bmask,
fmt.Amask
);
SDL_Surface* tempsurface = RecreateSurface(_src);
for (int x = 0; x < tempsurface->w; x++) {
for (int y = 0; y < tempsurface->h; y++) {
@ -295,8 +315,7 @@ void UpdateFilter(void)
SDL_Surface* ApplyFilter( SDL_Surface* _src )
{
SDL_Surface* _ret = SDL_CreateRGBSurface(_src->flags, _src->w, _src->h, _src->format->BitsPerPixel,
_src->format->Rmask, _src->format->Gmask, _src->format->Bmask, _src->format->Amask);
SDL_Surface* _ret = RecreateSurface(_src);
int redOffset = rand() % 4;