This is how I did it. Found most of the info from www.gamedev.net.
Textures.h
Code:
#ifndef _TEXTURES_
#define _TEXTURES_
#include <ddraw.h>
#include <d3d.h>
#include "DXEngine.h"
class BasicTexture
{
LPDIRECTDRAWSURFACE7 Surface;
LPDIRECTDRAWSURFACE7 Texture;
public:
BasicTexture(void) {};
~BasicTexture(void);
void LoadTexture(char * filename, LPDIRECT3DDEVICE7 D3DDevice, LPDIRECTDRAW7 DirectDraw);
LPDIRECTDRAWSURFACE7 GetSurface(void) {return Surface;};
};
Textures.cpp
Code:
TileTexture::TileTexture(void)
{
Width = 64;
Height = 64;
}
HRESULT CALLBACK EnumTextures( LPDDPIXELFORMAT DDPixelFormat, LPVOID pDDDesiredPixelFormat )
{
if( DDPixelFormat->dwFlags & DDPF_ALPHAPIXELS && DDPixelFormat->dwRGBBitCount == 16 )
{
if( DDPixelFormat->dwRGBAlphaBitMask == 1 ||
DDPixelFormat->dwRGBAlphaBitMask == 0x8000 )
{
memcpy( pDDDesiredPixelFormat, DDPixelFormat, sizeof(DDPIXELFORMAT) );
return D3DENUMRET_CANCEL;
}
}
return D3DENUMRET_OK;
}
void BasicTexture::LoadTexture(char * filename, LPDIRECT3DDEVICE7 D3DDevice, LPDIRECTDRAW7 DirectDraw)
{
DDPIXELFORMAT TexturePixelFormat;
if (FAILED(D3DDevice->EnumTextureFormats(( LPD3DENUMPIXELFORMATSCALLBACK )EnumTextures, ( void* )&TexturePixelFormat)))
{
exit(10);
};
DDSURFACEDESC2 Desc;
ZeroMemory( &Desc, sizeof( DDSURFACEDESC2 ));
Desc.dwSize = sizeof( Desc );
Desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_CKSRCBLT;
Desc.dwWidth = 64;
Desc.dwHeight = 64;
Desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
Desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
Desc.ddpfPixelFormat = TexturePixelFormat;
Desc.ddckCKSrcBlt.dwColorSpaceHighValue = _RGB16BIT565(128,0,128);
Desc.ddckCKSrcBlt.dwColorSpaceLowValue = _RGB16BIT565(128,0,128);
DirectDraw->CreateSurface( &Desc, &Surface, 0 );
HBITMAP hBM;
BITMAP BM;
HDC hDCImage, hDC;
hBM = ( HBITMAP ) LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0,LR_LOADFROMFILE | LR_CREATEDIBSECTION );
if (hBM == NULL)
{
exit(10);
};
GetObject( hBM, sizeof( BM ), &BM );
hDCImage = CreateCompatibleDC( NULL );
SelectObject( hDCImage, hBM );
if( SUCCEEDED( Surface->GetDC( &hDC )))
{
BitBlt( hDC, 0, 0, 64, 64, hDCImage, 0, 0, SRCCOPY );
Surface->ReleaseDC( hDC );
} else exit(10);
DeleteDC( hDCImage );
DeleteObject( hBM );
}
BasicTexture::~BasicTexture(void)
{
Surface->Release();
Texture->Release();
}
This sets the transparent color to bright purple and all textures are assumed to be 64x64 pixels. You can remove these constraints quite easily.