diff --git a/app/main.cpp b/app/main.cpp index 8aec92f..41f3366 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -8,9 +8,9 @@ #include #include +#include #include -#include int main(int argc, char *argv[]) diff --git a/src/resourcemanager.h b/src/resourcemanager.h index 7189677..8246a0c 100644 --- a/src/resourcemanager.h +++ b/src/resourcemanager.h @@ -24,10 +24,9 @@ public: * Should be given in qrc format: ':/path/to/asset.obj' * @return Absoulte system path to a qtk asset */ - static std::string getPath(const std::string & path) { - // Only construct qtk resource path if in qrc format; else return it as-is - return path[0] == ':' ? resourcesDir + path.substr(2) : path; - } + static std::string getPath(const std::string & path) + // Only construct qtk resource path if in qrc format; else return it as-is + { return path[0] == ':' ? resourcesDir + path.substr(2) : path; } static std::string resourcesDir; } RM; diff --git a/src/scene.cpp b/src/scene.cpp index a83974a..bf1c709 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -515,6 +515,9 @@ void Scene::init() mMeshes.back()->mProgram.bind(); mMeshes.back()->setTexture(":/crate.png"); + + // Test assignment of Texture + mMeshes.back()->mTexture = Texture(":/crate.png"); mMeshes.back()->setUniform("uTexture", 0); mMeshes.back()->mVAO.bind(); diff --git a/src/texture.cpp b/src/texture.cpp index 65df2e3..721eb01 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -11,6 +11,8 @@ #include +std::unordered_map TM::textureMap; + QImage * OpenGLTextureFactory::initImage(const char * image, bool flipX, bool flipY) { // Qt6 limits loaded images to 256MB by default diff --git a/src/texture.h b/src/texture.h index d2f2f82..192b7df 100644 --- a/src/texture.h +++ b/src/texture.h @@ -38,23 +38,47 @@ private: OpenGLTextureFactory() = default; }; +class Texture; +typedef class TextureManager { +public: + static inline Texture * getInstance(std::string path) + { return textureMap.count(path) > 0 ? textureMap[path] : Q_NULLPTR; } + static inline bool addInstance(const std::string & path, Texture * texture) + { return textureMap.emplace(path, texture).second ? true : false; } + +private: + static std::unordered_map textureMap; +} TM; + class Texture { +// explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) { } public: Texture() = default; - Texture(const Texture & value) = delete; -// Texture(const Texture & value) { -// mOpenGLTexture = OpenGLTextureFactory::initTexture2D(value.mPath); -// mPath = value.mPath; -// } + Texture(const Texture & value) { + if (TM::getInstance(value.mPath)) { + mOpenGLTexture = TM::getInstance(value.mPath)->mOpenGLTexture; + } + else { + mOpenGLTexture = OpenGLTextureFactory::initTexture2D(value.mPath); + TM::addInstance(value.mPath, this); + } + mPath = value.mPath; + } explicit Texture(const char * path, bool flipX=false, bool flipY=false) : - mOpenGLTexture(OpenGLTextureFactory::initTexture2D(path, flipX, flipY)), - mPath(path) { } -// explicit Texture(QOpenGLTexture * texture) : mOpenGLTexture(texture) { } - ~Texture() { mOpenGLTexture->destroy();} + mPath(path) { + if (TM::getInstance(path) == Q_NULLPTR) { + mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY); + TM::addInstance(path, this); + } + else { + mOpenGLTexture = TM::getInstance(path)->mOpenGLTexture; + } + } +// ~Texture() { if (mOpenGLTexture) mOpenGLTexture->destroy();} inline QOpenGLTexture & getOpenGLTexture() { return *mOpenGLTexture;} - void setTexture(const char * path, bool flipX=false, bool flipY=false) + inline void setTexture(const char * path, bool flipX=false, bool flipY=false) { mOpenGLTexture = OpenGLTextureFactory::initTexture2D(path, flipX, flipY);} inline void setTexture(QOpenGLTexture * texture) { mOpenGLTexture = texture;} @@ -64,5 +88,4 @@ public: const char * mPath; }; - #endif // QTOPENGL_TEXTURE_H