A CDXMenu Class for the CDX Library

The CDXMenu class is intended to deliver a fast and easy way to creating menu using CDX classes and a CDXBitmapFont class for computer games. You can select the font, size, color, alpha mode, and so forth for each menu.

Public Methods

  • CDXMenu
  • ~CDXMenu
  • Create
  • AddItem
  • Draw
  • SetTitle
  • SetColor
  • SetBackGround
  • SetAlpha
  • SetBorder
  • SetSound
  • SetItem
  • SetButtonSpeed
  • Reset
  • Enter

Example Program

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

#define CDXINCLUDEALL        // include all headers
#include <CDX.h>
#include <CDXBitmapFont.h>
#include <CDXMenu.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

CDXBitmapFont * Font1;       // Define the Font object
CDXMenu       * MainMenu;    // Define the Main Menu object

#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;
         }

         // Draw the main menu
         if(!MainMenu->Enter())
         int Option = MainMenu->Draw(10,10);
         else    // if clicked
         {
            switch(Option){.....}
         }
         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 that 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 the CDXBitmapFont object
   Font1 = new CDXBitmapFont();
   Font1->Create(Screen,"Arial",15,RGB(255,255,0));

   // Create the CDXMenu object
   MainMenu = new CDXMenu();
    MainMenu->Create(Screen,Font1,RGB(250,100,0),RGB(250,100,0),
                     "MAIN MENU",150);
  MainMenu->AddItem("START GAME");
  MainMenu->AddItem("OPTIONS");
  MainMenu->AddItem("HELP");
  MainMenu->AddItem("CREDITS");
  MainMenu->AddItem("QUIT GAME");

   // 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();
   }
}

Downloads

Download Library (Binary + source + documentation)

Download example 1

Download example 2

For more info, visit: CDXMenu Home page

Requirements

CDX Library: www.cdxlib.com

CDXBitmapFont: http://www.horningabout.com/cdx/cdxbitmapfont.html



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 13, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT If you are developing applications, you'll want to join us to learn how applications are changing as a result of gesture recognition. This technology will change how you and your users interact - not simply with your devices, but with the world around you. Your devices will be able to see and hear what your users are doing. Are your applications ready for this? Join us to learn about Intel® RealSense™ Technology, including never been …

  • Live Event Date: July 30, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this upcoming eSeminar that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds