1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-26 06:28:30 +02:00
VVVVVV/desktop_version/src/GraphicsResources.cpp
Misa 3ebdc1da89 Transfer param init responsibility to loadFileToMemory
So, the codebase was kind of undecided about who is responsible for
initializing the parameters passed to FILESYSTEM_loadFileToMemory() - is
it the caller? Is it FILESYSTEM_loadFileToMemory()? Sometimes callers
would initialize one variable but not the other, and it was always a
toss-up whether or not FILESYSTEM_loadFileToMemory() would end up
initializing everything in the end.

All of this is to say that the game dereferences an uninitialized
pointer if it can't load a sound effect. Which is bad. Now, I could
either fix that single case, or fix every case. Judging by the title of
this commit, you can infer that I decided to fix every case - fixing
every case means not just all cases that currently exist (which, as far
as I know, is only the sound effect one), but all cases that could exist
in the future.

So, FILESYSTEM_loadFileToMemory() is now guaranteed to initialize its
parameters even if the file fails to be loaded. This is better than
passing the responsibility to the caller anyway, because if the caller
initialized it, then that would be wasted work if the file succeeds
anyway because FILESYSTEM_loadFileToMemory() will overwrite it, and if
the file fails to load, well that's when the variables get initialized
anyway.
2021-04-18 15:01:43 -04:00

143 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;
size_t length;
FILESYSTEM_loadAssetToMemory(filename, &fileIn, &length, false);
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
{
SDL_free(data);
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
}