XFactorDev.net Article   XFactorDev.net Article

 

Prt-4- Finally Some Graphics..

 

Here is where the road to our graphics engine begins....of course it will grow in time, but in the beginning all it will do is Initilize our DirectX API functions...

 

 

 

So now we create the CGraphics class, which is contained in code/graphics/graphics.cpp and code/graphics/graphics.h....and note, I'm starting to put the files in folders...so a reusable library can be built up ;)

so what does this amazing CGraphics class look like?  Well its not the most advanced piece of coding you'll ever come across, and its use is very basic, but we need it!  Without it DirectX won't be very happy. It has 2 member variables:

 

      IDirect3D8          *m_pD3D;                      // DirectX Object

      IDirect3DDevice8    *m_pD3DDevice;                // Screen Object

 

The first is to initialize DirectX, the second is to hold the screen settings.

 

The code is as follows:

 

 

class CGraphics

{

      IDirect3D8              *m_pD3D;                          // DirectX Object

      IDirect3DDevice8        *m_pD3DDevice;                    // Screen Object

 

 

public:

      // Settup and destroy parts.

      bool Create();

      bool Release();

 

public:

      // We may need to draw to the screen, so we'll need a way of getting to

      // the Screen Object (m_pD3DDevice), and this is what this function does.

      // Returns true (1) if all went okay.

      bool GetScreen(IDirect3DDevice8 **pDevice);

 

};

 

 

And the code for the functions is in graphics.cpp:

 

 

// Settup and destroy parts, returns true (1) for all went okay, and

// returns false (0) if there was a problem or error.

 

bool CGraphics::Create()

{

    // First of all, create the main D3D object. If it is created successfully we

    // should get a pointer to an IDirect3D8 interface.

    m_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

 

 

    // Create a structure to hold the settings for our device

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

 

 

    // Fill the structure.

    // Set fullscreen 640x480x32 mode

    d3dpp.BackBufferWidth = 640;

    d3dpp.BackBufferHeight = 480;

    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

 

    // Create one backbuffer

    d3dpp.BackBufferCount = 1;

     

    // Set up how the backbuffer is "presented" to the frontbuffer each time

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

     

    //Create a Direct3D device.

    m_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,

                                          D3DCREATE_HARDWARE_VERTEXPROCESSING,

                                          &d3dpp, &m_pD3DDevice);

 

    return true; // all okay

}

 

 

bool CGraphics::Release()

{

      // Rule -1-, always tidy up after you've finished doing anything.

      m_pD3DDevice->Release();

      m_pD3D->Release();

 

      return true; // all okay

}

 

// This is our accessor member function - which we use to get a hold

// of our screen object.

bool CGraphics::GetScreen(IDirect3DDevice8 **pDevice)

{

      *pDevice = m_pD3DDevice;

 

      return true; // all okay

}

 

 

 

Well there you have it.... we call Create() at the start of our game and Release() at the end, and every time we need to get a hold of our Screen Object we just call GetScreen(..).... it can't be more simple.  I've cut out any error checking and majour error checking, as I thought it would obscure the meaning of the code...I don't want you to get lost at such an early stage.

 

[NOTE]  I think it might be worth noting that you should really check the return values of things like CreateDevice(..) etc and take the appropriate action, also member variables should be set to initial values in the constructor such as NULL for pointers.  Again we can add in these things later, but its useful to remember!

 

Now we have to plug it into our gameengine code...its doesn't take a sec.....just 3 easy steps:

 

-1- Add the declaration of it to the game.h header, e.g. "#include "code/graphics/graphics.h"

 

-2- Declare an instance of it in our CGame class, CGraphics m_g;

 

-3- Use it in our code...which means calling  m_g.Create() constructor, m_g.Release() in the destructor and using it with m_g.GetScreen(..) everywhere else.

 

What I've done with the code is I've set it up so it makes your screen go red!  Yup...so you know its working... ... I've put the extracts from game.cpp and game.h below so you can have a looksy at whats happening incase you've got lost.

 

// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

// NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

 

#include "code/graphics/graphics.h"

 

 

class CGame

{

protected:

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      // Lets create an instance of our CGraphics class!...m for member, and

      // I chose g for graphics.....not a very descriptive name but I like it.

      CGraphics m_g;

 

public:

      // Initilisation called only once in the constructor

      CGame();

 

      // GameLoop, gets called repeatedly while it returns 1;

      bool GameLoop();

 

      // Cleanup process, all done in our destructor

      ~CGame();

};

 

 

 

 

// Initilisation called only once in the constructor

CGame::CGame()

{

      m_g.Create();

}

 

// GameLoop, gets called repeatedly while it returns 1;

bool CGame::GameLoop()

{

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      // NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW

      IDirect3DDevice8 *pD3DDevice = NULL;

      m_g.GetScreen( &pD3DDevice );

 

      // Lets use it to clear the screen....set it to red ;)

      // Clear the backbuffer to black

      //                                                         r   g  b

      pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);

     

      // Begin the scene

      pD3DDevice->BeginScene();

 

      // End the scene

      pD3DDevice->EndScene();

     

      // Filp the back and front buffers so that whatever has been rendered on the

      // back buffer will now be visible on screen (front buffer).

      pD3DDevice->Present(NULL, NULL, NULL, NULL);

 

 

      // Return 1 for the game to continue, 0 to terminate.

      return 1;

}

 

// Cleanup process, all done in our destructor

CGame::~CGame()

{

      m_g.Release();

}

 

 

 

 

 

 

 

 

  XFactorDev.net Article



      
webmaster@xfactordev.net