Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / Win32

Half Life Game Level Viewer

Rate me:
Please Sign up or sign in to vote.
4.61/5 (23 votes)
7 Feb 2009CPOL26 min read 80.1K   2.3K   60  
DirectX based application to open and view Half Life 1 game files
//
//	DXRenderer.h
//
//	Wrapper class for DirectX 9.x rendering API
//
//	Copyright 2005 Paul Higinbotham
//

#ifndef __DXRENDERER_H_
#define __DXRENDERER_H_


#include "..\IRenderer.h"

// Direct3D includes
#include <d3d9.h>
#include <d3dx9.h>
#include <dxerr9.h>
#include <d3d9types.h>

#include "d3dsettings.h"
#include "d3denumeration.h"


namespace ZGraphics
{

class DXRenderer : public IRenderer
{
public:
	DXRenderer();
	~DXRenderer();

	bool Initialize(HWND hWnd);
	const CD3DSettings & GetDisplaySettings() const;
	void SetWindowMode(bool bWindowed);
	void SetWindowedSize(int nWidth, int nHeight);
	int LaunchSettingsDialog();
	void ReCreateDevice();

	// Very thin wrappers around DirectX9 APIs
	HRESULT SetMaterial(const D3DMATERIAL9 * pMtrl);
	IDirect3DTexture9 * GetTexture(const BYTE * pbTexture, UINT uLevels, DWORD dwUsage, D3DFORMAT format, int nWidth, int nHeight);
	IDirect3DTexture9 * CreateTextureFromFile(LPCTSTR pSrcFile);
	HRESULT SetTexture(DWORD dwStage, IDirect3DBaseTexture9 * pTexture);
	HRESULT SetTextureState(DWORD dwStage, D3DTEXTURESTAGESTATETYPE Type, DWORD dwValue);
	HRESULT SetSamplerState(DWORD dwSampler, D3DSAMPLERSTATETYPE Type, DWORD dwValue);
	HRESULT SetRenderState(D3DRENDERSTATETYPE Type, DWORD dwValue);
	HRESULT SetTransform(D3DTRANSFORMSTATETYPE Type, const Matrix4f & Matrix);
	HRESULT GetTransform(D3DTRANSFORMSTATETYPE Type, Matrix4f & Matrix);
	HRESULT SetLight(DWORD dwIndex, const D3DLIGHT9 * pLight);
	HRESULT EnableLight(DWORD dwIndex, BOOL bEnable);
	HRESULT SetFVF(DWORD dwFVF);
	HRESULT BeginScene();
	HRESULT EndScene();
	HRESULT Present(const RECT * pSourceRect, const RECT * pDestRect, HWND hDestWindowOverride, const RGNDATA * pDirtyRegion);
	HRESULT Clear(DWORD dwCount, const D3DRECT *pRects, DWORD dwFlags, D3DCOLOR Color, float fZ, DWORD Stencil);

	HRESULT CreateProjectionMatrixLH(Matrix4f & Matrix, float fFOV, float fAspect, float fNearD, float fFarD);
	HRESULT SaveTextureToFile(LPCSTR pszFilename, IDirect3DBaseTexture9 * pTexture, D3DXIMAGE_FILEFORMAT DestFormat);

	HRESULT CreateVertexBuffer(const Array<HFVERTEX> * pVertices, IDirect3DVertexBuffer9 ** ppVBuffer);
	HRESULT DrawPrimitiveT(D3DPRIMITIVETYPE Type, const Array<HFVERTEX> * pVertices, int nNumPrimitives);
	HRESULT DrawPrimitive(D3DPRIMITIVETYPE Type, int nStartIndex, int nNumPrimitives);

	HRESULT SetStreamSource(UINT uStream, IDirect3DVertexBuffer9 * pVBuffer, UINT uOffsetBytes);

	HRESULT CompileShader(LPCSTR pSrcData, UINT srcDataLen, CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName, 
						  LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER* ppErrorMsgs, LPD3DXCONSTANTTABLE *ppConstantTable);
	HRESULT CompileShaderFromFile(LPCSTR pSrcFile, CONST D3DXMACRO* pDefines, LPD3DXINCLUDE pInclude, LPCSTR pFunctionName,
								  LPCSTR pProfile, DWORD Flags, LPD3DXBUFFER* ppShader, LPD3DXBUFFER *ppErrorMsgs, LPD3DXCONSTANTTABLE *ppConstantTable);
	LPCSTR GetVertexShaderProfile();
	HRESULT CreateVertexShader(const DWORD * pFunction, IDirect3DVertexShader9 ** ppShader);
	HRESULT SetVertexShader(IDirect3DVertexShader9 * pShader);

	HRESULT SetCTableMatrix(LPD3DXCONSTANTTABLE pConstantTable, LPCSTR pszConstantName, const Matrix4f & Matrix);
	HRESULT SetCTableVector(LPD3DXCONSTANTTABLE pConstantTable, LPCSTR pszConstantName, const Vector4f & Vector);
	HRESULT SetCTableFloat(LPD3DXCONSTANTTABLE pConstantTable, LPCSTR pszConstantName, float fScalar);

	const D3DCAPS9 * GetD3DCaps() const;

private:
	bool _initD3DEnvironment();
	void _buildPresentParamsFromSettings();
	void _cleanUp();

private:
	HWND					m_hWnd;				//	Main app window handle
	LPDIRECT3D9				m_pD3D;				//	Main D3D object
    LPDIRECT3DDEVICE9		m_pD3Ddevice;		//	D3D rendering device
    D3DPRESENT_PARAMETERS	m_D3Dpp;			//	Parameters for CreateDevice/Reset
	D3DCAPS9				m_D3Dcaps;			//	Caps for the device
	DWORD					m_dwCreateFlags;	//	Indicate sw or hw vertex processing
	bool					m_bIsWindowMode;	//	Indicates whether render is running in windowed mode or fullscreen
	//
	CD3DEnumeration			m_D3DEnumeration;	//	MS SDK class to enumerate all adaptors and devices
	CD3DSettings			m_D3DSettings;		//	Current D3D adaptor/device settings from MS SDK
	D3DDEVTYPE				m_CurrDevType;
	int						m_nCurrAdaptor;
};


} /* namespace ZGraphics */


#endif /* __DXRENDERER_H_ */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
I am a senior software developer currently doing contract work for Microsoft. My educational background is in electrical engineering and I hold a masters degree from the University of Washington. I have experience in hardware and systems design but have done primarily software development for the last two decades. I have worked for various small companies as well as start-up companies, and have worked as a full time employee SDE at Microsoft Corporation.

Comments and Discussions