From fae20a1e0eef38f8b2c1d1a4725a5283fddf0f6d Mon Sep 17 00:00:00 2001 From: spartanj Date: Sun, 12 Dec 2010 05:10:15 -0300 Subject: [PATCH] Fixed SetIcon function. --- src/window/cengine.cpp | 60 ++++++++++++++++++++++++++++++------------ src/window/cengine.hpp | 4 ++- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/window/cengine.cpp b/src/window/cengine.cpp index e33b4c28f..69fbfb855 100755 --- a/src/window/cengine.cpp +++ b/src/window/cengine.cpp @@ -12,6 +12,7 @@ #include "../audio/caudiolistener.hpp" #include "../graphics/glhelper.hpp" #include "../helper/haikuttf/hkfontmanager.hpp" +#include "../helper/SOIL/stb_image.h" using namespace EE::Graphics; using namespace EE::Graphics::Private; @@ -143,6 +144,12 @@ bool cEngine::Init(const Uint32& Width, const Uint32& Height, const Uint8& BitCo return false; } + if ( "" != mIcon ) { + mInit = true; + SetIcon( mIcon ); + mInit = false; + } + mVideoInfo.Flags = SDL_OPENGL | SDL_HWPALETTE; const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo(); @@ -235,7 +242,9 @@ bool cEngine::Init(const Uint32& Width, const Uint32& Height, const Uint8& BitCo SetWindowCaption("EEPP"); cLog::instance()->Write( "Engine Initialized Succesfully.\nGL Vendor: " + GetVendor() + "\nGL Renderer: " + GetRenderer() + "\nGL Version: " + GetVersion() ); + mInit = true; + return true; } catch (...) { cLog::instance()->Write( "Error on cEngine::Init" ); @@ -618,17 +627,29 @@ void cEngine::SetCursor( const Uint32& TexId, const eeVector2i& HotSpot ) { SDL_SetCursor( mCursor ); } -bool cEngine::SetIcon( const Uint32& FromTexId ) { - cTexture* Tex = cTextureFactory::instance()->GetTexture( FromTexId ); +bool cEngine::SetIcon( const std::string& Path ) { + int x, y, c; - if ( NULL != Tex ) { - Int32 W = static_cast( Tex->Width() ); - Int32 H = static_cast( Tex->Height() ); + if ( !FileExists( Path ) ) + return false; + + if ( !mInit ) { + if ( stbi_info( Path.c_str(), &x, &y, &c ) ) { + mIcon = Path; + + return true; + } + + return false; + } + + unsigned char * Ptr = stbi_load( Path.c_str(), &x, &y, &c, 0 ); + + if ( NULL != Ptr ) { + Int32 W = x; + Int32 H = y; if ( ( W % 8 ) == 0 && ( H % 8 ) == 0 ) { - Tex->Lock(); - const Uint8* Ptr = Tex->GetPixelsPtr(); - Uint32 rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; @@ -641,25 +662,30 @@ bool cEngine::SetIcon( const Uint32& FromTexId ) { bmask = 0x00ff0000; amask = 0xff000000; #endif - SDL_Surface* TempGlyphSheet = SDL_CreateRGBSurface(SDL_SWSURFACE, W, H, 32, rmask, gmask, bmask, amask); - SDL_LockSurface(TempGlyphSheet); + SDL_Surface* TempGlyphSheet = SDL_CreateRGBSurface(SDL_SWSURFACE, W, H, c * 8, rmask, gmask, bmask, amask); - Uint32 ssize = TempGlyphSheet->w * TempGlyphSheet->h * 4; - for (Uint32 i=0; i(TempGlyphSheet->pixels))[i+0] = (Ptr)[i]; + SDL_LockSurface( TempGlyphSheet ); + + Uint32 ssize = TempGlyphSheet->w * TempGlyphSheet->h * c; + + for ( Uint32 i=0; i < ssize; i++ ) { + ( static_cast( TempGlyphSheet->pixels ) )[i+0] = (Ptr)[i]; } - SDL_UnlockSurface(TempGlyphSheet); + SDL_UnlockSurface( TempGlyphSheet ); - Tex->Unlock(); + SDL_WM_SetIcon( TempGlyphSheet, NULL ); - SDL_WM_SetIcon(TempGlyphSheet, NULL); + SDL_FreeSurface( TempGlyphSheet ); - SDL_FreeSurface(TempGlyphSheet); + free( Ptr ); return true; } + + free( Ptr ); } + return false; } diff --git a/src/window/cengine.hpp b/src/window/cengine.hpp index c729b1f9b..ad63f3ca3 100755 --- a/src/window/cengine.hpp +++ b/src/window/cengine.hpp @@ -127,7 +127,7 @@ class EE_API cEngine : public tSingleton { void SetCursor( const Uint32& TexId, const eeVector2i& HotSpot = eeVector2i() ); /** Set the Window icon */ - bool SetIcon( const Uint32& FromTexId ); + bool SetIcon( const std::string& Path ); /** This will attempt to iconify/minimize the window. */ void MinimizeWindow(); @@ -284,6 +284,8 @@ class EE_API cEngine : public tSingleton { //AGLContext mContext; #endif + std::string mIcon; + void CalculateFps(); void LimitFps(); void GetElapsedTime();