Fix analogue filter allocating/freeing surfaces every frame

This removes memory churn caused by using analogue mode.

The surfaces are only allocated if analogue mode is turned on, and kept
after they are initialized. Otherwise, if analogue mode is never turned
on (which will be the case for the vast majority of the time the game is
played), then no extra memory is used.
This commit is contained in:
Misa
2023-03-04 14:02:47 -08:00
parent 5eecc2a21d
commit 58d21e956b
3 changed files with 17 additions and 23 deletions

View File

@@ -144,11 +144,17 @@ void UpdateFilter(void)
}
}
void ApplyFilter(void)
void ApplyFilter(SDL_Surface* src, SDL_Surface* dest)
{
// Copy the screen to a temporary surface
SDL_Surface* src = SDL_CreateRGBSurface(0, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS, 32, 0, 0, 0, 0);
if (src == NULL)
{
src = SDL_CreateRGBSurface(0, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS, 32, 0, 0, 0, 0);
}
if (dest == NULL)
{
dest = SDL_CreateRGBSurface(0, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS, 32, 0, 0, 0, 0);
}
if (src == NULL || dest == NULL)
{
return;
}
@@ -161,18 +167,6 @@ void ApplyFilter(void)
return;
}
Uint32 rawFormat;
if (graphics.query_texture(graphics.gameTexture, &rawFormat, NULL, NULL, NULL) != 0)
{
return;
}
SDL_PixelFormat* format = SDL_AllocFormat(rawFormat);
// Have a second surface to do work on
SDL_Surface* dest = SDL_CreateRGBSurface(0, SCREEN_WIDTH_PIXELS, SCREEN_HEIGHT_PIXELS, 32, 0, 0, 0, 0);
const int red_offset = rand() % 4;
for (int x = 0; x < src->w; x++)
@@ -226,10 +220,5 @@ void ApplyFilter(void)
}
}
SDL_FreeFormat(format);
SDL_UpdateTexture(graphics.gameTexture, NULL, dest->pixels, dest->pitch);
SDL_FreeSurface(src);
SDL_FreeSurface(dest);
}