📄 textureutils.cpp
字号:
/*
* ==============================================================================
* Name : Textureutils.cpp
*
* Copyright (c) 2004-2006 Nokia Corporation.
* This material, including documentation and any related
* computer programs, is protected by copyright controlled by
* Nokia Corporation.
* ==============================================================================
*/
// INCLUDE FILES
#include "Textureutils.h"
// CONSTANTS
/** Define texture coordinates used by class CTextureManager for bliting operations. */
static const GLshort BlitTextureCoords [] =
{
1, 1,
1, 0,
0, 1,
0, 0,
};
/** Indices to BlitTextureCoords used by class CTextureManager for bliting operations. */
static const GLshort BlitIndices [] =
{
0,1,2,3
};
/**
* A constant used internally by class CTextureManager to store
* the textures loading request into a single linked list (class TSglQue).
*/
const TInt TTexture::iOffset = _FOFF(TTexture,iLink);
// =============================================================================
// CFiniteStateMachine
// =============================================================================
// ============================= MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CFiniteStateMachine::CFiniteStateMachine
// C++ default constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
//
CFiniteStateMachine::CFiniteStateMachine()
{
iPrevState = iState = -1;
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CFiniteStateMachine::ConstructL()
{
iPrevState = iState = -1;
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::NewL
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFiniteStateMachine* CFiniteStateMachine::NewL()
{
CFiniteStateMachine* self = NewLC();
CleanupStack::Pop();
return self;
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::NewLC
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CFiniteStateMachine* CFiniteStateMachine::NewLC()
{
CFiniteStateMachine* self = new (ELeave) CFiniteStateMachine;
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
// Destructor
CFiniteStateMachine::~CFiniteStateMachine()
{
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::SetStateL
// Set the current state and trigger OnEnterStateL.
// -----------------------------------------------------------------------------
//
void CFiniteStateMachine::SetStateL( TInt aNewState )
{
if ( aNewState != -1 )
{
if ( aNewState != iState)
{
if ( iPrevState != -1 )
{
OnLeaveStateL( iState );
}
iPrevState = iState;
iState = aNewState;
OnEnterStateL( iState );
}
}
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::GetCurrentState
// Return the current state.
// -----------------------------------------------------------------------------
//
TInt CFiniteStateMachine::GetCurrentState() const
{
return iState;
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::GetState
// Return the current state.
// -----------------------------------------------------------------------------
//
TInt CFiniteStateMachine::GetState() const
{
return GetCurrentState();
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::GetPreviousState
// Return the current previous state.
// -----------------------------------------------------------------------------
//
TInt CFiniteStateMachine::GetPreviousState() const
{
return iPrevState;
};
// -----------------------------------------------------------------------------
// CFiniteStateMachine::OnLeaveStateL
// Empty implementation of overloadable "leave state" handler.
// -----------------------------------------------------------------------------
//
void CFiniteStateMachine::OnLeaveStateL(TInt /*aState*/)
{
// Empty implementation
}
// -----------------------------------------------------------------------------
// CFiniteStateMachine::OnEnterStateL
// Empty implementation of overloadable "enter state" handler.
// -----------------------------------------------------------------------------
//
void CFiniteStateMachine::OnEnterStateL(TInt /*aState*/)
{
// Empty implementation
}
// =============================================================================
// CImageHandler, a utility class for loading images.
// =============================================================================
// ============================= MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CImageHandler::CImageHandler
// C++ constructor can NOT contain any code, that
// might leave.
// -----------------------------------------------------------------------------
//
CImageHandler::CImageHandler( CFbsBitmap* aBitmap,
CFbsBitmap* aBitmapMask,
RFs& aFs,
MImageHandlerCallback& aCallback )
: CActive(CActive::EPriorityStandard),
iDecoder(NULL),
iCallback(aCallback),
iBitmap(aBitmap),
iBitmapMask(aBitmapMask),
iFs(aFs)
{
}
// -----------------------------------------------------------------------------
// CImageHandler::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CImageHandler::ConstructL()
{
CActiveScheduler::Add(this);
}
// -----------------------------------------------------------------------------
// CImageHandler::NewLC
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CImageHandler* CImageHandler::NewL(CFbsBitmap* aBitmap, CFbsBitmap* aBitmapMask, RFs& aFs,
MImageHandlerCallback& aCallback)
{
CImageHandler* self = NewLC(aBitmap, aBitmapMask, aFs, aCallback);
CleanupStack::Pop();
return self;
}
// -----------------------------------------------------------------------------
// CImageHandler::NewLC
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CImageHandler* CImageHandler::NewLC(CFbsBitmap* aBitmap, CFbsBitmap* aBitmapMask,
RFs& aFs,
MImageHandlerCallback& aCallback)
{
CImageHandler* self = new (ELeave) CImageHandler(aBitmap, aBitmapMask, aFs, aCallback);
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
// Destructor
CImageHandler::~CImageHandler()
{
delete iDecoder;
}
// -----------------------------------------------------------------------------
// CImageHandler::SetBitmapL
// Set the destination bitmap.
// -----------------------------------------------------------------------------
//
TBool CImageHandler::SetBitmapL( CFbsBitmap *aBitmap, CFbsBitmap* aBitmapMask )
{
if ( IsActive() )
{
// If active, fail.
return EFalse;
}
// else
iBitmap = aBitmap;
iBitmapMask = aBitmapMask;
return ETrue;
}
// -----------------------------------------------------------------------------
// CImageHandler::LoadFileL
// Loads a selected frame from a named file
// -----------------------------------------------------------------------------
//
void CImageHandler::LoadFileL(const TFileName& aFileName,
TInt aSelectedFrame)
{
__ASSERT_ALWAYS(!IsActive(),User::Invariant());
if ( iDecoder )
{
delete iDecoder;
}
iDecoder = NULL;
iDecoder = CImageDecoder::FileNewL(iFs, aFileName);
// Get image information
iFrameInfo = iDecoder->FrameInfo(aSelectedFrame);
// Resize to fit.
TRect bitmapSize = iFrameInfo.iFrameCoordsInPixels;
iBitmap->Resize(bitmapSize.Size());
// Check for alpha channel availability
if (iFrameInfo.iFlags & (TFrameInfo::ETransparencyPossible | TFrameInfo::EAlphaChannel))
{
// Decode as bitmap + alpha channel mask.
iBitmapMask->Resize(bitmapSize.Size());
iDecoder->Convert(&iStatus, *iBitmap, *iBitmapMask, aSelectedFrame);
}
else
{
// Decode as bitmap.
iDecoder->Convert(&iStatus, *iBitmap, aSelectedFrame);
}
SetActive();
}
// -----------------------------------------------------------------------------
// CImageHandler::FrameInfo
// Get the current frame information.
// -----------------------------------------------------------------------------
//
const TFrameInfo& CImageHandler::FrameInfo() const
{
return iFrameInfo;
}
// -----------------------------------------------------------------------------
// CImageHandler::RunL
// CActive::RunL() implementation. Called on image load success/failure.
// -----------------------------------------------------------------------------
//
void CImageHandler::RunL()
{
// Invoke callback.
iCallback.ImageOperationCompleteL(iStatus.Int());
}
// -----------------------------------------------------------------------------
// CImageHandler::DoCancel
// CActive::Cancel() implementation. Stops decoding.
// -----------------------------------------------------------------------------
//
void CImageHandler::DoCancel()
{
if ( iDecoder )
{
iDecoder->Cancel();
}
}
// =============================================================================
// CTextureManager
// =============================================================================
// ============================= MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CTextureManager::CTextureManager
// C++ default constructor can NOT contain any code, that
// might leave.
// Initializes the loading queue and the base class
// -----------------------------------------------------------------------------
//
CTextureManager::CTextureManager()
: CFiniteStateMachine(),
iLoadingQueue( TTexture::iOffset ),
iLoadingQueueIterator(iLoadingQueue)
{
}
// -----------------------------------------------------------------------------
// CTextureManager::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTextureManager::ConstructL( TFileName & aTexturePath,
GLuint aScreenWidth,
GLuint aScreenHeight,
GLfloat aFrustumTop,
GLfloat aFrustumBottom,
GLfloat aFrustumRight,
GLfloat aFrustumLeft,
GLfloat aFrustumNear,
MTextureLoadingListener * aTextureLoadingListener )
{
SetStateL( EIdle );
// Check if texture path was given
// Texture path was given, just use it
iTexturePath = aTexturePath;
iTextureLoadingListener = aTextureLoadingListener;
iBitmap = new (ELeave) CFbsBitmap;
iBitmap->Create(TSize(10, 10), EColor16M);
iBitmapMask = new (ELeave) CFbsBitmap;
iBitmapMask->Create(TSize(10, 10), EGray256);
/* Create a bitmap utility for loading the textures. */
iFs.Connect();
iBmapUtil = CImageHandler::NewL( iBitmap, iBitmapMask, iFs, *this );
iTextureNames = NULL;
iFrustumTop = aFrustumTop;
iFrustumBottom = aFrustumBottom;
iFrustumRight = aFrustumRight;
iFrustumLeft = aFrustumLeft;
iFrustumNear = aFrustumNear;
iFrustumWidth = aFrustumRight-aFrustumLeft;
iFrustumHeight = aFrustumBottom-aFrustumTop;
iScreenWidth = aScreenWidth;
iScreenHeight = aScreenHeight;
}
// -----------------------------------------------------------------------------
// CTextureManager::ConstructL
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CTextureManager::ConstructL(
GLuint aScreenWidth,
GLuint aScreenHeight,
GLfloat aFrustumTop,
GLfloat aFrustumBottom,
GLfloat aFrustumRight,
GLfloat aFrustumLeft,
GLfloat aFrustumNear,
MTextureLoadingListener * aTextureLoadingListener )
{
TFileName fullTexturePath;
iFs.Connect();
// Parse the current drive letter from application location
TPtrC16 driveLetter = utilGetAppRootDirectoryL().Left( 2 );
fullTexturePath.Append( driveLetter );
// Append the application private path
TPath privateDir;
User::LeaveIfError( iFs.PrivatePath( privateDir ) );
fullTexturePath.Append( privateDir );
iFs.Close();
ConstructL( fullTexturePath, aScreenWidth, aScreenHeight,
aFrustumTop, aFrustumBottom, aFrustumRight,
aFrustumLeft, aFrustumNear, aTextureLoadingListener );
}
// -----------------------------------------------------------------------------
// CTextureManager::NewL
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CTextureManager* CTextureManager::NewL(
GLuint aScreenWidth,
GLuint aScreenHeight,
GLfloat aFrustumTop,
GLfloat aFrustumBottom,
GLfloat aFrustumRight,
GLfloat aFrustumLeft,
GLfloat aFrustumNear,
MTextureLoadingListener * aTextureLoadingListener )
{
CTextureManager* self = new (ELeave) CTextureManager();
CleanupStack::PushL( self );
self->ConstructL( aScreenWidth, aScreenHeight,
aFrustumTop, aFrustumBottom, aFrustumRight, aFrustumLeft,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -