home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP15 / PROG15_1.CPP next >
Encoding:
C/C++ Source or Header  |  2002-04-30  |  8.4 KB  |  294 lines

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