Windows 游戏编程大师技巧第四章第9个例子

来源:互联网 发布:数据采集卡 编辑:程序博客网 时间:2024/06/06 17:18
  1. // DEMO4_9.CPP - Starfield demo based on T3D console
  2. // INCLUDES ///////////////////////////////////////////////
  3. #define WIN32_LEAN_AND_MEAN  // just say no to MFC
  4. #include <windows.h>   // include important windows stuff
  5. #include <windowsx.h> 
  6. #include <mmsystem.h>
  7. #include <iostream.h> // include important C/C++ stuff
  8. #include <conio.h>
  9. #include <stdlib.h>
  10. #include <malloc.h>
  11. #include <memory.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <stdio.h> 
  15. #include <math.h>
  16. #include <io.h>
  17. #include <fcntl.h>
  18. // DEFINES ////////////////////////////////////////////////
  19. // defines for windows 
  20. #define WINDOW_CLASS_NAME "WINCLASS1"
  21. #define WINDOW_WIDTH      400
  22. #define WINDOW_HEIGHT     300
  23. // starfield defines
  24. #define NUM_STARS            256
  25. // MACROS /////////////////////////////////////////////////
  26. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  27. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  28. // TYPES //////////////////////////////////////////////////
  29. typedef struct STAR_TYP
  30.         {
  31.         int x,y;        // position of star
  32.         int vel;        // horizontal velocity of star
  33.         COLORREF col;   // color of star
  34.         } STAR, *STAR_PTR;
  35. // PROTOTYPES /////////////////////////////////////////////
  36. void Erase_Stars(void);
  37. void Draw_Stars(void);
  38. void Move_Stars(void);
  39. void Init_Stars(void);
  40. // GLOBALS ////////////////////////////////////////////////
  41. HWND      main_window_handle = NULL; // globally track main window
  42. HINSTANCE hinstance_app      = NULL; // globally track hinstance
  43. HDC       global_dc          = NULL; // tracks a global dc
  44. char buffer[80];                     // general printing buffer
  45. STAR stars[256];                     // holds the starfield
  46. // FUNCTIONS //////////////////////////////////////////////
  47. LRESULT CALLBACK WindowProc(HWND hwnd, 
  48.                             UINT msg, 
  49.                             WPARAM wparam, 
  50.                             LPARAM lparam)
  51. {
  52. // this is the main message handler of the system
  53. PAINTSTRUCT     ps;     // used in WM_PAINT
  54. HDC             hdc;    // handle to a device context
  55. char buffer[80];        // used to print strings
  56. // what is the message 
  57. switch(msg)
  58.     {   
  59.     case WM_CREATE: 
  60.         {
  61.         // do initialization stuff here
  62.         // return success
  63.         return(0);
  64.         } break;
  65.    
  66.     case WM_PAINT: 
  67.         {
  68.         // simply validate the window 
  69.         hdc = BeginPaint(hwnd,&ps);  
  70.         
  71.         // end painting
  72.         EndPaint(hwnd,&ps);
  73.         // return success
  74.         return(0);
  75.         } break;
  76.     case WM_DESTROY: 
  77.         {
  78.         // kill the application, this sends a WM_QUIT message 
  79.         PostQuitMessage(0);
  80.         // return success
  81.         return(0);
  82.         } break;
  83.     default:break;
  84.     } // end switch
  85. // process any messages that we didn't take care of 
  86. return (DefWindowProc(hwnd, msg, wparam, lparam));
  87. // end WinProc
  88. ///////////////////////////////////////////////////////////
  89. void Init_Stars(void)
  90. {
  91. // this function initializes all the stars
  92. for (int index=0; index < NUM_STARS; index++)
  93.     {
  94.     // select random position
  95.     stars[index].x = rand()%WINDOW_WIDTH;
  96.     stars[index].y = rand()%WINDOW_HEIGHT;
  97.     // set random velocity   
  98.     stars[index].vel = 1 + rand()%16;
  99.     // set intensity which is inversely prop to velocity for 3D effect
  100.     // note, I am mixing equal amounts of RGB to make black -> bright white    
  101.     int intensity = 15*(17 - stars[index].vel);
  102.     stars[index].col = RGB(intensity, intensity, intensity); 
  103.     } // end for index
  104. // end Init_Stars
  105. ////////////////////////////////////////////////////////////
  106. void Erase_Stars(void)
  107. {
  108. // this function erases all the stars
  109. for (int index=0; index < NUM_STARS; index++)
  110.     SetPixel(global_dc, stars[index].x, stars[index].y, RGB(0,0,0));
  111. // end Erase_Stars
  112. ////////////////////////////////////////////////////////////
  113. void Draw_Stars()
  114. {
  115. // this function draws all the stars
  116. for (int index=0; index < NUM_STARS; index++)
  117.     SetPixel(global_dc, stars[index].x, stars[index].y, stars[index].col);
  118. // end Draw_Stars
  119. ////////////////////////////////////////////////////////////
  120. void Move_Stars(void)
  121. {
  122. // this function moves all the stars and wraps them around the 
  123. // screen boundaries
  124. for (int index=0; index < NUM_STARS; index++)
  125.     {
  126.     // move the star and test for edge
  127.     stars[index].x+=stars[index].vel;
  128.     if (stars[index].x >= WINDOW_WIDTH)
  129.         stars[index].x -= WINDOW_WIDTH;
  130.     
  131.     } // end for index
  132. // end Move_Stars
  133. ////////////////////////////////////////////////////////////
  134. int Game_Main(void *parms = NULL, int num_parms = 0)
  135. {
  136. // this is the main loop of the game, do all your processing
  137. // here
  138. // get the time
  139. DWORD start_time = GetTickCount();
  140. // erase the stars
  141. Erase_Stars();
  142. // move the stars
  143. Move_Stars();
  144. // draw the stars
  145. Draw_Stars();
  146. // lock to 30 fps
  147. while((start_time - GetTickCount() < 33));
  148. // for now test if user is hitting ESC and send WM_CLOSE
  149. if (KEYDOWN(VK_ESCAPE))
  150.    SendMessage(main_window_handle,WM_CLOSE,0,0);
  151. // return success or failure or your own return code here
  152. return(1);
  153. // end Game_Main
  154. ////////////////////////////////////////////////////////////
  155. int Game_Init(void *parms = NULL, int num_parms = 0)
  156. {
  157. // this is called once after the initial window is created and
  158. // before the main event loop is entered, do all your initialization
  159. // here
  160. // first get the dc to the window
  161. global_dc = GetDC(main_window_handle);
  162. // initialize the star field here
  163. Init_Stars();
  164. // return success or failure or your own return code here
  165. return(1);
  166. // end Game_Init
  167. /////////////////////////////////////////////////////////////
  168. int Game_Shutdown(void *parms = NULL, int num_parms = 0)
  169. {
  170. // this is called after the game is exited and the main event
  171. // loop while is exited, do all you cleanup and shutdown here
  172. // release the global dc
  173. ReleaseDC(main_window_handle, global_dc);
  174. // return success or failure or your own return code here
  175. return(1);
  176. // end Game_Shutdown
  177. // WINMAIN ////////////////////////////////////////////////
  178. int WINAPI WinMain( HINSTANCE hinstance,
  179.                     HINSTANCE hprevinstance,
  180.                     LPSTR lpcmdline,
  181.                     int ncmdshow)
  182. {
  183. WNDCLASSEX winclass; // this will hold the class we create
  184. HWND       hwnd;     // generic window handle
  185. MSG        msg;      // generic message
  186. HDC        hdc;      // graphics device context
  187. // first fill in the window class stucture
  188. winclass.cbSize         = sizeof(WNDCLASSEX);
  189. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  190.                           CS_HREDRAW | CS_VREDRAW;
  191. winclass.lpfnWndProc    = WindowProc;
  192. winclass.cbClsExtra     = 0;
  193. winclass.cbWndExtra     = 0;
  194. winclass.hInstance      = hinstance;
  195. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  196. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  197. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  198. winclass.lpszMenuName   = NULL;
  199. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  200. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  201. // save hinstance in global
  202. hinstance_app = hinstance;
  203. // register the window class
  204. if (!RegisterClassEx(&winclass))
  205.     return(0);
  206. // create the window
  207. if (!(hwnd = CreateWindowEx(NULL,                  // extended style
  208.                             WINDOW_CLASS_NAME,     // class
  209.                             "T3D Game Console Star Demo"// title
  210.                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  211.                             0,0,      // initial x,y
  212.                             400,300,  // initial width, height
  213.                             NULL,     // handle to parent 
  214.                             NULL,     // handle to menu
  215.                             hinstance,// instance of this application
  216.                             NULL))) // extra creation parms
  217. return(0);
  218. // save main window handle
  219. main_window_handle = hwnd;
  220. // initialize game here
  221. Game_Init();
  222. // enter main event loop
  223. while(TRUE)
  224.     {
  225.     // test if there is a message in queue, if so get it
  226.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  227.        { 
  228.        // test if this is a quit
  229.        if (msg.message == WM_QUIT)
  230.            break;
  231.     
  232.        // translate any accelerator keys
  233.        TranslateMessage(&msg);
  234.        // send the message to the window proc
  235.        DispatchMessage(&msg);
  236.        } // end if
  237.     
  238.        // main game processing goes here
  239.        Game_Main();
  240.        
  241.     } // end while
  242. // closedown game here
  243. Game_Shutdown();
  244. // return to Windows like this
  245. return(msg.wParam);
  246. // end WinMain
  247. ///////////////////////////////////////////////////////////

 

 

终于看见,类似游戏的界面了.令我激动不已.

原创粉丝点击