diff --git a/source/graphics/TextureConverter.cpp b/source/graphics/TextureConverter.cpp index 572a051512..237911d410 100644 --- a/source/graphics/TextureConverter.cpp +++ b/source/graphics/TextureConverter.cpp @@ -275,10 +275,19 @@ CTextureConverter::CTextureConverter(PIVFS vfs) : // to avoid bugs caused by ABI changes debug_assert(nvtt::version() >= NVTT_VERSION); - // Start up the worker thread - sem_init(&m_WorkerSem, 0, 0); - pthread_mutex_init(&m_WorkerMutex, NULL); - pthread_create(&m_WorkerThread, NULL, &RunThread, this); + // Set up the worker thread: + + int ret; + + // Use SDL semaphores since OS X doesn't implement sem_init + m_WorkerSem = SDL_CreateSemaphore(0); + debug_assert(m_WorkerSem); + + ret = pthread_mutex_init(&m_WorkerMutex, NULL); + debug_assert(ret == 0); + + ret = pthread_create(&m_WorkerThread, NULL, &RunThread, this); + debug_assert(ret == 0); // Maybe we should share some centralised pool of worker threads? // For now we'll just stick with a single thread for this specific use. @@ -292,10 +301,14 @@ CTextureConverter::~CTextureConverter() pthread_mutex_unlock(&m_WorkerMutex); // Wake it up so it sees the notification - sem_post(&m_WorkerSem); + SDL_SemPost(m_WorkerSem); // Wait for it to shut down cleanly pthread_join(m_WorkerThread, NULL); + + // Clean up resources + SDL_DestroySemaphore(m_WorkerSem); + pthread_mutex_destroy(&m_WorkerMutex); } bool CTextureConverter::ConvertTexture(const CTexturePtr& texture, const VfsPath& src, const VfsPath& dest, const Settings& settings) @@ -412,7 +425,7 @@ bool CTextureConverter::ConvertTexture(const CTexturePtr& texture, const VfsPath pthread_mutex_unlock(&m_WorkerMutex); // Wake up the worker thread - sem_post(&m_WorkerSem); + SDL_SemPost(m_WorkerSem); return true; } @@ -475,7 +488,7 @@ void* CTextureConverter::RunThread(void* data) CTextureConverter* textureConverter = static_cast(data); // Wait until the main thread wakes us up - while (sem_wait(&textureConverter->m_WorkerSem) == 0) + while (SDL_SemWait(textureConverter->m_WorkerSem) == 0) { pthread_mutex_lock(&textureConverter->m_WorkerMutex); if (textureConverter->m_Shutdown) diff --git a/source/graphics/TextureConverter.h b/source/graphics/TextureConverter.h index e1482f14cf..aebfac8db3 100644 --- a/source/graphics/TextureConverter.h +++ b/source/graphics/TextureConverter.h @@ -19,6 +19,7 @@ #define INCLUDED_TEXTURECONVERTER #include "lib/file/vfs/vfs.h" +#include "lib/external_libraries/sdl.h" #include "TextureManager.h" @@ -201,8 +202,8 @@ private: PIVFS m_VFS; pthread_t m_WorkerThread; - sem_t m_WorkerSem; pthread_mutex_t m_WorkerMutex; + SDL_sem* m_WorkerSem; struct ConversionRequest; struct ConversionResult; diff --git a/source/graphics/TextureManager.cpp b/source/graphics/TextureManager.cpp index f9a2ffb149..2848ddc517 100644 --- a/source/graphics/TextureManager.cpp +++ b/source/graphics/TextureManager.cpp @@ -75,6 +75,7 @@ public: // without needing to load any files // Default placeholder texture (grey) + if (!m_DisableGL) { // Construct 1x1 24-bit texture shared_ptr data(new u8[3], ArrayDeleter()); @@ -91,6 +92,7 @@ public: } // Error texture (magenta) + if (!m_DisableGL) { // Construct 1x1 24-bit texture shared_ptr data(new u8[3], ArrayDeleter()); @@ -156,6 +158,9 @@ public: */ void LoadTexture(const CTexturePtr& texture, const VfsPath& path) { + if (m_DisableGL) + return; + Handle h = ogl_tex_load(m_VFS, path, RES_UNIQUE); if (h <= 0) { @@ -547,12 +552,14 @@ CTexture::CTexture(Handle handle, const CTextureProperties& props, CTextureManag { // Add a reference to the handle (it might be shared by multiple CTextures // so we can't take ownership of it) - h_add_ref(m_Handle); + if (m_Handle) + h_add_ref(m_Handle); } CTexture::~CTexture() { - ogl_tex_free(m_Handle); + if (m_Handle) + ogl_tex_free(m_Handle); } void CTexture::Bind(size_t unit) diff --git a/source/graphics/tests/test_TextureManager.h b/source/graphics/tests/test_TextureManager.h index 1b97e0ae65..ea9c33ea46 100644 --- a/source/graphics/tests/test_TextureManager.h +++ b/source/graphics/tests/test_TextureManager.h @@ -76,8 +76,12 @@ public: } TS_ASSERT(t1->IsLoaded()); - TS_ASSERT_EQUALS(t1->GetWidth(), (size_t)64); - TS_ASSERT_EQUALS(t1->GetHeight(), (size_t)64); + + // We can't test sizes because we had to disable GL function calls + // and therefore couldn't load the texture. Maybe we should try loading + // the texture file directly, to make sure it's actually worked. +// TS_ASSERT_EQUALS(t1->GetWidth(), (size_t)64); +// TS_ASSERT_EQUALS(t1->GetHeight(), (size_t)64); // CreateTexture should return the same object CTexturePtr t2 = texman.CreateTexture(CTextureProperties(L"art/textures/a/demo.png"));