Initial commit: ROW Client source code

Game client codebase including:
- CharacterActionControl: Character and creature management
- GlobalScript: Network, items, skills, quests, utilities
- RYLClient: Main client application with GUI and event handlers
- Engine: 3D rendering engine (RYLGL)
- MemoryManager: Custom memory allocation
- Library: Third-party dependencies (DirectX, boost, etc.)
- Tools: Development utilities

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-29 16:24:34 +09:00
commit e067522598
5135 changed files with 1745744 additions and 0 deletions

View File

@@ -0,0 +1,271 @@
//------------------------------------------------------------------------------
// File: AsyncIo.h
//
// Desc: DirectShow sample code - base library for I/O functionality.
//
// Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#ifndef __ASYNCIO_H__
#define __ASYNCIO_H__
//
// definition of CAsyncFile object that performs file access. It provides
// asynchronous, unbuffered, aligned reads from a file, using a worker thread
// on win95 and potentially overlapped i/o if available.
// !!! Need to use real overlapped i/o if available
// currently only uses worker thread, not overlapped i/o
class CAsyncIo;
class CAsyncStream;
//
// Model the stream we read from based on a file-like interface
//
class CAsyncStream
{
public:
virtual ~CAsyncStream() {};
virtual HRESULT SetPointer(LONGLONG llPos) = 0;
virtual HRESULT Read(PBYTE pbBuffer,
DWORD dwBytesToRead,
BOOL bAlign,
LPDWORD pdwBytesRead) = 0;
virtual LONGLONG Size(LONGLONG *pSizeAvailable = NULL) = 0;
virtual DWORD Alignment() = 0;
virtual void Lock() = 0;
virtual void Unlock() = 0;
//virtual void SetStopHandle(HANDLE hevStop) {}
};
// represents a single request and performs the i/o. Can be called on either
// worker thread or app thread, but must hold pcsFile across file accesses.
// (ie across SetFilePointer/ReadFile pairs)
class CAsyncRequest
{
CAsyncIo *m_pIo;
CAsyncStream *m_pStream;
LONGLONG m_llPos;
BOOL m_bAligned;
LONG m_lLength;
BYTE* m_pBuffer;
LPVOID m_pContext;
DWORD m_dwUser;
HRESULT m_hr;
public:
// init the params for this request. Issue the i/o
// if overlapped i/o is possible.
HRESULT Request(
CAsyncIo *pIo,
CAsyncStream *pStream,
LONGLONG llPos,
LONG lLength,
BOOL bAligned,
BYTE* pBuffer,
LPVOID pContext, // filter's context
DWORD dwUser); // downstream filter's context
// issue the i/o if not overlapped, and block until i/o complete.
// returns error code of file i/o
HRESULT Complete();
// cancels the i/o. blocks until i/o is no longer pending
HRESULT Cancel()
{
return S_OK;
};
// accessor functions
LPVOID GetContext()
{
return m_pContext;
};
DWORD GetUser()
{
return m_dwUser;
};
HRESULT GetHResult() {
return m_hr;
};
// we set m_lLength to the actual length
LONG GetActualLength() {
return m_lLength;
};
LONGLONG GetStart() {
return m_llPos;
};
};
typedef CGenericList<CAsyncRequest> CRequestList;
// this class needs a worker thread, but the ones defined in classes\base
// are not suitable (they assume you have one message sent or posted per
// request, whereas here for efficiency we want just to set an event when
// there is work on the queue).
//
// we create CAsyncRequest objects and queue them on m_listWork. The worker
// thread pulls them off, completes them and puts them on m_listDone.
// The events m_evWork and m_evDone are set when the corresponding lists are
// not empty.
//
// Synchronous requests are done on the caller thread. These should be
// synchronised by the caller, but to make sure we hold m_csFile across
// the SetFilePointer/ReadFile code.
//
// Flush by calling BeginFlush. This rejects all further requests (by
// setting m_bFlushing within m_csLists), cancels all requests and moves them
// to the done list, and sets m_evDone to ensure that no WaitForNext operations
// will block. Call EndFlush to cancel this state.
//
// we support unaligned calls to SyncRead. This is done by opening the file
// twice if we are using unbuffered i/o (m_dwAlign > 1).
// !!!fix this to buffer on top of existing file handle?
class CAsyncIo
{
CCritSec m_csReader;
CAsyncStream *m_pStream;
CCritSec m_csLists; // locks access to the list and events
BOOL m_bFlushing; // true if between BeginFlush/EndFlush
CRequestList m_listWork;
CRequestList m_listDone;
CAMEvent m_evWork; // set when list is not empty
CAMEvent m_evDone;
// for correct flush behaviour: all protected by m_csLists
LONG m_cItemsOut; // nr of items not on listDone or listWork
BOOL m_bWaiting; // TRUE if someone waiting for m_evAllDone
CAMEvent m_evAllDone; // signal when m_cItemsOut goes to 0 if m_cWaiting
CAMEvent m_evStop; // set when thread should exit
HANDLE m_hThread;
LONGLONG Size() {
ASSERT(m_pStream != NULL);
return m_pStream->Size();
};
// start the thread
HRESULT StartThread(void);
// stop the thread and close the handle
HRESULT CloseThread(void);
// manage the list of requests. hold m_csLists and ensure
// that the (manual reset) event hevList is set when things on
// the list but reset when the list is empty.
// returns null if list empty
CAsyncRequest* GetWorkItem();
// get an item from the done list
CAsyncRequest* GetDoneItem();
// put an item on the work list
HRESULT PutWorkItem(CAsyncRequest* pRequest);
// put an item on the done list
HRESULT PutDoneItem(CAsyncRequest* pRequest);
// called on thread to process any active requests
void ProcessRequests(void);
// initial static thread proc calls ThreadProc with DWORD
// param as this
static DWORD WINAPI InitialThreadProc(LPVOID pv) {
CAsyncIo * pThis = (CAsyncIo*) pv;
return pThis->ThreadProc();
};
DWORD ThreadProc(void);
public:
CAsyncIo(CAsyncStream *pStream);
~CAsyncIo();
// open the file
HRESULT Open(LPCTSTR pName);
// ready for async activity - call this before
// calling Request
HRESULT AsyncActive(void);
// call this when no more async activity will happen before
// the next AsyncActive call
HRESULT AsyncInactive(void);
// queue a requested read. must be aligned.
HRESULT Request(
LONGLONG llPos,
LONG lLength,
BOOL bAligned,
BYTE* pBuffer,
LPVOID pContext,
DWORD dwUser);
// wait for the next read to complete
HRESULT WaitForNext(
DWORD dwTimeout,
LPVOID *ppContext,
DWORD * pdwUser,
LONG * pcbActual
);
// perform a read of an already aligned buffer
HRESULT SyncReadAligned(
LONGLONG llPos,
LONG lLength,
BYTE* pBuffer,
LONG* pcbActual,
PVOID pvContext
);
// perform a synchronous read. will be buffered
// if not aligned.
HRESULT SyncRead(
LONGLONG llPos,
LONG lLength,
BYTE* pBuffer);
// return length
HRESULT Length(LONGLONG *pllTotal, LONGLONG* pllAvailable);
// all Reader positions, read lengths and memory locations must
// be aligned to this.
HRESULT Alignment(LONG* pl);
HRESULT BeginFlush();
HRESULT EndFlush();
LONG Alignment()
{
return m_pStream->Alignment();
};
BOOL IsAligned(LONG l) {
if ((l & (Alignment() -1)) == 0) {
return TRUE;
} else {
return FALSE;
}
};
BOOL IsAligned(LONGLONG ll) {
return IsAligned( (LONG) (ll & 0xffffffff));
};
// Accessor
HANDLE StopEvent() const { return m_evDone; }
};
#endif // __ASYNCIO_H__

View File

@@ -0,0 +1,226 @@
//------------------------------------------------------------------------------
// File: AsyncRdr.h
//
// Desc: DirectShow sample code - base library for I/O functionality.
//
// Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
#ifndef __ASYNCRDR_H__
#define __ASYNCRDR_H__
//
// AsyncRdr
//
// Defines an IO source filter.
//
// This filter (CAsyncReader) supports IBaseFilter and IFileSourceFilter interfaces from the
// filter object itself. It has a single output pin (CAsyncOutputPin)
// which supports IPin and IAsyncReader.
//
// This filter is essentially a wrapper for the CAsyncFile class that does
// all the work.
//
// the filter class (defined below)
class CAsyncReader;
// the output pin class
class CAsyncOutputPin
: public IAsyncReader,
public CBasePin
{
protected:
CAsyncReader* m_pReader;
CAsyncIo * m_pIo;
// This is set every time we're asked to return an IAsyncReader
// interface
// This allows us to know if the downstream pin can use
// this transport, otherwise we can hook up to thinks like the
// dump filter and nothing happens
BOOL m_bQueriedForAsyncReader;
HRESULT InitAllocator(IMemAllocator **ppAlloc);
public:
// constructor and destructor
CAsyncOutputPin(
HRESULT * phr,
CAsyncReader *pReader,
CAsyncIo *pIo,
CCritSec * pLock);
~CAsyncOutputPin();
// --- CUnknown ---
// need to expose IAsyncReader
DECLARE_IUNKNOWN
STDMETHODIMP NonDelegatingQueryInterface(REFIID, void**);
// --- IPin methods ---
STDMETHODIMP Connect(
IPin * pReceivePin,
const AM_MEDIA_TYPE *pmt // optional media type
);
// --- CBasePin methods ---
// return the types we prefer - this will return the known
// file type
HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
// can we support this type?
HRESULT CheckMediaType(const CMediaType* pType);
// Clear the flag so we see if IAsyncReader is queried for
HRESULT CheckConnect(IPin *pPin)
{
m_bQueriedForAsyncReader = FALSE;
return CBasePin::CheckConnect(pPin);
}
// See if it was asked for
HRESULT CompleteConnect(IPin *pReceivePin)
{
if (m_bQueriedForAsyncReader) {
return CBasePin::CompleteConnect(pReceivePin);
} else {
#ifdef VFW_E_NO_TRANSPORT
return VFW_E_NO_TRANSPORT;
#else
return E_FAIL;
#endif
}
}
// Remove our connection status
HRESULT BreakConnect()
{
m_bQueriedForAsyncReader = FALSE;
return CBasePin::BreakConnect();
}
// --- IAsyncReader methods ---
// pass in your preferred allocator and your preferred properties.
// method returns the actual allocator to be used. Call GetProperties
// on returned allocator to learn alignment and prefix etc chosen.
// this allocator will be not be committed and decommitted by
// the async reader, only by the consumer.
STDMETHODIMP RequestAllocator(
IMemAllocator* pPreferred,
ALLOCATOR_PROPERTIES* pProps,
IMemAllocator ** ppActual);
// queue a request for data.
// media sample start and stop times contain the requested absolute
// byte position (start inclusive, stop exclusive).
// may fail if sample not obtained from agreed allocator.
// may fail if start/stop position does not match agreed alignment.
// samples allocated from source pin's allocator may fail
// GetPointer until after returning from WaitForNext.
STDMETHODIMP Request(
IMediaSample* pSample,
DWORD dwUser); // user context
// block until the next sample is completed or the timeout occurs.
// timeout (millisecs) may be 0 or INFINITE. Samples may not
// be delivered in order. If there is a read error of any sort, a
// notification will already have been sent by the source filter,
// and STDMETHODIMP will be an error.
STDMETHODIMP WaitForNext(
DWORD dwTimeout,
IMediaSample** ppSample, // completed sample
DWORD * pdwUser); // user context
// sync read of data. Sample passed in must have been acquired from
// the agreed allocator. Start and stop position must be aligned.
// equivalent to a Request/WaitForNext pair, but may avoid the
// need for a thread on the source filter.
STDMETHODIMP SyncReadAligned(
IMediaSample* pSample);
// sync read. works in stopped state as well as run state.
// need not be aligned. Will fail if read is beyond actual total
// length.
STDMETHODIMP SyncRead(
LONGLONG llPosition, // absolute file position
LONG lLength, // nr bytes required
BYTE* pBuffer); // write data here
// return total length of stream, and currently available length.
// reads for beyond the available length but within the total length will
// normally succeed but may block for a long period.
STDMETHODIMP Length(
LONGLONG* pTotal,
LONGLONG* pAvailable);
// cause all outstanding reads to return, possibly with a failure code
// (VFW_E_TIMEOUT) indicating they were cancelled.
// these are defined on IAsyncReader and IPin
STDMETHODIMP BeginFlush(void);
STDMETHODIMP EndFlush(void);
};
//
// The filter object itself. Supports IBaseFilter through
// CBaseFilter and also IFileSourceFilter directly in this object
class CAsyncReader : public CBaseFilter
{
protected:
// filter-wide lock
CCritSec m_csFilter;
// all i/o done here
CAsyncIo m_Io;
// our output pin
CAsyncOutputPin m_OutputPin;
// Type we think our data is
CMediaType m_mt;
public:
// construction / destruction
CAsyncReader(
TCHAR *pName,
LPUNKNOWN pUnk,
CAsyncStream *pStream,
HRESULT *phr);
~CAsyncReader();
// --- CBaseFilter methods ---
int GetPinCount();
CBasePin *GetPin(int n);
// --- Access our media type
const CMediaType *LoadType() const
{
return &m_mt;
}
virtual HRESULT Connect(
IPin * pReceivePin,
const AM_MEDIA_TYPE *pmt // optional media type
)
{
return m_OutputPin.CBasePin::Connect(pReceivePin, pmt);
}
};
#endif //__ASYNCRDR_H__

View File

@@ -0,0 +1,211 @@
//-----------------------------------------------------------------------------
// File: D3DApp.h
//
// Desc: Application class for the Direct3D samples framework library.
//
// Copyright (c) 1998-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef D3DAPP_H
#define D3DAPP_H
// basetsd.h defines INT_PTR (used below). It is not included by default
// under VC 5.0. If you are using VC6 or later, it is included via Windows.h.
#include <basetsd.h>
//-----------------------------------------------------------------------------
// Error codes
//-----------------------------------------------------------------------------
enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
#define D3DAPPERR_NODIRECT3D 0x82000001
#define D3DAPPERR_NOWINDOW 0x82000002
#define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
#define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
#define D3DAPPERR_NOHARDWAREDEVICE 0x82000005
#define D3DAPPERR_HALNOTCOMPATIBLE 0x82000006
#define D3DAPPERR_NOWINDOWEDHAL 0x82000007
#define D3DAPPERR_NODESKTOPHAL 0x82000008
#define D3DAPPERR_NOHALTHISMODE 0x82000009
#define D3DAPPERR_NONZEROREFCOUNT 0x8200000a
#define D3DAPPERR_MEDIANOTFOUND 0x8200000b
#define D3DAPPERR_RESIZEFAILED 0x8200000c
#define D3DAPPERR_NULLREFDEVICE 0x8200000d
//-----------------------------------------------------------------------------
// Name: struct D3DModeInfo
// Desc: Structure for holding information about a display mode
//-----------------------------------------------------------------------------
struct D3DModeInfo
{
DWORD Width; // Screen width in this mode
DWORD Height; // Screen height in this mode
D3DFORMAT Format; // Pixel format in this mode
DWORD dwBehavior; // Hardware / Software / Mixed vertex processing
D3DFORMAT DepthStencilFormat; // Which depth/stencil format to use with this mode
};
//-----------------------------------------------------------------------------
// Name: struct D3DDeviceInfo
// Desc: Structure for holding information about a Direct3D device, including
// a list of modes compatible with this device
//-----------------------------------------------------------------------------
struct D3DDeviceInfo
{
// Device data
D3DDEVTYPE DeviceType; // Reference, HAL, etc.
D3DCAPS8 d3dCaps; // Capabilities of this device
const TCHAR* strDesc; // Name of this device
BOOL bCanDoWindowed; // Whether this device can work in windowed mode
// Modes for this device
DWORD dwNumModes;
D3DModeInfo modes[150];
// Current state
DWORD dwCurrentMode;
BOOL bWindowed;
D3DMULTISAMPLE_TYPE MultiSampleTypeWindowed;
D3DMULTISAMPLE_TYPE MultiSampleTypeFullscreen;
};
//-----------------------------------------------------------------------------
// Name: struct D3DAdapterInfo
// Desc: Structure for holding information about an adapter, including a list
// of devices available on this adapter
//-----------------------------------------------------------------------------
struct D3DAdapterInfo
{
// Adapter data
D3DADAPTER_IDENTIFIER8 d3dAdapterIdentifier;
D3DDISPLAYMODE d3ddmDesktop; // Desktop display mode for this adapter
// Devices for this adapter
DWORD dwNumDevices;
D3DDeviceInfo devices[5];
// Current state
DWORD dwCurrentDevice;
};
//-----------------------------------------------------------------------------
// Name: class CD3DApplication
// Desc: A base class for creating sample D3D8 applications. To create a simple
// Direct3D application, simply derive this class into a class (such as
// class CMyD3DApplication) and override the following functions, as
// needed:
// OneTimeSceneInit() - To initialize app data (alloc mem, etc.)
// InitDeviceObjects() - To initialize the 3D scene objects
// FrameMove() - To animate the scene
// Render() - To render the scene
// DeleteDeviceObjects() - To cleanup the 3D scene objects
// FinalCleanup() - To cleanup app data (for exitting the app)
// MsgProc() - To handle Windows messages
//-----------------------------------------------------------------------------
class CD3DApplication
{
protected:
// Internal variables for the state of the app
D3DAdapterInfo m_Adapters[10];
DWORD m_dwNumAdapters;
DWORD m_dwAdapter;
BOOL m_bWindowed;
BOOL m_bActive;
BOOL m_bReady;
BOOL m_bHasFocus;
// Internal variables used for timing
BOOL m_bFrameMoving;
BOOL m_bSingleStep;
// Internal error handling function
HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType );
// Internal functions to manage and render the 3D scene
HRESULT BuildDeviceList();
BOOL FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType,
D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );
HRESULT Initialize3DEnvironment();
HRESULT Resize3DEnvironment();
HRESULT ToggleFullscreen();
HRESULT ForceWindowed();
HRESULT UserSelectNewDevice();
VOID Cleanup3DEnvironment();
HRESULT Render3DEnvironment();
virtual HRESULT AdjustWindowForChange();
static INT_PTR CALLBACK SelectDeviceProc( HWND hDlg, UINT msg,
WPARAM wParam, LPARAM lParam );
protected:
// Main objects used for creating and rendering the 3D scene
D3DPRESENT_PARAMETERS m_d3dpp; // Parameters for CreateDevice/Reset
HWND m_hWnd; // The main app window
HWND m_hWndFocus; // The D3D focus window (usually same as m_hWnd)
HMENU m_hMenu; // App menu bar (stored here when fullscreen)
LPDIRECT3D8 m_pD3D; // The main D3D object
LPDIRECT3DDEVICE8 m_pd3dDevice; // The D3D rendering device
D3DCAPS8 m_d3dCaps; // Caps for the device
D3DSURFACE_DESC m_d3dsdBackBuffer; // Surface desc of the backbuffer
DWORD m_dwCreateFlags; // Indicate sw or hw vertex processing
DWORD m_dwWindowStyle; // Saved window style for mode switches
RECT m_rcWindowBounds; // Saved window bounds for mode switches
RECT m_rcWindowClient; // Saved client area size for mode switches
// Variables for timing
FLOAT m_fTime; // Current time in seconds
FLOAT m_fElapsedTime; // Time elapsed since last frame
FLOAT m_fFPS; // Instanteous frame rate
TCHAR m_strDeviceStats[90];// String to hold D3D device stats
TCHAR m_strFrameStats[90]; // String to hold frame stats
// Overridable variables for the app
TCHAR* m_strWindowTitle; // Title for the app's window
BOOL m_bUseDepthBuffer; // Whether to autocreate depthbuffer
DWORD m_dwMinDepthBits; // Minimum number of bits needed in depth buffer
DWORD m_dwMinStencilBits; // Minimum number of bits needed in stencil buffer
DWORD m_dwCreationWidth; // Width used to create window
DWORD m_dwCreationHeight; // Height used to create window
BOOL m_bShowCursorWhenFullscreen; // Whether to show cursor when fullscreen
BOOL m_bClipCursorWhenFullscreen; // Whether to limit cursor pos when fullscreen
// Overridable functions for the 3D scene created by the app
virtual HRESULT ConfirmDevice(D3DCAPS8*,DWORD,D3DFORMAT) { return S_OK; }
virtual HRESULT OneTimeSceneInit() { return S_OK; }
virtual HRESULT InitDeviceObjects() { return S_OK; }
virtual HRESULT RestoreDeviceObjects() { return S_OK; }
virtual HRESULT FrameMove() { return S_OK; }
virtual HRESULT Render() { return S_OK; }
virtual HRESULT InvalidateDeviceObjects() { return S_OK; }
virtual HRESULT DeleteDeviceObjects() { return S_OK; }
virtual HRESULT FinalCleanup() { return S_OK; }
public:
// Functions to create, run, pause, and clean up the application
virtual HRESULT Create( HINSTANCE hInstance );
virtual INT Run();
virtual LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
virtual VOID Pause( BOOL bPause );
// Internal constructor
CD3DApplication();
};
#endif

View File

@@ -0,0 +1,130 @@
//-----------------------------------------------------------------------------
// File: D3DFile.h
//
// Desc: Support code for loading DirectX .X files.
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef D3DFILE_H
#define D3DFILE_H
#include <tchar.h>
#include <d3d8.h>
#include <d3dx8.h>
//-----------------------------------------------------------------------------
// Name: class CD3DMesh
// Desc: Class for loading and rendering file-based meshes
//-----------------------------------------------------------------------------
class CD3DMesh
{
public:
TCHAR m_strName[512];
LPD3DXMESH m_pSysMemMesh; // SysMem mesh, lives through resize
LPD3DXMESH m_pLocalMesh; // Local mesh, rebuilt on resize
DWORD m_dwNumMaterials; // Materials for the mesh
D3DMATERIAL8* m_pMaterials;
LPDIRECT3DTEXTURE8* m_pTextures;
BOOL m_bUseMaterials;
public:
// Rendering
HRESULT Render( LPDIRECT3DDEVICE8 pd3dDevice,
BOOL bDrawOpaqueSubsets = TRUE,
BOOL bDrawAlphaSubsets = TRUE );
// Mesh access
LPD3DXMESH GetSysMemMesh() { return m_pSysMemMesh; }
LPD3DXMESH GetLocalMesh() { return m_pLocalMesh; }
// Rendering options
VOID UseMeshMaterials( BOOL bFlag ) { m_bUseMaterials = bFlag; }
HRESULT SetFVF( LPDIRECT3DDEVICE8 pd3dDevice, DWORD dwFVF );
// Initializing
HRESULT RestoreDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice );
HRESULT InvalidateDeviceObjects();
// Creation/destruction
HRESULT Create( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strFilename );
HRESULT Create( LPDIRECT3DDEVICE8 pd3dDevice, LPDIRECTXFILEDATA pFileData );
HRESULT Destroy();
CD3DMesh( TCHAR* strName = _T("CD3DFile_Mesh") );
virtual ~CD3DMesh();
};
//-----------------------------------------------------------------------------
// Name: class CD3DFrame
// Desc: Class for loading and rendering file-based meshes
//-----------------------------------------------------------------------------
class CD3DFrame
{
public:
TCHAR m_strName[512];
D3DXMATRIX m_mat;
CD3DMesh* m_pMesh;
TCHAR m_strMeshName[512];
CD3DFrame* m_pNext;
CD3DFrame* m_pChild;
public:
// Matrix access
VOID SetMatrix( D3DXMATRIX* pmat ) { m_mat = *pmat; }
D3DXMATRIX* GetMatrix() { return &m_mat; }
CD3DMesh* FindMesh( TCHAR* strMeshName );
CD3DFrame* FindFrame( TCHAR* strFrameName );
BOOL EnumMeshes( BOOL (*EnumMeshCB)(CD3DMesh*,VOID*),
VOID* pContext );
HRESULT Destroy();
HRESULT RestoreDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice );
HRESULT InvalidateDeviceObjects();
HRESULT Render( LPDIRECT3DDEVICE8 pd3dDevice,
BOOL bDrawOpaqueSubsets = TRUE,
BOOL bDrawAlphaSubsets = TRUE,
D3DXMATRIX* pmatWorldMartix = NULL);
CD3DFrame( TCHAR* strName = _T("CD3DFile_Frame") );
virtual ~CD3DFrame();
};
//-----------------------------------------------------------------------------
// Name: class CD3DFile
// Desc: Class for loading and rendering file-based meshes
//-----------------------------------------------------------------------------
class CD3DFile : public CD3DFrame
{
HRESULT LoadMesh( LPDIRECT3DDEVICE8 pd3dDevice, LPDIRECTXFILEDATA pFileData,
CD3DFrame* pParentFrame );
HRESULT LoadFrame( LPDIRECT3DDEVICE8 pd3dDevice, LPDIRECTXFILEDATA pFileData,
CD3DFrame* pParentFrame );
public:
HRESULT Create( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strFilename );
HRESULT CreateFromResource( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strResource, TCHAR* strType );
// For pure devices, specify the world transform. If the world transform is not
// specified on pure devices, this function will fail.
HRESULT Render( LPDIRECT3DDEVICE8 pd3dDevice, D3DXMATRIX* pmatWorldMatrix = NULL );
CD3DFile() : CD3DFrame( _T("CD3DFile_Root") ) {}
};
#endif

View File

@@ -0,0 +1,77 @@
//-----------------------------------------------------------------------------
// File: D3DFont.h
//
// Desc: Texture-based font class
//
// Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef D3DFONT_H
#define D3DFONT_H
#include <tchar.h>
#include <D3D8.h>
// Font creation flags
#define D3DFONT_BOLD 0x0001
#define D3DFONT_ITALIC 0x0002
#define D3DFONT_ZENABLE 0x0004
// Font rendering flags
#define D3DFONT_CENTERED 0x0001
#define D3DFONT_TWOSIDED 0x0002
#define D3DFONT_FILTERED 0x0004
//-----------------------------------------------------------------------------
// Name: class CD3DFont
// Desc: Texture-based font class for doing text in a 3D scene.
//-----------------------------------------------------------------------------
class CD3DFont
{
TCHAR m_strFontName[80]; // Font properties
DWORD m_dwFontHeight;
DWORD m_dwFontFlags;
LPDIRECT3DDEVICE8 m_pd3dDevice; // A D3DDevice used for rendering
LPDIRECT3DTEXTURE8 m_pTexture; // The d3d texture for this font
LPDIRECT3DVERTEXBUFFER8 m_pVB; // VertexBuffer for rendering text
DWORD m_dwTexWidth; // Texture dimensions
DWORD m_dwTexHeight;
FLOAT m_fTextScale;
FLOAT m_fTexCoords[128-32][4];
// Stateblocks for setting and restoring render states
DWORD m_dwSavedStateBlock;
DWORD m_dwDrawTextStateBlock;
public:
// 2D and 3D text drawing functions
HRESULT DrawText( FLOAT x, FLOAT y, DWORD dwColor,
TCHAR* strText, DWORD dwFlags=0L );
HRESULT DrawTextScaled( FLOAT x, FLOAT y, FLOAT z,
FLOAT fXScale, FLOAT fYScale, DWORD dwColor,
TCHAR* strText, DWORD dwFlags=0L );
HRESULT Render3DText( TCHAR* strText, DWORD dwFlags=0L );
// Function to get extent of text
HRESULT GetTextExtent( TCHAR* strText, SIZE* pSize );
// Initializing and destroying device-dependent objects
HRESULT InitDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice );
HRESULT RestoreDeviceObjects();
HRESULT InvalidateDeviceObjects();
HRESULT DeleteDeviceObjects();
// Constructor / destructor
CD3DFont( TCHAR* strFontName, DWORD dwHeight, DWORD dwFlags=0L );
~CD3DFont();
};
#endif

View File

@@ -0,0 +1,34 @@
//-----------------------------------------------------------------------------
// File: D3DRes.h
//
// Desc: Resource definitions required by the CD3DApplication class.
// Any application using the CD3DApplication class must include resources
// with the following identifiers.
//
// Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef D3DRES_H
#define D3DRES_H
#define IDI_MAIN_ICON 101 // Application icon
#define IDR_MAIN_ACCEL 113 // Keyboard accelerator
#define IDR_MENU 141 // Application menu
#define IDR_POPUP 142 // Popup menu
#define IDD_SELECTDEVICE 144 // "Change Device" dialog box
#define IDC_ADAPTER_COMBO 1002 // Adapter combobox for "SelectDevice" dlg
#define IDC_DEVICE_COMBO 1000 // Device combobox for "SelectDevice" dlg
#define IDC_FULLSCREENMODES_COMBO 1003 // Mode combobox for "SelectDevice" dlg
#define IDC_MULTISAMPLE_COMBO 1005 // MultiSample combobox for "SelectDevice" dlg
#define IDC_WINDOW 1016 // Radio button for windowed-mode
#define IDC_FULLSCREEN 1018 // Radio button for fullscreen-mode
#define IDM_CHANGEDEVICE 40002 // Command to invoke "Change Device" dlg
#define IDM_TOGGLEFULLSCREEN 40003 // Command to toggle fullscreen mode
#define IDM_TOGGLESTART 40004 // Command to toggle frame animation
#define IDM_SINGLESTEP 40005 // Command to single step frame animation
#define IDM_EXIT 40006 // Command to exit the application
#endif // D3DRES_H

View File

@@ -0,0 +1,314 @@
//-----------------------------------------------------------------------------
// File: D3DSaver.h
//
// Desc: Framework for screensavers that use Direct3D 8.0.
//
// Copyright (c) 2000 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef _D3DSAVER_H
#define _D3DSAVER_H
//-----------------------------------------------------------------------------
// Error codes
//-----------------------------------------------------------------------------
enum APPMSGTYPE { MSG_NONE, MSGERR_APPMUSTEXIT, MSGWARN_SWITCHEDTOREF };
#define D3DAPPERR_NODIRECT3D 0x82000001
#define D3DAPPERR_NOWINDOW 0x82000002
#define D3DAPPERR_NOCOMPATIBLEDEVICES 0x82000003
#define D3DAPPERR_NOWINDOWABLEDEVICES 0x82000004
#define D3DAPPERR_NOHARDWAREDEVICE 0x82000005
#define D3DAPPERR_HALNOTCOMPATIBLE 0x82000006
#define D3DAPPERR_NOWINDOWEDHAL 0x82000007
#define D3DAPPERR_NODESKTOPHAL 0x82000008
#define D3DAPPERR_NOHALTHISMODE 0x82000009
#define D3DAPPERR_NONZEROREFCOUNT 0x8200000a
#define D3DAPPERR_MEDIANOTFOUND 0x8200000b
#define D3DAPPERR_RESIZEFAILED 0x8200000c
#define D3DAPPERR_INITDEVICEOBJECTSFAILED 0x8200000d
#define D3DAPPERR_CREATEDEVICEFAILED 0x8200000e
#define D3DAPPERR_NOPREVIEW 0x8200000f
//-----------------------------------------------------------------------------
// Constants
//-----------------------------------------------------------------------------
#define MAX_DISPLAYS 9
#define NO_ADAPTER 0xffffffff
#define NO_MONITOR 0xffffffff
//***************************************************************************************
// Modes of operation for screensaver
enum SaverMode
{
sm_config, // Config dialog box
sm_preview, // Mini preview window in Display Properties dialog
sm_full, // Full-on screensaver mode
sm_test, // Test mode
sm_passwordchange // Change password
};
// Prototype for VerifyScreenSavePwd() in password.cpl, used on Win9x
typedef BOOL (PASCAL * VERIFYPWDPROC) (HWND);
//-----------------------------------------------------------------------------
// Name: struct D3DModeInfo
// Desc: Structure for holding information about a display mode
//-----------------------------------------------------------------------------
struct D3DModeInfo
{
DWORD Width; // Screen width in this mode
DWORD Height; // Screen height in this mode
D3DFORMAT Format; // Pixel format in this mode
DWORD dwBehavior; // Hardware / Software / Mixed vertex processing
D3DFORMAT DepthStencilFormat; // Which depth/stencil format to use with this mode
};
//-----------------------------------------------------------------------------
// Name: struct D3DWindowedModeInfo
// Desc: Structure for holding information about a display mode
//-----------------------------------------------------------------------------
struct D3DWindowedModeInfo
{
D3DFORMAT DisplayFormat;
D3DFORMAT BackBufferFormat;
DWORD dwBehavior; // Hardware / Software / Mixed vertex processing
D3DFORMAT DepthStencilFormat; // Which depth/stencil format to use with this mode
};
//-----------------------------------------------------------------------------
// Name: struct D3DDeviceInfo
// Desc: Structure for holding information about a Direct3D device, including
// a list of modes compatible with this device
//-----------------------------------------------------------------------------
struct D3DDeviceInfo
{
// Device data
D3DDEVTYPE DeviceType; // Reference, HAL, etc.
D3DCAPS8 d3dCaps; // Capabilities of this device
const TCHAR* strDesc; // Name of this device
BOOL bCanDoWindowed; // Whether this device can work in windowed mode
// Modes for this device
DWORD dwNumModes;
D3DModeInfo modes[150];
// Current state
DWORD dwCurrentMode;
BOOL bWindowed;
D3DMULTISAMPLE_TYPE MultiSampleType;
};
//-----------------------------------------------------------------------------
// Name: struct D3DAdapterInfo
// Desc: Structure for holding information about an adapter, including a list
// of devices available on this adapter
//-----------------------------------------------------------------------------
struct D3DAdapterInfo
{
// Adapter data
DWORD iMonitor; // Which MonitorInfo corresponds to this adapter
D3DADAPTER_IDENTIFIER8 d3dAdapterIdentifier;
D3DDISPLAYMODE d3ddmDesktop; // Desktop display mode for this adapter
// Devices for this adapter
DWORD dwNumDevices;
D3DDeviceInfo devices[3];
BOOL bHasHAL;
BOOL bHasAppCompatHAL;
BOOL bHasSW;
BOOL bHasAppCompatSW;
// User's preferred mode settings for this adapter
DWORD dwUserPrefWidth;
DWORD dwUserPrefHeight;
D3DFORMAT d3dfmtUserPrefFormat;
BOOL bLeaveBlack; // If TRUE, don't render to this display
BOOL bDisableHW; // If TRUE, don't use HAL on this display
// Current state
DWORD dwCurrentDevice;
HWND hWndDevice;
};
//-----------------------------------------------------------------------------
// Name: struct MonitorInfo
// Desc: Structure for holding information about a monitor
//-----------------------------------------------------------------------------
struct MonitorInfo
{
TCHAR strDeviceName[128];
TCHAR strMonitorName[128];
HMONITOR hMonitor;
RECT rcScreen;
DWORD iAdapter; // Which D3DAdapterInfo corresponds to this monitor
HWND hWnd;
// Error message state
FLOAT xError;
FLOAT yError;
FLOAT widthError;
FLOAT heightError;
FLOAT xVelError;
FLOAT yVelError;
};
//-----------------------------------------------------------------------------
// Name: struct RenderUnit
// Desc:
//-----------------------------------------------------------------------------
struct RenderUnit
{
UINT iAdapter;
UINT iMonitor;
D3DDEVTYPE DeviceType; // Reference, HAL, etc.
DWORD dwBehavior;
IDirect3DDevice8* pd3dDevice;
D3DPRESENT_PARAMETERS d3dpp;
BOOL bDeviceObjectsInited; // InitDeviceObjects was called
BOOL bDeviceObjectsRestored; // RestoreDeviceObjects was called
TCHAR strDeviceStats[90];// String to hold D3D device stats
TCHAR strFrameStats[40]; // String to hold frame stats
};
//-----------------------------------------------------------------------------
// Name: class CD3DScreensaver
// Desc: D3D screensaver class
//-----------------------------------------------------------------------------
class CD3DScreensaver
{
public:
CD3DScreensaver();
virtual HRESULT Create( HINSTANCE hInstance );
virtual INT Run();
HRESULT DisplayErrorMsg( HRESULT hr, DWORD dwType = 0 );
protected:
SaverMode ParseCommandLine( TCHAR* pstrCommandLine );
VOID ChangePassword();
HRESULT DoSaver();
virtual VOID DoConfig() { }
virtual VOID ReadSettings() {};
VOID ReadScreenSettings( HKEY hkeyParent );
VOID WriteScreenSettings( HKEY hkeyParent );
virtual VOID DoPaint( HWND hwnd, HDC hdc );
HRESULT Initialize3DEnvironment();
VOID Cleanup3DEnvironment();
HRESULT Render3DEnvironment();
static LRESULT CALLBACK SaverProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
virtual LRESULT SaverProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
VOID InterruptSaver();
VOID ShutdownSaver();
VOID DoScreenSettingsDialog( HWND hwndParent );
static INT_PTR CALLBACK ScreenSettingsDlgProcStub( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
INT_PTR ScreenSettingsDlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
VOID SetupAdapterPage(HWND hWnd);
HRESULT CreateSaverWindow();
HRESULT BuildDeviceList();
BOOL FindDepthStencilFormat( UINT iAdapter, D3DDEVTYPE DeviceType,
D3DFORMAT TargetFormat, D3DFORMAT* pDepthStencilFormat );
HRESULT CheckWindowedFormat( UINT iAdapter, D3DWindowedModeInfo* pD3DWindowedModeInfo );
HRESULT CreateFullscreenRenderUnit( RenderUnit* pRenderUnit );
HRESULT CreateWindowedRenderUnit( RenderUnit* pRenderUnit );
BOOL FindNextLowerMode( D3DDeviceInfo* pD3DDeviceInfo );
VOID SwitchToRenderUnit( UINT iRenderUnit );
VOID BuildProjectionMatrix( FLOAT fNear, FLOAT fFar, D3DXMATRIX* pMatrix );
HRESULT SetProjectionMatrix( FLOAT fNear, FLOAT fFar );
virtual VOID UpdateDeviceStats();
virtual VOID UpdateFrameStats();
virtual BOOL GetTextForError( HRESULT hr, TCHAR* pszError, DWORD dwNumChars );
VOID UpdateErrorBox();
VOID EnumMonitors( VOID );
BOOL GetBestAdapter( DWORD* piAdapter );
virtual VOID SetDevice( UINT iDevice ) { }
virtual HRESULT RegisterSoftwareDevice() { return S_OK; }
virtual HRESULT ConfirmDevice(D3DCAPS8* pCaps, DWORD dwBehavior,
D3DFORMAT fmtBackBuffer) { return S_OK; }
virtual HRESULT ConfirmMode( LPDIRECT3DDEVICE8 pd3dDev ) { return S_OK; }
virtual HRESULT OneTimeSceneInit() { return S_OK; }
virtual HRESULT InitDeviceObjects() { return S_OK; }
virtual HRESULT RestoreDeviceObjects() { return S_OK; }
virtual HRESULT FrameMove() { return S_OK; }
virtual HRESULT Render() { return S_OK; }
virtual HRESULT InvalidateDeviceObjects() { return S_OK; }
virtual HRESULT DeleteDeviceObjects() { return S_OK; }
virtual HRESULT FinalCleanup() { return S_OK; }
protected:
SaverMode m_SaverMode; // sm_config, sm_full, sm_preview, etc.
BOOL m_bAllScreensSame; // If TRUE, show same image on all screens
HWND m_hWnd; // Focus window and device window on primary
HWND m_hWndParent;
HINSTANCE m_hInstance;
BOOL m_bWaitForInputIdle; // Used to pause when preview starts
DWORD m_dwSaverMouseMoveCount;
BOOL m_bIs9x;
HINSTANCE m_hPasswordDLL;
VERIFYPWDPROC m_VerifySaverPassword;
BOOL m_bCheckingSaverPassword;
BOOL m_bWindowed;
// Variables for non-fatal error management
BOOL m_bErrorMode; // Whether to display an error
HRESULT m_hrError; // Error code to display
TCHAR m_szError[400]; // Error message text
MonitorInfo m_Monitors[MAX_DISPLAYS];
DWORD m_dwNumMonitors;
RenderUnit m_RenderUnits[MAX_DISPLAYS];
DWORD m_dwNumRenderUnits;
D3DAdapterInfo* m_Adapters[MAX_DISPLAYS];
DWORD m_dwNumAdapters;
IDirect3D8* m_pD3D;
IDirect3DDevice8* m_pd3dDevice; // Current D3D device
RECT m_rcRenderTotal; // Rect of entire area to be rendered
RECT m_rcRenderCurDevice; // Rect of render area of current device
D3DSURFACE_DESC m_d3dsdBackBuffer; // Info on back buffer for current device
TCHAR m_strWindowTitle[200]; // Title for the app's window
BOOL m_bAllowRef; // Whether to allow REF D3D device
BOOL m_bUseDepthBuffer; // Whether to autocreate depthbuffer
BOOL m_bMultithreaded; // Whether to make D3D thread-safe
BOOL m_bOneScreenOnly; // Only ever show screensaver on one screen
TCHAR m_strRegPath[200]; // Where to store registry info
DWORD m_dwMinDepthBits; // Minimum number of bits needed in depth buffer
DWORD m_dwMinStencilBits; // Minimum number of bits needed in stencil buffer
D3DSWAPEFFECT m_SwapEffectFullscreen; // SwapEffect to use in fullscreen Present()
D3DSWAPEFFECT m_SwapEffectWindowed; // SwapEffect to use in windowed Present()
// Variables for timing
FLOAT m_fTime; // Current time in seconds
FLOAT m_fElapsedTime; // Time elapsed since last frame
FLOAT m_fFPS; // Instanteous frame rate
TCHAR m_strDeviceStats[90];// D3D device stats for current device
TCHAR m_strFrameStats[40]; // Frame stats for current device
};
#endif

View File

@@ -0,0 +1,256 @@
//-----------------------------------------------------------------------------
// File: D3DUtil.h
//
// Desc: Helper functions and typing shortcuts for Direct3D programming.
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
#ifndef D3DUTIL_H
#define D3DUTIL_H
#include <D3D8.h>
#include <D3DX8Math.h>
//-----------------------------------------------------------------------------
// Name: D3DUtil_InitMaterial()
// Desc: Initializes a D3DMATERIAL8 structure, setting the diffuse and ambient
// colors. It does not set emissive or specular colors.
//-----------------------------------------------------------------------------
VOID D3DUtil_InitMaterial( D3DMATERIAL8& mtrl, FLOAT r=0.0f, FLOAT g=0.0f,
FLOAT b=0.0f, FLOAT a=1.0f );
//-----------------------------------------------------------------------------
// Name: D3DUtil_InitLight()
// Desc: Initializes a D3DLIGHT structure, setting the light position. The
// diffuse color is set to white, specular and ambient left as black.
//-----------------------------------------------------------------------------
VOID D3DUtil_InitLight( D3DLIGHT8& light, D3DLIGHTTYPE ltType,
FLOAT x=0.0f, FLOAT y=0.0f, FLOAT z=0.0f );
//-----------------------------------------------------------------------------
// Name: D3DUtil_CreateTexture()
// Desc: Helper function to create a texture. It checks the root path first,
// then tries the DXSDK media path (as specified in the system registry).
//-----------------------------------------------------------------------------
HRESULT D3DUtil_CreateTexture( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strTexture,
LPDIRECT3DTEXTURE8* ppTexture,
D3DFORMAT d3dFormat = D3DFMT_UNKNOWN );
//-----------------------------------------------------------------------------
// Name: D3DUtil_SetColorKey()
// Desc: Changes all texels matching the colorkey to transparent, black.
//-----------------------------------------------------------------------------
HRESULT D3DUtil_SetColorKey( LPDIRECT3DTEXTURE8 pTexture, DWORD dwColorKey );
//-----------------------------------------------------------------------------
// Name: D3DUtil_CreateVertexShader()
// Desc: Assembles and creates a file-based vertex shader
//-----------------------------------------------------------------------------
HRESULT D3DUtil_CreateVertexShader( LPDIRECT3DDEVICE8 pd3dDevice,
TCHAR* strFilename, DWORD* pdwVertexDecl,
DWORD* pdwVertexShader );
//-----------------------------------------------------------------------------
// Name: D3DUtil_GetCubeMapViewMatrix()
// Desc: Returns a view matrix for rendering to a face of a cubemap.
//-----------------------------------------------------------------------------
D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace );
//-----------------------------------------------------------------------------
// Name: D3DUtil_GetRotationFromCursor()
// Desc: Returns a quaternion for the rotation implied by the window's cursor
// position.
//-----------------------------------------------------------------------------
D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
FLOAT fTrackBallRadius=1.0f );
//-----------------------------------------------------------------------------
// Name: D3DUtil_SetDeviceCursor
// Desc: Builds and sets a cursor for the D3D device based on hCursor.
//-----------------------------------------------------------------------------
HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor,
BOOL bAddWatermark );
//-----------------------------------------------------------------------------
// Name: class CD3DArcBall
// Desc:
//-----------------------------------------------------------------------------
class CD3DArcBall
{
INT m_iWidth; // ArcBall's window width
INT m_iHeight; // ArcBall's window height
FLOAT m_fRadius; // ArcBall's radius in screen coords
FLOAT m_fRadiusTranslation; // ArcBall's radius for translating the target
D3DXQUATERNION m_qDown; // Quaternion before button down
D3DXQUATERNION m_qNow; // Composite quaternion for current drag
D3DXMATRIX m_matRotation; // Matrix for arcball's orientation
D3DXMATRIX m_matRotationDelta; // Matrix for arcball's orientation
D3DXMATRIX m_matTranslation; // Matrix for arcball's position
D3DXMATRIX m_matTranslationDelta; // Matrix for arcball's position
BOOL m_bDrag; // Whether user is dragging arcball
BOOL m_bRightHanded; // Whether to use RH coordinate system
D3DXVECTOR3 ScreenToVector( int sx, int sy );
public:
LRESULT HandleMouseMessages( HWND, UINT, WPARAM, LPARAM );
D3DXMATRIX* GetRotationMatrix() { return &m_matRotation; }
D3DXMATRIX* GetRotationDeltaMatrix() { return &m_matRotationDelta; }
D3DXMATRIX* GetTranslationMatrix() { return &m_matTranslation; }
D3DXMATRIX* GetTranslationDeltaMatrix() { return &m_matTranslationDelta; }
BOOL IsBeingDragged() { return m_bDrag; }
VOID SetRadius( FLOAT fRadius );
VOID SetWindow( INT w, INT h, FLOAT r=0.9 );
VOID SetRightHanded( BOOL bRightHanded ) { m_bRightHanded = bRightHanded; }
CD3DArcBall();
};
//-----------------------------------------------------------------------------
// Name: class CD3DCamera
// Desc:
//-----------------------------------------------------------------------------
class CD3DCamera
{
D3DXVECTOR3 m_vEyePt; // Attributes for view matrix
D3DXVECTOR3 m_vLookatPt;
D3DXVECTOR3 m_vUpVec;
D3DXVECTOR3 m_vView;
D3DXVECTOR3 m_vCross;
D3DXMATRIX m_matView;
D3DXMATRIX m_matBillboard; // Special matrix for billboarding effects
FLOAT m_fFOV; // Attributes for projection matrix
FLOAT m_fAspect;
FLOAT m_fNearPlane;
FLOAT m_fFarPlane;
D3DXMATRIX m_matProj;
public:
// Access functions
D3DXVECTOR3 GetEyePt() { return m_vEyePt; }
D3DXVECTOR3 GetLookatPt() { return m_vLookatPt; }
D3DXVECTOR3 GetUpVec() { return m_vUpVec; }
D3DXVECTOR3 GetViewDir() { return m_vView; }
D3DXVECTOR3 GetCross() { return m_vCross; }
D3DXMATRIX GetViewMatrix() { return m_matView; }
D3DXMATRIX GetBillboardMatrix() { return m_matBillboard; }
D3DXMATRIX GetProjMatrix() { return m_matProj; }
VOID SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
D3DXVECTOR3& vUpVec );
VOID SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
FLOAT fFarPlane );
CD3DCamera();
};
//-----------------------------------------------------------------------------
// Helper macros for pixel shader instructions
//-----------------------------------------------------------------------------
// Parameter writemasks
#define D3DPSP_WRITEMASK_B D3DSP_WRITEMASK_0
#define D3DPSP_WRITEMASK_G D3DSP_WRITEMASK_1
#define D3DPSP_WRITEMASK_R D3DSP_WRITEMASK_2
#define D3DPSP_WRITEMASK_A D3DSP_WRITEMASK_3
#define D3DPSP_WRITEMASK_C (D3DPSP_WRITEMASK_B|D3DPSP_WRITEMASK_G|D3DPSP_WRITEMASK_R)
#define D3DPSP_WRITEMASK_ALL (D3DSP_WRITEMASK_0|D3DSP_WRITEMASK_1|D3DSP_WRITEMASK_2|D3DSP_WRITEMASK_3)
#define D3DPSP_WRITEMASK_10 (D3DSP_WRITEMASK_0|D3DSP_WRITEMASK_1)
#define D3DPSP_WRITEMASK_32 (D3DSP_WRITEMASK_2|D3DSP_WRITEMASK_3)
// Source and destination parameter token
#define D3DPS_REGNUM_MASK(_Num) ( (1L<<31) | ((_Num)&D3DSP_REGNUM_MASK) )
#define D3DPS_DST(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSPR_TEMP | D3DPSP_WRITEMASK_ALL )
#define D3DPS_SRC_TEMP(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEMP )
#define D3DPS_SRC_INPUT(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_INPUT )
#define D3DPS_SRC_CONST(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_CONST )
#define D3DPS_SRC_TEXTURE(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEXTURE )
#define D3DVS_SRC_ADDR(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_ADDR )
#define D3DVS_SRC_RASTOUT(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_RASTOUT )
#define D3DVS_SRC_ATTROUT(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_ATTROUT )
#define D3DVS_SRC_TEXCRDOUT(_Num) ( D3DPS_REGNUM_MASK(_Num) | D3DSP_NOSWIZZLE | D3DSPR_TEXCRDOUT )
// Temp destination registers
#define D3DS_DR0 D3DPS_DST(0)
#define D3DS_DR1 D3DPS_DST(1)
#define D3DS_DR2 D3DPS_DST(2)
#define D3DS_DR3 D3DPS_DST(3)
#define D3DS_DR4 D3DPS_DST(4)
#define D3DS_DR5 D3DPS_DST(5)
#define D3DS_DR6 D3DPS_DST(6)
#define D3DS_DR7 D3DPS_DST(7)
// Temp source registers
#define D3DS_SR0 D3DPS_SRC_TEMP(0)
#define D3DS_SR1 D3DPS_SRC_TEMP(1)
#define D3DS_SR2 D3DPS_SRC_TEMP(2)
#define D3DS_SR3 D3DPS_SRC_TEMP(3)
#define D3DS_SR4 D3DPS_SRC_TEMP(4)
#define D3DS_SR5 D3DPS_SRC_TEMP(5)
#define D3DS_SR6 D3DPS_SRC_TEMP(6)
#define D3DS_SR7 D3DPS_SRC_TEMP(7)
// Texture parameters
#define D3DS_T0 D3DPS_SRC_TEXTURE(0)
#define D3DS_T1 D3DPS_SRC_TEXTURE(1)
#define D3DS_T2 D3DPS_SRC_TEXTURE(2)
#define D3DS_T3 D3DPS_SRC_TEXTURE(3)
#define D3DS_T4 D3DPS_SRC_TEXTURE(4)
#define D3DS_T5 D3DPS_SRC_TEXTURE(5)
#define D3DS_T6 D3DPS_SRC_TEXTURE(6)
#define D3DS_T7 D3DPS_SRC_TEXTURE(7)
// Constant (factor) source parameters
#define D3DS_C0 D3DPS_SRC_CONST(0)
#define D3DS_C1 D3DPS_SRC_CONST(1)
#define D3DS_C2 D3DPS_SRC_CONST(2)
#define D3DS_C3 D3DPS_SRC_CONST(3)
#define D3DS_C4 D3DPS_SRC_CONST(4)
#define D3DS_C5 D3DPS_SRC_CONST(5)
#define D3DS_C6 D3DPS_SRC_CONST(6)
#define D3DS_C7 D3DPS_SRC_CONST(7)
// Iterated source parameters (0==Diffuse, 1==specular)
#define D3DS_V0 D3DPS_SRC_INPUT(0)
#define D3DS_V1 D3DPS_SRC_INPUT(1)
#endif // D3DUTIL_H

View File

@@ -0,0 +1,140 @@
//-----------------------------------------------------------------------------
// File: ddutil.cpp
//
// Desc: Routines for loading bitmap and palettes from resources
//
// Copyright (C) 1998-2001 Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------------------
#ifndef DDUTIL_H
#define DDUTIL_H
#include <ddraw.h>
#include <d3d.h>
//-----------------------------------------------------------------------------
// Classes defined in this header file
//-----------------------------------------------------------------------------
class CDisplay;
class CSurface;
//-----------------------------------------------------------------------------
// Flags for the CDisplay and CSurface methods
//-----------------------------------------------------------------------------
#define DSURFACELOCK_READ
#define DSURFACELOCK_WRITE
//-----------------------------------------------------------------------------
// Name: class CDisplay
// Desc: Class to handle all DDraw aspects of a display, including creation of
// front and back buffers, creating offscreen surfaces and palettes,
// and blitting surface and displaying bitmaps.
//-----------------------------------------------------------------------------
class CDisplay
{
protected:
LPDIRECTDRAW7 m_pDD;
LPDIRECTDRAWSURFACE7 m_pddsFrontBuffer;
LPDIRECTDRAWSURFACE7 m_pddsBackBuffer;
LPDIRECTDRAWSURFACE7 m_pddsBackBufferLeft; // For stereo modes
HWND m_hWnd;
RECT m_rcWindow;
BOOL m_bWindowed;
BOOL m_bStereo;
public:
CDisplay();
~CDisplay();
// Access functions
HWND GetHWnd() { return m_hWnd; }
LPDIRECTDRAW7 GetDirectDraw() { return m_pDD; }
LPDIRECTDRAWSURFACE7 GetFrontBuffer() { return m_pddsFrontBuffer; }
LPDIRECTDRAWSURFACE7 GetBackBuffer() { return m_pddsBackBuffer; }
LPDIRECTDRAWSURFACE7 GetBackBufferLeft() { return m_pddsBackBufferLeft; }
// Status functions
BOOL IsWindowed() { return m_bWindowed; }
BOOL IsStereo() { return m_bStereo; }
// Creation/destruction methods
HRESULT CreateFullScreenDisplay( HWND hWnd, DWORD dwWidth, DWORD dwHeight,
DWORD dwBPP );
HRESULT CreateWindowedDisplay( HWND hWnd, DWORD dwWidth, DWORD dwHeight );
HRESULT InitClipper();
HRESULT UpdateBounds();
virtual HRESULT DestroyObjects();
// Methods to create child objects
HRESULT CreateSurface( CSurface** ppSurface, DWORD dwWidth,
DWORD dwHeight );
HRESULT CreateSurfaceFromBitmap( CSurface** ppSurface, TCHAR* strBMP,
DWORD dwDesiredWidth,
DWORD dwDesiredHeight );
HRESULT CreateSurfaceFromText( CSurface** ppSurface, HFONT hFont,
TCHAR* strText,
COLORREF crBackground,
COLORREF crForeground );
HRESULT CreatePaletteFromBitmap( LPDIRECTDRAWPALETTE* ppPalette, const TCHAR* strBMP );
// Display methods
HRESULT Clear( DWORD dwColor = 0L );
HRESULT ColorKeyBlt( DWORD x, DWORD y, LPDIRECTDRAWSURFACE7 pdds,
RECT* prc = NULL );
HRESULT Blt( DWORD x, DWORD y, LPDIRECTDRAWSURFACE7 pdds,
RECT* prc=NULL, DWORD dwFlags=0 );
HRESULT Blt( DWORD x, DWORD y, CSurface* pSurface, RECT* prc = NULL );
HRESULT ShowBitmap( HBITMAP hbm, LPDIRECTDRAWPALETTE pPalette=NULL );
HRESULT SetPalette( LPDIRECTDRAWPALETTE pPalette );
HRESULT Present();
};
//-----------------------------------------------------------------------------
// Name: class CSurface
// Desc: Class to handle aspects of a DirectDrawSurface.
//-----------------------------------------------------------------------------
class CSurface
{
LPDIRECTDRAWSURFACE7 m_pdds;
DDSURFACEDESC2 m_ddsd;
BOOL m_bColorKeyed;
public:
LPDIRECTDRAWSURFACE7 GetDDrawSurface() { return m_pdds; }
BOOL IsColorKeyed() { return m_bColorKeyed; }
HRESULT DrawBitmap( HBITMAP hBMP, DWORD dwBMPOriginX = 0, DWORD dwBMPOriginY = 0,
DWORD dwBMPWidth = 0, DWORD dwBMPHeight = 0 );
HRESULT DrawBitmap( TCHAR* strBMP, DWORD dwDesiredWidth, DWORD dwDesiredHeight );
HRESULT DrawText( HFONT hFont, TCHAR* strText, DWORD dwOriginX, DWORD dwOriginY,
COLORREF crBackground, COLORREF crForeground );
HRESULT SetColorKey( DWORD dwColorKey );
DWORD ConvertGDIColor( COLORREF dwGDIColor );
static HRESULT GetBitMaskInfo( DWORD dwBitMask, DWORD* pdwShift, DWORD* pdwBits );
HRESULT Create( LPDIRECTDRAW7 pDD, DDSURFACEDESC2* pddsd );
HRESULT Create( LPDIRECTDRAWSURFACE7 pdds );
HRESULT Destroy();
CSurface();
~CSurface();
};
#endif // DDUTIL_H

View File

@@ -0,0 +1,866 @@
//-----------------------------------------------------------------------------
// File: didcfgview.h
//
// Desc: Header file for DIDCfgView, a class that encapsulates a view of a
// DirectInput device. The DIDCfgView class exists to make it easier
// to make custom interfaces to view or configure action mappings for
// input devices(instead of using IDirectInput8::ConfigureDevices).
//
// To use the DIDCfgView class, you initialize it for a particular
// DirectInput device. You then set up state information for how the
// image should be drawn: colors, fonts, and details for callouts(the
// lines drawn from each axis/button to a label). Finally, you can
// call RenderView, passing in a bitmap or HDC for DIDCfgView to draw
// the image to.
//
// DIDCfgView is the only class in this file that you need to understand
// or interface to. The other classes shown here are only used to
// implement the DIDCfgView class.
//
//
// Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef __DIDCV_H__
#define __DIDCV_H__
#include <windows.h>
#include <dinput.h>
// invalid id, used for unique, signed identifiers
#define DIDCV_INVALID_ID -1
// flags for callout states
#define DIDCV_DRAWCALLOUT 0x1
#define DIDCV_DRAWOVERLAY 0x2
#define DIDCV_DRAWHIGHLIGHT 0x4
#define DIDCV_DRAWEMPTYCALLOUT 0x8
#define DIDCV_DRAWFULLNAME 0x10
// all flags set
#define DIDCV_ALL_CALLOUT_STATE_SET 0xFFFFFFFF
// default dimensions
#define DIDCV_MAX_IMAGE_WIDTH 512
#define DIDCV_MAX_IMAGE_HEIGHT 512
#define DIDCV_CUSTOM_VIEW_WIDTH 400
#define DIDCV_CUSTOM_VIEW_HEIGHT 300
// DidcvCreateFont attribute flags
#define DIDCV_FONT_BOLD 0x1
#define DIDCV_FONT_ITALIC 0x2
#define DIDCV_FONT_UNDERLINE 0x4
#define DIDCV_FONT_STRIKEOUT 0x8
// default font point width
#define DIDCV_DEFAULT_FONT_SIZE 90
// default size of callout array in DidcvCalloutManager
#define DIDCV_DEFAULT_CALLOUT_MANAGER_ARRAY_SIZE 0
// default size of callout array in DidcvCalloutSet
#define DIDCV_DEFAULT_CALLOUT_SET_ARRAY_SIZE 8
// forward declarations
class DidcvViewManager;
class DidcvCalloutManager;
class DidcvBitmap;
class DidcvCallout;
class DidcvCalloutSet;
struct DIDCfgViewInfo;
struct DidcvCalloutData;
struct DidcvCalloutState;
struct DidcvActionMap;
struct DidcvCustomViewInfo;
//-----------------------------------------------------------------------------
// Name: class DIDCfgView
// Desc: main interface for retrieving and rendering device configuration views
//
// Init() - initializes the object for a particular DirectInputDevice
// SetActionFormat() - sets the action mapping for the device
// SetCalloutState() - specifies state for a given callout
// SetAllCalloutState() - sets the state for all callouts
// GetObjectIDByLocation() - gets object id at location of current view
// SetCurrentView() - sets the current view to be rendered
// RebuildViews() - collapses or expand the views for this device
// RenderView() - renders device configuration view with image,
// callout information, action mapping.
//-----------------------------------------------------------------------------
class DIDCfgView
{
public:
DIDCfgView();
~DIDCfgView();
public:
// main device configuration view interface
HRESULT Init( LPDIRECTINPUTDEVICE8 pDevice );
HRESULT SetActionFormat( LPDIACTIONFORMAT pDiaf );
HRESULT SetCalloutState( DWORD dwFlags, DWORD dwObjID );
HRESULT SetAllCalloutState( DWORD dwFlags );
HRESULT GetObjectIDByLocation( LPDWORD pdwObjID, LPPOINT pPt );
HRESULT SetCurrentView( INT nView );
HRESULT RebuildViews( BOOL bCompact );
HRESULT RenderView( HBITMAP hBitmap, BOOL bIsDib );
HRESULT RenderView( HDC hdc );
// rendering options
HRESULT SetViewOrigin( const POINT* pPtOrg, POINT* pPtOldOrg );
HRESULT CalcCenterOrgForCurrentView( const SIZE* pImgSize, POINT* pPtOrg, BOOL bSetOrigin = FALSE );
VOID SetDefaultText( const TCHAR* pctszDefaultText, DWORD length );
VOID SetColors( COLORREF crFore, COLORREF crBack, COLORREF crHighlight, COLORREF crHighlightLine );
VOID GetColors( COLORREF* pCrFore, COLORREF* pCrBack, COLORREF* pCrHighlight, COLORREF* pCrHighlightLine );
HRESULT SetFont( const TCHAR* pctszFontName, DWORD dwAttributes );
HFONT GetFont();
// information access functios
HRESULT GetInfo( DIDCfgViewInfo* pCfgViewInfo );
HRESULT GetCurrentView( LPINT lpnView );
HRESULT GetCalloutState( LPDWORD lpdwFlags, DWORD dwObjID );
protected:
// protected helper functions
VOID CleanUp();
HRESULT InitAlloc();
HRESULT InitImageInfoRetrieve( LPDIRECTINPUTDEVICE8 pDevice );
HRESULT InitImageInfoProcess();
VOID CalcSizes( const DIDEVICEIMAGEINFO* prgImageInfoArray, DWORD dwNumElements, const LPDWORD pNumViews, LPDWORD pNumCallouts, LPDWORD pNumDistinctObjID );
HRESULT RenderView( HDC hdc, VOID* pBits, INT width, INT height );
HRESULT InitCustomViews( LPDIRECTINPUTDEVICE8 pDevice, BOOL bUseInternal = FALSE );
HRESULT BuildCustomViews();
VOID RenderFullname( HDC hdc, const TCHAR* pctszFullname, const RECT* pRect );
BOOL CopyActionMap( LPDIACTIONFORMAT pDiaf );
protected:
// data structures for managing views and callouts
DidcvViewManager* m_lpViewManager;
DidcvCalloutManager* m_lpCalloutManager;
// keeps track of whether this has been initialized
BOOL m_bIsInit;
// the index of the next view to render
INT m_nView;
// pointer to the DIDEVICEIMAGEINFOHEADER
LPDIDEVICEIMAGEINFOHEADER m_lpDidImgHeader;
// custom device view data
DidcvCustomViewInfo* m_lpCustomViewInfo;
// device reference
LPDIRECTINPUTDEVICE8 m_lpDIDevice;
// action mapping copy
LPDIACTIONFORMAT m_lpDiaf;
// can this view be collapsed
BOOL m_bCanBeCollapsed;
// is this view compacted
BOOL m_bIsCollapsed;
// rendering options
COLORREF m_crFore;
COLORREF m_crBack;
COLORREF m_crHighlight;
COLORREF m_crHighlightLine;
POINT m_ptOrigin;
HFONT m_hFont;
TCHAR m_tszDefaultText [MAX_PATH];
};
//-----------------------------------------------------------------------------
// Name: struct DIDCfgViewInfo
// Desc: struct containing current information about DIDCfgView states
//-----------------------------------------------------------------------------
struct DIDCfgViewInfo
{
BOOL bIsInit; // is the CfgView object initialized
INT iCurrentViewID; // the ID of the current view
INT iNumTotalViews; // total number of views
DWORD dwNumUniqueCallouts; // number of unique
BOOL bCanBeCollapsed; // can views for this CfgView object be compacted
BOOL bIsCollapsed; // is the CfgView currently using collapsed views
DIDCfgViewInfo()
: bIsInit( FALSE ),
iCurrentViewID( DIDCV_INVALID_ID ),
iNumTotalViews( 0 ),
dwNumUniqueCallouts( 0 ),
bCanBeCollapsed( FALSE ),
bIsCollapsed( FALSE )
{ }
};
//-----------------------------------------------------------------------------
// Name: struct DidcvCustomViewInfo
// Desc: object containing information about custom views
//-----------------------------------------------------------------------------
struct DidcvCustomViewInfo
{
DWORD dwType; // type of the operation(count or save)
DWORD dwCount; // the total count of items
DWORD dwSize; // number of items actually in array
LPDIDEVICEOBJECTINSTANCE* rgObjData; // array of control info
DidcvCustomViewInfo()
: dwType( 0 ),
dwCount( 0 ),
dwSize( 0 ),
rgObjData( NULL )
{ }
~DidcvCustomViewInfo() { CleanUp(); }
VOID CleanUp();
};
//-----------------------------------------------------------------------------
// Name: class DidcvView
// Desc: data structure for representing one view of a device
//-----------------------------------------------------------------------------
class DidcvView
{
public:
virtual ~DidcvView() { };
// DidcvView interface
void SetOffset( INT nOffset );
void SetID( INT nID );
INT GetOffset() const;
INT GetID() const;
// rendering
virtual BOOL GetViewSize( SIZE* pSize ) const = 0;
virtual void Render( HDC hdc, VOID* pBits, INT width, INT height, const POINT* pPtOrigin ) = 0;
protected:
// constructor
DidcvView();
// the original offset in the array returned by DirectInputDevice8::GetImage
INT m_nOffset;
// the internally assigned unique identifier
INT m_nID;
};
//-----------------------------------------------------------------------------
// Name: class DidcvBitmapView
// Desc: subclass of DidcvView that renders a bitmap
//-----------------------------------------------------------------------------
class DidcvBitmapView : public DidcvView
{
public:
~DidcvBitmapView();
// interface
virtual BOOL GetViewSize( SIZE* pSize ) const;
virtual void Render( HDC hdc, VOID* pBits, INT width, INT height, const POINT* pPtOrigin );
public:
// function to instantiate a DidcvBitmapView
static DidcvBitmapView* Create( LPCTSTR ptszImagePath, DWORD dwFlags );
private:
// constructor
DidcvBitmapView();
protected:
// bitmap of the view
DidcvBitmap* m_lpBitmap;
};
//-----------------------------------------------------------------------------
// Name: class DidcvCustomView
// Desc: subclass of DidcvView that renders a view from custom data
//-----------------------------------------------------------------------------
class DidcvCustomView : public DidcvView
{
public:
~DidcvCustomView() { }
// interface
virtual BOOL GetViewSize( SIZE* pSize ) const;
virtual void Render( HDC hdc, VOID* pBits, INT width, INT height, const POINT* pPtOrigin );
public:
// static function to instantiate a DidcvCustomView
static DidcvCustomView* Create( const DidcvCustomViewInfo* pInfo, DWORD dwStartIndex, DWORD* pFinishIndex );
static BOOL CalcImageInfo( DWORD index, LPRECT rcOverlay, LPDWORD pNumPoints, LPPOINT rgptCalloutLine, LPRECT rcCalloutRect );
static DWORD CalcNumViews( DWORD dwCount );
private:
// constructor
DidcvCustomView( const DidcvCustomViewInfo* pInfo, DWORD dwStartIndex, DWORD dwEndIndex );
protected:
// returns coordinates for a given index
void CalcCoordinates( DWORD dwIndex, LPRECT lpRect );
// custom data from which to render the view
const DidcvCustomViewInfo* m_lpCustomViewInfoRef;
DWORD m_dwStartIndex;
DWORD m_dwEndIndex;
};
//-----------------------------------------------------------------------------
// Name: class DidcvViewManager
// Desc: stores and manages all the views for particular device
//-----------------------------------------------------------------------------
class DidcvViewManager
{
public:
DidcvViewManager();
~DidcvViewManager();
// interface
BOOL SetCapacity( UINT uCapacity, BOOL bDeleteContent = TRUE );
BOOL AddImage( DidcvView* pView, INT nOffset = DIDCV_INVALID_ID );
DidcvView* GetImage( INT nID );
DidcvView* GetImageByOffset( INT nOffset );
UINT GetNumViews() const;
void CleanUp();
protected:
// table holding references to views, indexed by the internal unique identifier
DidcvView ** m_ppViewTable;
UINT m_capacity;
UINT m_size;
};
//-----------------------------------------------------------------------------
// Name: class DidcvCallout
// Desc: data structure for representing one callout on a particular view
// A callout is the line drawn from each axis/button to a label
//-----------------------------------------------------------------------------
class DidcvCallout
{
public:
~DidcvCallout();
// accessor functions to information retrieved from DirectInput
DWORD GetObjID() const;
const RECT & GetOverlayRect() const;
const RECT & GetCalloutRect() const;
const POINT* GetCalloutLine( DWORD* lpNumPts ) const;
DWORD GetTextAlign() const;
DWORD GetOverlayOffset() const;
// accessor functions to internal data
void SetAssociatedViewID( INT nViewRef );
INT GetAssociatedViewID() const;
void SetDataRef( const DidcvCalloutData* lpData );
const DidcvCalloutData* GetDataRef() const;
// hit test for a given point
DWORD HitTest( LPPOINT lpPt ) const;
// draw the overlay
DWORD DrawOverlay( HDC hDC, VOID* lpBits, INT width, INT height, const POINT* pptOrigin );
public:
// static functions to instantiate a callout object
static DidcvCallout* Create( LPDIDEVICEIMAGEINFO devImgInfo );
private:
// private constructor
DidcvCallout();
protected:
// copy of device information
DIDEVICEIMAGEINFO m_devImgInfo;
// the view that this particular callout is associated with
INT m_nViewRef;
// pointer to callout state/data
const DidcvCalloutData* m_lpDataRef;
// the bitmap of the overlay
DidcvBitmap* m_lpOverlayBitmap;
};
// default array size
#define GW_ARRAY_DEFAULT_SIZE 4
//-----------------------------------------------------------------------------
// Name: class GwArray
// Desc: templated c-style array class for PRIMITIVE data types only
//-----------------------------------------------------------------------------
template <class Item>
class GwArray
{
public:
// constructors
GwArray() { this->Alloc( GW_ARRAY_DEFAULT_SIZE ); m_size = 0; }
GwArray( UINT initCap ) { this->Alloc( initCap ); m_size = 0; }
~GwArray() { this->DeAlloc(); m_size = 0; }
public:
void SetSize( UINT newCap )
{
if( newCap == m_capacity )
return;
m_size =( newCap < m_size ? newCap : m_size );
UINT old_cap = m_capacity;
UINT numoverlap =( newCap > m_capacity ? m_capacity : newCap );
Item* oldList = m_list;
this->Alloc( newCap );
if( oldList )
{
memcpy( m_list, oldList, sizeof( Item )* numoverlap );
// free( oldList );
delete [] oldList;
}
}
Item & operator[]( UINT index )
{
if( index >= m_capacity )
assert( index >= 0 && index < m_capacity );
return m_list[index];
}
const Item & operator[]( UINT index ) const
{
if( index >= m_capacity )
assert( index >= 0 && index < m_capacity );
return m_list[index];
}
void PushBack( const Item & item )
{
if( m_capacity == 0 )
SetSize( 2 );
else if( m_size >= m_capacity )
SetSize( m_capacity* 2 );
m_list[m_size] = item;
m_size++;
}
void Resize( UINT newCap ) { SetSize( newCap ); }
void PopBack() { if( m_size ) m_size--; }
void Clear() { m_size = 0; }
void Trim() { Resize( m_size ); }
UINT Capacity() const { return m_capacity; }
UINT Size() const { return m_size; }
protected:
inline void Alloc( UINT cap )
{
if( cap == 0 )
m_list = NULL;
else
{
m_list = new Item[cap];
assert( m_list );
memset( m_list, 0, sizeof( Item )* cap );
}
m_capacity = cap;
}
inline void DeAlloc()
{
if( m_list != NULL )
{
//free( m_list );
delete [] m_list;
m_list = NULL;
}
m_capacity = 0;
}
protected:
// array
Item* m_list;
// number of entries the array can hold
UINT m_capacity;
// number of entries added using PushBack() minus # removed using PopBack()
UINT m_size;
};
//-----------------------------------------------------------------------------
// Name: class DidcvCalloutApplicant
// Desc: abstract base class for processing DidcvCallout
//-----------------------------------------------------------------------------
class DidcvCalloutApplicant
{
public:
virtual ~DidcvCalloutApplicant() { }
virtual BOOL Apply( DidcvCallout* pCallout ) = 0;
};
//-----------------------------------------------------------------------------
// Name: class DidcvCalloutSet
// Desc: a group of DidcvCallout references
// A callout is the line drawn from each axis/button to a label
//-----------------------------------------------------------------------------
class DidcvCalloutSet
{
public:
DidcvCalloutSet();
~DidcvCalloutSet();
public:
BOOL AddCallout( DidcvCallout* pCallout );
void Apply( DidcvCalloutApplicant* pCalloutApp );
void SetIdentifier( DWORD dwID );
DWORD GetIdentifier() const;
void SetData( void* pData );
void* GetData() const;
const GwArray <DidcvCallout*> & GetInternalArrayRef() const;
void TrimArrays();
protected:
void CleanUp();
protected:
GwArray <DidcvCallout*> m_calloutList;
DWORD m_dwSetID;
void* m_lpData;
};
//-----------------------------------------------------------------------------
// Name: class DidcvCalloutManager
// Desc: data structure for storing and managing callouts
// A callout is the line drawn from each axis/button to a label
//-----------------------------------------------------------------------------
class DidcvCalloutManager
{
public:
DidcvCalloutManager();
~DidcvCalloutManager();
// main interface
BOOL AddCallout( DidcvCallout* pCallout, INT nView );
BOOL SetCalloutState( const DidcvCalloutState* pCalloutState, DWORD dwObjID );
BOOL SetAllCalloutState( const DidcvCalloutState* pCalloutState );
BOOL SetActionMap( const LPDIACTION pAction, DWORD dwObjID );
void ClearAllActionMaps();
// information
DWORD GetObjectIDByLocation( const LPPOINT pPt, INT nView );
BOOL GetCalloutState( DidcvCalloutState* pCalloutState, DWORD dwObjID );
BOOL GetActionMap( DidcvActionMap* pActionMap, DWORD dwObjID );
const DidcvCalloutSet* GetCalloutSetByView( INT nView ) const;
const DidcvCalloutSet* GetCalloutSetByObjID( DWORD dwObjID ) const;
const DidcvCalloutData* GetCalloutDataRef( DWORD dwObjID ) const;
UINT GetNumUniqueCallouts() const;
BOOL EnumObjects( LPDIRECTINPUTDEVICE8 pDevice, LPDIENUMDEVICEOBJECTSCALLBACK pCallback, LPVOID pvRef, DWORD dwMapOnly );
BOOL CalcCanBeCollapsed();
// allocation
BOOL SetCapacity( DWORD dwNumCallouts, DWORD dwNumUniqueObjID, DWORD dwNumViews, BOOL bDeleteContent = TRUE );
void TrimArrays();
void CleanUp();
protected:
// helper functions
DidcvCalloutSet* Find( const GwArray <DidcvCalloutSet*> & array, DWORD dwIdentifier ) const;
DidcvCalloutData* GetCalloutData( DWORD dwObjID ) const;
protected:
// list of all callouts added
GwArray <DidcvCallout*> m_calloutList;
// list of callout sets, one for each unique callout id
GwArray <DidcvCalloutSet*> m_calloutSetListByObjID;
// list of callout sets, one for each view
GwArray <DidcvCalloutSet*> m_calloutSetListByView;
};
//-----------------------------------------------------------------------------
// Name: struct DidcvCalloutData
// Desc: data structure holding references to callout data components
//-----------------------------------------------------------------------------
struct DidcvCalloutData
{
DidcvCalloutState* lpState; // callout state info
DidcvActionMap* lpActionMap; // action mapped this callout
DidcvCalloutData( DidcvCalloutState* s, DidcvActionMap* a )
: lpState( s ), lpActionMap( a )
{ }
};
//-----------------------------------------------------------------------------
// Name: struct DidcvCalloutState
// Desc: state information for a callout
//-----------------------------------------------------------------------------
struct DidcvCalloutState
{
// whether to draw
BOOL bDrawCallout;
BOOL bDrawOverlay;
BOOL bDrawHighlight;
BOOL bDrawEmptyCallout;
BOOL bDrawFullname;
// specifies which state is valid
DWORD dwFlags;
// --- member functions ---
DidcvCalloutState( DWORD f = 0, BOOL c = FALSE, BOOL o = FALSE,
BOOL h = FALSE, BOOL e = FALSE, BOOL d = FALSE )
: dwFlags( f ), bDrawCallout( c ), bDrawOverlay( o ), bDrawHighlight( h ),
bDrawEmptyCallout( e ), bDrawFullname( d )
{ }
void SmartSet( const DidcvCalloutState* other );
void Copy( const DidcvCalloutState* other ) { *this = *other; }
DWORD MakeFlag() const;
void SetFlag( DWORD dwExtFlags );
};
//-----------------------------------------------------------------------------
// Name: struct DidcvActionMap
// Desc: action mapping information for a callout
//-----------------------------------------------------------------------------
struct DidcvActionMap
{
DIACTION dia;
DidcvActionMap() { ZeroMemory( &dia, sizeof( DIACTION ) ); }
void Copy( const DidcvActionMap* other ) { this->dia = other->dia; }
LPCSTR GetActionName() const { return dia.lptszActionName; }
};
// utility functions
void DidcvPolyLineArrow( HDC hDC, const POINT* rgpt, INT nPoints, BOOL bDoShadow = FALSE );
HFONT DidcvCreateFont( HDC hdc, const TCHAR* szFaceName, int iDeciPtHeight, int iDeciPtWidth, int iAttributes, BOOL fLogRes);
//-----------------------------------------------------------------------------
// Name: struct rgref
// Desc: templated lightweight c-style array
//-----------------------------------------------------------------------------
template <class T>
struct rgref {
rgref( T* p ) : pt( p ) {}
T & operator []( int i ) { return pt[i]; }
const T & operator []( int i ) const { return pt[i]; }
private:
T *pt;
};
//-----------------------------------------------------------------------------
// Name: struct SPOINT
// Desc: used by line drawing routine
//-----------------------------------------------------------------------------
struct SPOINT {
SPOINT()
#define SPOINT_INITIALIZERS \
p( u.p ), \
s( u.s ), \
a((( int* )( void* ) u.a ) ), \
x( u.p.x ), \
y( u.p.y ), \
cx( u.s.cx ), \
cy( u.s.cy )
: SPOINT_INITIALIZERS
{ x = y = 0; }
SPOINT( int, POINT *r )
: p( *r ),
s( *(( SIZE* )( void* ) r ) ),
a((( int* )( void* ) r ) ),
x( r->x ),
y( r->y ),
cx( r->x ),
cy( r->y )
{ }
SPOINT( const SPOINT & sp )
: SPOINT_INITIALIZERS
{ p = sp.p; }
SPOINT( int b, int c )
: SPOINT_INITIALIZERS
{ x = b; y = c; }
SPOINT( const POINT &point )
: SPOINT_INITIALIZERS
{ p = point; }
SPOINT( const SIZE &size )
: SPOINT_INITIALIZERS
{ s = size; }
#undef SPOINT_INITIALIZERS
SPOINT operator =( const SPOINT &sp ) { p = sp.p; return *this; }
SPOINT operator =( const POINT &_p ) { p = _p; return *this; }
SPOINT operator =( const SIZE &_s ) { s = _s; return *this; }
operator POINT() const { return p; }
operator SIZE() const { return s; }
long &x, &y, &cx, &cy;
POINT &p;
SIZE &s;
rgref<int> a;
private:
union {
POINT p;
SIZE s;
int a[2];
} u;
};
//-----------------------------------------------------------------------------
// Name: class DidcvBitmap
// Desc: object containing a bitmap
//-----------------------------------------------------------------------------
class DidcvBitmap
{
public:
~DidcvBitmap();
// drawing interface
BOOL Draw( HDC hDC, INT xStart, INT yStart);
BOOL Blend( HDC hDC, INT xStart, INT yStart );
BOOL Blend( VOID* lpBits, INT xStart, INT yStart, INT width, INT height );
// information
BOOL GetSize( SIZE* lpSize ) const;
HBITMAP GetHandle();
LPVOID GetBits();
public:
// static function for instantiating a DidcvBitmap
static DidcvBitmap* Create( LPCTSTR tszFilename );
static DidcvBitmap* Create( INT width, INT height );
private:
// private constructor
DidcvBitmap();
protected:
// helper functions
void CleanUp();
void FigureSize();
static DidcvBitmap* CreateViaD3dx( LPCTSTR tszFilename );
static DidcvBitmap* CreateViaLoadImage( HINSTANCE hinst, LPCTSTR tszName,
UINT uType, int cx, int cy, UINT fuLoad );
protected:
// GDI handle to bitmap
HBITMAP m_hbitmap;
VOID* m_lpBits;
SIZE m_size;
};
// alpha blending information
#define DIDCV_ALPHABLEND_DLL_NAME TEXT( "MSIMG32.DLL" )
#define DIDCV_ALPHABLEND_PROC_NAME TEXT( "AlphaBlend" )
#if( WINVER >= 0x400 )
typedef WINGDIAPI BOOL( WINAPI* DIDCV_ALPHABLEND )( HDC, int, int, int, int, HDC, int, int, int, int, BLENDFUNCTION );
#else
typedef DIDCV_ALPHABLEND DWORD
#endif
//-----------------------------------------------------------------------------
// Name: class DidcvAlphaBlend
// Desc: utility class for alpha blending
//-----------------------------------------------------------------------------
class DidcvAlphaBlend
{
public:
// reference counting interface
static BOOL AddClient();
static BOOL ReleaseClient();
// functions to perform blending
static BOOL Blend( HDC hDC, INT xStart, INT yStart, INT width, INT height, HBITMAP hbitmap, const SIZE* lpSize );
static BOOL Blend( VOID* lpDestBits, INT xStart, INT yStart, INT destWidth, INT destHeight, VOID* lpSrcBits, INT srcWidth, INT srcHeight );
protected:
static DIDCV_ALPHABLEND s_alphaBlendProc;
static HMODULE s_hDll;
static DWORD s_dwNumClients;
};
#endif // #ifndef __DIDCV_H__

View File

@@ -0,0 +1,62 @@
//-----------------------------------------------------------------------------
// File: DIUtil.h
//
// Desc: DirectInput support using action mapping
//
// Copyright (C) 1995-2001 Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------------------
#ifndef DIUTIL_H
#define DIUTIL_H
#include <dinput.h>
//-----------------------------------------------------------------------------
// Name: class CInputDeviceManager
// Desc: Input device manager using DX8 action mapping
//-----------------------------------------------------------------------------
class CInputDeviceManager
{
public:
struct DeviceInfo
{
LPDIRECTINPUTDEVICE8 pdidDevice;
LPVOID pParam;
};
typedef HRESULT (CALLBACK *LPDIMANAGERCALLBACK)(CInputDeviceManager::DeviceInfo* pDeviceInfo, const DIDEVICEINSTANCE* pdidi, LPVOID);
private:
BOOL m_bCleanupCOM;
HWND m_hWnd;
TCHAR* m_strUserName;
LPDIRECTINPUT8 m_pDI;
DeviceInfo* m_pDevices;
DWORD m_dwMaxDevices;
DWORD m_dwNumDevices;
DIACTIONFORMAT m_diaf;
LPDIMANAGERCALLBACK m_AddDeviceCallback;
LPVOID m_AddDeviceCallbackParam;
public:
// Device control
HRESULT AddDevice( const DIDEVICEINSTANCE* pdidi, LPDIRECTINPUTDEVICE8 pdidDevice );
HRESULT GetDevices( DeviceInfo** ppDeviceInfo, DWORD* pdwNumDevices );
HRESULT ConfigureDevices( HWND hWnd, IUnknown* pSurface, VOID* pCallback, DWORD dwFlags, LPVOID pvCBParam );
VOID UnacquireDevices();
VOID SetFocus( HWND hWnd );
// Construction
HRESULT SetActionFormat( DIACTIONFORMAT& diaf, BOOL bReenumerate );
HRESULT Create( HWND hWnd, TCHAR* strUserName, DIACTIONFORMAT& diaf, LPDIMANAGERCALLBACK AddDeviceCallback, LPVOID pCallbackParam );
CInputDeviceManager();
~CInputDeviceManager();
};
#endif

View File

@@ -0,0 +1,126 @@
//-----------------------------------------------------------------------------
// File: DMUtil.h
//
// Desc:
//
// Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef DMUTIL_H
#define DMUTIL_H
#include <dmusicc.h>
#include <dmusici.h>
#include <dsound.h>
//-----------------------------------------------------------------------------
// Classes used by this header
//-----------------------------------------------------------------------------
class CMusicManager;
class CMusicSegment;
class CMusicScript;
//-----------------------------------------------------------------------------
// Name: class CMusicManager
// Desc:
//-----------------------------------------------------------------------------
class CMusicManager
{
protected:
BOOL m_bCleanupCOM;
IDirectMusicLoader8* m_pLoader;
IDirectMusicPerformance8* m_pPerformance;
public:
CMusicManager();
~CMusicManager();
inline IDirectMusicLoader8* GetLoader() { return m_pLoader; }
inline IDirectMusicPerformance8* GetPerformance() { return m_pPerformance; }
IDirectMusicAudioPath8* GetDefaultAudioPath();
HRESULT Initialize( HWND hWnd, DWORD dwPChannels = 128, DWORD dwDefaultPathType = DMUS_APATH_DYNAMIC_STEREO );
HRESULT SetSearchDirectory( const TCHAR* strMediaPath );
VOID CollectGarbage();
HRESULT CreateSegmentFromFile( CMusicSegment** ppSegment, TCHAR* strFileName,
BOOL bDownloadNow = TRUE, BOOL bIsMidiFile = FALSE );
HRESULT CreateScriptFromFile( CMusicScript** ppScript, TCHAR* strFileName );
HRESULT CreateChordMapFromFile( IDirectMusicChordMap8** ppChordMap, TCHAR* strFileName );
HRESULT CreateStyleFromFile( IDirectMusicStyle8** ppStyle, TCHAR* strFileName );
HRESULT GetMotifFromStyle( IDirectMusicSegment8** ppMotif, TCHAR* strStyle, TCHAR* wstrMotif );
HRESULT CreateSegmentFromResource( CMusicSegment** ppSegment, TCHAR* strResource, TCHAR* strResourceType,
BOOL bDownloadNow = TRUE, BOOL bIsMidiFile = FALSE );
};
//-----------------------------------------------------------------------------
// Name: class CMusicSegment
// Desc: Encapsulates functionality of an IDirectMusicSegment
//-----------------------------------------------------------------------------
class CMusicSegment
{
protected:
IDirectMusicSegment8* m_pSegment;
IDirectMusicLoader8* m_pLoader;
IDirectMusicPerformance8* m_pPerformance;
IDirectMusicAudioPath8* m_pEmbeddedAudioPath;
BOOL m_bDownloaded;
public:
CMusicSegment( IDirectMusicPerformance8* pPerformance,
IDirectMusicLoader8* pLoader,
IDirectMusicSegment8* pSegment );
virtual ~CMusicSegment();
inline IDirectMusicSegment8* GetSegment() { return m_pSegment; }
HRESULT GetStyle( IDirectMusicStyle8** ppStyle, DWORD dwStyleIndex = 0 );
HRESULT SetRepeats( DWORD dwRepeats );
HRESULT Play( DWORD dwFlags = DMUS_SEGF_SECONDARY, IDirectMusicAudioPath8* pAudioPath = NULL );
HRESULT Stop( DWORD dwFlags = 0 );
HRESULT Download( IDirectMusicAudioPath8* pAudioPath = NULL );
HRESULT Unload( IDirectMusicAudioPath8* pAudioPath = NULL );
BOOL IsPlaying();
};
//-----------------------------------------------------------------------------
// Name: class CMusicScript
// Desc: Encapsulates functionality of an IDirectMusicScript
//-----------------------------------------------------------------------------
class CMusicScript
{
protected:
IDirectMusicScript8* m_pScript;
IDirectMusicLoader8* m_pLoader;
IDirectMusicPerformance8* m_pPerformance;
public:
CMusicScript( IDirectMusicPerformance8* pPerformance,
IDirectMusicLoader8* pLoader,
IDirectMusicScript8* pScript );
virtual ~CMusicScript();
inline IDirectMusicScript8* GetScript() { return m_pScript; }
HRESULT CallRoutine( TCHAR* strRoutine );
HRESULT SetVariableNumber( TCHAR* strVariable, LONG lValue );
HRESULT GetVariableNumber( TCHAR* strVariable, LONG* plValue );
};
#endif // DMUTIL_H

View File

@@ -0,0 +1,171 @@
//-----------------------------------------------------------------------------
// File: DSUtil.h
//
// Desc:
//
// Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#ifndef DSUTIL_H
#define DSUTIL_H
#include <windows.h>
#include <mmsystem.h>
#include <mmreg.h>
#include <dsound.h>
//-----------------------------------------------------------------------------
// Classes used by this header
//-----------------------------------------------------------------------------
class CSoundManager;
class CSound;
class CStreamingSound;
class CWaveFile;
//-----------------------------------------------------------------------------
// Typing macros
//-----------------------------------------------------------------------------
#define WAVEFILE_READ 1
#define WAVEFILE_WRITE 2
#define DSUtil_StopSound(s) { if(s) s->Stop(); }
#define DSUtil_PlaySound(s) { if(s) s->Play( 0, 0 ); }
#define DSUtil_PlaySoundLooping(s) { if(s) s->Play( 0, DSBPLAY_LOOPING ); }
//-----------------------------------------------------------------------------
// Name: class CSoundManager
// Desc:
//-----------------------------------------------------------------------------
class CSoundManager
{
protected:
LPDIRECTSOUND8 m_pDS;
public:
CSoundManager();
~CSoundManager();
HRESULT Initialize( HWND hWnd, DWORD dwCoopLevel, DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
inline LPDIRECTSOUND8 GetDirectSound() { return m_pDS; }
HRESULT SetPrimaryBufferFormat( DWORD dwPrimaryChannels, DWORD dwPrimaryFreq, DWORD dwPrimaryBitRate );
HRESULT Get3DListenerInterface( LPDIRECTSOUND3DLISTENER* ppDSListener );
HRESULT Create( CSound** ppSound, LPTSTR strWaveFileName, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
HRESULT CreateFromMemory( CSound** ppSound, BYTE* pbData, ULONG ulDataSize, LPWAVEFORMATEX pwfx, DWORD dwCreationFlags = 0, GUID guid3DAlgorithm = GUID_NULL, DWORD dwNumBuffers = 1 );
HRESULT CreateStreaming( CStreamingSound** ppStreamingSound, LPTSTR strWaveFileName, DWORD dwCreationFlags, GUID guid3DAlgorithm, DWORD dwNotifyCount, DWORD dwNotifySize, HANDLE hNotifyEvent );
};
//-----------------------------------------------------------------------------
// Name: class CSound
// Desc: Encapsulates functionality of a DirectSound buffer.
//-----------------------------------------------------------------------------
class CSound
{
protected:
LPDIRECTSOUNDBUFFER* m_apDSBuffer;
DWORD m_dwDSBufferSize;
CWaveFile* m_pWaveFile;
DWORD m_dwNumBuffers;
HRESULT RestoreBuffer( LPDIRECTSOUNDBUFFER pDSB, BOOL* pbWasRestored );
public:
CSound( LPDIRECTSOUNDBUFFER* apDSBuffer, DWORD dwDSBufferSize, DWORD dwNumBuffers, CWaveFile* pWaveFile );
virtual ~CSound();
HRESULT Get3DBufferInterface( DWORD dwIndex, LPDIRECTSOUND3DBUFFER* ppDS3DBuffer );
HRESULT FillBufferWithSound( LPDIRECTSOUNDBUFFER pDSB, BOOL bRepeatWavIfBufferLarger );
LPDIRECTSOUNDBUFFER GetFreeBuffer();
LPDIRECTSOUNDBUFFER GetBuffer( DWORD dwIndex );
HRESULT Play( DWORD dwPriority = 0, DWORD dwFlags = 0 );
HRESULT Stop();
HRESULT Reset();
BOOL IsSoundPlaying();
};
//-----------------------------------------------------------------------------
// Name: class CStreamingSound
// Desc: Encapsulates functionality to play a wave file with DirectSound.
// The Create() method loads a chunk of wave file into the buffer,
// and as sound plays more is written to the buffer by calling
// HandleWaveStreamNotification() whenever hNotifyEvent is signaled.
//-----------------------------------------------------------------------------
class CStreamingSound : public CSound
{
protected:
DWORD m_dwLastPlayPos;
DWORD m_dwPlayProgress;
DWORD m_dwNotifySize;
DWORD m_dwNextWriteOffset;
BOOL m_bFillNextNotificationWithSilence;
public:
CStreamingSound( LPDIRECTSOUNDBUFFER pDSBuffer, DWORD dwDSBufferSize, CWaveFile* pWaveFile, DWORD dwNotifySize );
~CStreamingSound();
HRESULT HandleWaveStreamNotification( BOOL bLoopedPlay );
HRESULT Reset();
};
//-----------------------------------------------------------------------------
// Name: class CWaveFile
// Desc: Encapsulates reading or writing sound data to or from a wave file
//-----------------------------------------------------------------------------
class CWaveFile
{
public:
WAVEFORMATEX* m_pwfx; // Pointer to WAVEFORMATEX structure
HMMIO m_hmmio; // MM I/O handle for the WAVE
MMCKINFO m_ck; // Multimedia RIFF chunk
MMCKINFO m_ckRiff; // Use in opening a WAVE file
DWORD m_dwSize; // The size of the wave file
MMIOINFO m_mmioinfoOut;
DWORD m_dwFlags;
BOOL m_bIsReadingFromMemory;
BYTE* m_pbData;
BYTE* m_pbDataCur;
ULONG m_ulDataSize;
CHAR* m_pResourceBuffer;
protected:
HRESULT ReadMMIO();
HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );
public:
CWaveFile();
~CWaveFile();
HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
HRESULT OpenFromMemory( BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
HRESULT Close();
HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );
DWORD GetSize();
HRESULT ResetFile();
WAVEFORMATEX* GetFormat() { return m_pwfx; };
};
#endif // DSUTIL_H

View File

@@ -0,0 +1,112 @@
//-----------------------------------------------------------------------------
// File: DXUtil.h
//
// Desc: Helper functions and typing shortcuts for DirectX programming.
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
#ifndef DXUTIL_H
#define DXUTIL_H
//-----------------------------------------------------------------------------
// Miscellaneous helper functions
//-----------------------------------------------------------------------------
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#define SAFE_DELETE_ARRAY(p) { if(p) { delete[] (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
//-----------------------------------------------------------------------------
// Name: DXUtil_GetDXSDKMediaPath() and DXUtil_FindMediaFile()
// Desc: Returns the DirectX SDK path, as stored in the system registry
// during the SDK install.
//-----------------------------------------------------------------------------
const TCHAR* DXUtil_GetDXSDKMediaPath();
HRESULT DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename );
//-----------------------------------------------------------------------------
// Name: DXUtil_Read*RegKey() and DXUtil_Write*RegKey()
// Desc: Helper functions to read/write a string registry key
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue );
HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue );
HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue );
HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue );
HRESULT DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue, DWORD dwLength, TCHAR* strDefault );
HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue, DWORD dwDefault );
HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue, GUID& guidDefault );
HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue, BOOL bDefault );
//-----------------------------------------------------------------------------
// Name: DXUtil_Timer()
// Desc: Performs timer opertations. Use the following commands:
// TIMER_RESET - to reset the timer
// TIMER_START - to start the timer
// TIMER_STOP - to stop (or pause) the timer
// TIMER_ADVANCE - to advance the timer by 0.1 seconds
// TIMER_GETABSOLUTETIME - to get the absolute system time
// TIMER_GETAPPTIME - to get the current time
// TIMER_GETELAPSEDTIME - to get the time that elapsed between
// TIMER_GETELAPSEDTIME calls
//-----------------------------------------------------------------------------
enum TIMER_COMMAND { TIMER_RESET, TIMER_START, TIMER_STOP, TIMER_ADVANCE,
TIMER_GETABSOLUTETIME, TIMER_GETAPPTIME, TIMER_GETELAPSEDTIME };
FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command );
//-----------------------------------------------------------------------------
// UNICODE support for converting between CHAR, TCHAR, and WCHAR strings
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource, int cchDestChar = -1 );
VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource, int cchDestChar = -1 );
VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource, int cchDestChar = -1 );
VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource, int cchDestChar = -1 );
VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource, int cchDestChar = -1 );
//-----------------------------------------------------------------------------
// GUID to String converting
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGUIDToString( const GUID* pGuidIn, TCHAR* strOut );
BOOL DXUtil_ConvertStringToGUID( const TCHAR* strIn, GUID* pGuidOut );
//-----------------------------------------------------------------------------
// Debug printing support
//-----------------------------------------------------------------------------
VOID DXUtil_Trace( TCHAR* strMsg, ... );
HRESULT _DbgOut( TCHAR*, DWORD, HRESULT, TCHAR* );
#if defined(DEBUG) | defined(_DEBUG)
#define DXTRACE DXUtil_Trace
#else
#define DXTRACE sizeof
#endif
#if defined(DEBUG) | defined(_DEBUG)
#define DEBUG_MSG(str) _DbgOut( __FILE__, (DWORD)__LINE__, 0, str )
#else
#define DEBUG_MSG(str) (0L)
#endif
#endif // DXUTIL_H

View File

@@ -0,0 +1,102 @@
//-----------------------------------------------------------------------------
// File: NetConnect.h
//
// Desc:
//
// Copyright (C) 2000-2001 Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------------------
#ifndef NETCLIENT_H
#define NETCLIENT_H
#include <windows.h>
#include <dplay8.h>
#include <dpaddr.h>
#include <tchar.h>
//-----------------------------------------------------------------------------
// Defines, structures, and error codes
//-----------------------------------------------------------------------------
#define DISPLAY_REFRESH_RATE 250
#define TIMERID_DISPLAY_HOSTS 1
#define TIMERID_CONNECT_COMPLETE 2
#define NCW_S_FORWARD 0x01000001 // Dialog success, so go forward
#define NCW_S_BACKUP 0x01000002 // Dialog canceled, show previous dialog
#define NCW_S_QUIT 0x01000003 // Dialog quit, close app
#define NCW_S_LOBBYCONNECT 0x01000004 // Dialog connected from lobby, connect success
class CNetClientWizard
{
public:
CNetClientWizard( HINSTANCE hInst, TCHAR* strAppName, GUID* pGuidApp );
virtual ~CNetClientWizard();
HRESULT WINAPI MessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer );
HRESULT WINAPI LobbyMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer );
VOID Init( IDirectPlay8Client* pDPClient, IDirectPlay8LobbiedApplication* pLobbiedApp );
HRESULT DoConnectWizard();
HRESULT ConnectUsingLobbySettings();
void SetPlayerName( TCHAR* strPlayerName ) { _tcscpy( m_strLocalPlayerName, strPlayerName ); }
TCHAR* GetPlayerName() { return m_strLocalPlayerName; }
BOOL HaveConnectionSettingsFromLobby() { return m_bHaveConnectionSettingsFromLobby; }
protected:
struct DPHostEnumInfo
{
DWORD dwRef;
DPN_APPLICATION_DESC* pAppDesc;
IDirectPlay8Address* pHostAddr;
IDirectPlay8Address* pDeviceAddr;
TCHAR szSession[MAX_PATH];
DWORD dwLastPollTime;
BOOL bValid;
DPHostEnumInfo* pNext;
};
static INT_PTR CALLBACK StaticSessionsDlgProc( HWND, UINT, WPARAM, LPARAM );
static INT_PTR CALLBACK StaticLobbyWaitDlgProc( HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK SessionsDlgProc( HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK LobbyWaitDlgProc( HWND, UINT, WPARAM, LPARAM );
VOID SessionsDlgInitListbox( HWND hDlg );
HRESULT SessionsDlgEnumHosts( HWND hDlg );
HRESULT SessionsDlgNoteEnumResponse( PDPNMSG_ENUM_HOSTS_RESPONSE pEnumHostsResponse );
VOID SessionsDlgExpireOldHostEnums();
HRESULT SessionsDlgDisplayEnumList( HWND hDlg );
HRESULT SessionsDlgJoinGame( HWND hDlg );
HRESULT SessionsDlgCreateGame( HWND hDlg );
VOID SessionsDlgEnumListCleanup();
IDirectPlay8Client* m_pDPClient;
IDirectPlay8LobbiedApplication* m_pLobbiedApp;
CRITICAL_SECTION m_csHostEnum;
GUID m_guidApp;
HRESULT m_hrDialog;
HWND m_hDlg;
HINSTANCE m_hInst;
DPHostEnumInfo m_DPHostEnumHead;
TCHAR m_strAppName[MAX_PATH];
TCHAR m_strLocalPlayerName[MAX_PATH];
BOOL m_bSearchingForSessions;
BOOL m_bEnumListChanged;
DPNHANDLE m_hEnumAsyncOp;
DWORD m_dwEnumHostExpireInterval;
BOOL m_bConnecting;
DPNHANDLE m_hConnectAsyncOp;
HANDLE m_hConnectCompleteEvent;
HANDLE m_hLobbyConnectionEvent;
HRESULT m_hrConnectComplete;
BOOL m_bHaveConnectionSettingsFromLobby;
DPNHANDLE m_hLobbyClient;
};
#endif // NETCLIENT_H

View File

@@ -0,0 +1,8 @@
#define IDD_CLIENT_CONNECT 12001
#define IDC_PLAYER_NAME_EDIT 12100
#define IDC_GAMES_LIST 12101
#define IDC_JOIN 12102
#define IDC_IP_ADDRESS 12103
#define IDC_SEARCH_CHECK 12104
#define IDC_WAIT_TEXT 12105
#define IDI_MAIN 12200

View File

@@ -0,0 +1,125 @@
//-----------------------------------------------------------------------------
// File: NetConnect.h
//
// Desc:
//
// Copyright (C) 2000-2001 Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------------------
#ifndef NETCONNECT_H
#define NETCONNECT_H
#include <windows.h>
#include <dplay8.h>
#include <dpaddr.h>
#include <tchar.h>
//-----------------------------------------------------------------------------
// Defines, structures, and error codes
//-----------------------------------------------------------------------------
#define DISPLAY_REFRESH_RATE 250
#define TIMERID_DISPLAY_HOSTS 1
#define TIMERID_CONNECT_COMPLETE 2
#define NCW_S_FORWARD 0x01000001 // Dialog success, so go forward
#define NCW_S_BACKUP 0x01000002 // Dialog canceled, show previous dialog
#define NCW_S_QUIT 0x01000003 // Dialog quit, close app
#define NCW_S_LOBBYCONNECT 0x01000004 // Dialog connected from lobby, connect success
class CNetConnectWizard
{
public:
CNetConnectWizard( HINSTANCE hInst, HWND hWndParent, TCHAR* strAppName, GUID* pGuidApp );
virtual ~CNetConnectWizard();
HRESULT WINAPI MessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer );
HRESULT WINAPI LobbyMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer );
VOID Init( IDirectPlay8Peer* pDP, IDirectPlay8LobbiedApplication* pLobbiedApp );
VOID Shutdown();
HRESULT DoConnectWizard( BOOL bBackTrack );
HRESULT ConnectUsingLobbySettings();
void SetMaxPlayers( DWORD dwMaxPlayers ) { m_dwMaxPlayers = dwMaxPlayers; }
void SetPlayerName( TCHAR* strPlayerName ) { _tcscpy( m_strLocalPlayerName, strPlayerName ); }
void SetSessionName( TCHAR* strSessionName ) { _tcscpy( m_strSessionName, strSessionName ); }
void SetPreferredProvider( TCHAR* strPreferredProvider ) { _tcscpy( m_strPreferredProvider, strPreferredProvider ); }
TCHAR* GetPlayerName() { return m_strLocalPlayerName; }
TCHAR* GetSessionName() { return m_strSessionName; }
TCHAR* GetPreferredProvider() { return m_strPreferredProvider; }
BOOL IsHostPlayer() { return m_bHostPlayer; }
BOOL IsMigrateHost() { return m_bMigrateHost; }
BOOL HaveConnectionSettingsFromLobby() { return m_bHaveConnectionSettingsFromLobby; }
protected:
struct DPHostEnumInfo
{
DWORD dwRef;
DPN_APPLICATION_DESC* pAppDesc;
IDirectPlay8Address* pHostAddr;
IDirectPlay8Address* pDeviceAddr;
TCHAR szSession[MAX_PATH];
DWORD dwLastPollTime;
BOOL bValid;
DPHostEnumInfo* pNext;
};
static INT_PTR CALLBACK StaticConnectionsDlgProc( HWND, UINT, WPARAM, LPARAM );
static INT_PTR CALLBACK StaticSessionsDlgProc( HWND, UINT, WPARAM, LPARAM );
static INT_PTR CALLBACK StaticCreateSessionDlgProc( HWND, UINT, WPARAM, LPARAM );
static INT_PTR CALLBACK StaticLobbyWaitDlgProc( HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK ConnectionsDlgProc( HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK SessionsDlgProc( HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK CreateSessionDlgProc( HWND, UINT, WPARAM, LPARAM );
INT_PTR CALLBACK LobbyWaitDlgProc( HWND, UINT, WPARAM, LPARAM );
HRESULT ConnectionsDlgFillListBox( HWND hDlg );
HRESULT ConnectionsDlgOnOK( HWND hDlg );
VOID ConnectionsDlgCleanup( HWND hDlg );
VOID SessionsDlgInitListbox( HWND hDlg );
HRESULT SessionsDlgEnumHosts( HWND hDlg );
HRESULT SessionsDlgNoteEnumResponse( PDPNMSG_ENUM_HOSTS_RESPONSE pEnumHostsResponse );
VOID SessionsDlgExpireOldHostEnums();
HRESULT SessionsDlgDisplayEnumList( HWND hDlg );
HRESULT SessionsDlgJoinGame( HWND hDlg );
HRESULT SessionsDlgCreateGame( HWND hDlg );
VOID SessionsDlgEnumListCleanup();
IDirectPlay8Peer* m_pDP;
IDirectPlay8LobbiedApplication* m_pLobbiedApp;
CRITICAL_SECTION m_csHostEnum;
GUID m_guidApp;
HRESULT m_hrDialog;
HWND m_hDlg;
HINSTANCE m_hInst;
HWND m_hWndParent;
DWORD m_dwMaxPlayers;
TCHAR m_strAppName[MAX_PATH];
TCHAR m_strPreferredProvider[MAX_PATH];
TCHAR m_strSessionName[MAX_PATH];
TCHAR m_strLocalPlayerName[MAX_PATH];
BOOL m_bSearchingForSessions;
BOOL m_bMigrateHost;
IDirectPlay8Address* m_pDeviceAddress;
IDirectPlay8Address* m_pHostAddress;
DPHostEnumInfo m_DPHostEnumHead;
BOOL m_bEnumListChanged;
DPNHANDLE m_hEnumAsyncOp;
BOOL m_bHostPlayer;
DWORD m_dwEnumHostExpireInterval;
BOOL m_bConnecting;
DPNHANDLE m_hConnectAsyncOp;
HANDLE m_hConnectCompleteEvent;
HANDLE m_hLobbyConnectionEvent;
HRESULT m_hrConnectComplete;
BOOL m_bHaveConnectionSettingsFromLobby;
DPNHANDLE m_hLobbyClient;
};
#endif // NETCONNECT_H

View File

@@ -0,0 +1,18 @@
#define IDD_MULTIPLAYER_CONNECT 10001
#define IDD_MULTIPLAYER_GAMES 10002
#define IDD_MULTIPLAYER_CREATE 10003
#define IDD_LOBBY_WAIT_STATUS 10004
#define IDC_RETURN 11001
#define IDC_PLAYER_NAME_EDIT 11002
#define IDC_GAMES_LIST 11003
#define IDC_JOIN 11004
#define IDC_CREATE 11005
#define IDC_CONNECTION_LIST 11006
#define IDC_BACK 11007
#define IDC_EDIT_SESSION_NAME 11009
#define IDC_SEARCH_CHECK 11010
#define IDC_LOBBYCONNECTCANCEL 11011
#define IDC_WAIT_TEXT 11012
#define IDC_MIGRATE_HOST 11013
#define IDI_MAIN 11014

View File

@@ -0,0 +1,53 @@
//-----------------------------------------------------------------------------
// File: NetVoice.h
//
// Desc:
//
// Copyright (C) 2000-2001 Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------------------
#ifndef NETVOICE_H
#define NETVOICE_H
#include <windows.h>
#include <dvoice.h>
class CNetVoice
{
public:
CNetVoice( LPDVMESSAGEHANDLER pfnDirectPlayClientVoiceMessageHandler,
LPDVMESSAGEHANDLER pfnDirectPlayServerVoiceMessageHandler );
virtual ~CNetVoice();
HRESULT Init( HWND hDlg, BOOL bCreateSession, BOOL bConnectToSession,
LPUNKNOWN pDirectPlay, DWORD dwSessionType, GUID* pGuidCT,
DVCLIENTCONFIG* pdvClientConfig, LPDIRECTSOUND lpds = NULL );
HRESULT Free();
HRESULT HostMigrate( LPDIRECTPLAYVOICESERVER pdvServerInterface );
HRESULT IsHalfDuplex() { return m_bHalfDuplex; }
HRESULT ChangeVoiceClientSettings( DVCLIENTCONFIG* pdvClientConfig );
LPDIRECTPLAYVOICECLIENT GetVoiceClient() { return m_pVoiceClient; }
LPDIRECTPLAYVOICESERVER GetVoiceServer() { return m_pVoiceServer; }
protected:
LPDIRECTPLAYVOICECLIENT m_pVoiceClient;
LPDIRECTPLAYVOICESERVER m_pVoiceServer;
LPDVMESSAGEHANDLER m_pfnDirectPlayClientVoiceMessageHandler;
LPDVMESSAGEHANDLER m_pfnDirectPlayServerVoiceMessageHandler;
HRESULT VoiceSessionCreate( LPUNKNOWN pDirectPlay, DWORD dwSessionType, GUID* pGuidCT );
HRESULT VoiceSessionTestAudioSetup( HWND hDlg );
HRESULT VoiceSessionConnect( HWND hDlg, LPUNKNOWN pDirectPlay,
DVCLIENTCONFIG* pdvClientConfig, LPDIRECTSOUND lpds = NULL );
HRESULT VoiceSessionDisconnect();
HRESULT VoiceSessionDestroy();
HRESULT m_bHalfDuplex;
};
#endif // NETVOICE_H

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,808 @@
//-----------------------------------------------------------------------------
// File: D3DFile.cpp
//
// Desc: Support code for loading DirectX .X files.
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <tchar.h>
#include <stdio.h>
#include <d3d8.h>
#include <d3dx8.h>
#include <dxfile.h>
#include <rmxfguid.h>
#include <rmxftmpl.h>
#include "D3DFile.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DMesh::CD3DMesh( TCHAR* strName )
{
_tcscpy( m_strName, strName );
m_pSysMemMesh = NULL;
m_pLocalMesh = NULL;
m_dwNumMaterials = 0L;
m_pMaterials = NULL;
m_pTextures = NULL;
m_bUseMaterials = TRUE;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DMesh::~CD3DMesh()
{
Destroy();
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Create( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strFilename )
{
TCHAR strPath[MAX_PATH];
CHAR strPathANSI[MAX_PATH];
LPD3DXBUFFER pAdjacencyBuffer = NULL;
LPD3DXBUFFER pMtrlBuffer = NULL;
HRESULT hr;
// Find the path for the file, and convert it to ANSI (for the D3DX API)
DXUtil_FindMediaFile( strPath, strFilename );
DXUtil_ConvertGenericStringToAnsi( strPathANSI, strPath );
// Load the mesh
if( FAILED( hr = D3DXLoadMeshFromX( strPathANSI, D3DXMESH_SYSTEMMEM, pd3dDevice,
&pAdjacencyBuffer, &pMtrlBuffer,
&m_dwNumMaterials, &m_pSysMemMesh ) ) )
{
return hr;
}
// Optimize the mesh for performance
if( FAILED( hr = m_pSysMemMesh->OptimizeInplace(
D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
{
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return hr;
}
// Get material info for the mesh
// Get the array of materials out of the buffer
if( pMtrlBuffer && m_dwNumMaterials > 0 )
{
// Allocate memory for the materials and textures
D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
m_pMaterials = new D3DMATERIAL8[m_dwNumMaterials];
m_pTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
// Copy each material and create its texture
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
// Copy the material
m_pMaterials[i] = d3dxMtrls[i].MatD3D;
m_pMaterials[i].Ambient = m_pMaterials[i].Diffuse;
m_pTextures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
TCHAR strTexture[MAX_PATH];
TCHAR strTextureTemp[MAX_PATH];
DXUtil_ConvertAnsiStringToGeneric( strTextureTemp, d3dxMtrls[i].pTextureFilename );
DXUtil_FindMediaFile( strTexture, strTextureTemp );
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexture,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Create( LPDIRECT3DDEVICE8 pd3dDevice,
LPDIRECTXFILEDATA pFileData )
{
LPD3DXBUFFER pMtrlBuffer = NULL;
LPD3DXBUFFER pAdjacencyBuffer = NULL;
HRESULT hr;
// Load the mesh from the DXFILEDATA object
if( FAILED( hr = D3DXLoadMeshFromXof( pFileData, D3DXMESH_SYSTEMMEM, pd3dDevice,
&pAdjacencyBuffer, &pMtrlBuffer,
&m_dwNumMaterials, &m_pSysMemMesh ) ) )
{
return hr;
}
// Optimize the mesh for performance
if( FAILED( hr = m_pSysMemMesh->OptimizeInplace(
D3DXMESHOPT_COMPACT | D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_VERTEXCACHE,
(DWORD*)pAdjacencyBuffer->GetBufferPointer(), NULL, NULL, NULL ) ) )
{
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return hr;
}
// Get material info for the mesh
// Get the array of materials out of the buffer
if( pMtrlBuffer && m_dwNumMaterials > 0 )
{
// Allocate memory for the materials and textures
D3DXMATERIAL* d3dxMtrls = (D3DXMATERIAL*)pMtrlBuffer->GetBufferPointer();
m_pMaterials = new D3DMATERIAL8[m_dwNumMaterials];
m_pTextures = new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
// Copy each material and create its texture
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
// Copy the material
m_pMaterials[i] = d3dxMtrls[i].MatD3D;
m_pMaterials[i].Ambient = m_pMaterials[i].Diffuse;
m_pTextures[i] = NULL;
// Create a texture
if( d3dxMtrls[i].pTextureFilename )
{
TCHAR strTexture[MAX_PATH];
TCHAR strTextureTemp[MAX_PATH];
DXUtil_ConvertAnsiStringToGeneric( strTextureTemp, d3dxMtrls[i].pTextureFilename );
DXUtil_FindMediaFile( strTexture, strTextureTemp );
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, strTexture,
&m_pTextures[i] ) ) )
m_pTextures[i] = NULL;
}
}
}
SAFE_RELEASE( pAdjacencyBuffer );
SAFE_RELEASE( pMtrlBuffer );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::SetFVF( LPDIRECT3DDEVICE8 pd3dDevice, DWORD dwFVF )
{
LPD3DXMESH pTempSysMemMesh = NULL;
LPD3DXMESH pTempLocalMesh = NULL;
if( m_pSysMemMesh )
{
if( FAILED( m_pSysMemMesh->CloneMeshFVF( D3DXMESH_SYSTEMMEM, dwFVF,
pd3dDevice, &pTempSysMemMesh ) ) )
return E_FAIL;
}
if( m_pLocalMesh )
{
if( FAILED( m_pLocalMesh->CloneMeshFVF( 0L, dwFVF, pd3dDevice,
&pTempLocalMesh ) ) )
{
SAFE_RELEASE( pTempSysMemMesh );
return E_FAIL;
}
}
SAFE_RELEASE( m_pSysMemMesh );
SAFE_RELEASE( m_pLocalMesh );
if( pTempSysMemMesh ) m_pSysMemMesh = pTempSysMemMesh;
if( pTempLocalMesh ) m_pLocalMesh = pTempLocalMesh;
// Compute normals in case the meshes have them
if( m_pSysMemMesh )
D3DXComputeNormals( m_pSysMemMesh, NULL );
if( m_pLocalMesh )
D3DXComputeNormals( m_pLocalMesh, NULL );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::RestoreDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice )
{
if( NULL == m_pSysMemMesh )
return E_FAIL;
// Make a local memory version of the mesh. Note: because we are passing in
// no flags, the default behavior is to clone into local memory.
if( FAILED( m_pSysMemMesh->CloneMeshFVF( 0L, m_pSysMemMesh->GetFVF(),
pd3dDevice, &m_pLocalMesh ) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::InvalidateDeviceObjects()
{
SAFE_RELEASE( m_pLocalMesh );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Destroy()
{
InvalidateDeviceObjects();
for( UINT i=0; i<m_dwNumMaterials; i++ )
SAFE_RELEASE( m_pTextures[i] );
SAFE_DELETE_ARRAY( m_pTextures );
SAFE_DELETE_ARRAY( m_pMaterials );
SAFE_RELEASE( m_pSysMemMesh );
m_dwNumMaterials = 0L;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DMesh::Render( LPDIRECT3DDEVICE8 pd3dDevice, BOOL bDrawOpaqueSubsets,
BOOL bDrawAlphaSubsets )
{
if( NULL == m_pLocalMesh )
return E_FAIL;
// Frist, draw the subsets without alpha
if( bDrawOpaqueSubsets )
{
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
if( m_bUseMaterials )
{
if( m_pMaterials[i].Diffuse.a < 1.0f )
continue;
pd3dDevice->SetMaterial( &m_pMaterials[i] );
pd3dDevice->SetTexture( 0, m_pTextures[i] );
}
m_pLocalMesh->DrawSubset( i );
}
}
// Then, draw the subsets with alpha
if( bDrawAlphaSubsets && m_bUseMaterials )
{
for( DWORD i=0; i<m_dwNumMaterials; i++ )
{
if( m_pMaterials[i].Diffuse.a == 1.0f )
continue;
// Set the material and texture
pd3dDevice->SetMaterial( &m_pMaterials[i] );
pd3dDevice->SetTexture( 0, m_pTextures[i] );
m_pLocalMesh->DrawSubset( i );
}
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DFrame::CD3DFrame( TCHAR* strName )
{
_tcscpy( m_strName, strName );
D3DXMatrixIdentity( &m_mat );
m_pMesh = NULL;
m_pChild = NULL;
m_pNext = NULL;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DFrame::~CD3DFrame()
{
SAFE_DELETE( m_pChild );
SAFE_DELETE( m_pNext );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
BOOL CD3DFrame::EnumMeshes( BOOL (*EnumMeshCB)(CD3DMesh*,VOID*),
VOID* pContext )
{
if( m_pMesh )
EnumMeshCB( m_pMesh, pContext );
if( m_pChild )
m_pChild->EnumMeshes( EnumMeshCB, pContext );
if( m_pNext )
m_pNext->EnumMeshes( EnumMeshCB, pContext );
return TRUE;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DMesh* CD3DFrame::FindMesh( TCHAR* strMeshName )
{
CD3DMesh* pMesh;
if( m_pMesh )
if( !lstrcmpi( m_pMesh->m_strName, strMeshName ) )
return m_pMesh;
if( m_pChild )
if( NULL != ( pMesh = m_pChild->FindMesh( strMeshName ) ) )
return pMesh;
if( m_pNext )
if( NULL != ( pMesh = m_pNext->FindMesh( strMeshName ) ) )
return pMesh;
return NULL;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DFrame* CD3DFrame::FindFrame( TCHAR* strFrameName )
{
CD3DFrame* pFrame;
if( !lstrcmpi( m_strName, strFrameName ) )
return this;
if( m_pChild )
if( NULL != ( pFrame = m_pChild->FindFrame( strFrameName ) ) )
return pFrame;
if( m_pNext )
if( NULL != ( pFrame = m_pNext->FindFrame( strFrameName ) ) )
return pFrame;
return NULL;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFrame::Destroy()
{
if( m_pMesh ) m_pMesh->Destroy();
if( m_pChild ) m_pChild->Destroy();
if( m_pNext ) m_pNext->Destroy();
SAFE_DELETE( m_pMesh );
SAFE_DELETE( m_pNext );
SAFE_DELETE( m_pChild );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFrame::RestoreDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice )
{
if( m_pMesh ) m_pMesh->RestoreDeviceObjects( pd3dDevice );
if( m_pChild ) m_pChild->RestoreDeviceObjects( pd3dDevice );
if( m_pNext ) m_pNext->RestoreDeviceObjects( pd3dDevice );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFrame::InvalidateDeviceObjects()
{
if( m_pMesh ) m_pMesh->InvalidateDeviceObjects();
if( m_pChild ) m_pChild->InvalidateDeviceObjects();
if( m_pNext ) m_pNext->InvalidateDeviceObjects();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFrame::Render( LPDIRECT3DDEVICE8 pd3dDevice, BOOL bDrawOpaqueSubsets,
BOOL bDrawAlphaSubsets, D3DXMATRIX* pmatWorldMatrix )
{
// For pure devices, specify the world transform. If the world transform is not
// specified on pure devices, this function will fail.
D3DXMATRIX matSavedWorld, matWorld;
if ( NULL == pmatWorldMatrix )
pd3dDevice->GetTransform( D3DTS_WORLD, &matSavedWorld );
else
matSavedWorld = *pmatWorldMatrix;
D3DXMatrixMultiply( &matWorld, &m_mat, &matSavedWorld );
pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
if( m_pMesh )
m_pMesh->Render( pd3dDevice, bDrawOpaqueSubsets, bDrawAlphaSubsets );
if( m_pChild )
m_pChild->Render( pd3dDevice, bDrawOpaqueSubsets, bDrawAlphaSubsets, &matWorld );
pd3dDevice->SetTransform( D3DTS_WORLD, &matSavedWorld );
if( m_pNext )
m_pNext->Render( pd3dDevice, bDrawOpaqueSubsets, bDrawAlphaSubsets, &matSavedWorld );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFile::LoadFrame( LPDIRECT3DDEVICE8 pd3dDevice,
LPDIRECTXFILEDATA pFileData,
CD3DFrame* pParentFrame )
{
LPDIRECTXFILEDATA pChildData = NULL;
LPDIRECTXFILEOBJECT pChildObj = NULL;
const GUID* pGUID;
DWORD cbSize;
CD3DFrame* pCurrentFrame;
HRESULT hr;
// Get the type of the object
if( FAILED( hr = pFileData->GetType( &pGUID ) ) )
return hr;
if( *pGUID == TID_D3DRMMesh )
{
hr = LoadMesh( pd3dDevice, pFileData, pParentFrame );
if( FAILED(hr) )
return hr;
}
if( *pGUID == TID_D3DRMFrameTransformMatrix )
{
D3DXMATRIX* pmatMatrix;
hr = pFileData->GetData( NULL, &cbSize, (VOID**)&pmatMatrix );
if( FAILED(hr) )
return hr;
// Update the parent's matrix with the new one
pParentFrame->SetMatrix( pmatMatrix );
}
if( *pGUID == TID_D3DRMFrame )
{
// Get the frame name
CHAR strAnsiName[512] = "";
TCHAR strName[MAX_PATH];
DWORD dwNameLength;
pFileData->GetName( NULL, &dwNameLength );
if( dwNameLength > 0 )
pFileData->GetName( strAnsiName, &dwNameLength );
DXUtil_ConvertAnsiStringToGeneric( strName, strAnsiName );
// Create the frame
pCurrentFrame = new CD3DFrame( strName );
pCurrentFrame->m_pNext = pParentFrame->m_pChild;
pParentFrame->m_pChild = pCurrentFrame;
// Enumerate child objects
while( SUCCEEDED( pFileData->GetNextObject( &pChildObj ) ) )
{
// Query the child for its FileData
hr = pChildObj->QueryInterface( IID_IDirectXFileData,
(VOID**)&pChildData );
if( SUCCEEDED(hr) )
{
hr = LoadFrame( pd3dDevice, pChildData, pCurrentFrame );
pChildData->Release();
}
pChildObj->Release();
if( FAILED(hr) )
return hr;
}
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFile::LoadMesh( LPDIRECT3DDEVICE8 pd3dDevice,
LPDIRECTXFILEDATA pFileData,
CD3DFrame* pParentFrame )
{
// Currently only allowing one mesh per frame
if( pParentFrame->m_pMesh )
return E_FAIL;
// Get the mesh name
CHAR strAnsiName[512] = {0};
TCHAR strName[MAX_PATH];
DWORD dwNameLength;
pFileData->GetName( NULL, &dwNameLength );
if( dwNameLength > 0 )
pFileData->GetName( strAnsiName, &dwNameLength );
DXUtil_ConvertAnsiStringToGeneric( strName, strAnsiName );
// Create the mesh
pParentFrame->m_pMesh = new CD3DMesh( strName );
pParentFrame->m_pMesh->Create( pd3dDevice, pFileData );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFile::CreateFromResource( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strResource, TCHAR* strType )
{
LPDIRECTXFILE pDXFile = NULL;
LPDIRECTXFILEENUMOBJECT pEnumObj = NULL;
LPDIRECTXFILEDATA pFileData = NULL;
HRESULT hr;
// Create a x file object
if( FAILED( hr = DirectXFileCreate( &pDXFile ) ) )
return E_FAIL;
// Register templates for d3drm and patch extensions.
if( FAILED( hr = pDXFile->RegisterTemplates( (VOID*)D3DRM_XTEMPLATES,
D3DRM_XTEMPLATE_BYTES ) ) )
{
pDXFile->Release();
return E_FAIL;
}
CHAR strTypeAnsi[MAX_PATH];
DXUtil_ConvertGenericStringToAnsi( strTypeAnsi, strType );
DXFILELOADRESOURCE dxlr;
dxlr.hModule = NULL;
dxlr.lpName = strResource;
dxlr.lpType = (TCHAR*) strTypeAnsi;
// Create enum object
hr = pDXFile->CreateEnumObject( (VOID*)&dxlr, DXFILELOAD_FROMRESOURCE,
&pEnumObj );
if( FAILED(hr) )
{
pDXFile->Release();
return hr;
}
// Enumerate top level objects (which are always frames)
while( SUCCEEDED( pEnumObj->GetNextDataObject( &pFileData ) ) )
{
hr = LoadFrame( pd3dDevice, pFileData, this );
pFileData->Release();
if( FAILED(hr) )
{
pEnumObj->Release();
pDXFile->Release();
return E_FAIL;
}
}
SAFE_RELEASE( pFileData );
SAFE_RELEASE( pEnumObj );
SAFE_RELEASE( pDXFile );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFile::Create( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strFilename )
{
LPDIRECTXFILE pDXFile = NULL;
LPDIRECTXFILEENUMOBJECT pEnumObj = NULL;
LPDIRECTXFILEDATA pFileData = NULL;
HRESULT hr;
// Create a x file object
if( FAILED( hr = DirectXFileCreate( &pDXFile ) ) )
return E_FAIL;
// Register templates for d3drm and patch extensions.
if( FAILED( hr = pDXFile->RegisterTemplates( (VOID*)D3DRM_XTEMPLATES,
D3DRM_XTEMPLATE_BYTES ) ) )
{
pDXFile->Release();
return E_FAIL;
}
// Find the path to the file, and convert it to ANSI (for the D3DXOF API)
TCHAR strPath[MAX_PATH];
CHAR strPathANSI[MAX_PATH];
DXUtil_FindMediaFile( strPath, strFilename );
DXUtil_ConvertGenericStringToAnsi( strPathANSI, strPath );
// Create enum object
hr = pDXFile->CreateEnumObject( (VOID*)strPathANSI, DXFILELOAD_FROMFILE,
&pEnumObj );
if( FAILED(hr) )
{
pDXFile->Release();
return hr;
}
// Enumerate top level objects (which are always frames)
while( SUCCEEDED( pEnumObj->GetNextDataObject( &pFileData ) ) )
{
hr = LoadFrame( pd3dDevice, pFileData, this );
pFileData->Release();
if( FAILED(hr) )
{
pEnumObj->Release();
pDXFile->Release();
return E_FAIL;
}
}
SAFE_RELEASE( pFileData );
SAFE_RELEASE( pEnumObj );
SAFE_RELEASE( pDXFile );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFile::Render( LPDIRECT3DDEVICE8 pd3dDevice, D3DXMATRIX* pmatWorldMatrix )
{
// For pure devices, specify the world transform. If the world transform is not
// specified on pure devices, this function will fail.
// Set up the world transformation
D3DXMATRIX matSavedWorld, matWorld;
if ( NULL == pmatWorldMatrix )
pd3dDevice->GetTransform( D3DTS_WORLD, &matSavedWorld );
else
matSavedWorld = *pmatWorldMatrix;
D3DXMatrixMultiply( &matWorld, &matSavedWorld, &m_mat );
pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
// Render opaque subsets in the meshes
if( m_pChild )
m_pChild->Render( pd3dDevice, TRUE, FALSE, &matWorld );
// Enable alpha blending
pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
// Render alpha subsets in the meshes
if( m_pChild )
m_pChild->Render( pd3dDevice, FALSE, TRUE, &matWorld );
// Restore state
pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
pd3dDevice->SetTransform( D3DTS_WORLD, &matSavedWorld );
return S_OK;
}

View File

@@ -0,0 +1,674 @@
//-----------------------------------------------------------------------------
// File: D3DFont.cpp
//
// Desc: Texture-based font class
//
// Copyright (c) 1999-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <stdio.h>
#include <tchar.h>
#include <D3DX8.h>
#include "D3DFont.h"
#include "D3DUtil.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Custom vertex types for rendering text
//-----------------------------------------------------------------------------
#define MAX_NUM_VERTICES 50*6
struct FONT2DVERTEX { D3DXVECTOR4 p; DWORD color; FLOAT tu, tv; };
struct FONT3DVERTEX { D3DXVECTOR3 p; D3DXVECTOR3 n; FLOAT tu, tv; };
#define D3DFVF_FONT2DVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)
#define D3DFVF_FONT3DVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
inline FONT2DVERTEX InitFont2DVertex( const D3DXVECTOR4& p, D3DCOLOR color,
FLOAT tu, FLOAT tv )
{
FONT2DVERTEX v; v.p = p; v.color = color; v.tu = tu; v.tv = tv;
return v;
}
inline FONT3DVERTEX InitFont3DVertex( const D3DXVECTOR3& p, const D3DXVECTOR3& n,
FLOAT tu, FLOAT tv )
{
FONT3DVERTEX v; v.p = p; v.n = n; v.tu = tu; v.tv = tv;
return v;
}
//-----------------------------------------------------------------------------
// Name: CD3DFont()
// Desc: Font class constructor
//-----------------------------------------------------------------------------
CD3DFont::CD3DFont( TCHAR* strFontName, DWORD dwHeight, DWORD dwFlags )
{
_tcscpy( m_strFontName, strFontName );
m_dwFontHeight = dwHeight;
m_dwFontFlags = dwFlags;
m_pd3dDevice = NULL;
m_pTexture = NULL;
m_pVB = NULL;
m_dwSavedStateBlock = 0L;
m_dwDrawTextStateBlock = 0L;
}
//-----------------------------------------------------------------------------
// Name: ~CD3DFont()
// Desc: Font class destructor
//-----------------------------------------------------------------------------
CD3DFont::~CD3DFont()
{
InvalidateDeviceObjects();
DeleteDeviceObjects();
}
//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initializes device-dependent objects, including the vertex buffer used
// for rendering text and the texture map which stores the font image.
//-----------------------------------------------------------------------------
HRESULT CD3DFont::InitDeviceObjects( LPDIRECT3DDEVICE8 pd3dDevice )
{
HRESULT hr;
// Keep a local copy of the device
m_pd3dDevice = pd3dDevice;
// Establish the font and texture size
m_fTextScale = 1.0f; // Draw fonts into texture without scaling
// Large fonts need larger textures
if( m_dwFontHeight > 40 )
m_dwTexWidth = m_dwTexHeight = 1024;
else if( m_dwFontHeight > 20 )
m_dwTexWidth = m_dwTexHeight = 512;
else
m_dwTexWidth = m_dwTexHeight = 256;
// If requested texture is too big, use a smaller texture and smaller font,
// and scale up when rendering.
D3DCAPS8 d3dCaps;
m_pd3dDevice->GetDeviceCaps( &d3dCaps );
if( m_dwTexWidth > d3dCaps.MaxTextureWidth )
{
m_fTextScale = (FLOAT)d3dCaps.MaxTextureWidth / (FLOAT)m_dwTexWidth;
m_dwTexWidth = m_dwTexHeight = d3dCaps.MaxTextureWidth;
}
// Create a new texture for the font
hr = m_pd3dDevice->CreateTexture( m_dwTexWidth, m_dwTexHeight, 1,
0, D3DFMT_A4R4G4B4,
D3DPOOL_MANAGED, &m_pTexture );
if( FAILED(hr) )
return hr;
// Prepare to create a bitmap
DWORD* pBitmapBits;
BITMAPINFO bmi;
ZeroMemory( &bmi.bmiHeader, sizeof(BITMAPINFOHEADER) );
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = (int)m_dwTexWidth;
bmi.bmiHeader.biHeight = -(int)m_dwTexHeight;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biBitCount = 32;
// Create a DC and a bitmap for the font
HDC hDC = CreateCompatibleDC( NULL );
HBITMAP hbmBitmap = CreateDIBSection( hDC, &bmi, DIB_RGB_COLORS,
(VOID**)&pBitmapBits, NULL, 0 );
SetMapMode( hDC, MM_TEXT );
// Create a font. By specifying ANTIALIASED_QUALITY, we might get an
// antialiased font, but this is not guaranteed.
INT nHeight = -MulDiv( m_dwFontHeight,
(INT)(GetDeviceCaps(hDC, LOGPIXELSY) * m_fTextScale), 72 );
DWORD dwBold = (m_dwFontFlags&D3DFONT_BOLD) ? FW_BOLD : FW_NORMAL;
DWORD dwItalic = (m_dwFontFlags&D3DFONT_ITALIC) ? TRUE : FALSE;
HFONT hFont = CreateFont( nHeight, 0, 0, 0, dwBold, dwItalic,
FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
VARIABLE_PITCH, m_strFontName );
if( NULL==hFont )
return E_FAIL;
SelectObject( hDC, hbmBitmap );
SelectObject( hDC, hFont );
// Set text properties
SetTextColor( hDC, RGB(255,255,255) );
SetBkColor( hDC, 0x00000000 );
SetTextAlign( hDC, TA_TOP );
// Loop through all printable character and output them to the bitmap..
// Meanwhile, keep track of the corresponding tex coords for each character.
DWORD x = 0;
DWORD y = 0;
TCHAR str[2] = _T("x");
SIZE size;
for( TCHAR c=32; c<127; c++ )
{
str[0] = c;
GetTextExtentPoint32( hDC, str, 1, &size );
if( (DWORD)(x+size.cx+1) > m_dwTexWidth )
{
x = 0;
y += size.cy+1;
}
ExtTextOut( hDC, x+0, y+0, ETO_OPAQUE, NULL, str, 1, NULL );
m_fTexCoords[c-32][0] = ((FLOAT)(x+0))/m_dwTexWidth;
m_fTexCoords[c-32][1] = ((FLOAT)(y+0))/m_dwTexHeight;
m_fTexCoords[c-32][2] = ((FLOAT)(x+0+size.cx))/m_dwTexWidth;
m_fTexCoords[c-32][3] = ((FLOAT)(y+0+size.cy))/m_dwTexHeight;
x += size.cx+1;
}
// Lock the surface and write the alpha values for the set pixels
D3DLOCKED_RECT d3dlr;
m_pTexture->LockRect( 0, &d3dlr, 0, 0 );
BYTE* pDstRow = (BYTE*)d3dlr.pBits;
WORD* pDst16;
BYTE bAlpha; // 4-bit measure of pixel intensity
for( y=0; y < m_dwTexHeight; y++ )
{
pDst16 = (WORD*)pDstRow;
for( x=0; x < m_dwTexWidth; x++ )
{
bAlpha = (BYTE)((pBitmapBits[m_dwTexWidth*y + x] & 0xff) >> 4);
if (bAlpha > 0)
{
*pDst16++ = (bAlpha << 12) | 0x0fff;
}
else
{
*pDst16++ = 0x0000;
}
}
pDstRow += d3dlr.Pitch;
}
// Done updating texture, so clean up used objects
m_pTexture->UnlockRect(0);
DeleteObject( hbmBitmap );
DeleteDC( hDC );
DeleteObject( hFont );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CD3DFont::RestoreDeviceObjects()
{
HRESULT hr;
// Create vertex buffer for the letters
if( FAILED( hr = m_pd3dDevice->CreateVertexBuffer( MAX_NUM_VERTICES*sizeof(FONT2DVERTEX),
D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC, 0,
D3DPOOL_DEFAULT, &m_pVB ) ) )
{
return hr;
}
// Create the state blocks for rendering text
for( UINT which=0; which<2; which++ )
{
m_pd3dDevice->BeginStateBlock();
m_pd3dDevice->SetTexture( 0, m_pTexture );
if ( D3DFONT_ZENABLE & m_dwFontFlags )
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
else
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ALPHAREF, 0x08 );
m_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
m_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
m_pd3dDevice->SetRenderState( D3DRS_STENCILENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_CLIPPING, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_EDGEANTIALIAS, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_CLIPPLANEENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_VERTEXBLEND, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_INDEXEDVERTEXBLENDENABLE, FALSE );
m_pd3dDevice->SetRenderState( D3DRS_FOGENABLE, FALSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_POINT );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTEXF_NONE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE );
m_pd3dDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
if( which==0 )
m_pd3dDevice->EndStateBlock( &m_dwSavedStateBlock );
else
m_pd3dDevice->EndStateBlock( &m_dwDrawTextStateBlock );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InvalidateDeviceObjects()
// Desc: Destroys all device-dependent objects
//-----------------------------------------------------------------------------
HRESULT CD3DFont::InvalidateDeviceObjects()
{
SAFE_RELEASE( m_pVB );
// Delete the state blocks
if( m_pd3dDevice )
{
if( m_dwSavedStateBlock )
m_pd3dDevice->DeleteStateBlock( m_dwSavedStateBlock );
if( m_dwDrawTextStateBlock )
m_pd3dDevice->DeleteStateBlock( m_dwDrawTextStateBlock );
}
m_dwSavedStateBlock = 0L;
m_dwDrawTextStateBlock = 0L;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DeleteDeviceObjects()
// Desc: Destroys all device-dependent objects
//-----------------------------------------------------------------------------
HRESULT CD3DFont::DeleteDeviceObjects()
{
SAFE_RELEASE( m_pTexture );
m_pd3dDevice = NULL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: GetTextExtent()
// Desc: Get the dimensions of a text string
//-----------------------------------------------------------------------------
HRESULT CD3DFont::GetTextExtent( TCHAR* strText, SIZE* pSize )
{
if( NULL==strText || NULL==pSize )
return E_FAIL;
FLOAT fRowWidth = 0.0f;
FLOAT fRowHeight = (m_fTexCoords[0][3]-m_fTexCoords[0][1])*m_dwTexHeight;
FLOAT fWidth = 0.0f;
FLOAT fHeight = fRowHeight;
while( *strText )
{
TCHAR c = *strText++;
if( c == _T('\n') )
{
fRowWidth = 0.0f;
fHeight += fRowHeight;
}
if( c < _T(' ') )
continue;
FLOAT tx1 = m_fTexCoords[c-32][0];
FLOAT tx2 = m_fTexCoords[c-32][2];
fRowWidth += (tx2-tx1)*m_dwTexWidth;
if( fRowWidth > fWidth )
fWidth = fRowWidth;
}
pSize->cx = (int)fWidth;
pSize->cy = (int)fHeight;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DrawTextScaled()
// Desc: Draws scaled 2D text. Note that x and y are in viewport coordinates
// (ranging from -1 to +1). fXScale and fYScale are the size fraction
// relative to the entire viewport. For example, a fXScale of 0.25 is
// 1/8th of the screen width. This allows you to output text at a fixed
// fraction of the viewport, even if the screen or window size changes.
//-----------------------------------------------------------------------------
HRESULT CD3DFont::DrawTextScaled( FLOAT x, FLOAT y, FLOAT z,
FLOAT fXScale, FLOAT fYScale, DWORD dwColor,
TCHAR* strText, DWORD dwFlags )
{
if( m_pd3dDevice == NULL )
return E_FAIL;
// Set up renderstate
m_pd3dDevice->CaptureStateBlock( m_dwSavedStateBlock );
m_pd3dDevice->ApplyStateBlock( m_dwDrawTextStateBlock );
m_pd3dDevice->SetVertexShader( D3DFVF_FONT2DVERTEX );
m_pd3dDevice->SetPixelShader( NULL );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(FONT2DVERTEX) );
// Set filter states
if( dwFlags & D3DFONT_FILTERED )
{
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
}
D3DVIEWPORT8 vp;
m_pd3dDevice->GetViewport( &vp );
FLOAT sx = (x+1.0f)*vp.Width/2;
FLOAT sy = (y+1.0f)*vp.Height/2;
FLOAT sz = z;
FLOAT rhw = 1.0f;
FLOAT fStartX = sx;
FLOAT fLineHeight = ( m_fTexCoords[0][3] - m_fTexCoords[0][1] ) * m_dwTexHeight;
// Fill vertex buffer
FONT2DVERTEX* pVertices;
DWORD dwNumTriangles = 0L;
m_pVB->Lock( 0, 0, (BYTE**)&pVertices, D3DLOCK_DISCARD );
while( *strText )
{
TCHAR c = *strText++;
if( c == _T('\n') )
{
sx = fStartX;
sy += fYScale*vp.Height;
}
if( c < _T(' ') )
continue;
FLOAT tx1 = m_fTexCoords[c-32][0];
FLOAT ty1 = m_fTexCoords[c-32][1];
FLOAT tx2 = m_fTexCoords[c-32][2];
FLOAT ty2 = m_fTexCoords[c-32][3];
FLOAT w = (tx2-tx1)*m_dwTexWidth;
FLOAT h = (ty2-ty1)*m_dwTexHeight;
w *= (fXScale*vp.Width)/fLineHeight;
h *= (fYScale*vp.Height)/fLineHeight;
if( c != _T(' ') )
{
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+h-0.5f,sz,rhw), dwColor, tx1, ty2 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,sz,rhw), dwColor, tx1, ty1 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,sz,rhw), dwColor, tx2, ty2 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+0-0.5f,sz,rhw), dwColor, tx2, ty1 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,sz,rhw), dwColor, tx2, ty2 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,sz,rhw), dwColor, tx1, ty1 );
dwNumTriangles += 2;
if( dwNumTriangles*3 > (MAX_NUM_VERTICES-6) )
{
// Unlock, render, and relock the vertex buffer
m_pVB->Unlock();
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles );
m_pVB->Lock( 0, 0, (BYTE**)&pVertices, D3DLOCK_DISCARD );
dwNumTriangles = 0L;
}
}
sx += w;
}
// Unlock and render the vertex buffer
m_pVB->Unlock();
if( dwNumTriangles > 0 )
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles );
// Restore the modified renderstates
m_pd3dDevice->ApplyStateBlock( m_dwSavedStateBlock );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DrawText()
// Desc: Draws 2D text
//-----------------------------------------------------------------------------
HRESULT CD3DFont::DrawText( FLOAT sx, FLOAT sy, DWORD dwColor,
TCHAR* strText, DWORD dwFlags )
{
if( m_pd3dDevice == NULL )
return E_FAIL;
// Setup renderstate
m_pd3dDevice->CaptureStateBlock( m_dwSavedStateBlock );
m_pd3dDevice->ApplyStateBlock( m_dwDrawTextStateBlock );
m_pd3dDevice->SetVertexShader( D3DFVF_FONT2DVERTEX );
m_pd3dDevice->SetPixelShader( NULL );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(FONT2DVERTEX) );
// Set filter states
if( dwFlags & D3DFONT_FILTERED )
{
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
}
FLOAT fStartX = sx;
// Fill vertex buffer
FONT2DVERTEX* pVertices = NULL;
DWORD dwNumTriangles = 0;
m_pVB->Lock( 0, 0, (BYTE**)&pVertices, D3DLOCK_DISCARD );
while( *strText )
{
TCHAR c = *strText++;
if( c == _T('\n') )
{
sx = fStartX;
sy += (m_fTexCoords[0][3]-m_fTexCoords[0][1])*m_dwTexHeight;
}
if( c < _T(' ') )
continue;
FLOAT tx1 = m_fTexCoords[c-32][0];
FLOAT ty1 = m_fTexCoords[c-32][1];
FLOAT tx2 = m_fTexCoords[c-32][2];
FLOAT ty2 = m_fTexCoords[c-32][3];
FLOAT w = (tx2-tx1) * m_dwTexWidth / m_fTextScale;
FLOAT h = (ty2-ty1) * m_dwTexHeight / m_fTextScale;
if( c != _T(' ') )
{
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+h-0.5f,0.9f,1.0f), dwColor, tx1, ty2 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,0.9f,1.0f), dwColor, tx1, ty1 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,0.9f,1.0f), dwColor, tx2, ty2 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+0-0.5f,0.9f,1.0f), dwColor, tx2, ty1 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+w-0.5f,sy+h-0.5f,0.9f,1.0f), dwColor, tx2, ty2 );
*pVertices++ = InitFont2DVertex( D3DXVECTOR4(sx+0-0.5f,sy+0-0.5f,0.9f,1.0f), dwColor, tx1, ty1 );
dwNumTriangles += 2;
if( dwNumTriangles*3 > (MAX_NUM_VERTICES-6) )
{
// Unlock, render, and relock the vertex buffer
m_pVB->Unlock();
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles );
pVertices = NULL;
m_pVB->Lock( 0, 0, (BYTE**)&pVertices, D3DLOCK_DISCARD );
dwNumTriangles = 0L;
}
}
sx += w;
}
// Unlock and render the vertex buffer
m_pVB->Unlock();
if( dwNumTriangles > 0 )
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles );
// Restore the modified renderstates
m_pd3dDevice->ApplyStateBlock( m_dwSavedStateBlock );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Render3DText()
// Desc: Renders 3D text
//-----------------------------------------------------------------------------
HRESULT CD3DFont::Render3DText( TCHAR* strText, DWORD dwFlags )
{
if( m_pd3dDevice == NULL )
return E_FAIL;
// Setup renderstate
m_pd3dDevice->CaptureStateBlock( m_dwSavedStateBlock );
m_pd3dDevice->ApplyStateBlock( m_dwDrawTextStateBlock );
m_pd3dDevice->SetVertexShader( D3DFVF_FONT3DVERTEX );
m_pd3dDevice->SetPixelShader( NULL );
m_pd3dDevice->SetStreamSource( 0, m_pVB, sizeof(FONT3DVERTEX) );
// Set filter states
if( dwFlags & D3DFONT_FILTERED )
{
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
}
// Position for each text element
FLOAT x = 0.0f;
FLOAT y = 0.0f;
// Center the text block at the origin
if( dwFlags & D3DFONT_CENTERED )
{
SIZE sz;
GetTextExtent( strText, &sz );
x = -(((FLOAT)sz.cx)/10.0f)/2.0f;
y = -(((FLOAT)sz.cy)/10.0f)/2.0f;
}
// Turn off culling for two-sided text
if( dwFlags & D3DFONT_TWOSIDED )
m_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
FLOAT fStartX = x;
TCHAR c;
// Fill vertex buffer
FONT3DVERTEX* pVertices;
DWORD dwVertex = 0L;
DWORD dwNumTriangles = 0L;
m_pVB->Lock( 0, 0, (BYTE**)&pVertices, D3DLOCK_DISCARD );
while( c = *strText++ )
{
if( c == '\n' )
{
x = fStartX;
y -= (m_fTexCoords[0][3]-m_fTexCoords[0][1])*m_dwTexHeight/10.0f;
}
if( c < 32 )
continue;
FLOAT tx1 = m_fTexCoords[c-32][0];
FLOAT ty1 = m_fTexCoords[c-32][1];
FLOAT tx2 = m_fTexCoords[c-32][2];
FLOAT ty2 = m_fTexCoords[c-32][3];
FLOAT w = (tx2-tx1) * m_dwTexWidth / ( 10.0f * m_fTextScale );
FLOAT h = (ty2-ty1) * m_dwTexHeight / ( 10.0f * m_fTextScale );
if( c != _T(' ') )
{
*pVertices++ = InitFont3DVertex( D3DXVECTOR3(x+0,y+0,0), D3DXVECTOR3(0,0,-1), tx1, ty2 );
*pVertices++ = InitFont3DVertex( D3DXVECTOR3(x+0,y+h,0), D3DXVECTOR3(0,0,-1), tx1, ty1 );
*pVertices++ = InitFont3DVertex( D3DXVECTOR3(x+w,y+0,0), D3DXVECTOR3(0,0,-1), tx2, ty2 );
*pVertices++ = InitFont3DVertex( D3DXVECTOR3(x+w,y+h,0), D3DXVECTOR3(0,0,-1), tx2, ty1 );
*pVertices++ = InitFont3DVertex( D3DXVECTOR3(x+w,y+0,0), D3DXVECTOR3(0,0,-1), tx2, ty2 );
*pVertices++ = InitFont3DVertex( D3DXVECTOR3(x+0,y+h,0), D3DXVECTOR3(0,0,-1), tx1, ty1 );
dwNumTriangles += 2;
if( dwNumTriangles*3 > (MAX_NUM_VERTICES-6) )
{
// Unlock, render, and relock the vertex buffer
m_pVB->Unlock();
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles );
m_pVB->Lock( 0, 0, (BYTE**)&pVertices, D3DLOCK_DISCARD );
dwNumTriangles = 0L;
}
}
x += w;
}
// Unlock and render the vertex buffer
m_pVB->Unlock();
if( dwNumTriangles > 0 )
m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, dwNumTriangles );
// Restore the modified renderstates
m_pd3dDevice->ApplyStateBlock( m_dwSavedStateBlock );
return S_OK;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,694 @@
//-----------------------------------------------------------------------------
// File: D3DUtil.cpp
//
// Desc: Shortcut macros and functions for using DX objects
//
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
#define STRICT
#include <tchar.h>
#include <stdio.h>
#include "D3DUtil.h"
#include "DXUtil.h"
#include "D3DX8.h"
//-----------------------------------------------------------------------------
// Name: D3DUtil_InitMaterial()
// Desc: Initializes a D3DMATERIAL8 structure, setting the diffuse and ambient
// colors. It does not set emissive or specular colors.
//-----------------------------------------------------------------------------
VOID D3DUtil_InitMaterial( D3DMATERIAL8& mtrl, FLOAT r, FLOAT g, FLOAT b,
FLOAT a )
{
ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) );
mtrl.Diffuse.r = mtrl.Ambient.r = r;
mtrl.Diffuse.g = mtrl.Ambient.g = g;
mtrl.Diffuse.b = mtrl.Ambient.b = b;
mtrl.Diffuse.a = mtrl.Ambient.a = a;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_InitLight()
// Desc: Initializes a D3DLIGHT structure, setting the light position. The
// diffuse color is set to white; specular and ambient are left as black.
//-----------------------------------------------------------------------------
VOID D3DUtil_InitLight( D3DLIGHT8& light, D3DLIGHTTYPE ltType,
FLOAT x, FLOAT y, FLOAT z )
{
ZeroMemory( &light, sizeof(D3DLIGHT8) );
light.Type = ltType;
light.Diffuse.r = 1.0f;
light.Diffuse.g = 1.0f;
light.Diffuse.b = 1.0f;
D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &D3DXVECTOR3(x, y, z) );
light.Position.x = x;
light.Position.y = y;
light.Position.z = z;
light.Range = 1000.0f;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_CreateTexture()
// Desc: Helper function to create a texture. It checks the root path first,
// then tries the DXSDK media path (as specified in the system registry).
//-----------------------------------------------------------------------------
HRESULT D3DUtil_CreateTexture( LPDIRECT3DDEVICE8 pd3dDevice, TCHAR* strTexture,
LPDIRECT3DTEXTURE8* ppTexture, D3DFORMAT d3dFormat )
{
// Get the path to the texture
TCHAR strPath[MAX_PATH];
DXUtil_FindMediaFile( strPath, strTexture );
// Create the texture using D3DX
return D3DXCreateTextureFromFileEx( pd3dDevice, strPath,
D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, d3dFormat,
D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR,
D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, 0, NULL, NULL, ppTexture );
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_SetColorKey()
// Desc: Changes all texels matching the colorkey to transparent, black.
//-----------------------------------------------------------------------------
HRESULT D3DUtil_SetColorKey( LPDIRECT3DTEXTURE8 pTexture, DWORD dwColorKey )
{
// Get colorkey's red, green, and blue components
DWORD r = ((dwColorKey&0x00ff0000)>>16);
DWORD g = ((dwColorKey&0x0000ff00)>>8);
DWORD b = ((dwColorKey&0x000000ff)>>0);
// Put the colorkey in the texture's native format
D3DSURFACE_DESC d3dsd;
pTexture->GetLevelDesc( 0, &d3dsd );
if( d3dsd.Format == D3DFMT_A4R4G4B4 )
dwColorKey = 0xf000 + ((r>>4)<<8) + ((g>>4)<<4) + (b>>4);
else if( d3dsd.Format == D3DFMT_A1R5G5B5 )
dwColorKey = 0x8000 + ((r>>3)<<10) + ((g>>3)<<5) + (b>>3);
else if( d3dsd.Format != D3DFMT_A8R8G8B8 )
return E_FAIL;
// Lock the texture
D3DLOCKED_RECT d3dlr;
if( FAILED( pTexture->LockRect( 0, &d3dlr, 0, 0 ) ) )
return E_FAIL;
// Scan through each pixel, looking for the colorkey to replace
for( DWORD y=0; y<d3dsd.Height; y++ )
{
for( DWORD x=0; x<d3dsd.Width; x++ )
{
if( d3dsd.Format==D3DFMT_A8R8G8B8 )
{
// Handle 32-bit formats
if( ((DWORD*)d3dlr.pBits)[d3dsd.Width*y+x] == dwColorKey )
((DWORD*)d3dlr.pBits)[d3dsd.Width*y+x] = 0x00000000;
}
else
{
// Handle 16-bit formats
if( ((WORD*)d3dlr.pBits)[d3dsd.Width*y+x] == dwColorKey )
((WORD*)d3dlr.pBits)[d3dsd.Width*y+x] = 0x0000;
}
}
}
// Unlock the texture and return OK.
pTexture->UnlockRect(0);
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_CreateVertexShader()
// Desc: Assembles and creates a file-based vertex shader
//-----------------------------------------------------------------------------
HRESULT D3DUtil_CreateVertexShader( LPDIRECT3DDEVICE8 pd3dDevice,
TCHAR* strFilename, DWORD* pdwVertexDecl,
DWORD* pdwVertexShader )
{
LPD3DXBUFFER pCode;
TCHAR strPath[MAX_PATH];
HRESULT hr;
// Get the path to the vertex shader file
DXUtil_FindMediaFile( strPath, strFilename );
// Assemble the vertex shader file
if( FAILED( hr = D3DXAssembleShaderFromFile( strPath, 0, NULL, &pCode, NULL ) ) )
return hr;
// Create the vertex shader
hr = pd3dDevice->CreateVertexShader( pdwVertexDecl,
(DWORD*)pCode->GetBufferPointer(),
pdwVertexShader, 0 );
pCode->Release();
return hr;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_GetCubeMapViewMatrix()
// Desc: Returns a view matrix for rendering to a face of a cubemap.
//-----------------------------------------------------------------------------
D3DXMATRIX D3DUtil_GetCubeMapViewMatrix( DWORD dwFace )
{
D3DXVECTOR3 vEyePt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vLookDir;
D3DXVECTOR3 vUpDir;
switch( dwFace )
{
case D3DCUBEMAP_FACE_POSITIVE_X:
vLookDir = D3DXVECTOR3( 1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_X:
vLookDir = D3DXVECTOR3(-1.0f, 0.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Y:
vLookDir = D3DXVECTOR3( 0.0f,-1.0f, 0.0f );
vUpDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
break;
case D3DCUBEMAP_FACE_POSITIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
case D3DCUBEMAP_FACE_NEGATIVE_Z:
vLookDir = D3DXVECTOR3( 0.0f, 0.0f,-1.0f );
vUpDir = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
break;
}
// Set the view transform for this cubemap surface
D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookDir, &vUpDir );
return matView;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_GetRotationFromCursor()
// Desc: Returns a quaternion for the rotation implied by the window's cursor
// position.
//-----------------------------------------------------------------------------
D3DXQUATERNION D3DUtil_GetRotationFromCursor( HWND hWnd,
FLOAT fTrackBallRadius )
{
POINT pt;
RECT rc;
GetCursorPos( &pt );
GetClientRect( hWnd, &rc );
ScreenToClient( hWnd, &pt );
FLOAT sx = ( ( ( 2.0f * pt.x ) / (rc.right-rc.left) ) - 1 );
FLOAT sy = ( ( ( 2.0f * pt.y ) / (rc.bottom-rc.top) ) - 1 );
FLOAT sz;
if( sx == 0.0f && sy == 0.0f )
return D3DXQUATERNION( 0.0f, 0.0f, 0.0f, 1.0f );
FLOAT d1 = 0.0f;
FLOAT d2 = sqrtf( sx*sx + sy*sy );
if( d2 < fTrackBallRadius * 0.70710678118654752440 ) // Inside sphere
sz = sqrtf( fTrackBallRadius*fTrackBallRadius - d2*d2 );
else // On hyperbola
sz = (fTrackBallRadius*fTrackBallRadius) / (2.0f*d2);
// Get two points on trackball's sphere
D3DXVECTOR3 p1( sx, sy, sz );
D3DXVECTOR3 p2( 0.0f, 0.0f, fTrackBallRadius );
// Get axis of rotation, which is cross product of p1 and p2
D3DXVECTOR3 vAxis;
D3DXVec3Cross( &vAxis, &p1, &p2);
// Calculate angle for the rotation about that axis
FLOAT t = D3DXVec3Length( &(p2-p1) ) / ( 2.0f*fTrackBallRadius );
if( t > +1.0f) t = +1.0f;
if( t < -1.0f) t = -1.0f;
FLOAT fAngle = 2.0f * asinf( t );
// Convert axis to quaternion
D3DXQUATERNION quat;
D3DXQuaternionRotationAxis( &quat, &vAxis, fAngle );
return quat;
}
//-----------------------------------------------------------------------------
// Name: D3DUtil_SetDeviceCursor
// Desc: Gives the D3D device a cursor with image and hotspot from hCursor.
//-----------------------------------------------------------------------------
HRESULT D3DUtil_SetDeviceCursor( LPDIRECT3DDEVICE8 pd3dDevice, HCURSOR hCursor,
BOOL bAddWatermark )
{
HRESULT hr = E_FAIL;
ICONINFO iconinfo;
BOOL bBWCursor;
LPDIRECT3DSURFACE8 pCursorBitmap = NULL;
HDC hdcColor = NULL;
HDC hdcMask = NULL;
HDC hdcScreen = NULL;
BITMAP bm;
DWORD dwWidth;
DWORD dwHeightSrc;
DWORD dwHeightDest;
COLORREF crColor;
COLORREF crMask;
UINT x;
UINT y;
BITMAPINFO bmi;
COLORREF* pcrArrayColor = NULL;
COLORREF* pcrArrayMask = NULL;
DWORD* pBitmap;
HGDIOBJ hgdiobjOld;
ZeroMemory( &iconinfo, sizeof(iconinfo) );
if( !GetIconInfo( hCursor, &iconinfo ) )
goto End;
if (0 == GetObject((HGDIOBJ)iconinfo.hbmMask, sizeof(BITMAP), (LPVOID)&bm))
goto End;
dwWidth = bm.bmWidth;
dwHeightSrc = bm.bmHeight;
if( iconinfo.hbmColor == NULL )
{
bBWCursor = TRUE;
dwHeightDest = dwHeightSrc / 2;
}
else
{
bBWCursor = FALSE;
dwHeightDest = dwHeightSrc;
}
// Create a surface for the fullscreen cursor
if( FAILED( hr = pd3dDevice->CreateImageSurface( dwWidth, dwHeightDest,
D3DFMT_A8R8G8B8, &pCursorBitmap ) ) )
{
goto End;
}
pcrArrayMask = new DWORD[dwWidth * dwHeightSrc];
ZeroMemory(&bmi, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = dwWidth;
bmi.bmiHeader.biHeight = dwHeightSrc;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
hdcScreen = GetDC( NULL );
hdcMask = CreateCompatibleDC( hdcScreen );
if( hdcMask == NULL )
{
hr = E_FAIL;
goto End;
}
hgdiobjOld = SelectObject(hdcMask, iconinfo.hbmMask);
GetDIBits(hdcMask, iconinfo.hbmMask, 0, dwHeightSrc,
pcrArrayMask, &bmi, DIB_RGB_COLORS);
SelectObject(hdcMask, hgdiobjOld);
if (!bBWCursor)
{
pcrArrayColor = new DWORD[dwWidth * dwHeightDest];
hdcColor = CreateCompatibleDC( GetDC( NULL ) );
if( hdcColor == NULL )
{
hr = E_FAIL;
goto End;
}
SelectObject(hdcColor, iconinfo.hbmColor);
GetDIBits(hdcColor, iconinfo.hbmColor, 0, dwHeightDest,
pcrArrayColor, &bmi, DIB_RGB_COLORS);
}
// Transfer cursor image into the surface
D3DLOCKED_RECT lr;
pCursorBitmap->LockRect( &lr, NULL, 0 );
pBitmap = (DWORD*)lr.pBits;
for( y = 0; y < dwHeightDest; y++ )
{
for( x = 0; x < dwWidth; x++ )
{
if (bBWCursor)
{
crColor = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
crMask = pcrArrayMask[dwWidth*(dwHeightSrc-1-y) + x];
}
else
{
crColor = pcrArrayColor[dwWidth*(dwHeightDest-1-y) + x];
crMask = pcrArrayMask[dwWidth*(dwHeightDest-1-y) + x];
}
if (crMask == 0)
pBitmap[dwWidth*y + x] = 0xff000000 | crColor;
else
pBitmap[dwWidth*y + x] = 0x00000000;
// It may be helpful to make the D3D cursor look slightly
// different from the Windows cursor so you can distinguish
// between the two when developing/testing code. When
// bAddWatermark is TRUE, the following code adds some
// small grey "D3D" characters to the upper-left corner of
// the D3D cursor image.
if( bAddWatermark && x < 12 && y < 5 )
{
// 11.. 11.. 11.. .... CCC0
// 1.1. ..1. 1.1. .... A2A0
// 1.1. .1.. 1.1. .... A4A0
// 1.1. ..1. 1.1. .... A2A0
// 11.. 11.. 11.. .... CCC0
const WORD wMask[5] = { 0xccc0, 0xa2a0, 0xa4a0, 0xa2a0, 0xccc0 };
if( wMask[y] & (1 << (15 - x)) )
{
pBitmap[dwWidth*y + x] |= 0xff808080;
}
}
}
}
pCursorBitmap->UnlockRect();
// Set the device cursor
if( FAILED( hr = pd3dDevice->SetCursorProperties( iconinfo.xHotspot,
iconinfo.yHotspot, pCursorBitmap ) ) )
{
goto End;
}
hr = S_OK;
End:
if( iconinfo.hbmMask != NULL )
DeleteObject( iconinfo.hbmMask );
if( iconinfo.hbmColor != NULL )
DeleteObject( iconinfo.hbmColor );
if( hdcScreen != NULL )
ReleaseDC( NULL, hdcScreen );
if( hdcColor != NULL )
DeleteDC( hdcColor );
if( hdcMask != NULL )
DeleteDC( hdcMask );
SAFE_DELETE_ARRAY( pcrArrayColor );
SAFE_DELETE_ARRAY( pcrArrayMask );
SAFE_RELEASE( pCursorBitmap );
return hr;
}
//-----------------------------------------------------------------------------
// Name: D3DXQuaternionUnitAxisToUnitAxis2
// Desc: Axis to axis quaternion double angle (no normalization)
// Takes two points on unit sphere an angle THETA apart, returns
// quaternion that represents a rotation around cross product by 2*THETA.
//-----------------------------------------------------------------------------
inline D3DXQUATERNION* WINAPI D3DXQuaternionUnitAxisToUnitAxis2
( D3DXQUATERNION *pOut, const D3DXVECTOR3 *pvFrom, const D3DXVECTOR3 *pvTo)
{
D3DXVECTOR3 vAxis;
D3DXVec3Cross(&vAxis, pvFrom, pvTo); // proportional to sin(theta)
pOut->x = vAxis.x;
pOut->y = vAxis.y;
pOut->z = vAxis.z;
pOut->w = D3DXVec3Dot( pvFrom, pvTo );
return pOut;
}
//-----------------------------------------------------------------------------
// Name: D3DXQuaternionAxisToAxis
// Desc: Axis to axis quaternion
// Takes two points on unit sphere an angle THETA apart, returns
// quaternion that represents a rotation around cross product by theta.
//-----------------------------------------------------------------------------
inline D3DXQUATERNION* WINAPI D3DXQuaternionAxisToAxis
( D3DXQUATERNION *pOut, const D3DXVECTOR3 *pvFrom, const D3DXVECTOR3 *pvTo)
{
D3DXVECTOR3 vA, vB;
D3DXVec3Normalize(&vA, pvFrom);
D3DXVec3Normalize(&vB, pvTo);
D3DXVECTOR3 vHalf(vA + vB);
D3DXVec3Normalize(&vHalf, &vHalf);
return D3DXQuaternionUnitAxisToUnitAxis2(pOut, &vA, &vHalf);
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DArcBall::CD3DArcBall()
{
D3DXQuaternionIdentity( &m_qDown );
D3DXQuaternionIdentity( &m_qNow );
D3DXMatrixIdentity( &m_matRotation );
D3DXMatrixIdentity( &m_matRotationDelta );
D3DXMatrixIdentity( &m_matTranslation );
D3DXMatrixIdentity( &m_matTranslationDelta );
m_bDrag = FALSE;
m_fRadiusTranslation = 1.0f;
m_bRightHanded = FALSE;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
VOID CD3DArcBall::SetWindow( int iWidth, int iHeight, float fRadius )
{
// Set ArcBall info
m_iWidth = iWidth;
m_iHeight = iHeight;
m_fRadius = fRadius;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
D3DXVECTOR3 CD3DArcBall::ScreenToVector( int sx, int sy )
{
// Scale to screen
FLOAT x = -(sx - m_iWidth/2) / (m_fRadius*m_iWidth/2);
FLOAT y = (sy - m_iHeight/2) / (m_fRadius*m_iHeight/2);
if( m_bRightHanded )
{
x = -x;
y = -y;
}
FLOAT z = 0.0f;
FLOAT mag = x*x + y*y;
if( mag > 1.0f )
{
FLOAT scale = 1.0f/sqrtf(mag);
x *= scale;
y *= scale;
}
else
z = sqrtf( 1.0f - mag );
// Return vector
return D3DXVECTOR3( x, y, z );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
VOID CD3DArcBall::SetRadius( FLOAT fRadius )
{
m_fRadiusTranslation = fRadius;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
LRESULT CD3DArcBall::HandleMouseMessages( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
static int iCurMouseX; // Saved mouse position
static int iCurMouseY;
static D3DXVECTOR3 s_vDown; // Button down vector
// Current mouse position
int iMouseX = LOWORD(lParam);
int iMouseY = HIWORD(lParam);
switch( uMsg )
{
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
// Store off the position of the cursor when the button is pressed
iCurMouseX = iMouseX;
iCurMouseY = iMouseY;
return TRUE;
case WM_LBUTTONDOWN:
// Start drag mode
m_bDrag = TRUE;
s_vDown = ScreenToVector( iMouseX, iMouseY );
m_qDown = m_qNow;
return TRUE;
case WM_LBUTTONUP:
// End drag mode
m_bDrag = FALSE;
return TRUE;
case WM_MOUSEMOVE:
// Drag object
if( MK_LBUTTON&wParam )
{
if( m_bDrag )
{
// recompute m_qNow
D3DXVECTOR3 vCur = ScreenToVector( iMouseX, iMouseY );
D3DXQUATERNION qAxisToAxis;
D3DXQuaternionAxisToAxis(&qAxisToAxis, &s_vDown, &vCur);
m_qNow = m_qDown;
m_qNow *= qAxisToAxis;
D3DXMatrixRotationQuaternion(&m_matRotationDelta, &qAxisToAxis);
}
else
D3DXMatrixIdentity(&m_matRotationDelta);
D3DXMatrixRotationQuaternion(&m_matRotation, &m_qNow);
m_bDrag = TRUE;
}
else if( (MK_RBUTTON&wParam) || (MK_MBUTTON&wParam) )
{
// Normalize based on size of window and bounding sphere radius
FLOAT fDeltaX = ( iCurMouseX-iMouseX ) * m_fRadiusTranslation / m_iWidth;
FLOAT fDeltaY = ( iCurMouseY-iMouseY ) * m_fRadiusTranslation / m_iHeight;
if( wParam & MK_RBUTTON )
{
D3DXMatrixTranslation( &m_matTranslationDelta, -2*fDeltaX, 2*fDeltaY, 0.0f );
D3DXMatrixMultiply( &m_matTranslation, &m_matTranslation, &m_matTranslationDelta );
}
else // wParam & MK_MBUTTON
{
D3DXMatrixTranslation( &m_matTranslationDelta, 0.0f, 0.0f, 5*fDeltaY );
D3DXMatrixMultiply( &m_matTranslation, &m_matTranslation, &m_matTranslationDelta );
}
// Store mouse coordinate
iCurMouseX = iMouseX;
iCurMouseY = iMouseY;
}
return TRUE;
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
CD3DCamera::CD3DCamera()
{
// Set attributes for the view matrix
SetViewParams( D3DXVECTOR3(0.0f,0.0f,0.0f), D3DXVECTOR3(0.0f,0.0f,1.0f),
D3DXVECTOR3(0.0f,1.0f,0.0f) );
// Set attributes for the projection matrix
SetProjParams( D3DX_PI/4, 1.0f, 1.0f, 1000.0f );
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
VOID CD3DCamera::SetViewParams( D3DXVECTOR3 &vEyePt, D3DXVECTOR3& vLookatPt,
D3DXVECTOR3& vUpVec )
{
// Set attributes for the view matrix
m_vEyePt = vEyePt;
m_vLookatPt = vLookatPt;
m_vUpVec = vUpVec;
D3DXVec3Normalize( &m_vView, &(m_vLookatPt - m_vEyePt) );
D3DXVec3Cross( &m_vCross, &m_vView, &m_vUpVec );
D3DXMatrixLookAtLH( &m_matView, &m_vEyePt, &m_vLookatPt, &m_vUpVec );
D3DXMatrixInverse( &m_matBillboard, NULL, &m_matView );
m_matBillboard._41 = 0.0f;
m_matBillboard._42 = 0.0f;
m_matBillboard._43 = 0.0f;
}
//-----------------------------------------------------------------------------
// Name:
// Desc:
//-----------------------------------------------------------------------------
VOID CD3DCamera::SetProjParams( FLOAT fFOV, FLOAT fAspect, FLOAT fNearPlane,
FLOAT fFarPlane )
{
// Set attributes for the projection matrix
m_fFOV = fFOV;
m_fAspect = fAspect;
m_fNearPlane = fNearPlane;
m_fFarPlane = fFarPlane;
D3DXMatrixPerspectiveFovLH( &m_matProj, fFOV, fAspect, fNearPlane, fFarPlane );
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,341 @@
//-----------------------------------------------------------------------------
// File: DIUtil.cpp
//
// Desc: DirectInput framework class using semantic mapping. Feel free to use
// this class as a starting point for adding extra functionality.
//
// Copyright (C) 1995-2001 Microsoft Corporation. All Rights Reserved.
//-----------------------------------------------------------------------------
#define STRICT
#define DIRECTINPUT_VERSION 0x0800
#include <basetsd.h>
#include <tchar.h>
#include <stdio.h>
#include "DIUtil.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: CInputDeviceManager()
// Desc: Constructor
//-----------------------------------------------------------------------------
CInputDeviceManager::CInputDeviceManager()
{
HRESULT hr = CoInitialize(NULL);
m_bCleanupCOM = SUCCEEDED(hr);
m_dwNumDevices = 0;
m_dwMaxDevices = 10;
m_pDI = NULL;
// Allocate DeviceInfo structs
m_pDevices = NULL;
m_pDevices = (DeviceInfo*) realloc( m_pDevices, m_dwMaxDevices*sizeof(DeviceInfo) );
ZeroMemory( m_pDevices, m_dwMaxDevices*sizeof(DeviceInfo) );
}
//-----------------------------------------------------------------------------
// Name: ~CInputDeviceManager()
// Desc: Destructor
//-----------------------------------------------------------------------------
CInputDeviceManager::~CInputDeviceManager()
{
if( m_pDevices )
{
// Release() all devices
for( DWORD i=0; i<m_dwNumDevices; i++ )
{
m_pDevices[i].pdidDevice->Unacquire();
m_pDevices[i].pdidDevice->Release();
m_pDevices[i].pdidDevice = NULL;
}
free( m_pDevices );
}
// Release() base object
SAFE_RELEASE( m_pDI );
if( m_bCleanupCOM )
CoUninitialize();
}
//-----------------------------------------------------------------------------
// Name: GetDevices()
// Desc: Get the DeviceInfo array and number of devices
//-----------------------------------------------------------------------------
HRESULT CInputDeviceManager::GetDevices( DeviceInfo** ppDeviceInfo,
DWORD* pdwCount )
{
if( NULL==ppDeviceInfo || NULL==pdwCount )
return E_INVALIDARG;
(*ppDeviceInfo) = m_pDevices;
(*pdwCount) = m_dwNumDevices;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: AddDevice()
// Desc: Add the provided device to the list and perform initialization
//-----------------------------------------------------------------------------
HRESULT CInputDeviceManager::AddDevice( const DIDEVICEINSTANCE* pdidi,
const LPDIRECTINPUTDEVICE8 pdidDevice )
{
HRESULT hr;
DWORD dwDeviceType = pdidi->dwDevType;
pdidDevice->Unacquire();
// Set the device's coop level
hr = pdidDevice->SetCooperativeLevel( m_hWnd, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND );
if( FAILED(hr) )
return hr;
// Add new DeviceInfo struct to list, and resize array if needed
m_dwNumDevices++;
if( m_dwNumDevices > m_dwMaxDevices )
{
m_dwMaxDevices += 10;
m_pDevices = (DeviceInfo*) realloc( m_pDevices, m_dwMaxDevices*sizeof(DeviceInfo) );
ZeroMemory( m_pDevices + m_dwMaxDevices - 10, 10*sizeof(DeviceInfo) );
}
DWORD dwCurrentDevice = m_dwNumDevices-1;
m_pDevices[dwCurrentDevice].pdidDevice = pdidDevice;
m_pDevices[dwCurrentDevice].pdidDevice->AddRef();
// Callback into the app so it can adjust the device and set
// the m_pDevices[dwCurrentDevice].pParam field with a device state struct
if( m_AddDeviceCallback )
{
hr = m_AddDeviceCallback( &m_pDevices[dwCurrentDevice], pdidi, m_AddDeviceCallbackParam );
if( FAILED(hr) )
return hr;
}
// Build the action map
hr = m_pDevices[dwCurrentDevice].pdidDevice->BuildActionMap( &m_diaf, m_strUserName, 0 );
if( FAILED(hr) )
return hr;
// Set the action map for the current device
hr = m_pDevices[dwCurrentDevice].pdidDevice->SetActionMap( &m_diaf, m_strUserName, 0 );
if( FAILED(hr) )
return hr;
// Continue enumerating suitable devices
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: EnumSuitableDevicesCB()
// Desc: Callback function for device enumeration. Adds all devices which
// met the search criteria
//-----------------------------------------------------------------------------
BOOL CALLBACK EnumSuitableDevicesCB( LPCDIDEVICEINSTANCE pdidi,
LPDIRECTINPUTDEVICE8 pdidDevice,
DWORD dwFlags, DWORD dwDeviceRemaining,
VOID* pContext )
{
// Add the device to the device manager's internal list
((CInputDeviceManager*)pContext)->AddDevice( pdidi, pdidDevice );
// Continue enumerating suitable devices
return DIENUM_CONTINUE;
}
//-----------------------------------------------------------------------------
// Name: SetActionFormat()
// Desc: Set the action format to the provided DIACTIONFORMAT structure, and
// destroy and recreate device list if flagged
//-----------------------------------------------------------------------------
HRESULT CInputDeviceManager::SetActionFormat( DIACTIONFORMAT& diaf, BOOL bReenumerate )
{
HRESULT hr = S_OK;
// Store the new action format
m_diaf = diaf;
// Only destroy and re-enumerate devices if the caller explicitly wants to. The
// device list may be used within a loop, and kicking off an enumeration while
// the device array is in use would cause problems.
if( bReenumerate )
{
// Cleanup any previously enumerated devices
for( DWORD i=0; i<m_dwNumDevices; i++ )
{
m_pDevices[i].pdidDevice->Unacquire();
m_pDevices[i].pdidDevice->Release();
m_pDevices[i].pdidDevice = NULL;
}
m_dwNumDevices = 0;
// Enumerate suitable DirectInput devices
hr = m_pDI->EnumDevicesBySemantics( m_strUserName, &m_diaf,
EnumSuitableDevicesCB, this, 0L );
if( FAILED(hr) )
return hr;
}
else // Just apply the new maps.
{
// Devices must be unacquired to have a new action map set.
UnacquireDevices();
// Apply the new action map to the current devices.
for( DWORD i=0; i<m_dwNumDevices; i++ )
{
hr = m_pDevices[i].pdidDevice->BuildActionMap( &m_diaf, m_strUserName, 0 );
if( FAILED(hr) )
return hr;
hr = m_pDevices[i].pdidDevice->SetActionMap( &m_diaf, m_strUserName, 0 );
if( FAILED(hr) )
return hr;
}
}
if( FAILED(hr) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Create()
// Desc: Create DirectInput object and perform initialization
//-----------------------------------------------------------------------------
HRESULT CInputDeviceManager::Create( HWND hWnd, TCHAR* strUserName,
DIACTIONFORMAT& diaf,
LPDIMANAGERCALLBACK AddDeviceCallback,
LPVOID pCallbackParam )
{
HRESULT hr;
// Store data
m_hWnd = hWnd;
m_strUserName = strUserName;
m_AddDeviceCallback = AddDeviceCallback;
m_AddDeviceCallbackParam = pCallbackParam;
// Create the base DirectInput object
hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&m_pDI, NULL );
if( FAILED(hr) )
return hr;
hr = SetActionFormat( diaf, TRUE );
if( FAILED(hr) )
return hr;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ConfigureDevices()
// Desc: Pause input and display the device configuration UI
//-----------------------------------------------------------------------------
HRESULT CInputDeviceManager::ConfigureDevices( HWND hWnd, IUnknown* pSurface,
VOID* ConfigureDevicesCB,
DWORD dwFlags, LPVOID pvCBParam )
{
HRESULT hr;
// Initialize all the colors here
DICOLORSET dics;
ZeroMemory(&dics, sizeof(DICOLORSET));
dics.dwSize = sizeof(DICOLORSET);
// Fill in all the params
DICONFIGUREDEVICESPARAMS dicdp;
ZeroMemory(&dicdp, sizeof(dicdp));
dicdp.dwSize = sizeof(dicdp);
dicdp.dwcFormats = 1;
dicdp.lprgFormats = &m_diaf;
dicdp.hwnd = hWnd;
dicdp.lpUnkDDSTarget = pSurface;
if( m_strUserName )
{
dicdp.dwcUsers = 1;
dicdp.lptszUserNames = m_strUserName;
}
// Unacquire the devices so that mouse doesn't control the game while in control panel
UnacquireDevices();
hr = m_pDI->ConfigureDevices( (LPDICONFIGUREDEVICESCALLBACK)ConfigureDevicesCB,
&dicdp, dwFlags, pvCBParam );
if( FAILED(hr) )
return hr;
if( dwFlags & DICD_EDIT )
{
// Re-set up the devices
hr = SetActionFormat( m_diaf, TRUE );
if( FAILED(hr) )
return hr;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: UnacquireDevices()
// Desc: Unacquire all devices in the member list
//-----------------------------------------------------------------------------
VOID CInputDeviceManager::UnacquireDevices()
{
for( DWORD i=0; i<m_dwNumDevices; i++ )
m_pDevices[i].pdidDevice->Unacquire();
}
//-----------------------------------------------------------------------------
// Name: SetFocus()
// Desc: Sets the DirectInput focus to a new HWND
//-----------------------------------------------------------------------------
VOID CInputDeviceManager::SetFocus( HWND hWnd )
{
m_hWnd = hWnd;
UnacquireDevices();
for( DWORD i=0; i<m_dwNumDevices; i++ )
{
// Set the device's coop level
m_pDevices[i].pdidDevice->SetCooperativeLevel( m_hWnd,
DISCL_NONEXCLUSIVE|DISCL_FOREGROUND );
}
}

View File

@@ -0,0 +1,689 @@
//-----------------------------------------------------------------------------
// File: DMUtil.cpp
//
// Desc: DirectMusic framework classes for playing DirectMusic segments and
// DirectMusic scripts. Feel free to use this class as a starting point
// for adding extra functionality.
//
// Copyright (c) 1999-2001 Microsoft Corp. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <dmusicc.h>
#include <dmusici.h>
#include <dsound.h>
#include <dxerr8.h>
#include "DMUtil.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: CMusicManager::CMusicManager()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicManager::CMusicManager()
{
m_pLoader = NULL;
m_pPerformance = NULL;
// Initialize COM
HRESULT hr = CoInitialize(NULL);
m_bCleanupCOM = SUCCEEDED(hr);
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::~CMusicManager()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicManager::~CMusicManager()
{
SAFE_RELEASE( m_pLoader );
if( m_pPerformance )
{
// If there is any music playing, stop it.
m_pPerformance->Stop( NULL, NULL, 0, 0 );
m_pPerformance->CloseDown();
SAFE_RELEASE( m_pPerformance );
}
if( m_bCleanupCOM )
CoUninitialize();
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::Initialize()
// Desc: Inits DirectMusic using a standard audio path
//-----------------------------------------------------------------------------
HRESULT CMusicManager::Initialize( HWND hWnd, DWORD dwPChannels, DWORD dwDefaultPathType )
{
HRESULT hr;
// Create loader object
if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC,
IID_IDirectMusicLoader8, (void**)&m_pLoader ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Create performance object
if( FAILED( hr = CoCreateInstance( CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC,
IID_IDirectMusicPerformance8, (void**)&m_pPerformance ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Initialize the performance with the standard audio path.
// This initializes both DirectMusic and DirectSound and
// sets up the synthesizer. Typcially its easist to use an
// audio path for playing music and sound effects.
if( FAILED( hr = m_pPerformance->InitAudio( NULL, NULL, hWnd, dwDefaultPathType,
dwPChannels, DMUS_AUDIOF_ALL, NULL ) ) )
{
if( hr == DSERR_NODRIVER )
{
DXTRACE( "Warning: No sound card found\n" );
return hr;
}
return DXTRACE_ERR( TEXT("InitAudio"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::SetSearchDirectory()
// Desc: Sets the search directory. If not called, the current working
// directory is used to load content.
//-----------------------------------------------------------------------------
HRESULT CMusicManager::SetSearchDirectory( const TCHAR* strMediaPath )
{
if( NULL == m_pLoader )
return E_UNEXPECTED;
// DMusic only takes wide strings
WCHAR wstrMediaPath[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrMediaPath, strMediaPath );
return m_pLoader->SetSearchDirectory( GUID_DirectMusicAllTypes,
wstrMediaPath, FALSE );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::GetDefaultAudioPath()
// Desc:
//-----------------------------------------------------------------------------
IDirectMusicAudioPath8* CMusicManager::GetDefaultAudioPath()
{
IDirectMusicAudioPath8* pAudioPath = NULL;
if( NULL == m_pPerformance )
return NULL;
m_pPerformance->GetDefaultAudioPath( &pAudioPath );
return pAudioPath;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CollectGarbage()
// Desc: Tells the loader to cleanup any garbage from previously
// released objects.
//-----------------------------------------------------------------------------
VOID CMusicManager::CollectGarbage()
{
if( m_pLoader )
m_pLoader->CollectGarbage();
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateSegmentFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateSegmentFromFile( CMusicSegment** ppSegment,
TCHAR* strFileName,
BOOL bDownloadNow,
BOOL bIsMidiFile )
{
HRESULT hr;
IDirectMusicSegment8* pSegment = NULL;
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment,
IID_IDirectMusicSegment8,
wstrFileName,
(LPVOID*) &pSegment ) ) )
{
if( hr == DMUS_E_LOADER_FAILEDOPEN )
return hr;
return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
}
*ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
if (!*ppSegment)
return E_OUTOFMEMORY;
if( bIsMidiFile )
{
if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile,
0xFFFFFFFF, 0, 0, NULL ) ) )
return DXTRACE_ERR( TEXT("SetParam"), hr );
}
if( bDownloadNow )
{
if( FAILED( hr = (*ppSegment)->Download() ) )
return DXTRACE_ERR( TEXT("Download"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateSegmentFromResource()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateSegmentFromResource( CMusicSegment** ppSegment,
TCHAR* strResource,
TCHAR* strResourceType,
BOOL bDownloadNow,
BOOL bIsMidiFile )
{
HRESULT hr;
IDirectMusicSegment8* pSegment = NULL;
HRSRC hres = NULL;
void* pMem = NULL;
DWORD dwSize = 0;
DMUS_OBJECTDESC objdesc;
// Find the resource
hres = FindResource( NULL,strResource,strResourceType );
if( NULL == hres )
return E_FAIL;
// Load the resource
pMem = (void*)LoadResource( NULL, hres );
if( NULL == pMem )
return E_FAIL;
// Store the size of the resource
dwSize = SizeofResource( NULL, hres );
// Set up our object description
ZeroMemory(&objdesc,sizeof(DMUS_OBJECTDESC));
objdesc.dwSize = sizeof(DMUS_OBJECTDESC);
objdesc.dwValidData = DMUS_OBJ_MEMORY | DMUS_OBJ_CLASS;
objdesc.guidClass = CLSID_DirectMusicSegment;
objdesc.llMemLength =(LONGLONG)dwSize;
objdesc.pbMemData = (BYTE*)pMem;
if (FAILED ( hr = m_pLoader->GetObject( &objdesc,
IID_IDirectMusicSegment8,
(void**)&pSegment ) ) )
{
if( hr == DMUS_E_LOADER_FAILEDOPEN )
return hr;
return DXTRACE_ERR( TEXT("LoadObjectFromFile"), hr );
}
*ppSegment = new CMusicSegment( m_pPerformance, m_pLoader, pSegment );
if( NULL == *ppSegment )
return E_OUTOFMEMORY;
if( bIsMidiFile )
{
// Do this to make sure that the default General MIDI set
// is connected appropriately to the MIDI file and
// all instruments sound correct.
if( FAILED( hr = pSegment->SetParam( GUID_StandardMIDIFile,
0xFFFFFFFF, 0, 0, NULL ) ) )
return DXTRACE_ERR( TEXT("SetParam"), hr );
}
if( bDownloadNow )
{
// The segment needs to be download first before playing.
// However, some apps may want to wait before calling this
// to because the download allocates memory for the
// instruments. The more instruments currently downloaded,
// the more memory is in use by the synthesizer.
if( FAILED( hr = (*ppSegment)->Download() ) )
return DXTRACE_ERR( TEXT("Download"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateScriptFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateScriptFromFile( CMusicScript** ppScript,
TCHAR* strFileName )
{
HRESULT hr;
IDirectMusicScript* pScript = NULL;
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
if ( FAILED( hr = m_pLoader->LoadObjectFromFile( CLSID_DirectMusicScript,
IID_IDirectMusicScript8,
wstrFileName,
(LPVOID*) &pScript ) ) )
return DXTRACE_ERR_NOMSGBOX( TEXT("LoadObjectFromFile"), hr );
if ( FAILED( hr = pScript->Init( m_pPerformance, NULL ) ) )
return DXTRACE_ERR( TEXT("Init"), hr );
*ppScript = new CMusicScript( m_pPerformance, m_pLoader, pScript );
if (!*ppScript)
return E_OUTOFMEMORY;
return hr;
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateChordMapFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateChordMapFromFile( IDirectMusicChordMap8** ppChordMap,
TCHAR* strFileName )
{
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
return m_pLoader->LoadObjectFromFile( CLSID_DirectMusicChordMap,
IID_IDirectMusicChordMap8,
wstrFileName, (LPVOID*) ppChordMap );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::CreateChordMapFromFile()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::CreateStyleFromFile( IDirectMusicStyle8** ppStyle,
TCHAR* strFileName )
{
// DMusic only takes wide strings
WCHAR wstrFileName[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrFileName, strFileName );
return m_pLoader->LoadObjectFromFile( CLSID_DirectMusicStyle,
IID_IDirectMusicStyle8,
wstrFileName, (LPVOID*) ppStyle );
}
//-----------------------------------------------------------------------------
// Name: CMusicManager::GetMotifFromStyle()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicManager::GetMotifFromStyle( IDirectMusicSegment8** ppMotif8,
TCHAR* strStyle, TCHAR* strMotif )
{
HRESULT hr;
IDirectMusicStyle8* pStyle = NULL;
IDirectMusicSegment* pMotif = NULL;
if( FAILED( hr = CreateStyleFromFile( &pStyle, strStyle ) ) )
return DXTRACE_ERR( TEXT("CreateStyleFromFile"), hr );
if( pStyle )
{
// DMusic only takes wide strings
WCHAR wstrMotif[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrMotif, strMotif );
hr = pStyle->GetMotif( wstrMotif, &pMotif );
SAFE_RELEASE( pStyle );
if( FAILED( hr ) )
return DXTRACE_ERR( TEXT("GetMotif"), hr );
pMotif->QueryInterface( IID_IDirectMusicSegment8, (LPVOID*) ppMotif8 );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::CMusicSegment()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicSegment::CMusicSegment( IDirectMusicPerformance8* pPerformance,
IDirectMusicLoader8* pLoader,
IDirectMusicSegment8* pSegment )
{
m_pPerformance = pPerformance;
m_pLoader = pLoader;
m_pSegment = pSegment;
m_pEmbeddedAudioPath = NULL;
m_bDownloaded = FALSE;
// Try to pull out an audio path from the segment itself if there is one.
// This embedded audio path will be used instead of the default
// audio path if the app doesn't wish to use an overriding audio path.
IUnknown* pConfig = NULL;
if( SUCCEEDED( m_pSegment->GetAudioPathConfig( &pConfig ) ) )
{
m_pPerformance->CreateAudioPath( pConfig, TRUE, &m_pEmbeddedAudioPath );
SAFE_RELEASE( pConfig );
}
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::~CMusicSegment()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicSegment::~CMusicSegment()
{
if( m_pSegment )
{
// Tell the loader that this object should now be released
if( m_pLoader )
m_pLoader->ReleaseObjectByUnknown( m_pSegment );
if( m_bDownloaded )
{
if( m_pEmbeddedAudioPath )
m_pSegment->Unload( m_pEmbeddedAudioPath );
else
m_pSegment->Unload( m_pPerformance );
}
SAFE_RELEASE( m_pEmbeddedAudioPath );
SAFE_RELEASE( m_pSegment );
}
m_pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Play()
// Desc: Plays the sound using voice management flags. Pass in DSBPLAY_LOOPING
// in the dwFlags to loop the sound
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Play( DWORD dwFlags, IDirectMusicAudioPath8* pAudioPath )
{
if( m_pSegment == NULL || m_pPerformance == NULL )
return CO_E_NOTINITIALIZED;
if( !m_bDownloaded )
return E_FAIL;
// If an audio path was passed in then use it, otherwise
// use the embedded audio path if there was one.
if( pAudioPath == NULL && m_pEmbeddedAudioPath != NULL )
pAudioPath = m_pEmbeddedAudioPath;
// If pAudioPath is NULL then this plays on the default audio path.
return m_pPerformance->PlaySegmentEx( m_pSegment, 0, NULL, dwFlags,
0, 0, NULL, pAudioPath );
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Download()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Download( IDirectMusicAudioPath8* pAudioPath )
{
HRESULT hr;
if( m_pSegment == NULL )
return CO_E_NOTINITIALIZED;
// If no audio path was passed in, then download
// to the embedded audio path if it exists
// else download to the performance
if( pAudioPath == NULL )
{
if( m_pEmbeddedAudioPath )
hr = m_pSegment->Download( m_pEmbeddedAudioPath );
else
hr = m_pSegment->Download( m_pPerformance );
}
else
{
hr = m_pSegment->Download( pAudioPath );
}
if ( SUCCEEDED( hr ) )
m_bDownloaded = TRUE;
return hr;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Unload()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Unload( IDirectMusicAudioPath8* pAudioPath )
{
HRESULT hr;
if( m_pSegment == NULL )
return CO_E_NOTINITIALIZED;
// If no audio path was passed in, then unload
// from the embedded audio path if it exists
// else unload from the performance
if( pAudioPath == NULL )
{
if( m_pEmbeddedAudioPath )
hr = m_pSegment->Unload( m_pEmbeddedAudioPath );
else
hr = m_pSegment->Unload( m_pPerformance );
}
else
{
hr = m_pSegment->Unload( pAudioPath );
}
if ( SUCCEEDED( hr ) )
m_bDownloaded = FALSE;
return hr;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::IsPlaying()
// Desc:
//-----------------------------------------------------------------------------
BOOL CMusicSegment::IsPlaying()
{
if( m_pSegment == NULL || m_pPerformance == NULL )
return CO_E_NOTINITIALIZED;
return ( m_pPerformance->IsPlaying( m_pSegment, NULL ) == S_OK );
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::Stop()
// Desc: Stops the sound from playing
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::Stop( DWORD dwFlags )
{
if( m_pSegment == NULL || m_pPerformance == NULL )
return CO_E_NOTINITIALIZED;
return m_pPerformance->Stop( m_pSegment, NULL, 0, dwFlags );;
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::SetRepeats()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::SetRepeats( DWORD dwRepeats )
{
if( m_pSegment == NULL )
return CO_E_NOTINITIALIZED;
return m_pSegment->SetRepeats( dwRepeats );
}
//-----------------------------------------------------------------------------
// Name: CMusicSegment::GetStyle()
// Desc:
//-----------------------------------------------------------------------------
HRESULT CMusicSegment::GetStyle( IDirectMusicStyle8** ppStyle, DWORD dwStyleIndex )
{
// Get the Style from the Segment by calling the Segment's GetData() with
// the data type GUID_StyleTrackStyle. 0xffffffff indicates to look at
// tracks in all TrackGroups in the segment. The first 0 indicates to
// retrieve the Style from the first Track in the indicated TrackGroup.
// The second 0 indicates to retrieve the Style from the beginning of the
// segment, i.e. time 0 in Segment time. If this Segment was loaded from a
// section file, there is only one Style and it is at time 0.
return m_pSegment->GetParam( GUID_IDirectMusicStyle, 0xffffffff, dwStyleIndex,
0, NULL, (VOID*)ppStyle );
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::CMusicScript()
// Desc: Constructs the class
//-----------------------------------------------------------------------------
CMusicScript::CMusicScript( IDirectMusicPerformance8* pPerformance,
IDirectMusicLoader8* pLoader,
IDirectMusicScript8* pScript )
{
m_pPerformance = pPerformance;
m_pLoader = pLoader;
m_pScript = pScript;
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::~CMusicScript()
// Desc: Destroys the class
//-----------------------------------------------------------------------------
CMusicScript::~CMusicScript()
{
if( m_pLoader )
{
// Tell the loader that this object should now be released
m_pLoader->ReleaseObjectByUnknown( m_pScript );
m_pLoader = NULL;
}
SAFE_RELEASE( m_pScript );
m_pPerformance = NULL;
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::Play()
// Desc: Calls a routine in the script
//-----------------------------------------------------------------------------
HRESULT CMusicScript::CallRoutine( TCHAR* strRoutine )
{
// DMusic only takes wide strings
WCHAR wstrRoutine[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrRoutine, strRoutine );
return m_pScript->CallRoutine( wstrRoutine, NULL );
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::SetVariableNumber()
// Desc: Sets the value of a variable in the script
//-----------------------------------------------------------------------------
HRESULT CMusicScript::SetVariableNumber( TCHAR* strVariable, LONG lValue )
{
// DMusic only takes wide strings
WCHAR wstrVariable[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrVariable, strVariable );
return m_pScript->SetVariableNumber( wstrVariable, lValue, NULL );
}
//-----------------------------------------------------------------------------
// Name: CMusicScript::GetVariableNumber()
// Desc: Gets the value of a variable in the script
//-----------------------------------------------------------------------------
HRESULT CMusicScript::GetVariableNumber( TCHAR* strVariable, LONG* plValue )
{
// DMusic only takes wide strings
WCHAR wstrVariable[MAX_PATH];
DXUtil_ConvertGenericStringToWide( wstrVariable, strVariable );
return m_pScript->GetVariableNumber( wstrVariable, plValue, NULL );
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,719 @@
//-----------------------------------------------------------------------------
// File: DXUtil.cpp
//
// Desc: Shortcut macros and functions for using DX objects
//
//
// Copyright (c) 1997-2001 Microsoft Corporation. All rights reserved
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <mmsystem.h>
#include <tchar.h>
#include <stdio.h>
#include <stdarg.h>
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: DXUtil_GetDXSDKMediaPath()
// Desc: Returns the DirectX SDK media path
//-----------------------------------------------------------------------------
const TCHAR* DXUtil_GetDXSDKMediaPath()
{
static TCHAR strNull[2] = _T("");
static TCHAR strPath[MAX_PATH];
DWORD dwType;
DWORD dwSize = MAX_PATH;
HKEY hKey;
// Open the appropriate registry key
LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
_T("Software\\Microsoft\\DirectX SDK"),
0, KEY_READ, &hKey );
if( ERROR_SUCCESS != lResult )
return strNull;
lResult = RegQueryValueEx( hKey, _T("DX81SDK Samples Path"), NULL,
&dwType, (BYTE*)strPath, &dwSize );
RegCloseKey( hKey );
if( ERROR_SUCCESS != lResult )
return strNull;
_tcscat( strPath, _T("\\Media\\") );
return strPath;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_FindMediaFile()
// Desc: Returns a valid path to a DXSDK media file
//-----------------------------------------------------------------------------
HRESULT DXUtil_FindMediaFile( TCHAR* strPath, TCHAR* strFilename )
{
HANDLE file;
TCHAR strFullPath[1024];
TCHAR *strShortName;
DWORD cchPath;
if( NULL==strFilename || NULL==strPath )
return E_INVALIDARG;
// Build full path name from strFileName (strShortName will be just the leaf filename)
cchPath = GetFullPathName(strFilename, sizeof(strFullPath)/sizeof(TCHAR), strFullPath, &strShortName);
if ((cchPath == 0) || (sizeof(strFullPath)/sizeof(TCHAR) <= cchPath))
return E_FAIL;
// first try to find the filename given a full path
file = CreateFile( strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL );
if( INVALID_HANDLE_VALUE != file )
{
_tcscpy( strPath, strFullPath );
CloseHandle( file );
return S_OK;
}
// next try to find the filename in the current working directory (path stripped)
file = CreateFile( strShortName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL );
if( INVALID_HANDLE_VALUE != file )
{
_tcscpy( strPath, strShortName );
CloseHandle( file );
return S_OK;
}
// last, check if the file exists in the media directory
_stprintf( strPath, _T("%s%s"), DXUtil_GetDXSDKMediaPath(), strShortName );
file = CreateFile( strPath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL );
if( INVALID_HANDLE_VALUE != file )
{
CloseHandle( file );
return S_OK;
}
// On failure, just return the file as the path
_tcscpy( strPath, strFilename );
return E_FAIL;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadStringRegKey()
// Desc: Helper function to read a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadStringRegKey( HKEY hKey, TCHAR* strRegName, TCHAR* strValue,
DWORD dwLength, TCHAR* strDefault )
{
DWORD dwType;
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(BYTE*)strValue, &dwLength ) )
{
_tcscpy( strValue, strDefault );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteStringRegKey()
// Desc: Helper function to write a registry key string
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteStringRegKey( HKEY hKey, TCHAR* strRegName,
TCHAR* strValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_SZ,
(BYTE*)strValue,
(_tcslen(strValue)+1)*sizeof(TCHAR) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadIntRegKey()
// Desc: Helper function to read a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD* pdwValue,
DWORD dwDefault )
{
DWORD dwType;
DWORD dwLength = sizeof(DWORD);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(BYTE*)pdwValue, &dwLength ) )
{
*pdwValue = dwDefault;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteIntRegKey()
// Desc: Helper function to write a registry key int
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteIntRegKey( HKEY hKey, TCHAR* strRegName, DWORD dwValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
(BYTE*)&dwValue, sizeof(DWORD) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadBoolRegKey()
// Desc: Helper function to read a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL* pbValue,
BOOL bDefault )
{
DWORD dwType;
DWORD dwLength = sizeof(BOOL);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(BYTE*)pbValue, &dwLength ) )
{
*pbValue = bDefault;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteBoolRegKey()
// Desc: Helper function to write a registry key BOOL
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteBoolRegKey( HKEY hKey, TCHAR* strRegName, BOOL bValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_DWORD,
(BYTE*)&bValue, sizeof(BOOL) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ReadGuidRegKey()
// Desc: Helper function to read a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_ReadGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID* pGuidValue,
GUID& guidDefault )
{
DWORD dwType;
DWORD dwLength = sizeof(GUID);
if( ERROR_SUCCESS != RegQueryValueEx( hKey, strRegName, 0, &dwType,
(LPBYTE) pGuidValue, &dwLength ) )
{
*pGuidValue = guidDefault;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_WriteGuidRegKey()
// Desc: Helper function to write a registry key guid
//-----------------------------------------------------------------------------
HRESULT DXUtil_WriteGuidRegKey( HKEY hKey, TCHAR* strRegName, GUID guidValue )
{
if( ERROR_SUCCESS != RegSetValueEx( hKey, strRegName, 0, REG_BINARY,
(BYTE*)&guidValue, sizeof(GUID) ) )
return E_FAIL;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_Timer()
// Desc: Performs timer opertations. Use the following commands:
// TIMER_RESET - to reset the timer
// TIMER_START - to start the timer
// TIMER_STOP - to stop (or pause) the timer
// TIMER_ADVANCE - to advance the timer by 0.1 seconds
// TIMER_GETABSOLUTETIME - to get the absolute system time
// TIMER_GETAPPTIME - to get the current time
// TIMER_GETELAPSEDTIME - to get the time that elapsed between
// TIMER_GETELAPSEDTIME calls
//-----------------------------------------------------------------------------
FLOAT __stdcall DXUtil_Timer( TIMER_COMMAND command )
{
static BOOL m_bTimerInitialized = FALSE;
static BOOL m_bUsingQPF = FALSE;
static BOOL m_bTimerStopped = TRUE;
static LONGLONG m_llQPFTicksPerSec = 0;
// Initialize the timer
if( FALSE == m_bTimerInitialized )
{
m_bTimerInitialized = TRUE;
// Use QueryPerformanceFrequency() to get frequency of timer. If QPF is
// not supported, we will timeGetTime() which returns milliseconds.
LARGE_INTEGER qwTicksPerSec;
m_bUsingQPF = QueryPerformanceFrequency( &qwTicksPerSec );
if( m_bUsingQPF )
m_llQPFTicksPerSec = qwTicksPerSec.QuadPart;
}
if( m_bUsingQPF )
{
static LONGLONG m_llStopTime = 0;
static LONGLONG m_llLastElapsedTime = 0;
static LONGLONG m_llBaseTime = 0;
double fTime;
double fElapsedTime;
LARGE_INTEGER qwTime;
// Get either the current time or the stop time, depending
// on whether we're stopped and what command was sent
if( m_llStopTime != 0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
qwTime.QuadPart = m_llStopTime;
else
QueryPerformanceCounter( &qwTime );
// Return the elapsed time
if( command == TIMER_GETELAPSEDTIME )
{
fElapsedTime = (double) ( qwTime.QuadPart - m_llLastElapsedTime ) / (double) m_llQPFTicksPerSec;
m_llLastElapsedTime = qwTime.QuadPart;
return (FLOAT) fElapsedTime;
}
// Return the current time
if( command == TIMER_GETAPPTIME )
{
double fAppTime = (double) ( qwTime.QuadPart - m_llBaseTime ) / (double) m_llQPFTicksPerSec;
return (FLOAT) fAppTime;
}
// Reset the timer
if( command == TIMER_RESET )
{
m_llBaseTime = qwTime.QuadPart;
m_llLastElapsedTime = qwTime.QuadPart;
m_llStopTime = 0;
m_bTimerStopped = FALSE;
return 0.0f;
}
// Start the timer
if( command == TIMER_START )
{
if( m_bTimerStopped )
m_llBaseTime += qwTime.QuadPart - m_llStopTime;
m_llStopTime = 0;
m_llLastElapsedTime = qwTime.QuadPart;
m_bTimerStopped = FALSE;
return 0.0f;
}
// Stop the timer
if( command == TIMER_STOP )
{
m_llStopTime = qwTime.QuadPart;
m_llLastElapsedTime = qwTime.QuadPart;
m_bTimerStopped = TRUE;
return 0.0f;
}
// Advance the timer by 1/10th second
if( command == TIMER_ADVANCE )
{
m_llStopTime += m_llQPFTicksPerSec/10;
return 0.0f;
}
if( command == TIMER_GETABSOLUTETIME )
{
fTime = qwTime.QuadPart / (double) m_llQPFTicksPerSec;
return (FLOAT) fTime;
}
return -1.0f; // Invalid command specified
}
else
{
// Get the time using timeGetTime()
static double m_fLastElapsedTime = 0.0;
static double m_fBaseTime = 0.0;
static double m_fStopTime = 0.0;
double fTime;
double fElapsedTime;
// Get either the current time or the stop time, depending
// on whether we're stopped and what command was sent
if( m_fStopTime != 0.0 && command != TIMER_START && command != TIMER_GETABSOLUTETIME)
fTime = m_fStopTime;
else
fTime = timeGetTime() * 0.001;
// Return the elapsed time
if( command == TIMER_GETELAPSEDTIME )
{
fElapsedTime = (double) (fTime - m_fLastElapsedTime);
m_fLastElapsedTime = fTime;
return (FLOAT) fElapsedTime;
}
// Return the current time
if( command == TIMER_GETAPPTIME )
{
return (FLOAT) (fTime - m_fBaseTime);
}
// Reset the timer
if( command == TIMER_RESET )
{
m_fBaseTime = fTime;
m_fLastElapsedTime = fTime;
m_fStopTime = 0;
m_bTimerStopped = FALSE;
return 0.0f;
}
// Start the timer
if( command == TIMER_START )
{
if( m_bTimerStopped )
m_fBaseTime += fTime - m_fStopTime;
m_fStopTime = 0.0f;
m_fLastElapsedTime = fTime;
m_bTimerStopped = FALSE;
return 0.0f;
}
// Stop the timer
if( command == TIMER_STOP )
{
m_fStopTime = fTime;
m_fLastElapsedTime = fTime;
m_bTimerStopped = TRUE;
return 0.0f;
}
// Advance the timer by 1/10th second
if( command == TIMER_ADVANCE )
{
m_fStopTime += 0.1f;
return 0.0f;
}
if( command == TIMER_GETABSOLUTETIME )
{
return (FLOAT) fTime;
}
return -1.0f; // Invalid command specified
}
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToWide()
// Desc: This is a UNICODE conversion utility to convert a CHAR string into a
// WCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToWide( WCHAR* wstrDestination, const CHAR* strSource,
int cchDestChar )
{
if( wstrDestination==NULL || strSource==NULL )
return;
if( cchDestChar == -1 )
cchDestChar = strlen(strSource)+1;
MultiByteToWideChar( CP_ACP, 0, strSource, -1,
wstrDestination, cchDestChar-1 );
wstrDestination[cchDestChar-1] = 0;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertWideStringToAnsi()
// Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
// CHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertWideStringToAnsi( CHAR* strDestination, const WCHAR* wstrSource,
int cchDestChar )
{
if( strDestination==NULL || wstrSource==NULL )
return;
if( cchDestChar == -1 )
cchDestChar = wcslen(wstrSource)+1;
WideCharToMultiByte( CP_ACP, 0, wstrSource, -1, strDestination,
cchDestChar-1, NULL, NULL );
strDestination[cchDestChar-1] = 0;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGenericStringToAnsi()
// Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
// CHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGenericStringToAnsi( CHAR* strDestination, const TCHAR* tstrSource,
int cchDestChar )
{
if( strDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
return;
#ifdef _UNICODE
DXUtil_ConvertWideStringToAnsi( strDestination, tstrSource, cchDestChar );
#else
if( cchDestChar == -1 )
{
strcpy( strDestination, tstrSource );
}
else
{
strncpy( strDestination, tstrSource, cchDestChar );
strDestination[cchDestChar-1] = '\0';
}
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGenericStringToWide()
// Desc: This is a UNICODE conversion utility to convert a TCHAR string into a
// WCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGenericStringToWide( WCHAR* wstrDestination, const TCHAR* tstrSource,
int cchDestChar )
{
if( wstrDestination==NULL || tstrSource==NULL || cchDestChar == 0 )
return;
#ifdef _UNICODE
if( cchDestChar == -1 )
{
wcscpy( wstrDestination, tstrSource );
}
else
{
wcsncpy( wstrDestination, tstrSource, cchDestChar );
wstrDestination[cchDestChar-1] = L'\0';
}
#else
DXUtil_ConvertAnsiStringToWide( wstrDestination, tstrSource, cchDestChar );
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToGeneric()
// Desc: This is a UNICODE conversion utility to convert a CHAR string into a
// TCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertAnsiStringToGeneric( TCHAR* tstrDestination, const CHAR* strSource,
int cchDestChar )
{
if( tstrDestination==NULL || strSource==NULL || cchDestChar == 0 )
return;
#ifdef _UNICODE
DXUtil_ConvertAnsiStringToWide( tstrDestination, strSource, cchDestChar );
#else
if( cchDestChar == -1 )
{
strcpy( tstrDestination, strSource );
}
else
{
strncpy( tstrDestination, strSource, cchDestChar );
tstrDestination[cchDestChar-1] = '\0';
}
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertAnsiStringToGeneric()
// Desc: This is a UNICODE conversion utility to convert a WCHAR string into a
// TCHAR string. cchDestChar defaults -1 which means it
// assumes strDest is large enough to store strSource
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertWideStringToGeneric( TCHAR* tstrDestination, const WCHAR* wstrSource,
int cchDestChar )
{
if( tstrDestination==NULL || wstrSource==NULL || cchDestChar == 0 )
return;
#ifdef _UNICODE
if( cchDestChar == -1 )
{
wcscpy( tstrDestination, wstrSource );
}
else
{
wcsncpy( tstrDestination, wstrSource, cchDestChar );
tstrDestination[cchDestChar-1] = L'\0';
}
#else
DXUtil_ConvertWideStringToAnsi( tstrDestination, wstrSource, cchDestChar );
#endif
}
//-----------------------------------------------------------------------------
// Name: _DbgOut()
// Desc: Outputs a message to the debug stream
//-----------------------------------------------------------------------------
HRESULT _DbgOut( TCHAR* strFile, DWORD dwLine, HRESULT hr, TCHAR* strMsg )
{
TCHAR buffer[256];
wsprintf( buffer, _T("%s(%ld): "), strFile, dwLine );
OutputDebugString( buffer );
OutputDebugString( strMsg );
if( hr )
{
wsprintf( buffer, _T("(hr=%08lx)\n"), hr );
OutputDebugString( buffer );
}
OutputDebugString( _T("\n") );
return hr;
}
//-----------------------------------------------------------------------------
// Name: DXUtil_Trace()
// Desc: Outputs to the debug stream a formatted string with a variable-
// argument list.
//-----------------------------------------------------------------------------
VOID DXUtil_Trace( TCHAR* strMsg, ... )
{
#if defined(DEBUG) | defined(_DEBUG)
TCHAR strBuffer[512];
va_list args;
va_start(args, strMsg);
_vsntprintf( strBuffer, 512, strMsg, args );
va_end(args);
OutputDebugString( strBuffer );
#else
UNREFERENCED_PARAMETER(strMsg);
#endif
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertStringToGUID()
// Desc: Converts a string to a GUID
//-----------------------------------------------------------------------------
BOOL DXUtil_ConvertStringToGUID( const TCHAR* strIn, GUID* pGuidOut )
{
UINT aiTmp[10];
if( _stscanf( strIn, TEXT("{%8X-%4X-%4X-%2X%2X-%2X%2X%2X%2X%2X%2X}"),
&pGuidOut->Data1,
&aiTmp[0], &aiTmp[1],
&aiTmp[2], &aiTmp[3],
&aiTmp[4], &aiTmp[5],
&aiTmp[6], &aiTmp[7],
&aiTmp[8], &aiTmp[9] ) != 11 )
{
ZeroMemory( pGuidOut, sizeof(GUID) );
return FALSE;
}
else
{
pGuidOut->Data2 = (USHORT) aiTmp[0];
pGuidOut->Data3 = (USHORT) aiTmp[1];
pGuidOut->Data4[0] = (BYTE) aiTmp[2];
pGuidOut->Data4[1] = (BYTE) aiTmp[3];
pGuidOut->Data4[2] = (BYTE) aiTmp[4];
pGuidOut->Data4[3] = (BYTE) aiTmp[5];
pGuidOut->Data4[4] = (BYTE) aiTmp[6];
pGuidOut->Data4[5] = (BYTE) aiTmp[7];
pGuidOut->Data4[6] = (BYTE) aiTmp[8];
pGuidOut->Data4[7] = (BYTE) aiTmp[9];
return TRUE;
}
}
//-----------------------------------------------------------------------------
// Name: DXUtil_ConvertGUIDToString()
// Desc: Converts a GUID to a string
//-----------------------------------------------------------------------------
VOID DXUtil_ConvertGUIDToString( const GUID* pGuidIn, TCHAR* strOut )
{
_stprintf( strOut, TEXT("{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}"),
pGuidIn->Data1, pGuidIn->Data2, pGuidIn->Data3,
pGuidIn->Data4[0], pGuidIn->Data4[1],
pGuidIn->Data4[2], pGuidIn->Data4[3],
pGuidIn->Data4[4], pGuidIn->Data4[5],
pGuidIn->Data4[6], pGuidIn->Data4[7] );
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,372 @@
//-----------------------------------------------------------------------------
// File: NetVoice.cpp
//
// Desc: DirectPlay Voice framework class. Feel free to use
// this class as a starting point for adding extra functionality.
//
//
// Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#define STRICT
#include <windows.h>
#include <dxerr8.h>
#include <dvoice.h>
#include "NetVoice.h"
#include "DXUtil.h"
//-----------------------------------------------------------------------------
// Name: CNetVoice
// Desc:
//-----------------------------------------------------------------------------
CNetVoice::CNetVoice( LPDVMESSAGEHANDLER pfnDirectPlayClientVoiceMessageHandler,
LPDVMESSAGEHANDLER pfnDirectPlayServerVoiceMessageHandler )
{
m_bHalfDuplex = FALSE;
m_pVoiceClient = NULL;
m_pVoiceServer = NULL;
m_pfnDirectPlayClientVoiceMessageHandler = pfnDirectPlayClientVoiceMessageHandler;
m_pfnDirectPlayServerVoiceMessageHandler = pfnDirectPlayServerVoiceMessageHandler;
}
//-----------------------------------------------------------------------------
// Name: ~CNetVoice
// Desc:
//-----------------------------------------------------------------------------
CNetVoice::~CNetVoice()
{
Free();
}
//-----------------------------------------------------------------------------
// Name: Init()
// Desc: Initializes DirectPlay Voice.
// Params: hDlg: the HWND of the parent window for use by the voice setup wizard
// bCreateSession: if TRUE then it creates the DirectPlay Voice sesson.
// bConnectToSession: if TRUE then it connects to the DirectPlay Voice
// session.
// pDirectPlay: inteface to the IDirectPlay8Client or
// IDirectPlay8Peer interface
// pGuidCT: guid of the voice compression codec
// pdvClientConfig: client config. Can be NULL if bConnectToSession is FALSE.
// lpds: pointer to an existing DirectSound object, or NULL
// if none exists yet.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::Init( HWND hDlg, BOOL bCreateSession, BOOL bConnectToSession,
LPUNKNOWN pDirectPlay, DWORD dwSessionType,
GUID* pGuidCT, DVCLIENTCONFIG* pdvClientConfig, LPDIRECTSOUND lpds )
{
HRESULT hr;
// Typically the host player creates the voice session
if( bCreateSession )
{
if( FAILED( hr = VoiceSessionCreate( pDirectPlay, dwSessionType, pGuidCT ) ) )
return DXTRACE_ERR( TEXT("VoiceSessionCreate"), hr );
}
if( bConnectToSession )
{
// Test the voice setup to make sure the voice setup wizard has been run
if( FAILED( hr = VoiceSessionTestAudioSetup( hDlg ) ) )
{
if( hr == DVERR_USERCANCEL || hr == DVERR_ALREADYPENDING )
return hr;
return DXTRACE_ERR( TEXT("VoiceSessionTestAudioSetup"), hr );
}
// Typically all of the clients connect to the voice session
if( FAILED( hr = VoiceSessionConnect( hDlg, pDirectPlay, pdvClientConfig, lpds ) ) )
return DXTRACE_ERR( TEXT("VoiceSessionConnect"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Free()
// Desc: Frees DirectPlayVoice
//-----------------------------------------------------------------------------
HRESULT CNetVoice::Free()
{
HRESULT hr;
if( m_pVoiceClient )
{
// Have all the clients disconnect from the session
if( FAILED( hr = VoiceSessionDisconnect() ) )
return DXTRACE_ERR( TEXT("VoiceSessionDisconnect"), hr );
}
if( m_pVoiceServer )
{
// Have all the host player destroy the session
if( FAILED( hr = VoiceSessionDestroy() ) )
return DXTRACE_ERR( TEXT("VoiceSessionDestroy"), hr );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: HostMigrate()
// Desc: Starts the DirectPlayVoice session
// The host player should call this to create the voice session. It
// stores the server interface, and addref's it.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::HostMigrate( LPDIRECTPLAYVOICESERVER pdvServerInterface )
{
if( pdvServerInterface == NULL )
return E_INVALIDARG;
SAFE_RELEASE( m_pVoiceServer );
m_pVoiceServer = pdvServerInterface;
// Addref the server since we are storing the pointer.
m_pVoiceServer->AddRef();
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: VoiceSessionTestAudioSetup()
// Desc: Uses IDirectPlayVoiceSetup to test the voice setup.
// All clients should call this once to test the voice audio setup.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::VoiceSessionTestAudioSetup( HWND hDlg )
{
HRESULT hr;
LPDIRECTPLAYVOICETEST pVoiceSetup = NULL;
// Create a DirectPlayVoice setup interface.
if( FAILED( hr = CoCreateInstance( CLSID_DirectPlayVoiceTest, NULL,
CLSCTX_INPROC_SERVER,
IID_IDirectPlayVoiceTest,
(LPVOID*) &pVoiceSetup) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Check to see if the audio tests have been run yet
GUID guidPlayback = DSDEVID_DefaultVoicePlayback;
GUID guidCapture = DSDEVID_DefaultVoiceCapture;
hr = pVoiceSetup->CheckAudioSetup( &guidPlayback,
&guidCapture,
hDlg, DVFLAGS_QUERYONLY );
if( hr == DVERR_RUNSETUP )
{
// Perform the audio tests, since they need to be done before
// any of the DPVoice calls will work.
hr = pVoiceSetup->CheckAudioSetup( &guidPlayback, &guidCapture, hDlg, DVFLAGS_ALLOWBACK );
if( FAILED(hr) )
{
SAFE_RELEASE( pVoiceSetup );
if( hr == DVERR_USERCANCEL || hr == DVERR_ALREADYPENDING )
return hr;
return DXTRACE_ERR( TEXT("CheckAudioSetup"), hr );
}
}
// Done with setup
SAFE_RELEASE( pVoiceSetup );
return hr;
}
//-----------------------------------------------------------------------------
// Name: VoiceSessionCreate()
// Desc: Starts the DirectPlayVoice session
// The host player should call this to create the voice session.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::VoiceSessionCreate( LPUNKNOWN pDirectPlay, DWORD dwSessionType,
GUID* pGuidCT )
{
HRESULT hr;
// Create a DirectPlayVoice server interface.
if( FAILED( hr = CoCreateInstance( CLSID_DirectPlayVoiceServer, NULL,
CLSCTX_INPROC_SERVER,
IID_IDirectPlayVoiceServer,
(LPVOID*) &m_pVoiceServer ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Init the DirectPlayVoice server
if( FAILED( hr = m_pVoiceServer->Initialize( pDirectPlay, m_pfnDirectPlayServerVoiceMessageHandler,
NULL, 0, 0 ) ) )
return DXTRACE_ERR( TEXT("Initialize"), hr );
// Setup and start a DirectPlay session based on globals set by user choices
// in the config dlg box.
DVSESSIONDESC dvSessionDesc;
ZeroMemory( &dvSessionDesc, sizeof(DVSESSIONDESC) );
dvSessionDesc.dwSize = sizeof( DVSESSIONDESC );
dvSessionDesc.dwBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT;
dvSessionDesc.dwBufferQuality = DVBUFFERQUALITY_DEFAULT;
dvSessionDesc.dwFlags = 0;
dvSessionDesc.dwSessionType = dwSessionType;
dvSessionDesc.guidCT = *pGuidCT;
if( FAILED( hr = m_pVoiceServer->StartSession( &dvSessionDesc, 0 ) ) )
return DXTRACE_ERR( TEXT("StartSession"), hr );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: VoiceSessionConnect()
// Desc: Connects to the DirectPlayVoice session.
/// All clients should call this once to join the voice session.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::VoiceSessionConnect( HWND hDlg, LPUNKNOWN pDirectPlay,
DVCLIENTCONFIG* pdvClientConfig, LPDIRECTSOUND lpds )
{
HRESULT hr;
// Create a DirectPlayVoice client interface.
if( FAILED( hr = CoCreateInstance( CLSID_DirectPlayVoiceClient, NULL,
CLSCTX_INPROC_SERVER,
IID_IDirectPlayVoiceClient,
(LPVOID*) &m_pVoiceClient ) ) )
return DXTRACE_ERR( TEXT("CoCreateInstance"), hr );
// Init the DirectPlayVoice client, passing in VoiceMessageHandler() as the
// callback handler for any messages sent to us.
if( FAILED( hr = m_pVoiceClient->Initialize( pDirectPlay,
m_pfnDirectPlayClientVoiceMessageHandler,
(LPVOID*) hDlg, // context value
0, 0 ) ) )
return DXTRACE_ERR( TEXT("Initialize"), hr );
// Setup the DirectPlayVoice sound devices. This just uses the defaults.
DVSOUNDDEVICECONFIG dvSoundDeviceConfig;
dvSoundDeviceConfig.dwSize = sizeof( DVSOUNDDEVICECONFIG );
dvSoundDeviceConfig.dwFlags = 0;
dvSoundDeviceConfig.guidPlaybackDevice = DSDEVID_DefaultVoicePlayback;
dvSoundDeviceConfig.lpdsPlaybackDevice = lpds;
dvSoundDeviceConfig.guidCaptureDevice = DSDEVID_DefaultVoiceCapture;
dvSoundDeviceConfig.lpdsCaptureDevice = NULL;
dvSoundDeviceConfig.hwndAppWindow = hDlg;
dvSoundDeviceConfig.lpdsMainBuffer = NULL;
dvSoundDeviceConfig.dwMainBufferFlags = 0;
dvSoundDeviceConfig.dwMainBufferPriority = 0;
// Connect to the voice session
if( FAILED( hr = m_pVoiceClient->Connect( &dvSoundDeviceConfig,
pdvClientConfig,
DVFLAGS_SYNC ) ) )
return DXTRACE_ERR( TEXT("Connect"), hr );
// Talk to everyone in the session
DVID dvid = DVID_ALLPLAYERS;
if( FAILED( hr = m_pVoiceClient->SetTransmitTargets( &dvid, 1, 0 ) ) )
return DXTRACE_ERR( TEXT("SetTransmitTargets"), hr );
// Get the sound device config and extract if its half duplex
DWORD dwSize = 0;
DVSOUNDDEVICECONFIG* pdvSoundDeviceConfig = NULL;
hr = m_pVoiceClient->GetSoundDeviceConfig( pdvSoundDeviceConfig, &dwSize );
if( FAILED(hr) && hr != DVERR_BUFFERTOOSMALL )
return DXTRACE_ERR( TEXT("GetSoundDeviceConfig"), hr );
pdvSoundDeviceConfig = (DVSOUNDDEVICECONFIG*) new BYTE[ dwSize ];
ZeroMemory( pdvSoundDeviceConfig, dwSize );
pdvSoundDeviceConfig->dwSize = sizeof(DVSOUNDDEVICECONFIG);
hr = m_pVoiceClient->GetSoundDeviceConfig( pdvSoundDeviceConfig, &dwSize );
if( FAILED(hr) )
return DXTRACE_ERR( TEXT("GetSoundDeviceConfig"), hr );
m_bHalfDuplex = ( (pdvSoundDeviceConfig->dwFlags & DVSOUNDCONFIG_HALFDUPLEX) != 0 );
SAFE_DELETE_ARRAY( pdvSoundDeviceConfig );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ChangeVoiceClientSettings()
// Desc: Changes the client config to globals set by user choices
// in the config dlg box.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::ChangeVoiceClientSettings( DVCLIENTCONFIG* pdvClientConfig )
{
HRESULT hr;
if( m_pVoiceClient == NULL )
return CO_E_NOTINITIALIZED;
// Change the client config
if( FAILED( hr = m_pVoiceClient->SetClientConfig( pdvClientConfig) ) )
return DXTRACE_ERR( TEXT("SetClientConfig"), hr );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: VoiceSessionDisconnect()
// Desc: Disconnects from the DirectPlayVoice session
// All clients should call this once to leave the voice session.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::VoiceSessionDisconnect()
{
if( m_pVoiceClient )
{
m_pVoiceClient->Disconnect( DVFLAGS_SYNC );
SAFE_RELEASE( m_pVoiceClient );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: VoiceSessionDestroy()
// Desc: Stops the DirectPlayVoice session
// The host player should call this once to destroy the voice session.
//-----------------------------------------------------------------------------
HRESULT CNetVoice::VoiceSessionDestroy()
{
if( m_pVoiceServer )
{
m_pVoiceServer->StopSession( 0 );
SAFE_RELEASE( m_pVoiceServer );
}
return S_OK;
}