From 89dc42c3520c2f081e5aa94d2b10eaff0a2bc5dc Mon Sep 17 00:00:00 2001 From: spartanj Date: Tue, 8 Mar 2011 00:55:15 -0300 Subject: [PATCH] Added Unload to the object loaders and resource loader. --- src/audio/tsoundloader.hpp | 11 ++++ src/base.hpp | 7 +-- src/ee.h | 5 +- src/graphics/ctexturefontloader.cpp | 18 ++++++ src/graphics/ctexturefontloader.hpp | 8 ++- src/graphics/ctextureloader.cpp | 24 ++++++++ src/graphics/ctextureloader.hpp | 4 ++ src/graphics/cttffontloader.cpp | 15 +++++ src/graphics/cttffontloader.hpp | 6 +- src/system/cobjectloader.cpp | 5 ++ src/system/cobjectloader.hpp | 4 ++ src/system/cresourceloader.cpp | 12 ++++ src/system/cresourceloader.hpp | 2 + src/utils/cperlinnoise.cpp | 95 ++++++++++++++++------------- 14 files changed, 163 insertions(+), 53 deletions(-) diff --git a/src/audio/tsoundloader.hpp b/src/audio/tsoundloader.hpp index 9dd44b635..cb2467f75 100644 --- a/src/audio/tsoundloader.hpp +++ b/src/audio/tsoundloader.hpp @@ -27,6 +27,8 @@ class EE_API tSoundLoader : public cObjectLoader { void Update(); + void Unload(); + const T& Id() const; protected: Uint32 mLoadType; @@ -159,6 +161,15 @@ const T& tSoundLoader::Id() const { return mId; } +template +void tSoundLoader::Unload() { + if ( mLoaded ) { + mSndMngr->Remove( mId ); + + Reset(); + } +} + typedef tSoundLoader cSoundLoader; }} diff --git a/src/base.hpp b/src/base.hpp index 4b87ae162..c7eef5a5b 100644 --- a/src/base.hpp +++ b/src/base.hpp @@ -4,10 +4,7 @@ #include #include #include -#include #include -#include -#include #include #include @@ -16,12 +13,12 @@ #include #include + #include #include +#include #include #include -#include -#include #include #include diff --git a/src/ee.h b/src/ee.h index c7e3305bf..ca88a74a6 100755 --- a/src/ee.h +++ b/src/ee.h @@ -25,8 +25,11 @@ @TODO Check for endianness problems, and make EEPP endianness agnostic. @TODO Add backend for SDL 1.3 ( support for Android ). And may be SFML backend ( just for fun ). @TODO Support for Android and iOS. - @TODO Support color cursors - Imposible with SDL 1.2. Allegro 5 have this implemented ( could implement this for the Allegro backend at least - DONE ). + @TODO Support color cursors \n + SDL 1.2 Not even posible ( win32 and x11 backends for this ready ) \n + Allegro 5 DONE @TODO Add Scripting support ( squirrel or python ). + @TODO Fix classes bad padding, optimize memory consumption. */ // General includes and declarations diff --git a/src/graphics/ctexturefontloader.cpp b/src/graphics/ctexturefontloader.cpp index 5666211a1..7834b03dc 100644 --- a/src/graphics/ctexturefontloader.cpp +++ b/src/graphics/ctexturefontloader.cpp @@ -1,4 +1,5 @@ #include "ctexturefontloader.hpp" +#include "cfontmanager.hpp" namespace EE { namespace Graphics { @@ -131,4 +132,21 @@ cFont * cTextureFontLoader::Font() const { return mFont; } +void cTextureFontLoader::Unload() { + if ( mLoaded ) { + mTexLoader->Unload(); + + cFontManager::instance()->Remove( mFont ); + + Reset(); + } +} + +void cTextureFontLoader::Reset() { + cObjectLoader::Reset(); + + mTexLoaded = false; + mFontLoaded = false; +} + }} diff --git a/src/graphics/ctexturefontloader.hpp b/src/graphics/ctexturefontloader.hpp index feaea6e2d..b02c2428b 100644 --- a/src/graphics/ctexturefontloader.hpp +++ b/src/graphics/ctexturefontloader.hpp @@ -28,13 +28,15 @@ class EE_API cTextureFontLoader : public cObjectLoader { void Update(); - const std::string& Id() const; + void Unload(); + + const std::string& Id() const; cFont * Font() const; protected: Uint32 mLoadType; // From memory, from path, from pack - cTextureFont * mFont; + cTextureFont * mFont; std::string mFontName; cTextureLoader * mTexLoader; @@ -54,6 +56,8 @@ class EE_API cTextureFontLoader : public cObjectLoader { Uint32 mDataSize; void Start(); + + void Reset(); private: bool mTexLoaded; bool mFontLoaded; diff --git a/src/graphics/ctextureloader.cpp b/src/graphics/ctextureloader.cpp index 513cab24a..6fae12505 100644 --- a/src/graphics/ctextureloader.cpp +++ b/src/graphics/ctextureloader.cpp @@ -277,5 +277,29 @@ cTexture * cTextureLoader::GetTexture() const { return NULL; } +void cTextureLoader::Unload() { + if ( mLoaded ) { + cTextureFactory::instance()->Remove( mTexId ); + + Reset(); + } +} + +void cTextureLoader::Reset() { + cObjectLoader::Reset(); + + mPixels = NULL; + mTexId = 0; + mImgWidth = 0; + mImgHeight = 0; + mWidth = 0; + mHeight = 0; + mChannels = 0; + mSize = 0; + mTexLoaded = false; + mIsDDS = false; + mIsDDSCompressed = 0; +} + }} diff --git a/src/graphics/ctextureloader.hpp b/src/graphics/ctextureloader.hpp index e966f0ee6..5ecac60db 100644 --- a/src/graphics/ctextureloader.hpp +++ b/src/graphics/ctextureloader.hpp @@ -29,6 +29,8 @@ class EE_API cTextureLoader : public cObjectLoader { void Update(); + void Unload(); + const std::string& Filepath() const; const Uint32& Id() const; @@ -57,6 +59,8 @@ class EE_API cTextureLoader : public cObjectLoader { eeColor * mColorKey; void Start(); + + void Reset(); private: bool mTexLoaded; bool mIsDDS; diff --git a/src/graphics/cttffontloader.cpp b/src/graphics/cttffontloader.cpp index 3adf3da24..023f5c560 100644 --- a/src/graphics/cttffontloader.cpp +++ b/src/graphics/cttffontloader.cpp @@ -1,4 +1,5 @@ #include "cttffontloader.hpp" +#include "cfontmanager.hpp" namespace EE { namespace Graphics { @@ -111,4 +112,18 @@ cFont * cTTFFontLoader::Font() const { return mFont; } +void cTTFFontLoader::Unload() { + if ( mLoaded ) { + cFontManager::instance()->Remove( mFont ); + + Reset(); + } +} + +void cTTFFontLoader::Reset() { + cObjectLoader::Reset(); + + mFontLoaded = false; +} + }} diff --git a/src/graphics/cttffontloader.hpp b/src/graphics/cttffontloader.hpp index 27251e256..65da2679b 100644 --- a/src/graphics/cttffontloader.hpp +++ b/src/graphics/cttffontloader.hpp @@ -24,7 +24,9 @@ class EE_API cTTFFontLoader : public cObjectLoader { void Update(); - const std::string& Id() const; + void Unload(); + + const std::string& Id() const; cFont * Font() const; protected: @@ -47,6 +49,8 @@ class EE_API cTTFFontLoader : public cObjectLoader { eeUint mDataSize; void Start(); + + void Reset(); private: bool mFontLoaded; diff --git a/src/system/cobjectloader.cpp b/src/system/cobjectloader.cpp index 92543d176..477249095 100644 --- a/src/system/cobjectloader.cpp +++ b/src/system/cobjectloader.cpp @@ -81,4 +81,9 @@ const Uint32& cObjectLoader::Type() const { return mObjType; } +void cObjectLoader::Reset() { + mLoaded = false; + mLoading = false; +} + }} diff --git a/src/system/cobjectloader.hpp b/src/system/cobjectloader.hpp index 36f19d9ef..c757a988d 100644 --- a/src/system/cobjectloader.hpp +++ b/src/system/cobjectloader.hpp @@ -26,6 +26,8 @@ class EE_API cObjectLoader : cThread { void Load( ObjLoadCallback Cb ); + virtual void Unload() = 0; + virtual void Update(); void Launch(); @@ -50,6 +52,8 @@ class EE_API cObjectLoader : cThread { virtual void Start(); virtual void SetLoaded(); + + virtual void Reset(); private: virtual void Run(); }; diff --git a/src/system/cresourceloader.cpp b/src/system/cresourceloader.cpp index a3acf510f..85d2a7676 100644 --- a/src/system/cresourceloader.cpp +++ b/src/system/cresourceloader.cpp @@ -130,6 +130,18 @@ void cResourceLoader::Update() { Load(); } +void cResourceLoader::Unload() { + if ( mLoaded ) { + std::list::iterator it; + + for ( it = mObjs.begin(); it != mObjs.end(); it++ ) { + (*it)->Unload(); + } + + mLoaded = false; + } +} + bool cResourceLoader::IsLoaded() { return mLoaded; } diff --git a/src/system/cresourceloader.hpp b/src/system/cresourceloader.hpp index 52560c444..12e3e302f 100644 --- a/src/system/cresourceloader.hpp +++ b/src/system/cresourceloader.hpp @@ -23,6 +23,8 @@ class EE_API cResourceLoader { void Load(); + void Unload(); + virtual void Update(); virtual bool IsLoaded(); diff --git a/src/utils/cperlinnoise.cpp b/src/utils/cperlinnoise.cpp index b4ea84d20..bc116f030 100755 --- a/src/utils/cperlinnoise.cpp +++ b/src/utils/cperlinnoise.cpp @@ -9,77 +9,84 @@ cPerlinNoise::cPerlinNoise() { cPerlinNoise::~cPerlinNoise() {} void cPerlinNoise::Init() { - mPersistence = 0.25f; - mOctaves = 4; - mFrequency = 0.015f; - mAmplitude = 1; - mFreqOctaveDep = false; - mAmpOctaveDep = false; + mPersistence = 0.25f; + mOctaves = 4; + mFrequency = 0.015f; + mAmplitude = 1; + mFreqOctaveDep = false; + mAmpOctaveDep = false; } eeFloat cPerlinNoise::PerlinNoise2D(eeFloat x, eeFloat y) { - eeFloat total = 0; - eeFloat p = mPersistence; - eeFloat n = (eeFloat)mOctaves - 1; + eeFloat total = 0; + eeFloat p = mPersistence; + eeFloat n = static_cast( mOctaves - 1 ); eeFloat tmpFreq = mFrequency; - eeFloat amp = mAmplitude; + eeFloat amp = mAmplitude; + mCurrSeed = 1; - mCurrSeed = 1; - - for (Int32 i=0; i <= n; i++) { - if (mFreqOctaveDep) + for ( Int32 i = 0; i <= n; i++ ) { + if ( mFreqOctaveDep ) { tmpFreq *= 2; - if (mAmpOctaveDep) - amp *= p; + } - total = total + InterpolatedNoise2D(x * tmpFreq, y * tmpFreq) * amp; + if ( mAmpOctaveDep ) { + amp *= p; + } + + total = total + InterpolatedNoise2D( x * tmpFreq, y * tmpFreq ) * amp; } return total; } -eeFloat cPerlinNoise::Noise2D(Int32 x, Int32 y) { - Int32 n; - n = x + y * 57; - n = (n<<13) ^ n; - eeFloat res = (eeFloat)( 1.0 - ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff ) / 1073741824.0); - return res; +eeFloat cPerlinNoise::Noise2D( Int32 x, Int32 y ) { + Int32 n = x + y * 57; + + n = ( n << 13 ) ^ n; + + n = ( (n * (n * n * 15731 + 789221) + 1376312589) & 0x7fffffff ); + + return static_cast( 1.0 - static_cast( n ) / 1073741824.0); } eeFloat cPerlinNoise::SmoothedNoise2D(eeFloat x, eeFloat y) { - eeFloat corners = ( Noise2D( (Int32)(x-1), (Int32)(y-1) ) + Noise2D( (Int32)(x+1), (Int32)(y-1) )+Noise2D( (Int32)(x-1), (Int32)(y+1) ) + Noise2D( (Int32)(x+1), (Int32)(y+1) ) ) / 16; - eeFloat sides = ( Noise2D( (Int32)(x-1), (Int32)y) + Noise2D((Int32)x+1, (Int32)y) + Noise2D( (Int32)x, (Int32)(y-1) ) + Noise2D( (Int32)x, (Int32)(y+1) ) ) / 8; - eeFloat center = Noise2D((Int32)x, (Int32)y) / 4; + register Int32 tx = static_cast( x ); + register Int32 ty = static_cast( y ); + + eeFloat corners = ( Noise2D( tx - 1, ty - 1 ) + Noise2D( tx + 1, ty - 1 ) + Noise2D( tx - 1 , ty + 1 ) + Noise2D( tx + 1, ty + 1 ) ) / 16; + eeFloat sides = ( Noise2D( tx - 1, ty ) + Noise2D( tx + 1, ty ) + Noise2D( tx , ty - 1 ) + Noise2D( tx , ty + 1 ) ) / 8; + eeFloat center = Noise2D( tx, ty ) / 4; + return corners + sides + center; } -eeFloat cPerlinNoise::Interpolate(eeFloat a, eeFloat b, eeFloat x) { - eeFloat fac1 = 3*eepow(1-x, 2) - 2*eepow(1-x,3); - eeFloat fac2 = 3*eepow(x, 2) - 2*eepow(x, 3); - - return a*fac1 + b*fac2; //add the weighted factors +eeFloat cPerlinNoise::Interpolate( eeFloat a, eeFloat b, eeFloat x ) { + eeFloat fac1 = 3 * eepow( 1 - x, 2 ) - 2 * eepow( 1 - x, 3 ); + eeFloat fac2 = 3 * eepow( x, 2 ) - 2 * eepow( x, 3 ); + return a * fac1 + b * fac2; //add the weighted factors } eeFloat cPerlinNoise::InterpolatedNoise2D(eeFloat x, eeFloat y) { - Int32 Int32eger_X = Int32(x); - eeFloat fractional_X = x - Int32eger_X; + Int32 eger_X = static_cast( x ); + Int32 eger_Y = static_cast( y ); - Int32 Int32eger_Y = Int32(y); - eeFloat fractional_Y = y - Int32eger_Y; + eeFloat fractional_X = x - eger_X; + eeFloat fractional_Y = y - eger_Y; - eeFloat feger_X = (eeFloat)Int32eger_X; - eeFloat feger_Y = (eeFloat)Int32eger_Y; + eeFloat feger_X = static_cast ( eger_X ); + eeFloat feger_Y = static_cast ( eger_Y ); - eeFloat v1 = SmoothedNoise2D(feger_X, feger_Y); - eeFloat v2 = SmoothedNoise2D(feger_X + 1.f, feger_Y); - eeFloat v3 = SmoothedNoise2D(feger_X, feger_Y + 1.f); - eeFloat v4 = SmoothedNoise2D(feger_X + 1.f, feger_Y + 1.f); + eeFloat v1 = SmoothedNoise2D( feger_X , feger_Y ); + eeFloat v2 = SmoothedNoise2D( feger_X + 1.f , feger_Y ); + eeFloat v3 = SmoothedNoise2D( feger_X , feger_Y + 1.f ); + eeFloat v4 = SmoothedNoise2D( feger_X + 1.f , feger_Y + 1.f ); - eeFloat i1 = Interpolate(v1 , v2 , fractional_X); - eeFloat i2 = Interpolate(v3 , v4 , fractional_X); + eeFloat i1 = Interpolate( v1 , v2 , fractional_X ); + eeFloat i2 = Interpolate( v3 , v4 , fractional_X ); - return Interpolate(i1 , i2 , fractional_Y); + return Interpolate( i1 , i2 , fractional_Y ); } }}