www.xbdev.net xbdev - software development
Monday December 10, 2018
home | about | contact | Donations

Prt1. Windows and DirectX - hand in hand...
by bkenwright@xbdev.net

Setting up a simple directx arrangment... (download full source code)

This is responsible for creation of our window... its the windows entry point and setup... no directx is in this file...just the windows setup.  Its one of those pieces of code with more or less remains the same. This is where we do the DirectX iniilisation and de-initilsation!  Two functions, one thats called at the start of our program, and the other just before our window is destroyed.  Again you could just copy and past this over and over again as it more or less stays the same! Now for our actual code!  We can change this to our hearts content, and call other directX functions, classes etc from this file... it keep it nice and simple this way.

So what do these wonderful functions do and what will you get on the screen if you run them!  Well not much at the moment, as I'm going to be showing you various tutorials for drawing 3D shapes, lighting, texturing etc.  And so that you don't get lost with the principle of Classes etc I chose to do a very simple layout.  To me this seems very simple to follow and you should be able to keep up with me as I go along.  I'll also not have to constantly repeat certain areas of code!  As I've mentioned above, the files main.cpp and init.cpp will be the same... I'll warn you way way in advance with big red lettering if I change anything ;)

Its not much to look at when you first power it up!  You typed all that code in and this is all you get...lol  Well remember a lot of that code is repetative... but you'll soon get used to it.  As you progress with DirectX you'll start to develop your own library's etc where you squirrel away your code in functions and classes so that when you need it you just dig it out.


Well I better show you some code... get it out the way nice and early so you can worry about other things.  Think it might be best if you grab a really strong cup of coffee before even looking at it... and for those who like scribbling.. I recomment printing it out and religously understanding each part of it first... write on it etc..  understand why I'm doing each part.... as after this we'll just burry these pieces away :)



/*                                                                         */

/* File: main.cpp                                                          */

/* This is the entry point for our program, an all where going to do in    */

/* here is set up the windows stuff!  Its going to more or less stay the   */

/* same.  So this file will usually be one of those copy and past jobs...  */

/* .. all we do is initilize a window and then set up the message          */

/* processing function, and when "no" messages are benig processed we call */

/* our mainloop() function which is in game.cpp.  This is so that we can   */

/* just keep changing that one file (game.cpp) and leave the windows code  */

/* alone.  One other thing, I put the directX intilisation and de-init     */

/* code in another seperate file... init.cpp I called it... the directX    */

/* init code is called at the start of our program, and the de-init code   */

/* is called at the end.. just before our window is destroyed.!  So this   */

/* in init.cpp is the same over and over again!  So we can just learn it   */

/* once and copy and past it in future.

/*                                                                         */



//First Step Tutorial to 3D programming with DirectX 8


#include <windows.h>

#include <stdio.h>


// These few lines are compiler directives that include the DirectX library's

// during linking.  You could instead inside visual studio goto Project->

// settings menu and under the "Link" tabe add the necessary librarys instead.

#pragma comment(lib, "D3d8.lib") //directX 8

#pragma comment(lib, "D3dx8.lib")

#include <d3dx8.h>



// Our directX once init/de_init code will be put in the "init.cpp" file.

#include "init.cpp"

// Any functions will be put in "game.cpp"

#include "game.cpp"



/*                                                                         */

/* Handle all messages for the main window here                            */

/*                                                                         */


long _stdcall MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)


    if(uMsg == WM_DESTROY)


            // inside init.cpp we destroy any directx memory we allocated etc,

            // tidy up before leaving.

            // TIDY UP DIRECTX HERE BEFORE EXITING HERE!!!!!!!!!!!!!!!!!!!!!!!!!




    return DefWindowProc(hWnd, uMsg, wParam, lParam);




/*                                                                         */

/* Program entry point.                                                    */

/*                                                                         */


int _stdcall WinMain(HINSTANCE i, HINSTANCE, char* k, int)


    MSG msg;

      char szname[] = "DirectX3D";

    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,

                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,

                      szname, NULL };

    RegisterClassEx( &wc );

    HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW,

                              szname, "Basic Bones Sample",

                              WS_OVERLAPPEDWINDOW,//for fullscreen make into WS_POPUP

                              50, 50, 500,500, //for full screen GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),

                              GetDesktopWindow(), NULL, wc.hInstance, NULL);


    // Initilise or directX code here!

      // INIT OUR DIRECTX ONCE HERE AT THE START HERE!!!!!!!!!!!!!!!!!!!!!!!!!



      ShowWindow(hWnd, SW_SHOW);




    // Message loop. Note that this has been modified to allow

    // us to execute if no messages are being processed.



        if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))


            if (!GetMessage(&msg, NULL, 0, 0))





        // Idle-time processing - call our loop function in game.cpp

            // CALLING OUR DIRECTX CODE IN GAME.CPP FROM HERE!!!!!!!!!!!!!!!!!



    return 0;




You could put this in the game.cpp file, but its only the DirectX initilisation code.  And I won't say it again... its pretty much repetitive code.. we initialise it once.  Of course I have left out loads of checking code which checks if the Direct3DCreate8(..) function has succeeded.. .and if certain types of graphics are supported etc.  But if you want to get to know the DirectX8 API first I think its necessary to see the code in its simplicity first.


/*                                                                         */

/* File: init.cpp                                                          */

/*                                                                         */







void init(HWND hWnd)


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

    //should get a pointer to an IDirect3D8 interface.

    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);


    //Get the current display mode


    g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);


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


    ZeroMemory(&d3dpp, sizeof(d3dpp));


    //Fill the structure.

    //We want our program to be windowed, and set the back buffer to a format

    //that matches our current display mode

    d3dpp.Windowed = TRUE;

    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

    d3dpp.BackBufferFormat = d3ddm.Format;


      //For depth buffering (e.g.) the z-buffer


      d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

      d3dpp.EnableAutoDepthStencil = TRUE;


    //Create a Direct3D device.



      //Turn off lighting becuase we are specifying that our vertices have colour

    g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);


      //Turn on z-buffering

      g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);





void de_init()



      g_pD3DDevice = NULL;



      g_pD3D = NULL;



This is the part of the code we will be changing over and over again to generate the effects, patterns, etc etc.


/*                                                                         */

/* File: game.cpp                                                          */

/*                                                                         */



#include <d3dx8.h>

#include <d3d8.h>


// Well if we need to do any drawing or rendering triangles we can do it in here!

void Render()




      // Clear the back buffer to a blue color

      g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );


      // After rendering the scene we display it.

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



void mainloop()
















 Visitor: 9534626  { } Copyright (c) 2002-2017 xbdev.net - All rights reserved.
Designated tutorial and software are the property of their respective owners.