From 27dec2fbf8a4e722d841afb0e96ef049e632ce99 Mon Sep 17 00:00:00 2001 From: hiker Date: Thu, 26 Mar 2015 11:28:22 +1100 Subject: [PATCH] Added attribute to irrlicht's driver to enable automatic resize of images that are too big. This is then used to automatically resize any textures (except fonts, background screen) to be automatically resized to 512x512 if they are bigger. --- lib/irrlicht/include/IAttributes.h | 4 ++-- lib/irrlicht/include/IVideoDriver.h | 2 ++ lib/irrlicht/source/Irrlicht/CAttributes.cpp | 4 ++-- lib/irrlicht/source/Irrlicht/CAttributes.h | 4 ++-- lib/irrlicht/source/Irrlicht/CNullDriver.cpp | 5 ++++ lib/irrlicht/source/Irrlicht/CNullDriver.h | 3 +++ .../source/Irrlicht/COpenGLTexture.cpp | 12 ++++++++++ src/graphics/2dutils.cpp | 2 +- src/graphics/irr_driver.cpp | 15 +++++++++++- src/graphics/irr_driver.hpp | 2 +- src/guiengine/scalable_font.cpp | 24 +++++++++++++------ src/main.cpp | 4 ++++ 12 files changed, 65 insertions(+), 16 deletions(-) diff --git a/lib/irrlicht/include/IAttributes.h b/lib/irrlicht/include/IAttributes.h index f1376358e..0361ea125 100644 --- a/lib/irrlicht/include/IAttributes.h +++ b/lib/irrlicht/include/IAttributes.h @@ -501,11 +501,11 @@ public: //! Gets an attribute as dimension2d //! \param attributeName: Name of the attribute to get. //! \return Returns value of the attribute previously set by setAttribute() - virtual core::dimension2d getAttributeAsDimension2d(const c8* attributeName) = 0; + virtual core::dimension2d getAttributeAsDimension2d(const c8* attributeName) const = 0; //! Gets an attribute as dimension2d //! \param index: Index value, must be between 0 and getAttributeCount()-1. - virtual core::dimension2d getAttributeAsDimension2d(s32 index) = 0; + virtual core::dimension2d getAttributeAsDimension2d(s32 index) const = 0; //! Sets an attribute as dimension2d virtual void setAttribute(s32 index, core::dimension2d v) = 0; diff --git a/lib/irrlicht/include/IVideoDriver.h b/lib/irrlicht/include/IVideoDriver.h index 79b1b2002..3f3851425 100644 --- a/lib/irrlicht/include/IVideoDriver.h +++ b/lib/irrlicht/include/IVideoDriver.h @@ -324,6 +324,8 @@ namespace video AntiAlias (int) Number of Samples the driver uses for each pixel. 0 and 1 means anti aliasing is off, typical values are 2,4,8,16,32 */ virtual const io::IAttributes& getDriverAttributes() const=0; + //! Non-const version (with a different name to avoid involuntary mistakes). */ + virtual io::IAttributes& getNonConstDriverAttributes() = 0; //! Check if the driver was recently reset. /** For d3d devices you will need to recreate the RTTs if the diff --git a/lib/irrlicht/source/Irrlicht/CAttributes.cpp b/lib/irrlicht/source/Irrlicht/CAttributes.cpp index 21ebe1f1c..71068ca60 100644 --- a/lib/irrlicht/source/Irrlicht/CAttributes.cpp +++ b/lib/irrlicht/source/Irrlicht/CAttributes.cpp @@ -429,7 +429,7 @@ void CAttributes::setAttribute(const c8* attributeName, core::dimension2d v //! Gets an attribute as dimension2d //! \param attributeName: Name of the attribute to get. //! \return Returns value of the attribute previously set by setAttribute() -core::dimension2d CAttributes::getAttributeAsDimension2d(const c8* attributeName) +core::dimension2d CAttributes::getAttributeAsDimension2d(const c8* attributeName) const { IAttribute* att = getAttributeP(attributeName); if (att) @@ -737,7 +737,7 @@ core::rect CAttributes::getAttributeAsRect(s32 index) //! Gets an attribute as dimension2d //! \param index: Index value, must be between 0 and getAttributeCount()-1. -core::dimension2d CAttributes::getAttributeAsDimension2d(s32 index) +core::dimension2d CAttributes::getAttributeAsDimension2d(s32 index) const { if ((u32)index < Attributes.size()) return Attributes[index]->getDimension2d(); diff --git a/lib/irrlicht/source/Irrlicht/CAttributes.h b/lib/irrlicht/source/Irrlicht/CAttributes.h index 1675a3ab3..3e597a47b 100644 --- a/lib/irrlicht/source/Irrlicht/CAttributes.h +++ b/lib/irrlicht/source/Irrlicht/CAttributes.h @@ -465,11 +465,11 @@ public: //! Gets an attribute as dimension2d //! \param attributeName: Name of the attribute to get. //! \return Returns value of the attribute previously set by setAttribute() - virtual core::dimension2d getAttributeAsDimension2d(const c8* attributeName); + virtual core::dimension2d getAttributeAsDimension2d(const c8* attributeName) const; //! Gets an attribute as dimension2d //! \param index: Index value, must be between 0 and getAttributeCount()-1. - virtual core::dimension2d getAttributeAsDimension2d(s32 index); + virtual core::dimension2d getAttributeAsDimension2d(s32 index) const; //! Sets an attribute as dimension2d virtual void setAttribute(s32 index, core::dimension2d v); diff --git a/lib/irrlicht/source/Irrlicht/CNullDriver.cpp b/lib/irrlicht/source/Irrlicht/CNullDriver.cpp index 11144c114..4ef9c44de 100644 --- a/lib/irrlicht/source/Irrlicht/CNullDriver.cpp +++ b/lib/irrlicht/source/Irrlicht/CNullDriver.cpp @@ -298,6 +298,11 @@ const io::IAttributes& CNullDriver::getDriverAttributes() const return *DriverAttributes; } +//! Get attributes of the actual video driver +io::IAttributes& CNullDriver::getNonConstDriverAttributes() +{ + return *DriverAttributes; +} //! sets transformation void CNullDriver::setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat) diff --git a/lib/irrlicht/source/Irrlicht/CNullDriver.h b/lib/irrlicht/source/Irrlicht/CNullDriver.h index c2fb5cc16..251062b68 100644 --- a/lib/irrlicht/source/Irrlicht/CNullDriver.h +++ b/lib/irrlicht/source/Irrlicht/CNullDriver.h @@ -64,6 +64,9 @@ namespace video //! Get attributes of the actual video driver const io::IAttributes& getDriverAttributes() const; + //! Non-const version (with a different name to avoid involuntary mistakes). */ + virtual io::IAttributes& getNonConstDriverAttributes(); + //! sets transformation virtual void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat); diff --git a/lib/irrlicht/source/Irrlicht/COpenGLTexture.cpp b/lib/irrlicht/source/Irrlicht/COpenGLTexture.cpp index 72a750a50..ef20da830 100644 --- a/lib/irrlicht/source/Irrlicht/COpenGLTexture.cpp +++ b/lib/irrlicht/source/Irrlicht/COpenGLTexture.cpp @@ -11,6 +11,8 @@ #include "COpenGLDriver.h" #include "os.h" #include "CColorConverter.h" +#include "IAttributes.h" +#include "IrrlichtDevice.h" #include "irrString.h" @@ -314,7 +316,17 @@ void COpenGLTexture::getImageValues(IImage* image) ImageSize.Width = (u32)(Driver->MaxTextureSize*ratio); } TextureSize=ImageSize.getOptimalSize(!Driver->queryFeature(EVDF_TEXTURE_NPOT)); + const core::dimension2du max_size = Driver->getDriverAttributes() + .getAttributeAsDimension2d("MAX_TEXTURE_SIZE"); + if (max_size.Width> 0 && TextureSize.Width > max_size.Width) + { + TextureSize.Width = max_size.Width; + } + if (max_size.Height> 0 && TextureSize.Height > max_size.Height) + { + TextureSize.Height = max_size.Height; + } ColorFormat = getBestColorFormat(image->getColorFormat()); } diff --git a/src/graphics/2dutils.cpp b/src/graphics/2dutils.cpp index 537ff43b8..386f1ac24 100644 --- a/src/graphics/2dutils.cpp +++ b/src/graphics/2dutils.cpp @@ -206,7 +206,7 @@ void draw2DImage(const video::ITexture* texture, const core::rect& destRect tex_width, tex_height, tex_center_pos_x, tex_center_pos_y; - getSize(texture->getOriginalSize().Width, texture->getOriginalSize().Height, texture->isRenderTarget(), + getSize(texture->getSize().Width, texture->getSize().Height, texture->isRenderTarget(), destRect, sourceRect, width, height, center_pos_x, center_pos_y, tex_width, tex_height, tex_center_pos_x, tex_center_pos_y); diff --git a/src/graphics/irr_driver.cpp b/src/graphics/irr_driver.cpp index 3db7aebb3..e8a6b7232 100644 --- a/src/graphics/irr_driver.cpp +++ b/src/graphics/irr_driver.cpp @@ -592,10 +592,23 @@ void IrrDriver::initDevice() m_pointer_shown = true; } // initDevice +// ---------------------------------------------------------------------------- +void IrrDriver::setMaxTextureSize() +{ + if( (UserConfigParams::m_high_definition_textures & 0x01) == 0) + { + io::IAttributes &att = m_video_driver->getNonConstDriverAttributes(); + att.setAttribute("MAX_TEXTURE_SIZE", core::dimension2du(512, 512)); + } +} // setMaxTextureSize + +// ---------------------------------------------------------------------------- void IrrDriver::cleanSunInterposer() { delete m_sun_interposer; -} +} // cleanSunInterposer + +// ---------------------------------------------------------------------------- void IrrDriver::createSunInterposer() { scene::IMesh * sphere = m_scene_manager->getGeometryCreator()->createSphereMesh(1, 16, 16); diff --git a/src/graphics/irr_driver.hpp b/src/graphics/irr_driver.hpp index b04b380bc..6d9097b83 100644 --- a/src/graphics/irr_driver.hpp +++ b/src/graphics/irr_driver.hpp @@ -36,7 +36,6 @@ #include #include "IrrlichtDevice.h" #include "ISkinnedMesh.h" -//#include "graphics/rtts.hpp" #include "graphics/shaders.hpp" #include "graphics/wind.hpp" #include "io/file_manager.hpp" @@ -354,6 +353,7 @@ public: ~IrrDriver(); void initDevice(); void reset(); + void setMaxTextureSize(); void getOpenGLData(std::string *vendor, std::string *renderer, std::string *version); diff --git a/src/guiengine/scalable_font.cpp b/src/guiengine/scalable_font.cpp index 980fcd836..381abdb06 100644 --- a/src/guiengine/scalable_font.cpp +++ b/src/guiengine/scalable_font.cpp @@ -4,16 +4,17 @@ #include "guiengine/scalable_font.hpp" -#include -#include -#include -#include -#include - +#include "graphics/2dutils.hpp" #include "guiengine/engine.hpp" #include "io/file_manager.hpp" #include "utils/translation.hpp" -#include "graphics/2dutils.hpp" + +#include +#include +#include +#include +#include +#include namespace irr { @@ -732,7 +733,16 @@ void ScalableFont::lazyLoadTexture(int texID) // load texture assert(m_texture_files[texID].m_file_name.size() > 0); + + // Font textures can not be resized (besides the impact on quality in + // this case, the indices in the xml files would become wrong). + core::dimension2du old_max_size = Driver->getDriverAttributes() + .getAttributeAsDimension2d("MAX_TEXTURE_SIZE"); + Driver->getNonConstDriverAttributes().setAttribute("MAX_TEXTURE_SIZE", + core::dimension2du(0, 0)); + SpriteBank->setTexture(texID, Driver->getTexture( m_texture_files[texID].m_file_name )); + Driver->getNonConstDriverAttributes().setAttribute("MAX_TEXTURE_SIZE", old_max_size); // set previous mip-map+filter state //Driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, mipmap); diff --git a/src/main.cpp b/src/main.cpp index 4b43fc2fb..01b8ed5fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1128,6 +1128,10 @@ void initRest() powerup_manager = new PowerupManager (); attachment_manager = new AttachmentManager (); highscore_manager = new HighscoreManager (); + + // The maximum texture size can not be set earlier, since + // e.g. the background image needs to be loaded in high res. + irr_driver->setMaxTextureSize(); KartPropertiesManager::addKartSearchDir( file_manager->getAddonsFile("karts/")); track_manager->addTrackSearchDir(