home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP15 / PROG15_2.CPP < prev    next >
C/C++ Source or Header  |  2002-04-30  |  10KB  |  327 lines

  1.  
  2. // PROG15_2.CPP - DirectSetup demo with callback
  3.  
  4. // INCLUDES ///////////////////////////////////////////////
  5.  
  6. #define WIN32_LEAN_AND_MEAN  
  7. #define INITGUID
  8.  
  9. #include <windows.h>   // include important windows stuff
  10. #include <windowsx.h> 
  11. #include <mmsystem.h>
  12. #include <iostream.h> // include important C/C++ stuff
  13. #include <conio.h>
  14. #include <stdlib.h>
  15. #include <malloc.h>
  16. #include <memory.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <stdio.h> 
  20. #include <math.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23.  
  24. #include <dsetup.h>
  25.  
  26. // DEFINES ////////////////////////////////////////////////
  27.  
  28. // defines for windows 
  29. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  30.  
  31. #define WINDOW_WIDTH    320   // size of window
  32. #define WINDOW_HEIGHT   240
  33.  
  34. // number of DirectSetup return values as currently defined
  35. // in DirectX 5.0
  36. #define NUM_DSETUP_RVALUES             14
  37.  
  38. // MACROS /////////////////////////////////////////////////
  39.  
  40. // these read the keyboard asynchronously
  41. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  42. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  43.  
  44. // PROTOTYPES /////////////////////////////////////////////
  45.  
  46. // game console
  47. int Game_Init(void *parms=NULL);
  48. int Game_Shutdown(void *parms=NULL);
  49. int Game_Main(void *parms=NULL);
  50.  
  51. // GLOBALS ////////////////////////////////////////////////
  52.  
  53. HWND main_window_handle           = NULL; // save the window handle
  54. HINSTANCE main_instance           = NULL; // save the instance
  55. char buffer[80];                          // used to print text
  56.  
  57. // exact path of contents of REDIST\
  58. // note the use of "\\" instead of "\"
  59. // this is neccessary since "\" is an escape character
  60. char DIRECTX_REDIST_PATH[]=".\\REDIST";
  61.  
  62. // a lookup table that holds the return values
  63. DWORD dsetup_rvalues[] = {DSETUPERR_SUCCESS,
  64.                           DSETUPERR_SUCCESS_RESTART,
  65.                           DSETUPERR_BADSOURCESIZE,
  66.                           DSETUPERR_BADSOURCETIME, 
  67.                           DSETUPERR_BADWINDOWSVERSION,
  68.                           DSETUPERR_CANTFINDDIR,
  69.                           DSETUPERR_CANTFINDINF,
  70.                           DSETUPERR_INTERNAL,
  71.                           DSETUPERR_NOCOPY,
  72.                           DSETUPERR_NOTPREINSTALLEDONNT,
  73.                           DSETUPERR_OUTOFDISKSPACE,
  74.                           DSETUPERR_SOURCEFILENOTFOUND,
  75.                           DSETUPERR_UNKNOWNOS,
  76.                           DSETUPERR_USERHITCANCEL };
  77.  
  78. // another lookup table so the return values can be converted
  79. // into a string for display
  80. char *dsetup_rstrings[] = {"DSETUPERR_SUCCESS",
  81.                            "DSETUPERR_SUCCESS_RESTART",
  82.                            "DSETUPERR_BADSOURCESIZE" ,
  83.                            "DSETUPERR_BADSOURCETIME", 
  84.                            "DSETUPERR_BADWINDOWSVERSION" ,
  85.                            "DSETUPERR_CANTFINDDIR" ,
  86.                            "DSETUPERR_CANTFINDINF" ,
  87.                            "DSETUPERR_INTERNAL" ,
  88.                            "DSETUPERR_NOCOPY" ,
  89.                            "DSETUPERR_NOTPREINSTALLEDONNT" ,
  90.                            "DSETUPERR_OUTOFDISKSPACE" ,
  91.                            "DSETUPERR_SOURCEFILENOTFOUND" ,
  92.                            "DSETUPERR_UNKNOWNOS" ,
  93.                            "DSETUPERR_USERHITCANCEL" };
  94.  
  95. // FUNCTIONS //////////////////////////////////////////////
  96.  
  97. LRESULT CALLBACK WindowProc(HWND hwnd, 
  98.                             UINT msg, 
  99.                             WPARAM wparam, 
  100.                             LPARAM lparam)
  101. {
  102. // this is the main message handler of the system
  103. PAINTSTRUCT    ps;           // used in WM_PAINT
  104. HDC            hdc;       // handle to a device context
  105.  
  106. // what is the message 
  107. switch(msg)
  108.     {    
  109.     case WM_CREATE: 
  110.         {
  111.         // do initialization stuff here
  112.         return(0);
  113.         } break;
  114.  
  115.     case WM_PAINT:
  116.          {
  117.          // start painting
  118.          hdc = BeginPaint(hwnd,&ps);
  119.  
  120.          // end painting
  121.          EndPaint(hwnd,&ps);
  122.          return(0);
  123.         } break;
  124.  
  125.     case WM_DESTROY: 
  126.         {
  127.         // kill the application            
  128.         PostQuitMessage(0);
  129.         return(0);
  130.         } break;
  131.  
  132.     default:break;
  133.  
  134.     } // end switch
  135.  
  136. // process any messages that we didn't take care of 
  137. return (DefWindowProc(hwnd, msg, wparam, lparam));
  138.  
  139. } // end WinProc
  140.  
  141. // WINMAIN ////////////////////////////////////////////////
  142.  
  143. int WINAPI WinMain(    HINSTANCE hinstance,
  144.                     HINSTANCE hprevinstance,
  145.                     LPSTR lpcmdline,
  146.                     int ncmdshow)
  147. {
  148. // this is the winmain function
  149.  
  150. WNDCLASS winclass;    // this will hold the class we create
  151. HWND     hwnd;        // generic window handle
  152. MSG         msg;        // generic message
  153. HDC      hdc;       // generic dc
  154. PAINTSTRUCT ps;     // generic paintstruct
  155.  
  156. // first fill in the window class stucture
  157. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  158.                           CS_HREDRAW | CS_VREDRAW;
  159. winclass.lpfnWndProc    = WindowProc;
  160. winclass.cbClsExtra        = 0;
  161. winclass.cbWndExtra        = 0;
  162. winclass.hInstance        = hinstance;
  163. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  164. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  165. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  166. winclass.lpszMenuName    = NULL; 
  167. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  168.  
  169. // register the window class
  170. if (!RegisterClass(&winclass))
  171.     return(0);
  172.  
  173. // create the window, note the use of WS_POPUP
  174. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  175.                           "DUMB Setup Parent Window",     // title
  176.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  177.                            0,0,       // x,y
  178.                           WINDOW_WIDTH,  // width
  179.                           WINDOW_HEIGHT, // height
  180.                           NULL,       // handle to parent 
  181.                           NULL,       // handle to menu
  182.                           hinstance,// instance
  183.                           NULL)))    // creation parms
  184. return(0);
  185.  
  186. // save the window handle and instance in a global
  187. main_window_handle = hwnd;
  188. main_instance      = hinstance;
  189.  
  190. // perform all game console specific initialization
  191. Game_Init();
  192.  
  193. // enter main event loop
  194. while(1)
  195.     {
  196.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  197.         { 
  198.         // test if this is a quit
  199.         if (msg.message == WM_QUIT)
  200.            break;
  201.     
  202.         // translate any accelerator keys
  203.         TranslateMessage(&msg);
  204.  
  205.         // send the message to the window proc
  206.         DispatchMessage(&msg);
  207.         } // end if
  208.     
  209.     // main game processing goes here
  210.     Game_Main();
  211.  
  212.     } // end while
  213.  
  214. // shutdown game and release all resources
  215. Game_Shutdown();
  216.  
  217. // return to Windows like this
  218. return(msg.wParam);
  219.  
  220. } // end WinMain
  221.  
  222. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  223.  
  224. // this prototype is very important!
  225. DWORD WINAPI DSetupCallback(DWORD Reason, 
  226.                             DWORD MsgType, 
  227.                             char *szMessage, 
  228.                             char *szName, 
  229.                             void *pUpgradeInfo)
  230. {
  231. // this is the most generic callback function you can have
  232. // it simply returns IDOK for status calls, otherwise
  233. // it pops up a messagebox and allows you to make the
  234. // decision
  235.  
  236. if (MsgType==0)        
  237.     return IDOK;
  238.  
  239. // call the messagebox function and return its value
  240. // remember, DirectSetup is designed to respond to the
  241. // return values of MessageBox
  242.  
  243. return(MessageBox(main_window_handle, szMessage, 
  244.                   "DirectX Setup Demo -- Running", MsgType));
  245.  
  246. } // end DSetupCallback
  247.  
  248. ///////////////////////////////////////////////////////////
  249.  
  250. int Game_Init(void *parms)
  251. {
  252. // this function is where you do all the initialization 
  253. // for your game
  254.  
  255. char messbuffer[256]; // used to print messages
  256.  
  257. // tell user whats up
  258. MessageBox(main_window_handle,
  259.   "This simple application installs the\nDirectX Run-Time libraries.\n\nPress OK to Continue.\n",
  260.   "DirectX Setup Demo -- Starting...",MB_OK);
  261.  
  262. // set callback function
  263. DirectXSetupSetCallback(DSetupCallback);
  264.  
  265. // call directsetup
  266. DWORD dsetup_result = 
  267. DirectXSetup(main_window_handle,DIRECTX_REDIST_PATH,DSETUP_DIRECTX);
  268.  
  269. // test the return value and print out the DirectSetup
  270. // error string
  271.  
  272. for (int index=0; index<NUM_DSETUP_RVALUES; index++)
  273.     {
  274.     if (dsetup_rvalues[index] == dsetup_result)
  275.         {
  276.         // build up message
  277.         sprintf(messbuffer,"DirectSetup Has Completed.\n\nResult=%s\n\nPress OK to Continue.",dsetup_rstrings[index]);
  278.  
  279.         // print results of DirectXSetup to user
  280.         MessageBox(main_window_handle,
  281.                    messbuffer,
  282.                    "DirectX Setup Demo -- Final Results",MB_OK);
  283.         break;
  284.         } // end if
  285.     } // end for
  286.  
  287. // return success
  288. return(1);
  289.  
  290. } // end Game_Init
  291.  
  292. ///////////////////////////////////////////////////////////
  293.  
  294. int Game_Shutdown(void *parms)
  295. {
  296. // this function is where you shutdown your game and
  297. // release all resources that you allocated
  298.  
  299. // shut everything down
  300.  
  301.  
  302.  
  303.  
  304. // return success
  305. return(1);
  306. } // end Game_Shutdown
  307.  
  308. ///////////////////////////////////////////////////////////
  309.  
  310. int Game_Main(void *parms)
  311. {
  312. // this is the workhorse of your game it will be called
  313. // continuously in real-time this is like main() in C
  314. // all the calls for you game go here!
  315.  
  316. // check of user is trying to exit
  317. if (KEY_DOWN(VK_ESCAPE))
  318.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  319.  
  320.  
  321.  
  322. // return success
  323. return(1);
  324.  
  325. } // end Game_Main
  326.  
  327. //////////////////////////////////////////////////////////