2001-11-03 10:52:42 +00:00
|
|
|
#include "stdafx.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
File: RageBitmapTexture.h
|
|
|
|
|
|
|
|
|
|
Desc: Holder for a static texture with metadata. Can load just about any image format.
|
|
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
2001-11-04 19:34:28 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
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"
|
2002-05-01 19:14:55 +00:00
|
|
|
#include "RageLog.h"
|
2002-04-16 17:31:00 +00:00
|
|
|
#include "ErrorCatcher/ErrorCatcher.h"
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// RageBitmapTexture constructor
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-03-30 20:00:13 +00:00
|
|
|
RageBitmapTexture::RageBitmapTexture(
|
2002-05-19 01:59:48 +00:00
|
|
|
RageDisplay* pScreen,
|
2002-03-30 20:00:13 +00:00
|
|
|
const CString &sFilePath,
|
2002-05-19 01:59:48 +00:00
|
|
|
DWORD dwMaxSize,
|
|
|
|
|
DWORD dwTextureColorDepth,
|
|
|
|
|
int iMipMaps,
|
|
|
|
|
int iAlphaBits,
|
|
|
|
|
bool bDither,
|
|
|
|
|
bool bStretch
|
|
|
|
|
) :
|
|
|
|
|
RageTexture( pScreen, sFilePath, dwMaxSize, dwTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch )
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
2002-05-01 19:14:55 +00:00
|
|
|
// LOG->WriteLine( "RageBitmapTexture::RageBitmapTexture()" );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2001-12-19 01:50:57 +00:00
|
|
|
m_pd3dTexture = NULL;
|
|
|
|
|
|
2002-05-27 08:23:27 +00:00
|
|
|
//if( !LoadFromCacheFile() )
|
|
|
|
|
Create( dwMaxSize, dwTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-05-27 08:23:27 +00:00
|
|
|
//SaveToCache();
|
|
|
|
|
|
2001-11-03 10:52:42 +00:00
|
|
|
CreateFrameRects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageBitmapTexture::~RageBitmapTexture()
|
|
|
|
|
{
|
|
|
|
|
SAFE_RELEASE(m_pd3dTexture);
|
|
|
|
|
}
|
|
|
|
|
|
2002-04-28 20:42:32 +00:00
|
|
|
void RageBitmapTexture::Reload(
|
2002-05-19 01:59:48 +00:00
|
|
|
DWORD dwMaxSize,
|
|
|
|
|
DWORD dwTextureColorDepth,
|
|
|
|
|
int iMipMaps,
|
|
|
|
|
int iAlphaBits,
|
|
|
|
|
bool bDither,
|
|
|
|
|
bool bStretch
|
|
|
|
|
)
|
2002-04-28 20:42:32 +00:00
|
|
|
{
|
|
|
|
|
SAFE_RELEASE(m_pd3dTexture);
|
2002-05-19 01:59:48 +00:00
|
|
|
Create( dwMaxSize, dwTextureColorDepth, iMipMaps, iAlphaBits, bDither, bStretch );
|
2002-04-28 20:42:32 +00:00
|
|
|
// leave m_iRefCount alone!
|
|
|
|
|
CreateFrameRects();
|
|
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// GetTexture
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
LPDIRECT3DTEXTURE8 RageBitmapTexture::GetD3DTexture()
|
|
|
|
|
{
|
|
|
|
|
return m_pd3dTexture;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
void RageBitmapTexture::Create(
|
|
|
|
|
DWORD dwMaxSize,
|
|
|
|
|
DWORD dwTextureColorDepth,
|
|
|
|
|
int iMipMaps,
|
|
|
|
|
int iAlphaBits,
|
|
|
|
|
bool bDither,
|
|
|
|
|
bool bStretch
|
|
|
|
|
)
|
2001-11-03 10:52:42 +00:00
|
|
|
{
|
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
// look in the file name for a format hints
|
|
|
|
|
m_sFilePath.MakeLower();
|
|
|
|
|
|
|
|
|
|
if( -1 != m_sFilePath.Find("no alpha") )
|
|
|
|
|
iAlphaBits = 0;
|
|
|
|
|
else if( -1 != m_sFilePath.Find("1 alpha") )
|
|
|
|
|
iAlphaBits = 1;
|
|
|
|
|
if( -1 != m_sFilePath.Find("dither") )
|
|
|
|
|
bDither = true;
|
|
|
|
|
|
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-03-30 20:00:13 +00:00
|
|
|
switch( dwTextureColorDepth )
|
2002-01-24 08:01:24 +00:00
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
case 16:
|
2002-05-19 01:59:48 +00:00
|
|
|
switch( iAlphaBits )
|
|
|
|
|
{
|
|
|
|
|
case 0: fmtTexture = D3DFMT_R5G6B5; break;
|
|
|
|
|
case 1: fmtTexture = D3DFMT_A1R5G5B5; break;
|
|
|
|
|
default: fmtTexture = D3DFMT_A4R4G4B4; break;
|
|
|
|
|
}
|
2002-05-27 08:23:27 +00:00
|
|
|
break;
|
2002-03-30 20:00:13 +00:00
|
|
|
case 32:
|
|
|
|
|
fmtTexture = D3DFMT_A8R8G8B8;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2002-05-19 01:59:48 +00:00
|
|
|
FatalError( "Invalid color depth: %d bits", dwTextureColorDepth );
|
2002-01-24 08:01:24 +00:00
|
|
|
}
|
2002-01-28 21:24:17 +00:00
|
|
|
|
2002-01-29 21:56:14 +00:00
|
|
|
|
2002-01-29 08:19:03 +00:00
|
|
|
/////////////////////
|
|
|
|
|
// Figure out whether the texture can fit into texture memory unscaled
|
|
|
|
|
/////////////////////
|
2001-11-03 10:52:42 +00:00
|
|
|
D3DXIMAGE_INFO ddii;
|
2002-01-29 08:19:03 +00:00
|
|
|
if( FAILED( hr = D3DXGetImageInfoFromFile(
|
|
|
|
|
m_sFilePath,
|
|
|
|
|
&ddii ) ) )
|
|
|
|
|
{
|
2002-04-16 17:31:00 +00:00
|
|
|
FatalErrorHr( hr, "D3DXGetImageInfoFromFile() failed for file '%s'.", m_sFilePath );
|
2002-01-29 08:19:03 +00:00
|
|
|
}
|
|
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
// find out what the min texture size is
|
2002-05-19 01:59:48 +00:00
|
|
|
dwMaxSize = min( dwMaxSize, DISPLAY->GetDeviceCaps().MaxTextureWidth );
|
2002-03-06 09:25:57 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
bStretch |= ddii.Width > dwMaxSize || ddii.Height > dwMaxSize;
|
2002-03-30 20:00:13 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
// 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.
|
2002-03-30 20:00:13 +00:00
|
|
|
*/
|
|
|
|
|
// 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
|
2002-05-19 01:59:48 +00:00
|
|
|
bStretch ? dwMaxSize : D3DX_DEFAULT, // width
|
|
|
|
|
bStretch ? dwMaxSize : D3DX_DEFAULT, // height
|
|
|
|
|
iMipMaps, // mip map levels
|
2002-03-30 20:00:13 +00:00
|
|
|
0, // usage (is a render target?)
|
|
|
|
|
fmtTexture, // our preferred texture format
|
|
|
|
|
D3DPOOL_MANAGED, // which memory pool
|
2002-05-19 01:59:48 +00:00
|
|
|
(bStretch ? D3DX_FILTER_BOX : D3DX_FILTER_NONE) | (bDither ? D3DX_FILTER_DITHER : 0), // filter
|
2002-05-01 19:14:55 +00:00
|
|
|
D3DX_FILTER_BOX | (bDither ? D3DX_FILTER_DITHER : 0), // mip filter
|
2002-03-30 20:00:13 +00:00
|
|
|
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
|
|
|
{
|
2002-04-16 17:31:00 +00:00
|
|
|
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
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
/////////////////////
|
|
|
|
|
// Save information about the texture
|
|
|
|
|
/////////////////////
|
|
|
|
|
m_iSourceWidth = ddii.Width;
|
|
|
|
|
m_iSourceHeight= ddii.Height;
|
2002-01-29 08:19:03 +00:00
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
D3DSURFACE_DESC ddsd;
|
|
|
|
|
if ( FAILED( hr = m_pd3dTexture->GetLevelDesc( 0, &ddsd ) ) )
|
2002-04-16 17:31:00 +00:00
|
|
|
FatalErrorHr( hr, "Could not get level Description of D3DX texture!" );
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-03-30 20:00:13 +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
|
|
|
|
2002-01-29 08:19:03 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
if( bStretch )
|
2002-01-29 08:19:03 +00:00
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
m_iImageWidth = m_iTextureWidth;
|
|
|
|
|
m_iImageHeight = m_iTextureHeight;
|
2002-01-29 08:19:03 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
m_iImageWidth = m_iSourceWidth;
|
|
|
|
|
m_iImageHeight = m_iSourceHeight;
|
2002-01-29 08:19:03 +00:00
|
|
|
}
|
2002-04-28 20:42:32 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
LOG->WriteLine( "RageBitmapTexture: Loaded '%s' (%ux%u) from disk. bStretch = %d, source %d,%d; image %d,%d.",
|
2002-04-28 20:42:32 +00:00
|
|
|
m_sFilePath,
|
|
|
|
|
GetTextureWidth(),
|
|
|
|
|
GetTextureHeight(),
|
2002-05-19 01:59:48 +00:00
|
|
|
bStretch,
|
2002-04-28 20:42:32 +00:00
|
|
|
m_iSourceWidth,
|
|
|
|
|
m_iSourceHeight,
|
|
|
|
|
m_iImageWidth,
|
|
|
|
|
m_iImageHeight
|
|
|
|
|
);
|
2001-11-03 10:52:42 +00:00
|
|
|
}
|
|
|
|
|
|