1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-10 21:19:43 +01:00

Per-level custom asset loading functionality (#262)

This commit is contained in:
Matt Aaldenberg 2020-05-31 19:31:02 -04:00 committed by GitHub
parent cfcfccf58b
commit b217fec3aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 127 additions and 20 deletions

View file

@ -7,6 +7,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "Graphics.h"
#include <iterator> #include <iterator>
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
@ -76,7 +78,7 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath)
mkdirResult = mkdir(output, 0777); mkdirResult = mkdir(output, 0777);
/* Mount our base user directory */ /* Mount our base user directory */
PHYSFS_mount(output, NULL, 1); PHYSFS_mount(output, NULL, 0);
PHYSFS_setWriteDir(output); PHYSFS_setWriteDir(output);
printf("Base directory: %s\n", output); printf("Base directory: %s\n", output);
@ -149,6 +151,33 @@ char *FILESYSTEM_getUserLevelDirectory()
return levelDir; return levelDir;
} }
bool FILESYSTEM_directoryExists(const char *fname)
{
return PHYSFS_exists(fname);
}
void FILESYSTEM_mount(const char *fname)
{
std::string path(PHYSFS_getRealDir(fname));
path += PHYSFS_getDirSeparator();
path += fname;
if (!PHYSFS_mount(path.c_str(), NULL, 0)) {
printf("Error mounting: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
} else
graphics.assetdir = path.c_str();
}
void FILESYSTEM_unmountassets()
{
if (graphics.assetdir != "")
{
printf("Unmounting %s\n", graphics.assetdir.c_str());
PHYSFS_unmount(graphics.assetdir.c_str());
graphics.assetdir = "";
graphics.reloadresources();
} else printf("Cannot unmount when no asset directory is mounted\n");
}
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
size_t *len, bool addnull) size_t *len, bool addnull)
{ {

View file

@ -12,6 +12,10 @@ void FILESYSTEM_deinit();
char *FILESYSTEM_getUserSaveDirectory(); char *FILESYSTEM_getUserSaveDirectory();
char *FILESYSTEM_getUserLevelDirectory(); char *FILESYSTEM_getUserLevelDirectory();
bool FILESYSTEM_directoryExists(const char *fname);
void FILESYSTEM_mount(const char *fname);
void FILESYSTEM_unmountassets();
void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem, void FILESYSTEM_loadFileToMemory(const char *name, unsigned char **mem,
size_t *len, bool addnull = false); size_t *len, bool addnull = false);
void FILESYSTEM_freeMemory(unsigned char **mem); void FILESYSTEM_freeMemory(unsigned char **mem);

View file

@ -2819,3 +2819,48 @@ bool Graphics::onscreen(int t)
{ {
return (t >= -40 && t <= 280); return (t >= -40 && t <= 280);
} }
void Graphics::reloadresources() {
grphx = GraphicsResources();
grphx.init();
for (size_t i = 0; i < images.size(); i++){ SDL_FreeSurface(images[i]); }
images.clear();
for (size_t i = 0; i < tiles.size(); i++){ SDL_FreeSurface(tiles[i]); }
tiles.clear();
for (size_t i = 0; i < tiles2.size(); i++){ SDL_FreeSurface(tiles2[i]); }
tiles2.clear();
for (size_t i = 0; i < tiles3.size(); i++){ SDL_FreeSurface(tiles3[i]); }
tiles3.clear();
for (size_t i = 0; i < entcolours.size(); i++){ SDL_FreeSurface(entcolours[i]); }
entcolours.clear();
for (size_t i = 0; i < sprites.size(); i++){ SDL_FreeSurface(sprites[i]); }
sprites.clear();
for (size_t i = 0; i < flipsprites.size(); i++){ SDL_FreeSurface(flipsprites[i]); }
flipsprites.clear();
for (size_t i = 0; i < tele.size(); i++){ SDL_FreeSurface(tele[i]); }
tele.clear();
MakeTileArray();
MakeSpriteArray();
maketelearray();
Makebfont();
images.push_back(grphx.im_image0);
images.push_back(grphx.im_image1);
images.push_back(grphx.im_image2);
images.push_back(grphx.im_image3);
images.push_back(grphx.im_image4);
images.push_back(grphx.im_image5);
images.push_back(grphx.im_image6);
images.push_back(grphx.im_image7);
images.push_back(grphx.im_image8);
images.push_back(grphx.im_image9);
images.push_back(grphx.im_image10);
images.push_back(grphx.im_image11);
images.push_back(grphx.im_image12);
music.init();
}

View file

@ -185,6 +185,9 @@ public:
bool onscreen(int t); bool onscreen(int t);
void reloadresources();
std::string assetdir;
void menuoffrender(); void menuoffrender();

View file

@ -18,6 +18,7 @@
#include <string> #include <string>
#include <utf8/unchecked.h> #include <utf8/unchecked.h>
#include <physfs.h>
edlevelclass::edlevelclass() edlevelclass::edlevelclass()
{ {
@ -78,6 +79,10 @@ bool compare_nocase (std::string first, std::string second)
return false; return false;
} }
static bool endsWith(const std::string& str, const std::string& suffix) {
return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
}
void replace_all(std::string& str, const std::string& from, const std::string& to) void replace_all(std::string& str, const std::string& from, const std::string& to)
{ {
if (from.empty()) if (from.empty())
@ -1640,6 +1645,38 @@ void editorclass::load(std::string& _path)
_path = levelDir + _path; _path = levelDir + _path;
} }
FILESYSTEM_unmountassets();
std::string zippath = "levels/" + _path.substr(7,_path.size()-14) + ".data.zip";
std::string dirpath = "levels/" + _path.substr(7,_path.size()-14) + "/";
std::string zip_path;
const char* cstr = PHYSFS_getRealDir(_path.c_str());
if (cstr) zip_path = cstr;
if (cstr && FILESYSTEM_directoryExists(zippath.c_str())) {
printf("Custom asset directory exists at %s\n",zippath.c_str());
FILESYSTEM_mount(zippath.c_str());
graphics.reloadresources();
music.init();
} else if (zip_path != "data.zip" && !endsWith(zip_path, "/data.zip") && endsWith(zip_path, ".zip")) {
printf("Custom asset directory is .zip at %s\n", zip_path.c_str());
PHYSFS_File* zip = PHYSFS_openRead(zip_path.c_str());
zip_path += ".data.zip";
if (zip == NULL) {
printf("error loading .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
} else if (PHYSFS_mountHandle(zip, zip_path.c_str(), "/", 0) == 0) {
printf("error mounting .zip: %s\n", PHYSFS_getErrorByCode(PHYSFS_getLastErrorCode()));
} else {
graphics.assetdir = zip_path;
}
graphics.reloadresources();
} else if (FILESYSTEM_directoryExists(dirpath.c_str())) {
printf("Custom asset directory exists at %s\n",dirpath.c_str());
FILESYSTEM_mount(dirpath.c_str());
graphics.reloadresources();
} else {
printf("Custom asset directory does not exist\n");
}
TiXmlDocument doc; TiXmlDocument doc;
if (!FILESYSTEM_loadTiXmlDocument(_path.c_str(), &doc)) if (!FILESYSTEM_loadTiXmlDocument(_path.c_str(), &doc))
{ {
@ -3661,6 +3698,13 @@ void editorinput()
game.press_action = true; game.press_action = true;
}; };
if (key.keymap[SDLK_F9] && (ed.keydelay==0)) {
ed.keydelay = 30;
ed.note="Reloaded resources";
ed.notedelay=45;
graphics.reloadresources();
}
if (key.isDown(KEYBOARD_ENTER)) game.press_map = true; if (key.isDown(KEYBOARD_ENTER)) game.press_map = true;
if (key.isDown(27) && !ed.settingskey) if (key.isDown(27) && !ed.settingskey)
{ {

View file

@ -168,25 +168,7 @@ int main(int argc, char *argv[])
game.init(); game.init();
game.infocus = true; game.infocus = true;
graphics.MakeTileArray(); graphics.reloadresources();
graphics.MakeSpriteArray();
graphics.maketelearray();
graphics.images.push_back(graphics.grphx.im_image0);
graphics.images.push_back(graphics.grphx.im_image1);
graphics.images.push_back(graphics.grphx.im_image2);
graphics.images.push_back(graphics.grphx.im_image3);
graphics.images.push_back(graphics.grphx.im_image4);
graphics.images.push_back(graphics.grphx.im_image5);
graphics.images.push_back(graphics.grphx.im_image6);
graphics.images.push_back(graphics.grphx.im_image7);
graphics.images.push_back(graphics.grphx.im_image8);
graphics.images.push_back(graphics.grphx.im_image9);
graphics.images.push_back(graphics.grphx.im_image10);
graphics.images.push_back(graphics.grphx.im_image11);
graphics.images.push_back(graphics.grphx.im_image12);
const SDL_PixelFormat* fmt = gameScreen.GetFormat(); const SDL_PixelFormat* fmt = gameScreen.GetFormat();
graphics.backBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, fmt->BitsPerPixel, fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask); graphics.backBuffer = SDL_CreateRGBSurface(SDL_SWSURFACE, 320, 240, fmt->BitsPerPixel, fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);