1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-18 08:59:42 +01:00
VVVVVV/desktop_version/src/GraphicsResources.cpp
Misa 769f99f590 Reduce dependency on libc functions
During 2.3 development, there's been a gradual shift to using SDL stdlib
functions instead of libc functions, but there are still some libc
functions (or the same libc function but from the STL) in the code.

Well, this patch replaces all the rest of them in one fell swoop.

SDL's stdlib can replace most of these, but its SDL_min() and SDL_max()
are inadequate - they aren't really functions, they're more like macros
with a nasty penchant for double-evaluation. So I just made my own
VVV_min() and VVV_max() functions and placed them in Maths.h instead,
then replaced all the previous usages of min(), max(), std::min(),
std::max(), SDL_min(), and SDL_max() with VVV_min() and VVV_max().

Additionally, there's no SDL_isxdigit(), so I just implemented my own
VVV_isxdigit().

SDL has SDL_malloc() and SDL_free(), but they have some refcounting
built in to them, so in order to use them with LodePNG, I have to
replace the malloc() and free() that LodePNG uses. Which isn't too hard,
I did it in a new file called ThirdPartyDeps.c, and LodePNG is now
compiled with the LODEPNG_NO_COMPILE_ALLOCATORS definition.

Lastly, I also refactored the awful strcpy() and strcat() usages in
PLATFORM_migrateSaveData() to use SDL_snprintf() instead. I know save
migration is getting axed in 2.4, but it still bothers me to have
something like that in the codebase otherwise.

Without further ado, here is the full list of functions that the
codebase now uses:

- SDL_strlcpy() instead of strcpy()
- SDL_strlcat() instead of strcat()
- SDL_snprintf() instead of sprintf(), strcpy(), or strcat() (see above)
- VVV_min() instead of min(), std::min(), or SDL_min()
- VVV_max() instead of max(), std::max(), or SDL_max()
- VVV_isxdigit() instead of isxdigit()
- SDL_strcmp() instead of strcmp()
- SDL_strcasecmp() instead of strcasecmp() or Win32 strcmpi()
- SDL_strstr() instead of strstr()
- SDL_strlen() instead of strlen()
- SDL_sscanf() instead of sscanf()
- SDL_getenv() instead of getenv()
- SDL_malloc() instead of malloc() (replacing in LodePNG as well)
- SDL_free() instead of free() (replacing in LodePNG as well)
2021-01-12 14:02:31 -05:00

141 lines
3.3 KiB
C++

#include "GraphicsResources.h"
#include <stdio.h>
#include <stdlib.h>
#include "FileSystemUtils.h"
// Used to load PNG data
extern "C"
{
extern unsigned lodepng_decode24(
unsigned char** out,
unsigned* w,
unsigned* h,
const unsigned char* in,
size_t insize
);
extern unsigned lodepng_decode32(
unsigned char** out,
unsigned* w,
unsigned* h,
const unsigned char* in,
size_t insize
);
}
static SDL_Surface* LoadImage(const char *filename, bool noBlend = true, bool noAlpha = false)
{
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
unsigned char *data;
unsigned int width, height;
unsigned char *fileIn = NULL;
size_t length = 0;
FILESYSTEM_loadFileToMemory(filename, &fileIn, &length);
if (noAlpha)
{
lodepng_decode24(&data, &width, &height, fileIn, length);
}
else
{
lodepng_decode32(&data, &width, &height, fileIn, length);
}
FILESYSTEM_freeMemory(&fileIn);
loadedImage = SDL_CreateRGBSurfaceFrom(
data,
width,
height,
noAlpha ? 24 : 32,
width * (noAlpha ? 3 : 4),
0x000000FF,
0x0000FF00,
0x00FF0000,
noAlpha ? 0x00000000 : 0xFF000000
);
if (loadedImage != NULL)
{
optimizedImage = SDL_ConvertSurfaceFormat(
loadedImage,
SDL_PIXELFORMAT_ABGR8888, // FIXME: Format? -flibit
0
);
SDL_FreeSurface( loadedImage );
SDL_free(data);
if (noBlend)
{
SDL_SetSurfaceBlendMode(optimizedImage, SDL_BLENDMODE_BLEND);
}
return optimizedImage;
}
else
{
fprintf(stderr,"Image not found: %s\n", filename);
SDL_assert(0 && "Image not found! See stderr.");
return NULL;
}
}
void GraphicsResources::init(void)
{
im_tiles = LoadImage("graphics/tiles.png");
im_tiles2 = LoadImage("graphics/tiles2.png");
im_tiles3 = LoadImage("graphics/tiles3.png");
im_entcolours = LoadImage("graphics/entcolours.png");
im_sprites = LoadImage("graphics/sprites.png");
im_flipsprites = LoadImage("graphics/flipsprites.png");
im_bfont = LoadImage("graphics/font.png");
im_teleporter = LoadImage("graphics/teleporter.png");
im_image0 = LoadImage("graphics/levelcomplete.png", false);
im_image1 = LoadImage("graphics/minimap.png", true, true);
im_image2 = LoadImage("graphics/covered.png", true, true);
im_image3 = LoadImage("graphics/elephant.png");
im_image4 = LoadImage("graphics/gamecomplete.png", false);
im_image5 = LoadImage("graphics/fliplevelcomplete.png", false);
im_image6 = LoadImage("graphics/flipgamecomplete.png", false);
im_image7 = LoadImage("graphics/site.png", false);
im_image8 = LoadImage("graphics/site2.png");
im_image9 = LoadImage("graphics/site3.png");
im_image10 = LoadImage("graphics/ending.png");
im_image11 = LoadImage("graphics/site4.png");
im_image12 = LoadImage("graphics/minimap.png");
}
void GraphicsResources::destroy(void)
{
#define CLEAR(img) \
SDL_FreeSurface(img); \
img = NULL;
CLEAR(im_tiles);
CLEAR(im_tiles2);
CLEAR(im_tiles3);
CLEAR(im_entcolours);
CLEAR(im_sprites);
CLEAR(im_flipsprites);
CLEAR(im_bfont);
CLEAR(im_teleporter);
CLEAR(im_image0);
CLEAR(im_image1);
CLEAR(im_image2);
CLEAR(im_image3);
CLEAR(im_image4);
CLEAR(im_image5);
CLEAR(im_image6);
CLEAR(im_image7);
CLEAR(im_image8);
CLEAR(im_image9);
CLEAR(im_image10);
CLEAR(im_image11);
CLEAR(im_image12);
#undef CLEAR
}