Skip to content

Commit

Permalink
Load textures from current graphics_overrides, if present
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Sep 7, 2023
1 parent cefd833 commit ffbc79b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/ivis_opengl/gfx_api.cpp
Expand Up @@ -221,13 +221,13 @@ optional<gfx_api::max_texture_compression_level> gfx_api::getMaxTextureCompressi

#include "png_util.h"

static gfx_api::texture* loadImageTextureFromFile_PNG(const std::string& filename, gfx_api::texture_type textureType, int maxWidth /*= -1*/, int maxHeight /*= -1*/)
static gfx_api::texture* loadImageTextureFromFile_PNG(const std::string& filename, gfx_api::texture_type textureType, int maxWidth /*= -1*/, int maxHeight /*= -1*/, bool quiet)
{
iV_Image loadedUncompressedImage;

// 1.) Load the PNG into an iV_Image
bool forceRGB = (textureType == gfx_api::texture_type::game_texture) || (textureType == gfx_api::texture_type::user_interface);
if (!iV_loadImage_PNG2(filename.c_str(), loadedUncompressedImage, forceRGB))
if (!iV_loadImage_PNG2(filename.c_str(), loadedUncompressedImage, forceRGB, quiet))
{
// Failed to load the image
return nullptr;
Expand Down Expand Up @@ -301,7 +301,7 @@ bool gfx_api::checkImageFilesWouldLoadFromSameParentMountPath(const std::vector<

// Load a texture from a file
// (which loads straight to a texture based on the appropriate texture_type, handling mip_maps, compression, etc)
gfx_api::texture* gfx_api::context::loadTextureFromFile(const char *filename, gfx_api::texture_type textureType, int maxWidth /*= -1*/, int maxHeight /*= -1*/)
gfx_api::texture* gfx_api::context::loadTextureFromFile(const char *filename, gfx_api::texture_type textureType, int maxWidth /*= -1*/, int maxHeight /*= -1*/, bool quiet /*= false*/)
{
auto imageLoadFilename = imageLoadFilenameFromInputFilename(filename);

Expand All @@ -314,7 +314,7 @@ gfx_api::texture* gfx_api::context::loadTextureFromFile(const char *filename, gf
#endif
if (imageLoadFilename.endsWith(".png"))
{
return loadImageTextureFromFile_PNG(imageLoadFilename.toUtf8(), textureType, maxWidth, maxHeight);
return loadImageTextureFromFile_PNG(imageLoadFilename.toUtf8(), textureType, maxWidth, maxHeight, quiet);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion lib/ivis_opengl/gfx_api.h
Expand Up @@ -427,7 +427,7 @@ namespace gfx_api
virtual void draw_elements_instanced(const std::size_t& offset, const std::size_t& count, const primitive_type& primitive, const index_type& index, std::size_t instance_count) = 0;
public:
// High-level API for getting a texture object from file / uncompressed bitmap
gfx_api::texture* loadTextureFromFile(const char *filename, gfx_api::texture_type textureType, int maxWidth = -1, int maxHeight = -1);
gfx_api::texture* loadTextureFromFile(const char *filename, gfx_api::texture_type textureType, int maxWidth = -1, int maxHeight = -1, bool quiet = false);
gfx_api::texture* loadTextureFromUncompressedImage(iV_Image&& image, gfx_api::texture_type textureType, const std::string& filename, int maxWidth = -1, int maxHeight = -1);
typedef std::function<std::unique_ptr<iV_Image> (int width, int height, int channels)> GenerateDefaultTextureFunc;
gfx_api::texture_array* loadTextureArrayFromFiles(const std::vector<WzString>& filenames, gfx_api::texture_type textureType, int maxWidth = -1, int maxHeight = -1, const GenerateDefaultTextureFunc& defaultTextureGenerator = nullptr, const std::function<void ()>& progressCallback = nullptr, const std::string& debugName = "");
Expand Down
14 changes: 12 additions & 2 deletions lib/ivis_opengl/png_util.cpp
Expand Up @@ -214,7 +214,7 @@ bool iV_loadImage_PNG(const char *fileName, iV_Image *image)
return true;
}

bool iV_loadImage_PNG2(const char *fileName, iV_Image& image, bool forceRGBA8 /*= false*/)
bool iV_loadImage_PNG2(const char *fileName, iV_Image& image, bool forceRGBA8 /*= false*/, bool quietOnOpenFail /*= false*/)
{
unsigned char PNGheader[PNG_BYTES_TO_CHECK];
PHYSFS_sint64 readSize;
Expand All @@ -234,7 +234,17 @@ bool iV_loadImage_PNG2(const char *fileName, iV_Image& image, bool forceRGBA8 /*

// Open file
PHYSFS_file *fileHandle = PHYSFS_openRead(fileName);
ASSERT_OR_RETURN(false, fileHandle != nullptr, "Could not open %s: %s", fileName, WZ_PHYSFS_getLastError());
if (fileHandle == nullptr)
{
if (!quietOnOpenFail)
{
ASSERT_OR_RETURN(false, fileHandle != nullptr, "Could not open %s: %s", fileName, WZ_PHYSFS_getLastError());
}
else
{
return false;
}
}
WZ_PHYSFS_SETBUFFER(fileHandle, 4096)//;

// Read PNG header from file
Expand Down
2 changes: 1 addition & 1 deletion lib/ivis_opengl/png_util.h
Expand Up @@ -51,7 +51,7 @@ struct IMGSaveError
*/
bool iV_loadImage_PNG(const char *fileName, iV_Image *image);

bool iV_loadImage_PNG2(const char *fileName, iV_Image& image, bool forceRGBA8 = false);
bool iV_loadImage_PNG2(const char *fileName, iV_Image& image, bool forceRGBA8 = false, bool quietOnOpenFail = false);

/*!
* Load a PNG from a memory buffer into an image
Expand Down
17 changes: 12 additions & 5 deletions lib/ivis_opengl/tex.cpp
Expand Up @@ -207,14 +207,21 @@ optional<size_t> iV_GetTexture(const char *filename, gfx_api::texture_type textu
return it->second;
}

// Try to load it
std::string loadPath = "texpages/";
// First, try to load from the current graphics_overrides (if enabled)
std::string loadPath = WZ_CURRENT_GRAPHICS_OVERRIDES_PREFIX "/texpages/";
loadPath += filename;
gfx_api::texture *pTexture = gfx_api::context::get().loadTextureFromFile(loadPath.c_str(), textureType, maxWidth, maxHeight);
gfx_api::texture *pTexture = gfx_api::context::get().loadTextureFromFile(loadPath.c_str(), textureType, maxWidth, maxHeight, true);
if (!pTexture)
{
debug(LOG_ERROR, "Failed to load %s", loadPath.c_str());
return nullopt;
// Try to load it from the regular path
loadPath = "texpages/";
loadPath += filename;
pTexture = gfx_api::context::get().loadTextureFromFile(loadPath.c_str(), textureType, maxWidth, maxHeight);
if (!pTexture)
{
debug(LOG_ERROR, "Failed to load %s", loadPath.c_str());
return nullopt;
}
}

size_t page = pie_AddTexPage(pTexture, path.c_str(), textureType);
Expand Down

0 comments on commit ffbc79b

Please sign in to comment.