home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP16 / GPTEMP2.CPP < prev    next >
C/C++ Source or Header  |  2002-05-01  |  8KB  |  320 lines

  1.  
  2. // GPTEMP2.CPP - Template to drop code into
  3. // make sure to include GPDUMB1.CPP, GPDUMB2.CPP, DDRAW.LIB
  4. // DINPUT.LIB, DINPUT8.LIB, DSOUND.LIB, WINMM.LIB
  5.  
  6. // INCLUDES ///////////////////////////////////////////////
  7.  
  8. #define WIN32_LEAN_AND_MEAN  
  9. #define INITGUID
  10.  
  11. #include <windows.h>   // include important windows stuff
  12. #include <windowsx.h> 
  13. #include <mmsystem.h>
  14. #include <iostream.h> // include important C/C++ stuff
  15. #include <conio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <memory.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include <stdio.h> 
  22. #include <math.h>
  23. #include <io.h>
  24. #include <fcntl.h>
  25.  
  26. #include <ddraw.h>  // directX includes
  27. #include <dsound.h>
  28. #include <dinput.h>
  29. #include "gpdumb1.h" // game library includes
  30. #include "gpdumb2.h"
  31.  
  32. // DEFINES ////////////////////////////////////////////////
  33.  
  34. // defines for windows 
  35. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  36.  
  37. #define WINDOW_WIDTH    320   // size of window
  38. #define WINDOW_HEIGHT   64
  39.  
  40. // PROTOTYPES /////////////////////////////////////////////
  41.  
  42. // game console
  43. int Game_Init(void *parms=NULL);
  44. int Game_Shutdown(void *parms=NULL);
  45. int Game_Main(void *parms=NULL);
  46.  
  47. // GLOBALS ////////////////////////////////////////////////
  48.  
  49. HWND main_window_handle           = NULL; // save the window handle
  50. HINSTANCE main_instance           = NULL; // save the instance
  51. char buffer[80];                          // used to print text
  52.  
  53. // FUNCTIONS //////////////////////////////////////////////
  54.  
  55. LRESULT CALLBACK WindowProc(HWND hwnd, 
  56.                             UINT msg, 
  57.                             WPARAM wparam, 
  58.                             LPARAM lparam)
  59. {
  60. // this is the main message handler of the system
  61. PAINTSTRUCT    ps;           // used in WM_PAINT
  62. HDC            hdc;       // handle to a device context
  63.  
  64. // what is the message 
  65. switch(msg)
  66.     {    
  67.     case WM_CREATE: 
  68.         {
  69.         // do initialization stuff here
  70.         return(0);
  71.         } break;
  72.  
  73.     case WM_PAINT:
  74.          {
  75.          // start painting
  76.          hdc = BeginPaint(hwnd,&ps);
  77.  
  78.          // end painting
  79.          EndPaint(hwnd,&ps);
  80.          return(0);
  81.         } break;
  82.  
  83.     case WM_DESTROY: 
  84.         {
  85.         // kill the application            
  86.         PostQuitMessage(0);
  87.         return(0);
  88.         } break;
  89.  
  90.     default:break;
  91.  
  92.     } // end switch
  93.  
  94. // process any messages that we didn't take care of 
  95. return (DefWindowProc(hwnd, msg, wparam, lparam));
  96.  
  97. } // end WinProc
  98.  
  99. // WINMAIN ////////////////////////////////////////////////
  100.  
  101. int WINAPI WinMain(    HINSTANCE hinstance,
  102.                     HINSTANCE hprevinstance,
  103.                     LPSTR lpcmdline,
  104.                     int ncmdshow)
  105. {
  106. // this is the winmain function
  107.  
  108. WNDCLASS winclass;    // this will hold the class we create
  109. HWND     hwnd;        // generic window handle
  110. MSG         msg;        // generic message
  111. HDC      hdc;       // generic dc
  112. PAINTSTRUCT ps;     // generic paintstruct
  113.  
  114. // first fill in the window class stucture
  115. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  116.                           CS_HREDRAW | CS_VREDRAW;
  117. winclass.lpfnWndProc    = WindowProc;
  118. winclass.cbClsExtra        = 0;
  119. winclass.cbWndExtra        = 0;
  120. winclass.hInstance        = hinstance;
  121. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  122. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  123. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  124. winclass.lpszMenuName    = NULL; 
  125. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  126.  
  127. // register the window class
  128. if (!RegisterClass(&winclass))
  129.     return(0);
  130.  
  131. // create the window, note the use of WS_POPUP
  132. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  133.                           "WinX Game Console",     // title
  134.                           WS_POPUP | WS_VISIBLE,
  135.                            0,0,       // x,y
  136.                           WINDOW_WIDTH,  // width
  137.                           WINDOW_HEIGHT, // height
  138.                           NULL,       // handle to parent 
  139.                           NULL,       // handle to menu
  140.                           hinstance,// instance
  141.                           NULL)))    // creation parms
  142. return(0);
  143.  
  144. // save the window handle and instance in a global
  145. main_window_handle = hwnd;
  146. main_instance      = hinstance;
  147.  
  148. // perform all game console specific initialization
  149. Game_Init();
  150.  
  151. // enter main event loop
  152. while(1)
  153.     {
  154.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  155.         { 
  156.         // test if this is a quit
  157.         if (msg.message == WM_QUIT)
  158.            break;
  159.     
  160.         // translate any accelerator keys
  161.         TranslateMessage(&msg);
  162.  
  163.         // send the message to the window proc
  164.         DispatchMessage(&msg);
  165.         } // end if
  166.     
  167.     // main game processing goes here
  168.     Game_Main();
  169.  
  170.     } // end while
  171.  
  172. // shutdown game and release all resources
  173. Game_Shutdown();
  174.  
  175. // return to Windows like this
  176. return(msg.wParam);
  177.  
  178. } // end WinMain
  179.  
  180. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  181.  
  182. int Game_Init(void *parms)
  183. {
  184. // this function is where you do all the initialization 
  185. // for your game
  186.  
  187. // start up DirectDraw (replace the parms as you desire)
  188. screen_width = 640;
  189. screen_height = 480;
  190. screen_bpp     = 8;
  191.  
  192. DD_Init(screen_width, screen_height, screen_bpp);
  193.  
  194. // load a very good palette from disk based on the Deluxe
  195. // paint palette for 8-bit modes
  196. if (screen_bpp == 8)
  197.    {
  198.    Load_Palette_From_File("DPAINT.PAL",palette);
  199.    Set_Palette(palette);
  200.    } // end if
  201.  
  202. // initilize DirectSound
  203. DSound_Init();
  204.  
  205. // load all your sounds...
  206.  
  207.  
  208. // initialize DirectInput
  209. DInput_Init();
  210.  
  211. // initialize keyboard
  212. DI_Init_Keyboard();
  213.  
  214. // initialize mouse
  215. DI_Init_Mouse();
  216.  
  217. // initialize joystick
  218. DI_Init_Joystick();
  219.  
  220. // return success
  221. return(1);
  222.  
  223. } // end Game_Init
  224.  
  225. ///////////////////////////////////////////////////////////
  226.  
  227. int Game_Shutdown(void *parms)
  228. {
  229. // this function is where you shutdown your game and
  230. // release all resources that you allocated
  231.  
  232. // shut everything down
  233.  
  234. // shutdown directdraw last
  235. DD_Shutdown();
  236.  
  237. // now directsound
  238. DSound_Shutdown();
  239.  
  240. // finally directinput
  241. DI_Release_Keyboard();
  242. DI_Release_Mouse();
  243. DI_Release_Joystick();
  244.  
  245. DInput_Shutdown();
  246.  
  247. // return success
  248. return(1);
  249. } // end Game_Shutdown
  250.  
  251. ///////////////////////////////////////////////////////////
  252.  
  253. int Game_Main(void *parms)
  254. {
  255. // this is the workhorse of your game it will be called
  256. // continuously in real-time this is like main() in C
  257. // all the calls for you game go here!
  258.  
  259. static int green_tint  = 0,
  260.            green_delta = 4;
  261.  
  262. // check of user is trying to exit
  263. if (KEY_DOWN(VK_ESCAPE))
  264.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  265.  
  266. // start the timing clock
  267. Start_Clock();
  268.  
  269. // clear the drawing surface
  270. DD_Fill_Surface(lpddsback, 0);
  271.  
  272. // get input
  273. DI_Read_Keyboard();
  274. DI_Read_Mouse();
  275. DI_Read_Joystick();
  276.  
  277. // process input...test mouse_state, keyboard_state, joy_state
  278.  
  279. // play sounds...
  280.  
  281. // do all your drawing here to the back buffer
  282. if ((green_tint+=green_delta) > 255 || (green_tint < 0))
  283.     {
  284.     // change direction
  285.     green_delta = -green_delta;
  286.     green_tint+=green_delta;
  287.  
  288.     } // end if
  289.  
  290. Draw_Text_GDI("Your Computer is Still Alive...",
  291.               screen_width/2 - 4*strlen("Your Computer is Still Alive..."),screen_height/2,
  292.               RGB(0,green_tint,0),lpddsback);
  293.  
  294.  
  295. // display joystick
  296. sprintf(buffer,"Joystick: X-Axis=%d, Y-Axis=%d, buttons(%d,%d,%d,%d)",joy_state.lX,joy_state.lY,
  297.                                                                       joy_state.rgbButtons[0],
  298.                                                                       joy_state.rgbButtons[1],
  299.                                                                       joy_state.rgbButtons[2],
  300.                                                                       joy_state.rgbButtons[3]);
  301.  
  302. Draw_Text_GDI(buffer,0,SCREEN_HEIGHT-20,RGB(255,0,0),lpddsback);
  303.  
  304. // print out name of joystick
  305. Draw_Text_GDI(joyname,0,SCREEN_HEIGHT-40,RGB(255,255,50),lpddsback);
  306.  
  307.  
  308.  
  309. // flip the surfaces
  310. DD_Flip();
  311.  
  312. // sync to 30ish fps
  313. Wait_Clock(30);
  314.  
  315. // return success
  316. return(1);
  317.  
  318. } // end Game_Main
  319.  
  320. //////////////////////////////////////////////////////////