CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> Visual C++ / C++ >> Graphics & Multimedia >> DirectX


CDX Game Development Kit
Rating: none

Bil Simser (view profile)
November 5, 2001


(continued)



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

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
YOU ARE A GOLDEN GOD!! - Legacy CodeGuru (05/13/2003)
Jus' Downloaded - Legacy CodeGuru (04/24/2003)
directx problem-help me!! - Legacy CodeGuru (04/04/2003)
Great Work!! - Legacy CodeGuru (10/29/2002)
CDXMap & CDXIsoMap - Legacy CodeGuru (05/15/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES