home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / Source / GPCHAP12 / GPTEMP.CPP < prev    next >
C/C++ Source or Header  |  2002-04-29  |  6KB  |  254 lines

  1. // GPTEMP.CPP - Template to drop code into
  2.  
  3. // INCLUDES ///////////////////////////////////////////////
  4.  
  5. #define WIN32_LEAN_AND_MEAN  
  6. #define INITGUID
  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 <ddraw.h>  // directX includes
  24. #include "gpdumb1.h"
  25.  
  26. // DEFINES ////////////////////////////////////////////////
  27.  
  28. // defines for windows 
  29. #define WINDOW_CLASS_NAME "WINXCLASS"  // class name
  30.  
  31. #define WINDOW_WIDTH    64   // size of window
  32. #define WINDOW_HEIGHT   48
  33.  
  34. // PROTOTYPES /////////////////////////////////////////////
  35.  
  36. // game console
  37. int Game_Init(void *parms=NULL);
  38. int Game_Shutdown(void *parms=NULL);
  39. int Game_Main(void *parms=NULL);
  40.  
  41. // GLOBALS ////////////////////////////////////////////////
  42.  
  43. HWND main_window_handle  = NULL; // save the window handle
  44. HINSTANCE main_instance  = NULL; // save the instance
  45. char buffer[80];                 // used to print text
  46.  
  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. // this is the winmain function
  102.  
  103. WNDCLASS winclass;    // this will hold the class we create
  104. HWND     hwnd;        // generic window handle
  105. MSG         msg;        // generic message
  106. HDC      hdc;       // generic dc
  107. PAINTSTRUCT ps;     // generic paintstruct
  108.  
  109. // first fill in the window class stucture
  110. winclass.style            = CS_DBLCLKS | CS_OWNDC | 
  111.                           CS_HREDRAW | CS_VREDRAW;
  112. winclass.lpfnWndProc    = WindowProc;
  113. winclass.cbClsExtra        = 0;
  114. winclass.cbWndExtra        = 0;
  115. winclass.hInstance        = hinstance;
  116. winclass.hIcon            = LoadIcon(NULL, IDI_APPLICATION);
  117. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  118. winclass.hbrBackground    = (HBRUSH)GetStockObject(BLACK_BRUSH);
  119. winclass.lpszMenuName    = NULL; 
  120. winclass.lpszClassName    = WINDOW_CLASS_NAME;
  121.  
  122. // register the window class
  123. if (!RegisterClass(&winclass))
  124.     return(0);
  125.  
  126. // create the window, note the use of WS_POPUP
  127. if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
  128.                           "WinX Game Console",     // title
  129.                           WS_POPUP | WS_VISIBLE,
  130.                            0,0,       // x,y
  131.                           WINDOW_WIDTH,  // width
  132.                           WINDOW_HEIGHT, // height
  133.                           NULL,       // handle to parent 
  134.                           NULL,       // handle to menu
  135.                           hinstance,// instance
  136.                           NULL)))    // creation parms
  137. return(0);
  138.  
  139. // save the window handle and instance in a global
  140. main_window_handle = hwnd;
  141. main_instance      = hinstance;
  142.  
  143. // perform all game console specific initialization
  144. Game_Init();
  145.  
  146. // enter main event loop
  147. while(1)
  148.     {
  149.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  150.         { 
  151.         // test if this is a quit
  152.         if (msg.message == WM_QUIT)
  153.            break;
  154.     
  155.         // translate any accelerator keys
  156.         TranslateMessage(&msg);
  157.  
  158.         // send the message to the window proc
  159.         DispatchMessage(&msg);
  160.         } // end if
  161.     
  162.     // main game processing goes here
  163.     Game_Main();
  164.  
  165.     } // end while
  166.  
  167. // shutdown game and release all resources
  168. Game_Shutdown();
  169.  
  170. // return to Windows like this
  171. return(msg.wParam);
  172.  
  173. } // end WinMain
  174.  
  175. // WINX GAME PROGRAMMING CONSOLE FUNCTIONS ////////////////
  176.  
  177. int Game_Init(void *parms)
  178. {
  179. // this function is where you do all the initialization 
  180. // for your game
  181.  
  182. // start up DirectDraw (replace the parms as you desire)
  183. DD_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
  184.  
  185. // load a very good palette from disk based on the Deluxe
  186. // paint palette for 8-bit modes
  187. if (SCREEN_BPP==8)
  188. Load_Palette_From_File("DPAINT.PAL",palette);
  189. Set_Palette(palette);
  190. } // end if
  191.  
  192. // else 16-bit mode do nothing
  193.  
  194. // do rest of inits
  195.  
  196. // return success
  197. return(1);
  198.  
  199. } // end Game_Init
  200.  
  201. ///////////////////////////////////////////////////////////
  202.  
  203. int Game_Shutdown(void *parms)
  204. {
  205. // this function is where you shutdown your game and
  206. // release all resources that you allocated
  207.  
  208. // shut everything down
  209.  
  210.  
  211. // shutdown directdraw last
  212. DD_Shutdown();
  213.  
  214. // return success
  215. return(1);
  216. } // end Game_Shutdown
  217.  
  218. ///////////////////////////////////////////////////////////
  219.  
  220. int Game_Main(void *parms)
  221. {
  222. // this is the workhorse of your game it will be called
  223. // continuously in real-time this is like main() in C
  224. // all the calls for you game go here!
  225.  
  226. // check of user is trying to exit
  227. if (KEY_DOWN(VK_ESCAPE))
  228.     PostMessage(main_window_handle, WM_DESTROY,0,0);
  229.  
  230. // start the timing clock
  231. Start_Clock();
  232.  
  233. // clear the drawing surface
  234. DD_Fill_Surface(lpddsback, 0);
  235.  
  236.  
  237. // do all your drawing here to the back buffer
  238. Draw_Text_GDI("Your Computer is Alive!",
  239.               screen_width/2 - 2*strlen("Your Computer is Alive!"),screen_height/2,
  240.               RGB(255,255,50),lpddsback);
  241.  
  242. // flip the surfaces
  243. DD_Flip();
  244.  
  245. // sync to 30ish fps
  246. Wait_Clock(30);
  247.  
  248. // return success
  249. return(1);
  250.  
  251. } // end Game_Main
  252.  
  253. //////////////////////////////////////////////////////////