home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / IKLOWNS / CGOPTION.CPP < prev    next >
C/C++ Source or Header  |  1996-08-28  |  13KB  |  424 lines

  1. /*===========================================================================*\
  2. |
  3. |  File:        cgoption.cpp
  4. |
  5. |  Description: 
  6. |   Routines to display lines of text (from resource strings) overlayed on 
  7. |   a bitmap.  The text will be sized appropriately to fit within the 
  8. |   rectangle specified in the profile.  The user may hilight a particular 
  9. |   line and the index of the line will be returned to the caller.
  10. |
  11. |   Also, routines to scroll text across a bitmap image (useful for
  12. |   story lines and credits).
  13. |       
  14. |-----------------------------------------------------------------------------
  15. |
  16. |  Copyright (C) 1995-1996 Microsoft Corporation.  All Rights Reserved.
  17. |
  18. |  Written by Moss Bay Engineering, Inc. under contract to Microsoft Corporation
  19. |
  20. \*===========================================================================*/
  21.  
  22. /**************************************************************************
  23.  
  24.     (C) Copyright 1995-1996 Microsoft Corp.  All rights reserved.
  25.  
  26.     You have a royalty-free right to use, modify, reproduce and 
  27.     distribute the Sample Files (and/or any modified version) in 
  28.     any way you find useful, provided that you agree that 
  29.     Microsoft has no warranty obligations or liability for any 
  30.     Sample Application Files which are modified. 
  31.  
  32.     we do not recomend you base your game on IKlowns, start with one of
  33.     the other simpler sample apps in the GDK
  34.  
  35.  **************************************************************************/
  36.  
  37. #include <windows.h>
  38. #include "cgglobl.h"
  39. #include "cgdib.h"
  40. #include "strrec.h"
  41. #include "cginput.h"
  42. #include "cgtext.h"
  43. #include "cgmidi.h"
  44. #include "cgimage.h"
  45. #include "cgrsrce.h"
  46. #include "cgoption.h"
  47.  
  48. #define MIN_JOY_MOVE    15
  49. #define MIN_MOUSE_MOVE  50
  50.  
  51. #define SOURCEX SCREEN_WIDTH
  52. #define SOURCEY SCREEN_HEIGHT
  53. #define SCREENX GetSystemMetrics(SM_CXSCREEN)
  54. #define SCREENY GetSystemMetrics(SM_CYSCREEN)
  55. #define CenterX (GetSystemMetrics(SM_CXSCREEN) / 2)
  56. #define CenterY (GetSystemMetrics(SM_CXSCREEN) / 2)
  57.  
  58. #define PIXEL_STEP  2
  59.  
  60. #define BASE_HWND   ghMainWnd
  61. #define NUM_ENTRIES(x)  (sizeof(x)/sizeof(x[0]))
  62.  
  63. //** external functions **
  64. extern void GetRectFromProfile(RECT &, LPSTR, LPSTR, LPSTR);
  65. COLORREF GetColorFromProfile(LPSTR, LPSTR, LPSTR);
  66.  
  67. //** external data **
  68. //** public data **
  69.  
  70. //** private data **
  71. static HDC hdcIntro;
  72. static CGameDIB *pDib;
  73. static HPALETTE hPal;
  74.  
  75. static int iMouseMove;
  76. static int iJoyMove;
  77. static int iCycleRate;
  78.  
  79. //** public functions **
  80. //** private functions **
  81.  
  82. // ----------------------------------------------------------
  83. // LoadBitmap - load bitmap from a file into a DC and setup
  84. //  palette.
  85. // ----------------------------------------------------------
  86. HDC LoadBitmapFile (
  87.     LPSTR   pBitmapFile // Name of .BMP file
  88. )
  89. {
  90.     HDC     hdcBase, hdcBitmap;
  91.  
  92.     // Loadup bitmap
  93.     pDib = new CGameDIB(pBitmapFile);
  94.     
  95.     // Create a DC and copy bitmap to it
  96.     hdcBase = GetDC(BASE_HWND);
  97.     hdcBitmap = CreateCompatibleDC(hdcBase);
  98.     SelectObject(hdcBitmap, pDib->GetHBitmap());
  99.  
  100.     // Set palette so image looks ok!
  101.     hPal = pDib->CreatePalette();
  102.     SelectPalette(hdcBase, hPal, FALSE);
  103.     RealizePalette(hdcBase);
  104.  
  105.     ReleaseDC( BASE_HWND, hdcBase );
  106.  
  107.     return(hdcBitmap);
  108. }
  109.  
  110. // ----------------------------------------------------------
  111. // GetUpDownDone - poll user input devices looking to see if
  112. //  there are any selection changes.
  113. // ----------------------------------------------------------
  114. void GetUpDownDone(CGameInput *Input, BOOL &up, BOOL &down, BOOL &done)
  115. {
  116.     JOYINFO joy;
  117.     int x, y, buttons;
  118.  
  119.     up = down = done = FALSE;
  120.  
  121.     // Check for keyboard input
  122.     down = (Input->GetKeyboard(VK_DOWN) !=0) ||
  123.            (Input->GetKeyboard(VK_NUMPAD2) != 0);
  124.     up = (Input->GetKeyboard(VK_UP) != 0) ||
  125.            (Input->GetKeyboard(VK_NUMPAD8) != 0);
  126.     done = ((Input->GetKeyboard(VK_RETURN)) || (Input->GetKeyboard(VK_SPACE)));
  127.  
  128.     // No keyboard input?
  129.     if (!(up || down || done))
  130.     {
  131.  
  132.         // Check for mouse input
  133.         if (Input->GetMouse(x, y, buttons))
  134.         {
  135.             down = (y - CenterY > iMouseMove);
  136.             up   = (y - CenterY < -iMouseMove);
  137.             done = (buttons & 1);
  138.  
  139.             // Keep mouse in center of screen
  140.             SetCursorPos(CenterX, CenterY);
  141.         }
  142.         
  143.         // No mouse input?
  144.         if (!(up || down || done))
  145.         {
  146.             // Check for joystick input
  147.             Input->UpdateJoystick();
  148.             if (Input->GetJoystick(1, &joy))
  149.             {
  150.                 down = ((int)joy.wYpos >= iJoyMove);
  151.                 up = ((int)joy.wYpos <= -iJoyMove);
  152.                 done = (joy.wButtons & JOY_BUTTON1);
  153.             }           
  154.         }
  155.     }
  156. }   
  157.  
  158. // ----------------------------------------------------------
  159. // NewStringResource -  Load string from resource into memory
  160. // ----------------------------------------------------------
  161. LPSTR NewStringResource(
  162.     HINSTANCE   hInst,
  163.     int         idString
  164. )
  165. {
  166.     char    TempBuf[256];
  167.     DWORD   nBytes=0;
  168.     LPSTR   pStr=NULL;
  169.  
  170.     nBytes = LoadString(hInst, idString, TempBuf, sizeof(TempBuf));
  171.     if (nBytes > 0)
  172.     {
  173.         pStr = new char [nBytes+1];
  174.         lstrcpy(pStr, TempBuf);
  175.     }
  176.     return(pStr);
  177. }
  178.  
  179. // ----------------------------------------------------------
  180. // COptionScreen constructor - Options screen object for 
  181. //  displaying option screen while allowing repaints to occur
  182. // These routines do not take advantage of DirectDraw, unfortunately.
  183. // ----------------------------------------------------------
  184. COptionScreen::COptionScreen()
  185. {
  186.     mHdcScreen = NULL;
  187.     mHdcIntro = NULL;
  188.     mInput = NULL;
  189.     pText = NULL;
  190.     pDib = NULL;
  191.     mnChoices = 0;
  192. }
  193.  
  194. // ----------------------------------------------------------
  195. // Init - initialises context for option screen
  196. //  NOTE: resource strings are assumed to be sequential!
  197. // ----------------------------------------------------------
  198. BOOL COptionScreen::Init(
  199.     LPSTR       pBitmapName,    // name of .BMP file or NULL to use default
  200.     LPSTR       ProfileName,    // filename of game config file
  201.     CGameInput  *Input,     // ptr to input object or NULL if no input
  202.     int     timeout     // maximum time to wait or -1 forever
  203. )
  204. {
  205.     return( TRUE );
  206. }
  207.  
  208. // ----------------------------------------------------------
  209. // Init - initialises context for option screen
  210. //  NOTE: resource strings are assumed to be sequential!
  211. // ----------------------------------------------------------
  212. BOOL COptionScreen::Init(
  213.     LPSTR       pBitmapName,    // name of .BMP file or NULL to use default
  214.     int     idStringBase,   // first resource id of text lines
  215.     int     nChoices,   // number of text lines to display
  216.     LPSTR       ProfileName,    // filename of game config file
  217.     CGameInput  *Input,     // ptr to input object or NULL if no input
  218.     int     defSelect,  // line to hilight first or -1 if none
  219.     int     timeout,        // maximum time to wait or -1 forever
  220.     int     spacing,
  221.     int     lines
  222. )
  223. {
  224.     return( TRUE );
  225. }
  226.  
  227. // ----------------------------------------------------------
  228. // AddText - 
  229. // ----------------------------------------------------------
  230. int COptionScreen::AddText(
  231.     LPSTR       pNewText
  232. )
  233. {
  234.     if (pText == NULL)
  235.     {
  236.         return(-1);
  237.     }
  238.  
  239.     mnChoices++;
  240.     pText->AddLine(pNewText, colorDefault, colorDefaultShadow);
  241.     return(mnChoices);
  242. }
  243.  
  244. // ----------------------------------------------------------
  245. // SelectText - 
  246. // ----------------------------------------------------------
  247. BOOL COptionScreen::SelectText(
  248.     int     defSelect   // line to hilight first or -1 if none
  249. )
  250. {
  251.     if (pText == NULL)
  252.     {
  253.         return(FALSE);
  254.     }
  255.  
  256.     CurSel = defSelect;
  257.  
  258.     pText->TextBlt();
  259.     pText->ChangeColor(CurSel+1, colorSelected, colorSelectedShadow);
  260.     return( TRUE );
  261. }
  262.  
  263. // ----------------------------------------------------------
  264. // SetSpacing - 
  265. // ----------------------------------------------------------
  266. void COptionScreen::SetSpacing(int spacing)
  267. {
  268.     if (pText != NULL)
  269.     {
  270.         pText->SetSpacing(spacing);
  271.     }
  272.  
  273. }   
  274.  
  275. // ----------------------------------------------------------
  276. // SetMaxLines - 
  277. // ----------------------------------------------------------
  278. void COptionScreen::SetMaxLines(int lines)
  279. {
  280.     if (pText != NULL)
  281.     {
  282.         pText->SetMaxLines(lines);
  283.     }
  284.  
  285. }   
  286.  
  287. // ----------------------------------------------------------
  288. // Shutdown - closes down context
  289. // ----------------------------------------------------------
  290. void COptionScreen::Shutdown( void )
  291. {
  292.     // Cleanup
  293.     if ( mHdcIntro ) {
  294.         DeleteDC(mHdcIntro);
  295.         mHdcIntro = NULL;
  296.     }
  297.     if ( pText ) {
  298.         delete pText;   // destructor will delete our strings for us!
  299.     }
  300.     if ( pDib ) {
  301.         delete pDib;
  302.     }
  303.     if ( mHdcScreen ) {
  304.         ReleaseDC(BASE_HWND, mHdcScreen);
  305.         mHdcScreen = NULL;
  306.     }
  307. }
  308.  
  309. // ----------------------------------------------------------
  310. // DlgProcOptions - main game options dialog
  311. // ----------------------------------------------------------
  312. BOOL CALLBACK DlgProcOptions(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
  313. {
  314.     static LONG iIndex = -1;
  315.     static HWND hWndCtl;
  316.     // char   chBuffer[128];
  317.  
  318.     switch (msg)
  319.     {
  320.     case WM_INITDIALOG:
  321.  
  322.         iIndex = -1;
  323.  
  324.         hWndCtl = GetDlgItem(hDlg, IDC_LISTOPTION);
  325.         if (hWndCtl == NULL)
  326.         {
  327.             EndDialog(hDlg, -1);
  328.             return(TRUE);
  329.         }
  330.         SendMessage(hWndCtl, LB_ADDSTRING, 0, (LPARAM) "Play Solo");
  331.         SendMessage(hWndCtl, LB_ADDSTRING, 0, (LPARAM) "Two Player Game");
  332.         SendMessage(hWndCtl, LB_ADDSTRING, 0, (LPARAM) "Play Remote Opponents");
  333.         SendMessage(hWndCtl, LB_ADDSTRING, 0, (LPARAM) "Quit Game");
  334.         SendMessage(hWndCtl, LB_SETCURSEL, 0, 0);
  335.         SetFocus(hWndCtl);
  336.         iIndex = 0;
  337.  
  338.         return(FALSE);
  339.  
  340.     case WM_COMMAND:
  341.  
  342.         switch( HIWORD(wParam))
  343.         {
  344.         case LBN_SELCHANGE:
  345.             iIndex = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
  346.             hWndCtl = (HWND) lParam;
  347.             //wsprintf( chBuffer, "Index set to %d, %8x %8x %8x\r\n",
  348.             //     iIndex, msg, wParam, lParam);
  349.             // OutputDebugString(chBuffer);
  350.             return(FALSE);
  351.  
  352.         case LBN_DBLCLK:
  353.             iIndex = SendMessage((HWND) lParam, LB_GETCURSEL, 0, 0);
  354.             //wsprintf( chBuffer, "Index set to %d, %8x %8x %8x\r\n",
  355.             //     iIndex, msg, wParam, lParam);
  356.             // OutputDebugString(chBuffer);
  357.             EndDialog(hDlg, iIndex);
  358.             return(TRUE);
  359.  
  360.         case 0:
  361.             //wsprintf( chBuffer, "Index returned %d, %8x %8x %8x\r\n",
  362.             //     iIndex, msg, wParam, lParam);
  363.             // OutputDebugString(chBuffer);
  364.  
  365.             if (LOWORD(wParam) == IDOK)
  366.             {
  367.                 EndDialog(hDlg, iIndex);
  368.                 return(TRUE);
  369.             }
  370.             else if (LOWORD(wParam) == IDCANCEL)
  371.             {
  372.                 EndDialog(hDlg, -1);
  373.                 return(TRUE);
  374.             }
  375.             break;
  376.  
  377.         }
  378.     }
  379.     //wsprintf( chBuffer, "Ret False Index set to %d, %8x %8x %8x\r\n",
  380.     //     iIndex, msg, wParam, lParam);
  381.     // OutputDebugString(chBuffer);
  382.     return (FALSE);
  383. }
  384.  
  385. // ----------------------------------------------------------
  386. // DoOptionScreen - display bitmap & text and allow user to 
  387. //  make a selection.
  388. // ----------------------------------------------------------
  389. int COptionScreen::DoOptionScreen( void )
  390. {
  391.     // Allow user to change options
  392.     BOOL    done = FALSE;
  393.     BOOL    switchedAway = FALSE;
  394.     DWORD   startTime = timeGetTime();
  395.     DWORD   dwRet;
  396.  
  397.     pDib = new CGameDIB("instruct.bmp");
  398.  
  399.     InvalidateRect(NULL, NULL, TRUE);
  400.  
  401.     dwRet = DialogBox (ghInst, (LPCTSTR) IDD_OPTIONS, ghMainWnd, (DLGPROC) DlgProcOptions);
  402.     return(dwRet);
  403. }
  404.  
  405.  
  406. // ----------------------------------------------------------
  407. // Paint - 
  408. // ----------------------------------------------------------
  409. void COptionScreen::Paint( void )
  410. {
  411.     HDC     hdcScreen = GetDC(BASE_HWND);
  412.     HDC     hdcBitmap;
  413.  
  414.     hdcBitmap = CreateCompatibleDC(hdcScreen);
  415.     SelectObject( hdcBitmap, pDib->GetHBitmap() );
  416.  
  417.     StretchBlt(hdcScreen, 0, 0, SCREENX, SCREENY, hdcBitmap
  418.     , 0, 0, SOURCEX, SOURCEY, SRCCOPY);
  419.  
  420.     ReleaseDC(BASE_HWND, hdcScreen);
  421.     DeleteObject(hdcBitmap);
  422. }
  423.  
  424.