Files
itgmania212121/stepmania/src/RageTexture.cpp
T

121 lines
3.9 KiB
C++
Raw Normal View History

2001-11-03 10:52:42 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
File: RageTexture.h
Desc: Abstract class for a texture with metadata.
Copyright (c) 2001 Chris Danford. All rights reserved.
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "RageTexture.h"
#include "RageUtil.h"
//-----------------------------------------------------------------------------
// RageTexture constructor
//-----------------------------------------------------------------------------
RageTexture::RageTexture( LPRageScreen pScreen, CString sFilePath ) :
2001-11-25 04:31:44 +00:00
m_uSourceWidth( 0 ),
m_uSourceHeight( 0 ),
2001-11-03 10:52:42 +00:00
m_uTextureWidth( 0 ),
m_uTextureHeight( 0 ),
m_uFramesWide( 1 ),
m_uFramesHigh( 1 ),
2001-11-25 04:31:44 +00:00
m_uSourceFrameWidth( 0 ),
m_uSourceFrameHeight( 0 )
2001-11-03 10:52:42 +00:00
{
// RageLog( "RageTexture::RageTexture()" );
// save a pointer to the D3D device
m_pd3dDevice = pScreen->GetDevice();
assert( m_pd3dDevice != NULL );
// save the file path
m_sFilePath = sFilePath;
m_pd3dTexture = NULL;
m_iRefCount = 1;
}
RageTexture::~RageTexture()
{
}
void RageTexture::CreateFrameRects()
2001-11-03 10:52:42 +00:00
{
2001-11-25 04:31:44 +00:00
GetFrameDimensionsFromFileName( m_sFilePath, &m_uFramesWide, &m_uFramesHigh );
m_uSourceFrameWidth = GetSourceWidth() / m_uFramesWide;
m_uSourceFrameHeight = GetSourceHeight() / m_uFramesHigh;
2001-11-03 10:52:42 +00:00
///////////////////////////////////
// Fill in the m_FrameRects with the bounds of each frame in the animation.
///////////////////////////////////
2001-11-25 04:31:44 +00:00
for( UINT j=0; j<m_uFramesHigh; j++ ) // traverse along Y
2001-11-03 10:52:42 +00:00
{
2001-11-25 04:31:44 +00:00
for( UINT i=0; i<m_uFramesWide; i++ ) // traverse along X (important that this is the inner loop)
2001-11-03 10:52:42 +00:00
{
2001-11-25 04:31:44 +00:00
FRECT frect( (i+0)/(float)m_uFramesWide, // these will all be between 0.0 and 1.0
(j+0)/(float)m_uFramesHigh,
(i+1)/(float)m_uFramesWide,
(j+1)/(float)m_uFramesHigh );
m_TextureCoordRects.Add( frect ); // the index of this array element will be (i + j*m_uFramesWide)
2001-11-03 10:52:42 +00:00
2001-11-25 04:31:44 +00:00
//RageLog( "Adding frect%d %f %f %f %f", (i + j*m_uFramesWide), frect.left, frect.top, frect.right, frect.bottom );
2001-11-03 10:52:42 +00:00
}
}
}
#include "string.h"
void RageTexture::GetFrameDimensionsFromFileName( CString sPath, UINT* puFramesWide, UINT* puFramesHigh ) const
2001-11-03 10:52:42 +00:00
{
//////////////////////////////////////////////////
// Parse m_sFilePath for the frame dimensions
//
// The file name must look like "name 4x15.gif". The the space is required.
//////////////////////////////////////////////////
CString sDimensionsString = sPath; // we will chop this down to the "4x15" part. Need regular... expressions... choke :-)
// chop off the extension
int index_of_last_period = sDimensionsString.ReverseFind( '.' );
if( index_of_last_period == -1 ) // this file name has no extension, but it the texture was loaded. I doubt this code will ever execute :-)
{
2001-11-25 04:31:44 +00:00
*puFramesWide = *puFramesHigh = 1;
2001-11-03 10:52:42 +00:00
return;
}
sDimensionsString = sDimensionsString.Left(index_of_last_period);
// chop off everything before the last space
int index_of_last_space = sDimensionsString.ReverseFind( ' ' );
if( index_of_last_space == -1 ) // this file name has space, so the dimensions tag cannot be read
{
2001-11-25 04:31:44 +00:00
*puFramesWide = *puFramesHigh = 1;
2001-11-03 10:52:42 +00:00
return;
}
sDimensionsString.Delete( 0, index_of_last_space+1 );
// now we are left with "4x15"
int index_of_x = sDimensionsString.Find( 'x' );
if( index_of_x == -1 ) // this file name doesn't have an x in the last token. So, this probably isn't a dimension tag.
{
2001-11-25 04:31:44 +00:00
*puFramesWide = *puFramesHigh = 1;
2001-11-03 10:52:42 +00:00
return;
}
2001-11-25 04:31:44 +00:00
*puFramesWide = (UINT) atoi( sDimensionsString.Left( index_of_x ) );
2001-11-03 10:52:42 +00:00
// chop off the frames wide part and read in the frames high
sDimensionsString.Delete( 0, index_of_x+1 );
2001-11-25 04:31:44 +00:00
*puFramesHigh = (UINT) atoi( sDimensionsString );
2001-11-03 10:52:42 +00:00
2001-11-25 04:31:44 +00:00
if( *puFramesWide == 0 ) *puFramesWide = 1;
if( *puFramesHigh == 0 ) *puFramesHigh = 1;
2001-11-03 10:52:42 +00:00
}