Files
itgmania212121/stepmania/src/RageBitmapTexture.cpp
T

178 lines
5.0 KiB
C++
Raw Normal View History

2001-11-03 10:52:42 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
File: RageBitmapTexture.h
Desc: Holder for a static texture with metadata. Can load just about any image format.
Copyright (c) 2001 Chris Danford. All rights reserved.
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// In-line Links
//-----------------------------------------------------------------------------
//#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "dxerr8.lib")
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "RageBitmapTexture.h"
#include "dxerr8.h"
#include "DXUtil.h"
#include "RageUtil.h"
#include "RageHelper.h"
2001-11-03 10:52:42 +00:00
//-----------------------------------------------------------------------------
// RageBitmapTexture constructor
//-----------------------------------------------------------------------------
RageBitmapTexture::RageBitmapTexture(
RageScreen* pScreen,
const CString &sFilePath,
const DWORD dwMaxSize,
const DWORD dwTextureColorDepth,
const DWORD dwHints ) :
2001-11-03 10:52:42 +00:00
RageTexture( pScreen, sFilePath )
{
// HELPER.Log( "RageBitmapTexture::RageBitmapTexture()" );
2001-11-03 10:52:42 +00:00
2001-12-19 01:50:57 +00:00
m_pd3dTexture = NULL;
Create( dwMaxSize, dwTextureColorDepth, dwHints );
2001-11-03 10:52:42 +00:00
CreateFrameRects();
}
RageBitmapTexture::~RageBitmapTexture()
{
SAFE_RELEASE(m_pd3dTexture);
}
//-----------------------------------------------------------------------------
// GetTexture
//-----------------------------------------------------------------------------
LPDIRECT3DTEXTURE8 RageBitmapTexture::GetD3DTexture()
{
return m_pd3dTexture;
}
void RageBitmapTexture::Create( DWORD dwMaxSize, DWORD dwTextureColorDepth, DWORD dwHints )
2001-11-03 10:52:42 +00:00
{
HRESULT hr;
2002-01-28 21:24:17 +00:00
///////////////////////
// Figure out which texture format to use
///////////////////////
2002-01-20 23:29:03 +00:00
D3DFORMAT fmtTexture;
2002-01-28 21:24:17 +00:00
2002-01-29 21:56:14 +00:00
// look in the file name for a format hint
2002-01-28 21:24:17 +00:00
m_sFilePath.MakeLower();
switch( dwTextureColorDepth )
{
case 16:
if( -1 != m_sFilePath.Find("no alpha") )
fmtTexture = D3DFMT_R5G6B5;
else if( -1 != m_sFilePath.Find("1 alpha") )
fmtTexture = D3DFMT_A1R5G5B5;
else
fmtTexture = D3DFMT_A4R4G4B4;
break;
case 32:
fmtTexture = D3DFMT_A8R8G8B8;
break;
default:
ASSERT( false ); // invalid color depth value
}
2002-01-28 21:24:17 +00:00
2002-01-29 21:56:14 +00:00
// look in the file name for a dither hint
bool bDither = (dwHints & TEXTURE_HINT_DITHER)
|| -1 != m_sFilePath.Find("dither");
bool bCreateMipMaps = !(dwHints & TEXTURE_HINT_NOMIPMAPS);
2002-01-29 21:56:14 +00:00
/////////////////////
// Figure out whether the texture can fit into texture memory unscaled
/////////////////////
bool bScaleImageToTextureSize;
2001-11-03 10:52:42 +00:00
D3DXIMAGE_INFO ddii;
if( FAILED( hr = D3DXGetImageInfoFromFile(
m_sFilePath,
&ddii ) ) )
{
HELPER.FatalErrorHr( hr, "D3DXGetImageInfoFromFile() failed for file '%s'.", m_sFilePath );
}
D3DCAPS8 caps;
m_pd3dDevice->GetDeviceCaps( &caps );
// find out what the min texture size is
dwMaxSize = min( dwMaxSize, caps.MaxTextureWidth );
2002-03-06 09:25:57 +00:00
bScaleImageToTextureSize = ddii.Width > dwMaxSize || ddii.Height > dwMaxSize;
/*
// HACK: The stupid Savage driver will report that it can hold the entire texture,
2002-03-06 09:14:52 +00:00
// then allocate something smaller than the dimensions we need!
// after allocating the texture, make sure it's the size we expect. If not,
// load it again with scaling turned on.
*/
// I'm taking out the Savage hack because it's causing problems. Tough luck for them. I think
// the problem can be worked around by setting MaxTextureSize to 512.
if( FAILED( hr = D3DXCreateTextureFromFileEx(
m_pd3dDevice, // device
m_sFilePath, // soure file
D3DX_DEFAULT, // width
D3DX_DEFAULT, // height
bCreateMipMaps ? 4 : 0, // mip map levels
0, // usage (is a render target?)
fmtTexture, // our preferred texture format
D3DPOOL_MANAGED, // which memory pool
(bScaleImageToTextureSize ? D3DX_FILTER_BOX : D3DX_FILTER_NONE) | (bDither ? D3DX_FILTER_DITHER : 0), // filter
D3DX_DEFAULT | (bDither ? D3DX_FILTER_DITHER : 0), // mip filter
0, // no color key
&ddii, // struct to fill with source image info
NULL, // no palette
&m_pd3dTexture ) ) )
2002-01-20 09:40:21 +00:00
{
HELPER.FatalErrorHr( hr, "D3DXCreateTextureFromFileEx() failed for file '%s'.", m_sFilePath );
2002-01-20 09:40:21 +00:00
}
2001-11-03 10:52:42 +00:00
/////////////////////
// Save information about the texture
/////////////////////
m_iSourceWidth = ddii.Width;
m_iSourceHeight= ddii.Height;
D3DSURFACE_DESC ddsd;
if ( FAILED( hr = m_pd3dTexture->GetLevelDesc( 0, &ddsd ) ) )
HELPER.FatalErrorHr( hr, "Could not get level Description of D3DX texture!" );
2001-11-03 10:52:42 +00:00
// save information about the texture
m_iTextureWidth = ddsd.Width;
m_iTextureHeight = ddsd.Height;
m_TextureFormat = ddsd.Format;
2001-11-03 10:52:42 +00:00
if( bScaleImageToTextureSize )
{
m_iImageWidth = m_iTextureWidth;
m_iImageHeight = m_iTextureHeight;
}
else
{
m_iImageWidth = m_iSourceWidth;
m_iImageHeight = m_iSourceHeight;
}
2001-11-03 10:52:42 +00:00
}