mirror of
https://github.com/TerryCavanagh/VVVVVV.git
synced 2024-11-16 16:09:42 +01:00
5a316d65e6
This commit makes `help`, `graphics`, `music`, `game`, `key`, `map`, and `obj` essentially static global objects that can be used everywhere. This is useful in case we ever need to add a new function in the future, so we don't have to bother with passing a new argument in which means we have to pass a new argument in to the function that calls that function which means having to pass a new argument into the function that calls THAT function, etc. which is a real headache when working on fan mods of the source code. Note that this changes NONE of the existing function signatures, it merely just makes those variables accessible everywhere in the same way `script` and `ed` are. Also note that some classes had to be initialized after the filesystem was initialized, but C++ would keep initializing them before the filesystem got initialized, because I *had* to put them at the top of `main.cpp`, or else they wouldn't be global variables. The only way to work around this was to use entityclass's initialization style (which I'm pretty sure entityclass of all things doesn't need to be initialized this way), where you actually initialize the class in an `init()` function, and so then you do `graphics.init()` after the filesystem initialization, AFTER doing `Graphics graphics` up at the top. I've had to do this for `graphics` (but only because its child GraphicsResources `grphx` needs to be initialized this way), `music`, and `game`. I don't think this will affect anything. Other than that, `help`, `key`, and `map` are still using the C++-intended method of having ClassName::ClassName() functions.
113 lines
2.9 KiB
C++
113 lines
2.9 KiB
C++
#include "GraphicsResources.h"
|
|
#include "FileSystemUtils.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.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
|
|
);
|
|
}
|
|
|
|
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 );
|
|
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_bfontmask = LoadImage("graphics/fontmask.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");
|
|
}
|
|
|
|
|
|
GraphicsResources::~GraphicsResources(void)
|
|
{
|
|
}
|