home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / Source / GPCHAP06 / WINX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-25  |  5.3 KB  |  216 lines

  1. // WINX.CPP - Game Console Shell
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4. #define WIN32_LEAN_AND_MEAN  
  5. #define INITGUID       // for directx
  6.  
  7. #include <windows.h>   // include important windows stuff
  8. #include <windowsx.h> 
  9. #include <mmsystem.h>
  10. #include <iostream.h> // include important C/C++ stuff
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include <malloc.h>
  14. #include <memory.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <io.h>
  20. #include <fcntl.h>
  21.  
  22. // DEFINES ////////////////////////////////////////////////
  23.  
  24. // defines for windows 
  25. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  26.  
  27. #define WINDOW_WIDTH  320              // size of window
  28. #define WINDOW_HEIGHT 200
  29.  
  30. // MACROS /////////////////////////////////////////////////
  31.  
  32. // these read the keyboard asynchronously
  33. #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  34. #define KEY_UP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  35.  
  36. // PROTOTYPES /////////////////////////////////////////////
  37.  
  38. int Game_Init(void *parms=NULL);
  39. int Game_Shutdown(void *parms=NULL);
  40. int Game_Main(void *parms=NULL);
  41.  
  42. // GLOBALS ////////////////////////////////////////////////
  43.  
  44. HWND main_window_handle = NULL; // save the window handle
  45. HINSTANCE main_instance = NULL; // save the instance
  46. char buffer[80];                // used to print text
  47.  
  48. // FUNCTIONS //////////////////////////////////////////////
  49.  
  50. LRESULT CALLBACK WindowProc(HWND hwnd, 
  51.                             UINT msg, 
  52.                             WPARAM wparam, 
  53.                             LPARAM lparam)
  54. {
  55. // this is the main message handler of the system
  56. PAINTSTRUCT    ps;           // used in WM_PAINT
  57. HDC            hdc;       // handle to a device context
  58.  
  59. // what is the message 
  60. switch(msg)
  61.     {    
  62.     case WM_CREATE: 
  63.         {
  64.         // do initialization stuff here
  65.         return(0);
  66.         } break;
  67.  
  68.     case WM_PAINT:
  69.          {
  70.          // start painting
  71.          hdc = BeginPaint(hwnd,&ps);
  72.  
  73.          // end painting
  74.          EndPaint(hwnd,&ps);
  75.          return(0);
  76.         } break;
  77.  
  78.     case WM_DESTROY: 
  79.         {
  80.         // kill the application            
  81.         PostQuitMessage(0);
  82.         return(0);
  83.         } break;
  84.  
  85.     default:break;
  86.  
  87.     } // end switch
  88.  
  89. // process any messages that we didn't take care of 
  90. return (DefWindowProc(hwnd, msg, wparam, lparam));
  91.  
  92. } // end WinProc
  93.  
  94. // WINMAIN ////////////////////////////////////////////////
  95.  
  96. int WINAPI WinMain(    HINSTANCE hinstance,
  97.                     HINSTANCE hprevinstance,
  98.                     LPSTR lpcmdline,
  99.                     int ncmdshow)
  100. {
  101.  
  102. WNDCLASS winclass;    // this will hold the class we create
  103. HWND     hwnd;        // generic window handle
  104. MSG         msg;        // generic message
  105. HDC      hdc;       // generic dc
  106. PAINTSTRUCT ps;     // generic paintstruct
  107.  
  108. // first fill in the window class stucture
  109. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  110.                           CS_HREDRAW | CS_VREDRAW;
  111. winclass.lpfnWndProc    = WindowProc;
  112. winclass.cbClsExtra        = 0;
  113. winclass.cbWndExtra        = 0;
  114. winclass.hInstance        = hinstance;
  115. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  116. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  117. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  118. winclass.lpszMenuName    = NULL; 
  119. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  120.  
  121. // register the window class
  122. if (!RegisterClass(&winclass))
  123.     return(0);
  124.  
  125. // create the window
  126. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  127.                           "WinX Game Console",     // title
  128.                           WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  129.                            0,0,       // x,y
  130.                           WINDOW_WIDTH,  // width
  131.                           WINDOW_HEIGHT, // height
  132.                           NULL,       // handle to parent 
  133.                           NULL,       // handle to menu
  134.                           hinstance,// instance
  135.                           NULL)))    // creation parms
  136. return(0);
  137.  
  138. // save the window handle and instance in a global
  139. main_window_handle = hwnd;
  140. main_instance      = hinstance;
  141.  
  142. // perform all game console specific initialization
  143. Game_Init();
  144.  
  145. // enter main event loop
  146. while(1)
  147.     {
  148.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  149.         { 
  150.         // test if this is a quit
  151.         if (msg.message == WM_QUIT)
  152.            break;
  153.     
  154.         // translate any accelerator keys
  155.         TranslateMessage(&msg);
  156.  
  157.         // send the message to the window proc
  158.         DispatchMessage(&msg);
  159.         } // end if
  160.     
  161.     // main game processing goes here
  162.     Game_Main();
  163.  
  164.     } // end while
  165.  
  166. // shutdown game and release all resources
  167. Game_Shutdown();
  168.  
  169. // return to Windows like this
  170. return(msg.wParam);
  171.  
  172. } // end WinMain
  173.  
  174. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  175.  
  176. int Game_Init(void *parms)
  177. {
  178. // this function is where you do all the initialization 
  179. // for your game
  180.  
  181. // your code goes here
  182.  
  183. // return success
  184. return(1);
  185. } // end Game_Init
  186.  
  187. ///////////////////////////////////////////////////////////
  188.  
  189. int Game_Shutdown(void *parms)
  190. {
  191. // this function is where you shutdown your game and
  192. // release all resources that you allocated
  193.  
  194. // your code goes here
  195.  
  196. // return success
  197. return(1);
  198. } // end Game_Shutdown
  199.  
  200. ///////////////////////////////////////////////////////////
  201.  
  202. int Game_Main(void *parms)
  203. {
  204. // this is the workhorse of your game it will be called
  205. // continuously in real-time this is like main() in C
  206. // all the calls for you game go here!
  207.  
  208. // your code goes here
  209.  
  210. // return success
  211. return(1);
  212. } // end Game_Main
  213.  
  214. ///////////////////////////////////////////////////////////
  215.  
  216.