From 7eae466865d806a8e71f0c28b25785b6c3ee1c56 Mon Sep 17 00:00:00 2001 From: iProgramInCpp Date: Tue, 26 Dec 2023 01:18:54 +0200 Subject: [PATCH] * Fixes to fancy pants grass detection, etc. --- game/assets/patches/patch_data.txt | 1 - platforms/android/AppPlatform_android.cpp | 7 +++-- platforms/sdl/base/AppPlatform_sdl_base.hpp | 2 +- platforms/sdl/desktop/AppPlatform_sdl.cpp | 5 ++-- platforms/sdl/emscripten/AppPlatform_sdl.cpp | 3 ++- platforms/windows/AppPlatform_win32.cpp | 5 +++- platforms/windows/AppPlatform_win32.hpp | 2 +- source/client/app/AppPlatform.cpp | 2 +- source/client/app/AppPlatform.hpp | 2 +- source/client/app/Minecraft.cpp | 28 ++++++++++---------- source/client/options/Options.cpp | 2 -- source/client/renderer/PatchManager.cpp | 25 ++++++++++++++--- source/client/renderer/Textures.cpp | 4 +-- 13 files changed, 56 insertions(+), 32 deletions(-) diff --git a/game/assets/patches/patch_data.txt b/game/assets/patches/patch_data.txt index 04cf358..4407911 100644 --- a/game/assets/patches/patch_data.txt +++ b/game/assets/patches/patch_data.txt @@ -13,7 +13,6 @@ # * The X and Y destination coordinates will be multiplied by 16. # * The texture doesn't have to be 16x16, all of it will be patched on to terrain.png. -terrain|4|5|grass_side_transparent.png grass_sides_tint|true # Stop now to ignore the below commands. They're for a patch I'm working on that I don't want to release yet. diff --git a/platforms/android/AppPlatform_android.cpp b/platforms/android/AppPlatform_android.cpp index dae72fa..6279280 100644 --- a/platforms/android/AppPlatform_android.cpp +++ b/platforms/android/AppPlatform_android.cpp @@ -134,7 +134,7 @@ std::string AppPlatform_android::getDateString(int time) return std::string(buffer); } -Texture AppPlatform_android::loadTexture(const std::string& str, bool b) +Texture AppPlatform_android::loadTexture(const std::string& str, bool bIsRequired) { std::string realPath = str; if (realPath.size() && realPath[0] == '/') @@ -144,19 +144,22 @@ Texture AppPlatform_android::loadTexture(const std::string& str, bool b) AAsset* asset = AAssetManager_open(m_app->activity->assetManager, str.c_str(), AASSET_MODE_BUFFER); if (!asset) { LOG_E("File %s couldn't be opened", realPath.c_str()); + assert(!bIsRequired && "Hey, a texture couldn't be loaded"); + return Texture(0, 0, nullptr, 1, 0); } size_t cnt = AAsset_getLength(asset); unsigned char* buffer = (unsigned char*)calloc(cnt, sizeof(unsigned char)); AAsset_read(asset, (void*)buffer, cnt); AAsset_close(asset); - int width = 0, height = 0, channels = 0; stbi_uc* img = stbi_load_from_memory(buffer, cnt, &width, &height, &channels, STBI_rgb_alpha); if (!img) { LOG_E("File %s couldn't be loaded via stb_image", realPath.c_str()); + assert(!bIsRequired && "Hey, a texture couldn't be loaded"); + return Texture(0, 0, nullptr, 1, 0); } free(buffer); diff --git a/platforms/sdl/base/AppPlatform_sdl_base.hpp b/platforms/sdl/base/AppPlatform_sdl_base.hpp index 942dda6..ef705b6 100644 --- a/platforms/sdl/base/AppPlatform_sdl_base.hpp +++ b/platforms/sdl/base/AppPlatform_sdl_base.hpp @@ -25,7 +25,7 @@ public: const char* const getWindowTitle() const; int getScreenWidth() const override; int getScreenHeight() const override; - Texture loadTexture(const std::string& path, bool b = false) override = 0; + Texture loadTexture(const std::string& path, bool bIsRequired = false) override = 0; virtual bool doesTextureExist(const std::string& path) = 0; int getUserInputStatus() override; SoundSystem* const getSoundSystem() const override { return m_pSoundSystem; } diff --git a/platforms/sdl/desktop/AppPlatform_sdl.cpp b/platforms/sdl/desktop/AppPlatform_sdl.cpp index c39153c..df0f45d 100644 --- a/platforms/sdl/desktop/AppPlatform_sdl.cpp +++ b/platforms/sdl/desktop/AppPlatform_sdl.cpp @@ -15,7 +15,7 @@ AppPlatform_sdl::AppPlatform_sdl(std::string storageDir, SDL_Window *window) : AppPlatform_sdl_base(storageDir, window) { - setIcon(loadTexture("icon.png")); + setIcon(loadTexture("icon.png", false)); } // Take Screenshot @@ -128,7 +128,7 @@ void AppPlatform_sdl::saveScreenshot(const std::string& filename, int glWidth, i } } -Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b) +Texture AppPlatform_sdl::loadTexture(const std::string& path, bool bIsRequired) { Texture out; out.field_C = 1; @@ -156,6 +156,7 @@ Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b) if (!img) { // Failed To Parse Image + LOG_E("The image could not be loaded properly: %s", path.c_str()); return out; } diff --git a/platforms/sdl/emscripten/AppPlatform_sdl.cpp b/platforms/sdl/emscripten/AppPlatform_sdl.cpp index 19661dd..3671c9d 100644 --- a/platforms/sdl/emscripten/AppPlatform_sdl.cpp +++ b/platforms/sdl/emscripten/AppPlatform_sdl.cpp @@ -7,7 +7,7 @@ AppPlatform_sdl::AppPlatform_sdl(std::string storageDir, SDL_Window *window) { } -Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b) +Texture AppPlatform_sdl::loadTexture(const std::string& path, bool bIsRequired) { Texture out; out.field_C = 1; @@ -28,6 +28,7 @@ Texture AppPlatform_sdl::loadTexture(const std::string& path, bool b) LOG_E("Couldn't find file: %s", realPath.c_str()); return out; } + bool AppPlatform_sdl::doesTextureExist(const std::string& path) { // Get Full Path diff --git a/platforms/windows/AppPlatform_win32.cpp b/platforms/windows/AppPlatform_win32.cpp index a3f0bcb..8e23990 100644 --- a/platforms/windows/AppPlatform_win32.cpp +++ b/platforms/windows/AppPlatform_win32.cpp @@ -151,7 +151,7 @@ std::string AppPlatform_win32::getDateString(int time) return std::string(buf); } -Texture AppPlatform_win32::loadTexture(const std::string& str, bool b) +Texture AppPlatform_win32::loadTexture(const std::string& str, bool bIsRequired) { std::string realPath = str; if (realPath.size() && realPath[0] == '/') @@ -166,6 +166,9 @@ Texture AppPlatform_win32::loadTexture(const std::string& str, bool b) LOG_E("File %s couldn't be opened", realPath.c_str()); _error: + if (!bIsRequired) + return Texture(0, 0, nullptr, 1, 0); + const std::string msg = "Error loading " + realPath + ". Did you unzip the Minecraft assets?"; MessageBoxA(GetHWND(), msg.c_str(), getWindowTitle(), MB_OK); diff --git a/platforms/windows/AppPlatform_win32.hpp b/platforms/windows/AppPlatform_win32.hpp index 98856bc..17fe95c 100644 --- a/platforms/windows/AppPlatform_win32.hpp +++ b/platforms/windows/AppPlatform_win32.hpp @@ -35,7 +35,7 @@ public: int getScreenHeight() const override { return m_ScreenHeight; } void showDialog(eDialogType) override; std::string getDateString(int time) override; - Texture loadTexture(const std::string& str, bool b) override; + Texture loadTexture(const std::string& str, bool bIsRequired) override; // From v0.1.1. Also add these to determine touch screen use within the game. bool isTouchscreen() override; diff --git a/source/client/app/AppPlatform.cpp b/source/client/app/AppPlatform.cpp index f0b5f2a..d61993e 100644 --- a/source/client/app/AppPlatform.cpp +++ b/source/client/app/AppPlatform.cpp @@ -101,7 +101,7 @@ void AppPlatform::uploadPlatformDependentData(int, void*) } -Texture AppPlatform::loadTexture(const std::string&, bool) +Texture AppPlatform::loadTexture(const std::string&, bool bIsRequired) { return Texture(0, 0, nullptr, 1, 0); } diff --git a/source/client/app/AppPlatform.hpp b/source/client/app/AppPlatform.hpp index ba29cb0..a197f67 100644 --- a/source/client/app/AppPlatform.hpp +++ b/source/client/app/AppPlatform.hpp @@ -46,7 +46,7 @@ public: virtual void saveScreenshot(const std::string&, int, int); virtual void showDialog(eDialogType); virtual void uploadPlatformDependentData(int, void*); - virtual Texture loadTexture(const std::string&, bool); + virtual Texture loadTexture(const std::string&, bool bIsRequired); #ifndef ORIGINAL_CODE // From v0.1.1. Also add these to determine touch screen use within the game. diff --git a/source/client/app/Minecraft.cpp b/source/client/app/Minecraft.cpp index a6b6455..67e708f 100644 --- a/source/client/app/Minecraft.cpp +++ b/source/client/app/Minecraft.cpp @@ -791,6 +791,20 @@ void Minecraft::init() { GetPatchManager()->LoadPatchData(platform()->getPatchData()); + m_pTextures = new Textures(m_options, platform()); + m_pTextures->addDynamicTexture(new WaterTexture); + m_pTextures->addDynamicTexture(new WaterSideTexture); + m_pTextures->addDynamicTexture(new LavaTexture); + m_pTextures->addDynamicTexture(new LavaSideTexture); + m_pTextures->addDynamicTexture(new FireTexture(0)); + + m_pTextures->loadAndBindTexture(C_TERRAIN_NAME); + GetPatchManager()->PatchTextures(platform(), TYPE_TERRAIN); + m_pTextures->loadAndBindTexture(C_ITEMS_NAME); + GetPatchManager()->PatchTextures(platform(), TYPE_ITEMS); + + GetPatchManager()->PatchTiles(); + if (platform()->hasFileSystemAccess()) m_options = new Options(m_externalStorageDir); else @@ -805,12 +819,6 @@ void Minecraft::init() m_pSoundEngine = new SoundEngine(platform()->getSoundSystem()); m_pSoundEngine->init(m_options); - m_pTextures = new Textures(m_options, platform()); - m_pTextures->addDynamicTexture(new WaterTexture); - m_pTextures->addDynamicTexture(new WaterSideTexture); - m_pTextures->addDynamicTexture(new LavaTexture); - m_pTextures->addDynamicTexture(new LavaSideTexture); - m_pTextures->addDynamicTexture(new FireTexture(0)); m_pLevelRenderer = new LevelRenderer(this, m_pTextures); m_pGameRenderer = new GameRenderer(this); m_pParticleEngine = new ParticleEngine(m_pLevel, m_pTextures); @@ -832,14 +840,6 @@ void Minecraft::init() { FoliageColor::init(m_pPlatform->loadTexture("misc/foliagecolor.png", true)); } - - - m_pTextures->loadAndBindTexture(C_TERRAIN_NAME); - GetPatchManager()->PatchTextures(platform(), TYPE_TERRAIN); - m_pTextures->loadAndBindTexture(C_ITEMS_NAME); - GetPatchManager()->PatchTextures(platform(), TYPE_ITEMS); - - GetPatchManager()->PatchTiles(); } Minecraft::~Minecraft() diff --git a/source/client/options/Options.cpp b/source/client/options/Options.cpp index 83c8c4e..c33ffd9 100644 --- a/source/client/options/Options.cpp +++ b/source/client/options/Options.cpp @@ -258,9 +258,7 @@ void Options::_load() { m_bFancyGrass = readBool(value); if (!(GetPatchManager()->IsGrassSidesTinted())) - { m_bFancyGrass = false; - } } else if (key == "gfx_biomecolors") { diff --git a/source/client/renderer/PatchManager.cpp b/source/client/renderer/PatchManager.cpp index a24ded4..585f54b 100644 --- a/source/client/renderer/PatchManager.cpp +++ b/source/client/renderer/PatchManager.cpp @@ -144,6 +144,11 @@ void PatchManager::LoadPatchData(const std::string& patchData) if (command == "grass_sides_tint") { ReadBool(lineStream, m_bGrassSidesTinted); + + if (m_bGrassSidesTinted) + // push a magic value so we can determine whether to disable it if the file doesn't exist + m_patchData.push_back(PatchData(TYPE_TERRAIN, 100, 100, "grass_side_transparent.png")); + continue; } @@ -160,10 +165,24 @@ void PatchManager::PatchTextures(AppPlatform* pAppPlatform, ePatchType patchType if (pd.m_type != patchType) continue; - Texture texture = pAppPlatform->loadTexture("patches/" + pd.m_filename, true); - if (texture.m_width == 0) + bool bDisableFancyGrassIfFailed = false; + + // got the magic value, we can determine whether to disable fancy pants grass if the file doesn't exist + if (pd.m_destX == 1600 && pd.m_destY == 1600 && pd.m_type == TYPE_TERRAIN) { - LOG_W("Image %s has width 0, not found?! Skipping", pd.m_filename.c_str()); + pd.m_destX = 4 * 16; + pd.m_destY = 5 * 16; + + bDisableFancyGrassIfFailed = true; + } + + // N.B. Well, in some cases, you do want things to fail nicely. + Texture texture = pAppPlatform->loadTexture("patches/" + pd.m_filename, false); + if (!texture.m_pixels || !texture.m_width || !texture.m_height) + { + LOG_W("Image %s was not found?! Skipping", pd.m_filename.c_str()); + if (bDisableFancyGrassIfFailed) + m_bGrassSidesTinted = false; continue; } diff --git a/source/client/renderer/Textures.cpp b/source/client/renderer/Textures.cpp index d53f5f7..f282ddb 100644 --- a/source/client/renderer/Textures.cpp +++ b/source/client/renderer/Textures.cpp @@ -11,13 +11,13 @@ bool Textures::MIPMAP = false; -int Textures::loadTexture(const std::string& name, bool b) +int Textures::loadTexture(const std::string& name, bool bIsRequired) { std::map::iterator i = m_textures.find(name); if (i != m_textures.end()) return i->second; - Texture t = m_pPlatform->loadTexture(name, b); + Texture t = m_pPlatform->loadTexture(name, bIsRequired); int result = -1; if (t.m_pixels)