CDX Game Development Kit


Environment: Windows 95/98/2000, DirectX 3-8, Visual C++ 6

CDX is a free GDK (Game Development Kit) which is comprised of a set of C++ wrapper classes for writing Windows games. It is built on top of Microsoft Windows and DirectX technology and offers simple to use C++ wrappers for all aspects of game development. This includes things like sprites, tiles, scrollable maps, alpha blending and even 3D primitives (using Direct3D).

CDX takes care of the low-level details of using DirectX, providing you with an easy to use toolkit for implementing your own games using simple yet flexible C++ classes. With only 2 dozen classes, CDX is quick to learn and easy to use.

The Classes

These are the main classes that make up CDX. There are a few more (like file
and image handling, tiles, sprite lists, and resource files) but these are the ones you’ll primarily use.

CDXInput CDXInput
is a class wrapper for DirectInput and contains functions to receive
data from the mouse, keyboard and joystick.
CDXLayer CDXLayer
is derived from CDXSurface
and allows you to do scrollable side-shooter games.
CDXMap CDXMap allows you to render tile based maps like those
seen in games like StarCraft and Age of Empires.
CDXMapCell CDXMapCell is an overridable class that lets you store
whatever information you want in each map tile.
CDXMusic CDXMusic
allows you to load and play MIDI files for the music in your game.
CDXMusicCd CDXMusicCd
allows you to load and play audio CD files for the music in your game.
CDXScreen CDXScreen
is the primary object of the library and every program that uses CDX
must include a CDXScreen
object.
CDXSound CDXSound
is a simple wrapper for a DirectSound object.
CDXSprite CDXSprite contains the data and functions required to
display animated sprites.
CDXSurface CDXSurface is the class that lets you access DirectDraw
surfaces. Two of these are automatically created in the CDXScreen class.

Screen Flipping Example

CDX is made up of some very simple classes to use and learning them is very easy. The main class, CDXScreen, handles all aspects of screen drawing. Below is a very simple, but complete, example of using CDXScreen in a Win32 application. The example is the same as the long-winded DirectX sample that Microsoft provides to show you how to do page flipping. This example shows you how little code is needed to accomplish the same task and can be a starting point for more complex applications and games.

#include <windows.h>
#include <windowsx.h>
#include <stdio.h>

#define CDXINCLUDEALL // include all headers
#include <CDX.h>

CDXScreen * Screen = 0; // The screen object, every
// program must have one
// remember to set all CDX objects
// to 0 when you declare them!

int Toggle = 0; // flag for the screen color

#define TIMER_ID 1 // variables used for the timer
#define TIMER_RATE 500

#define NAME "CDXExample"
#define TITLE "CDX Example"

long PASCAL WinProc(HWND hWnd,
UINT message,
WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_TIMER:
if( Toggle ) // fill the screen depending
// on toggle flag

{
Screen->Fill(0);
Toggle = 0;
}
else
{
Screen->Fill(255);
Toggle = 1;
}
Screen->Flip(); // flip back and front buffers
break;

case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE: // if ESC key was hit, quit program
PostMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
break;

case WM_DESTROY:
SAFEDELETE( Screen ); // delete the screen object
PostQuitMessage(0); // terminate the program
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}

BOOL InitApp(HINSTANCE hInst, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndClass;

WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = WinProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInst;
WndClass.hIcon = LoadIcon(0, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(0, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
WndClass.lpszMenuName = NAME;
WndClass.lpszClassName = NAME;
RegisterClass(&WndClass);

// create a window which covers the whole screen
// this is needed for fullscreen CDX apps
hWnd = CreateWindowEx(
WS_EX_TOPMOST,
NAME,
TITLE,
WS_POPUP,
0,0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hInst,
NULL);

// when hWnd = -1 there was an error creating the main
// window CDXError needs a CDXScreen object, if there is
// none at this early program stage, pass it NULL

if(!hWnd)
CDXError( NULL , "Could not create the main window" );

// show the main window
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

// Create the CDXSreen object
Screen = new CDXScreen();
if(FAILED(Screen->CreateFullScreen(hWnd, 640, 480, 8)))
CDXError( NULL , "Could not set video mode 640x480x8" );

// Create our timer for flipping the screen display

SetTimer(hWnd, TIMER_ID, TIMER_RATE, NULL);

return TRUE;
}

int PASCAL WinMain( HINSTANCE hInst,
HINSTANCE hPrevInst,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;

if(!InitApp(hInst, nCmdShow))
CDXError( NULL , "Could not initialize CDX application");

while(1)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{
if(!GetMessage(&msg, NULL, 0, 0 ))
return msg.wParam;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else WaitMessage();
}
}

Essentially, you create the CDXScreen object, draw to it and then call the Flip() method to reveal your drawing to the user.

Using CDX with MFC

If you don’t want to write a standard Windows application with all those message handlers and case statements, you can use MFC with CDX. First, somewhere in your initialization code you need to create your CDXScreen object. This can be done in your own function or in the OnCreate method of your MainFrame.

BOOL CMainFrame::InitDirectDraw()
{
HWND hWnd = AfxGetMainWnd()->m_hWnd;
Screen = new CDXScreen();
Screen->CreateFullScreen(hWnd,640,480,8);
return TRUE;
}

Then create a timer to handle the flipping of the screen and in the OnTimer method, do the page flipping.

void CMainFrame::OnTimer(UINT nIDEvent)
{
if(Toggle)
{
Screen->Fill(4); // Fill the back buffer red
Toggle = FALSE;
}
else
{
Screen->Fill(1); // Fill the back buffer blue
Toggle = TRUE;
}

Screen->Flip(); // Flip the back buffer to the front
CFrameWnd::OnTimer(nIDEvent);
}

That’s all it takes to get up and running with CDX! So if you’re interesting in writing apps or games using DirectX but are daunted by the complexity that’s involved in getting into DirectX programming give CDX a try!

Downloads


Download setup program (includes demos and source) – 2.5 Mb

The CDX Website can be found

here

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read