Alex Russell's Game Programming Tutorial using DX

Introduction . Chapter 1 . Chapter 2 . Chapter 3 . Chapter 4 . Chapter 5 . Chapter 6 . Chapter 7

Chapter 1

Install DX SDK and DXSmith

Download and install the DirectX SDk from Microsoft's web site. All code has been tested with V8 and V9 of the DX SDK. DirectX SDK from Microsoft

Download, install, and compile the DXsmith Library.

Compile and run the small test programs that come with DXsmith. They must work before you can continue with this course. Once you finish this course you may want to look at the source code to DXSmith to learn more about MS DirectDraw.

Create Your First Small DX Program

Start up MS VC++
Create a "New Project"
Choose "Win32 console Application"
Choose a directory, and make the project name "dx1"
Click OK
Choose "Empty Project", and Click Finish.
Click on "Create New Text File" button
Add the following code, save as dx_01.cpp, and insert dx1_0.cpp into the project. Please read the source code and the comments in it.
/*

  dx_01.cppp

  First small DX program

*/

#include <stdio.h>
#include <conio.h>


#include <DXSmithIO.h>


int main(int argc, char *argv[])
{
    CgsdxIO dx; // main DX smith object
    CString errstr; // to hold error strings
    gsdxMode_t *modeInfo; // to hold video mode info
    int i, j;
    RECT r;  // a rectangle

    // must do this to init the dx smith lib
    dx.InitResource();
    dx.AddResourcePath("c:\\temp");
    if ( dx.GetModes() )
        {
        i=dx.GetLastError(&errstr);
        printf("GetModes failed Last error %d %s\n", i, (LPCSTR )errstr);
        getch();
        return 1;
        }

    j=dx.GetNumGraphicModes();
    if ( j > 0 )
        {
        for ( i=0; i < j; i++ )
            {
            modeInfo=dx.GetGraphicModeInfo(i);
            printf("width: %d height %d color %d\n",
                modeInfo->width,
                modeInfo->height,
                modeInfo->colorBits);
            }
        }
    else
        {
        printf("Error getting modes\n");
        }

    printf("press a key\n");
    getch();

    // enter a full screen graphics mode.
    // 1024 pixels wide, 768 pixels high, 32bit colour.
    // choose any 16, 24, or 32 bit mode that your video card supports
    if ( dx.InitGraphics(1024, 768, 32, GSDX_FULL_SCREEN|GSDX_CONSOLE_MODE) )
        {
        TRACE("Failed to init graphics %d\n");
        printf("failed to init graphics\n");
        }
    else
        {
        // clear the screen to black
        dx.RectFill(NULL, 0);
        dx.Flip();
        dx.RectFill(NULL, 0);
        dx.Flip();

        // fill the whole screen to clear it
        dx.RectFill(NULL, 0);

        // draw a rectangle
        r.top=20;
        r.bottom=100;
        r.left=30;
        r.right=250;
        dx.RectFill(&r, RGB(0, 255,0));

        // draw a few lines
        dx.Line(10, 10, 200, 200, 10, RGB(255, 0,0));
        dx.Line(10, 10, 300, 200, 10, RGB(255, 0,0));
        dx.Line(10, 10, 400, 250, 10, RGB(255, 0,0));
        dx.Line(10, 10, 500, 300, 10, RGB(255, 0,0));

        // make the lines visible
        dx.Flip();

        // wait
        getch();

        // and clean up
        // actually, the dx object does it for us.
        }

    return 0;

}

Set the following "Project Settings"
General tab- Use MFC in a Shared DLL

Link tab, Debug, add the following libraries (change path to reflect where you installed DXSmith):

Ddraw.lib dxguid.lib winmm.lib dinput.lib C:\dev\DXSmith\CSmithDXLowIO_Lib\Debug\DXSmithLib.lib

Link tab, Release, add the following libraries

Ddraw.lib dxguid.lib winmm.lib dinput.lib C:\dev\DXSmith\CSmithDXLowIO_Lib\Release\DXSmithLib.lib

Add "C:\dev\DXSmith\CSmithDXLowIO_Lib" as an "Include" path under the "Tools/Options" menu (Change the path to reflect where you install DXSmith on your PC). You may have to add "Include" and "Library" paths for the DX SDK. If you get "file not found" errors you likely need to add "include" paths, and linker errors are usually missing libraries.

Compile and Run the program. It should display all the video modes supported by DX on your PC. Press a key to display a rectangle and some lines, and a final keypress to exit.

A Note on Project Types

We are using console mode programs to keep the window specific code to a minimum. A real game would use a "Win32 Application" type project. Using a real windows framework will be covered in the last chapter. Using a console mode program simplifies the small test programs used for the course, but DX works better running under a normal win32 application framework. Do NOT press alt-tab while a console based DX program is running in full-screen mode - it will very likely hang your computer.

Once you get this small test program to work you will be able use dx_01.cpp's setting as a template for all following console type test programs.

Introduction . Chapter 1 . Chapter 2 . Chapter 3 . Chapter 4 . Chapter 5 . Chapter 6 . Chapter 7

Copyright 2004 (c), Alex Russell, All rights reserved