Engine
The Game Engine for Ian Parberry's "Introduction to Game Physics with Box2D"
 All Classes Files Functions Variables Pages
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
CRenderer Class Reference

The renderer. More...

#include <renderer.h>

Public Member Functions

 CRenderer ()
 Constructor.
 
void Initialize ()
 Initialize Direct3D.
 
BOOL BeginScene ()
 Start of frame.
 
void DrawBackground ()
 Draw the background to the screen.
 
void EndScene ()
 End of frame.
 
void End ()
 Release all DirectX resources.
 
void Load (int sprite, char *name)
 Load sprite.
 
void draw (int sprite, float x, float y, float angle=0.0f, float xsize=1.0f, float ysize=1.0f)
 Draw sprite.
 
void drawtop (int sprite, float x, float y, float angle=0.0f, float size=1.0f)
 Draw sprite at top of screen.
 
void GetWorldSize (int &x, int &y)
 Get the Render World width and height.
 

Protected Member Functions

HRESULT CreateD3DDevice (HWND hwnd)
 Create D3D device.
 
void SetWorldMatrix (float dx=0.0f)
 Set the world matrix.
 
void SetViewMatrix (float x, float y, float z)
 Set the view matrix.
 
void SetProjectionMatrix ()
 Set the projection matrix.
 
void SetAmbientLighting ()
 Set ambient lighting.
 
void SetTextureStates ()
 Set texture stage states.
 
void SetD3dStates ()
 Set D3D states.
 
BOOL RestoreDevice ()
 Restore lost device.
 
BOOL DeviceLost ()
 Check for lost device.
 
void ReleaseD3D ()
 Release the D3D object.
 
void LoadTexture (LPDIRECT3DTEXTURE9 &texture, char *filename)
 Load a texture.
 
BOOL PageFlip ()
 Graphics device page flip.
 
void GetTextDC (COLORREF color)
 Get Windows device context.
 
void ReleaseTextDC ()
 Release device context.
 
void TextWrite (char *string, COLORREF color=RGB(255, 255, 255))
 Write text to screen.
 
void InitBackgroundVertexBuffer ()
 Initialize vertex buffer for background.
 
void LoadBackground ()
 Load the background image.
 

Protected Attributes

D3DPRESENT_PARAMETERS m_d3dpp
 Device specifications.
 
IDirect3D9 * m_d3d
 D3D object.
 
IDirect3DSurface9 * m_d3dbackbuffer
 Back buffer for rendering.
 
CTextManager m_cTextManager
 Text manager.
 
LPDIRECT3DVERTEXBUFFER9 m_pBackgroundVB
 Vertex buffer for background.
 
LPDIRECT3DTEXTURE9 m_pBackgroundTexture
 Texture for background.
 
IDirect3DDevice9 * m_d3ddevice
 Graphics device.
 
CSpriteManager m_cSpriteManager
 Sprite manager.
 
int m_nScreenWidth
 Screen width in pixels.
 
int m_nScreenHeight
 Screen height in pixels.
 

Friends

void InitGame ()
 

Detailed Description

The renderer.

The renderer class handles all the nasty Direct3D details that I don't want readers to get into, or even be distracted by.

Member Function Documentation

BOOL CRenderer::BeginScene ( )

Start of frame.

Start the render pipeline.

Returns
TRUE if it starts correctly.
HRESULT CRenderer::CreateD3DDevice ( HWND  hwnd)
protected

Create D3D device.

Parameters
hwndWindow handle.
Returns
The error code returned by D3D, if any.
BOOL CRenderer::DeviceLost ( )
protected

Check for lost device.

Returns
TRUE if the D3D device is lost
void CRenderer::draw ( int  sprite,
float  x,
float  y,
float  angle = 0.0f,
float  xsize = 1.0f,
float  ysize = 1.0f 
)

Draw sprite.

Draw a sprite, using the Sprite Manager to do the heavy lifting.

Parameters
spriteIndex of sprite in Sprite Manager.
xHorizontal coordinate in screen space.
yVertical coordinate in screen space.
angleOrientation in radians.
xsizeWidth of sprite, 1.0 is actual size.
ysizeHeight of sprite, 1.0 is actual size.
void CRenderer::drawtop ( int  sprite,
float  x,
float  y,
float  angle = 0.0f,
float  size = 1.0f 
)

Draw sprite at top of screen.

Draw a sprite, using the Sprite Manager to do the heavy lifting. This time the vertical coordinate is measured down from top of screen.

Parameters
spriteIndex of sprite in Sprite Manager.
xHorizontal coordinate in screen space.
yVertical coordinate in screen space, measured from top of screen.
angleOrientation in radians.
sizeSize of sprite, 1.0 is actual size.
void CRenderer::End ( )

Release all DirectX resources.

Release game textures. All textures used in the game are released - the release function is kind of like a destructor for DirectX entities, which are COM objects.

void CRenderer::EndScene ( )

End of frame.

Stop the render pipeline.

void CRenderer::GetTextDC ( COLORREF  color)
protected

Get Windows device context.

Get device context for drawing screen text.

Parameters
colorColor to draw text in.
void CRenderer::GetWorldSize ( int &  w,
int &  h 
)

Get the Render World width and height.

Parameters
wWidth.
hHeight.
void CRenderer::InitBackgroundVertexBuffer ( )
protected

Initialize vertex buffer for background.

Create and set up a vertex buffer for the background image.

void CRenderer::Initialize ( )

Initialize Direct3D.

Initialize the renderer. Create a D3D object, set the graphics mode, create a vertex buffer, get a pointer to the back buffer, squirrel away the screen width and height.

void CRenderer::Load ( int  sprite,
char *  name 
)

Load sprite.

Load a sprite using the Sprite Manager to do the heavy lifting.

Parameters
spriteIndex of sprite in Sprite Manager.
nameName of sprite in GameSettings.xml
void CRenderer::LoadBackground ( )
protected

Load the background image.

Load the background image after getting the file name from g_xmlSettings.

void CRenderer::LoadTexture ( LPDIRECT3DTEXTURE9 &  texture,
char *  filename 
)
protected

Load a texture.

Load a texture from a file. Loads an image from a file into a D3D texture. Aborts if the file doesn't exist.

Parameters
texturepointer to D3D texture to receive the image
filenamename of the fle containing the texture
BOOL CRenderer::PageFlip ( )
protected

Graphics device page flip.

Show the back buffer. If we are fullscreen, flip back buffer to front to show next frame of animation. If we are windowed, blit the back buffer to the front instead. D3D takes care of the details.

Returns
TRUE if it succeeded
void CRenderer::ReleaseD3D ( )
protected

Release the D3D object.

Release the D3D object after releasing the background vertex buffer and D3D device, and D3D object. D3D requires that it be done in this order.

BOOL CRenderer::RestoreDevice ( )
protected

Restore lost device.

Restore the D3D device. The D3D device can be lost, for example, by the use alt+tabbing out of the app. This function restores the device, resets the default states that we initially set, and tests the result.

Returns
TRUE if it succeeds
void CRenderer::SetAmbientLighting ( )
protected

Set ambient lighting.

Set the ambient lighting and the material to bright white.

void CRenderer::SetD3dStates ( )
protected

Set D3D states.

Set the D3D render states (texture states, world matrix, view matrix, projection matrix, background vertex buffer) to useful values for the game.

void CRenderer::SetProjectionMatrix ( )
protected

Set the projection matrix.

Set the projection matrix to orthogonal projection with the window aspect ratio and a 90 degree field of view.

void CRenderer::SetTextureStates ( )
protected

Set texture stage states.

Set the D3D texture states to useful defaults.

void CRenderer::SetViewMatrix ( float  x,
float  y,
float  z 
)
protected

Set the view matrix.

Set the view matrix's orientation to default, position to parameters.

Parameters
xx-coordinate of camera position
yy-coordinate of camera position
zz-coordinate of camera position
void CRenderer::SetWorldMatrix ( float  dx = 0.0f)
protected

Set the world matrix.

Set the world matrix to a translation matrix a given distance along the X-axis.

Parameters
dxdistance along the X-axis
void CRenderer::TextWrite ( char *  string,
COLORREF  color = RGB(255, 255, 255) 
)
protected

Write text to screen.

Use the Text Manager to draw colored text to the back buffer.

Parameters
stringText to display.
colorColor to display it in.

The documentation for this class was generated from the following files: