diff --git a/common/lc_stringcache.cpp b/common/lc_stringcache.cpp index 4748c82b..e30c2832 100644 --- a/common/lc_stringcache.cpp +++ b/common/lc_stringcache.cpp @@ -6,7 +6,7 @@ lcStringCache gStringCache; lcStringCache::lcStringCache() - : mTexture(nullptr), mRefCount(0) + : mTexture(nullptr) { } @@ -15,27 +15,17 @@ lcStringCache::~lcStringCache() delete mTexture; } -void lcStringCache::AddRef(lcContext* Context) +void lcStringCache::Initialize(lcContext* Context) { Q_UNUSED(Context); - mRefCount++; - - if (mRefCount == 1) - mTexture = new lcTexture(); + mTexture = new lcTexture(); } -void lcStringCache::Release(lcContext* Context) +void lcStringCache::Reset() { - mRefCount--; - - if (mRefCount == 0) - { - Context->UnbindTexture2D(mTexture->mTexture); // todo: unbind from all contexts - - delete mTexture; - mTexture = nullptr; - } + delete mTexture; + mTexture = nullptr; } void lcStringCache::CacheStrings(const QStringList& Strings) diff --git a/common/lc_stringcache.h b/common/lc_stringcache.h index 6eb6096a..24075810 100644 --- a/common/lc_stringcache.h +++ b/common/lc_stringcache.h @@ -11,8 +11,8 @@ public: lcStringCache(); ~lcStringCache(); - void AddRef(lcContext* Context); - void Release(lcContext* Context); + void Initialize(lcContext* Context); + void Reset(); void CacheStrings(const QStringList& Strings); void GetStringDimensions(int* cx, int* cy, const QString& String) const; @@ -20,7 +20,6 @@ public: protected: lcTexture* mTexture; - int mRefCount; std::map mStrings; }; diff --git a/common/texfont.cpp b/common/texfont.cpp index 71a8433e..761fa89e 100644 --- a/common/texfont.cpp +++ b/common/texfont.cpp @@ -93,16 +93,13 @@ TexFont gTexFont; TexFont::TexFont() { - mRefCount = 0; mTexture = 0; memset(&mGlyphs, 0, sizeof(mGlyphs)); } -bool TexFont::Load(lcContext* Context) +bool TexFont::Initialize(lcContext* Context) { - mRefCount++; - - if (mRefCount != 1) + if (mTexture) return true; mFontHeight = 16; @@ -151,14 +148,9 @@ bool TexFont::Load(lcContext* Context) return true; } -void TexFont::Release() +void TexFont::Reset() { - mRefCount--; - if (mRefCount == 0) - { - glDeleteTextures(1, &mTexture); - mTexture = 0; - } + mTexture = 0; } void TexFont::GetStringDimensions(int* cx, int* cy, const char* Text) const diff --git a/common/texfont.h b/common/texfont.h index 40b3a694..e970226b 100644 --- a/common/texfont.h +++ b/common/texfont.h @@ -15,8 +15,8 @@ public: return mTexture; } - bool Load(lcContext* Context); - void Release(); + bool Initialize(lcContext* Context); + void Reset(); void PrintText(lcContext* Context, float Left, float Top, float Z, const char* Text) const; void GetTriangles(const lcMatrix44& Transform, const char* Text, float* Buffer) const; @@ -34,7 +34,6 @@ protected: int mTextureWidth; int mTextureHeight; int mFontHeight; - int mRefCount; }; extern TexFont gTexFont; diff --git a/qt/lc_qglwidget.cpp b/qt/lc_qglwidget.cpp index 89a30286..dd8a16bf 100644 --- a/qt/lc_qglwidget.cpp +++ b/qt/lc_qglwidget.cpp @@ -96,11 +96,12 @@ lcQGLWidget::lcQGLWidget(QWidget *parent, lcGLWidget *owner, bool view) widget->MakeCurrent(); - // TODO: Find a better place for the grid texture and font - gStringCache.AddRef(widget->mContext); - gTexFont.Load(widget->mContext); if (gWidgetList.isEmpty()) { + // TODO: Find a better place for the grid texture and font + gStringCache.Initialize(widget->mContext); + gTexFont.Initialize(widget->mContext); + lcInitializeGLExtensions(context()); lcContext::CreateResources(); View::CreateResources(widget->mContext); @@ -115,6 +116,7 @@ lcQGLWidget::lcQGLWidget(QWidget *parent, lcGLWidget *owner, bool view) gPlaceholderMesh = new lcMesh; gPlaceholderMesh->CreateBox(); } + gWidgetList.append(this); widget->OnInitialUpdate(); @@ -133,11 +135,12 @@ lcQGLWidget::lcQGLWidget(QWidget *parent, lcGLWidget *owner, bool view) lcQGLWidget::~lcQGLWidget() { gWidgetList.removeOne(this); - gStringCache.Release(widget->mContext); - gTexFont.Release(); - makeCurrent(); + if (gWidgetList.isEmpty()) { + gStringCache.Reset(); + gTexFont.Reset(); + lcGetPiecesLibrary()->ReleaseBuffers(widget->mContext); View::DestroyResources(widget->mContext); lcContext::DestroyResources();