天天看點

Windows 遊戲程式設計大師技巧第四章第9個例子

  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.     case WM_PAINT: 
  66.         {
  67.         // simply validate the window 
  68.         hdc = BeginPaint(hwnd,&ps);  
  69.         // end painting
  70.         EndPaint(hwnd,&ps);
  71.         // return success
  72.         return(0);
  73.         } break;
  74.     case WM_DESTROY: 
  75.         {
  76.         // kill the application, this sends a WM_QUIT message 
  77.         PostQuitMessage(0);
  78.         // return success
  79.         return(0);
  80.         } break;
  81.     default:break;
  82.     } // end switch
  83. // process any messages that we didn't take care of 
  84. return (DefWindowProc(hwnd, msg, wparam, lparam));
  85. } // end WinProc
  86. ///
  87. void Init_Stars(void)
  88. {
  89. // this function initializes all the stars
  90. for (int index=0; index < NUM_STARS; index++)
  91.     {
  92.     // select random position
  93.     stars[index].x = rand()%WINDOW_WIDTH;
  94.     stars[index].y = rand()%WINDOW_HEIGHT;
  95.     // set random velocity   
  96.     stars[index].vel = 1 + rand()%16;
  97.     // set intensity which is inversely prop to velocity for 3D effect
  98.     // note, I am mixing equal amounts of RGB to make black -> bright white    
  99.     int intensity = 15*(17 - stars[index].vel);
  100.     stars[index].col = RGB(intensity, intensity, intensity); 
  101.     } // end for index
  102. } // end Init_Stars
  103. void Erase_Stars(void)
  104. {
  105. // this function erases all the stars
  106. for (int index=0; index < NUM_STARS; index++)
  107.     SetPixel(global_dc, stars[index].x, stars[index].y, RGB(0,0,0));
  108. } // end Erase_Stars
  109. void Draw_Stars()
  110. {
  111. // this function draws all the stars
  112. for (int index=0; index < NUM_STARS; index++)
  113.     SetPixel(global_dc, stars[index].x, stars[index].y, stars[index].col);
  114. } // end Draw_Stars
  115. void Move_Stars(void)
  116. {
  117. // this function moves all the stars and wraps them around the 
  118. // screen boundaries
  119. for (int index=0; index < NUM_STARS; index++)
  120.     {
  121.     // move the star and test for edge
  122.     stars[index].x+=stars[index].vel;
  123.     if (stars[index].x >= WINDOW_WIDTH)
  124.         stars[index].x -= WINDOW_WIDTH;
  125.     } // end for index
  126. } // end Move_Stars
  127. int Game_Main(void *parms = NULL, int num_parms = 0)
  128. {
  129. // this is the main loop of the game, do all your processing
  130. // here
  131. // get the time
  132. DWORD start_time = GetTickCount();
  133. // erase the stars
  134. Erase_Stars();
  135. // move the stars
  136. Move_Stars();
  137. // draw the stars
  138. Draw_Stars();
  139. // lock to 30 fps
  140. while((start_time - GetTickCount() < 33));
  141. // for now test if user is hitting ESC and send WM_CLOSE
  142. if (KEYDOWN(VK_ESCAPE))
  143.    SendMessage(main_window_handle,WM_CLOSE,0,0);
  144. // return success or failure or your own return code here
  145. return(1);
  146. } // end Game_Main
  147. int Game_Init(void *parms = NULL, int num_parms = 0)
  148. {
  149. // this is called once after the initial window is created and
  150. // before the main event loop is entered, do all your initialization
  151. // here
  152. // first get the dc to the window
  153. global_dc = GetDC(main_window_handle);
  154. // initialize the star field here
  155. Init_Stars();
  156. // return success or failure or your own return code here
  157. return(1);
  158. } // end Game_Init
  159. /
  160. int Game_Shutdown(void *parms = NULL, int num_parms = 0)
  161. {
  162. // this is called after the game is exited and the main event
  163. // loop while is exited, do all you cleanup and shutdown here
  164. // release the global dc
  165. ReleaseDC(main_window_handle, global_dc);
  166. // return success or failure or your own return code here
  167. return(1);
  168. } // end Game_Shutdown
  169. // WINMAIN 
  170. int WINAPI WinMain( HINSTANCE hinstance,
  171.                     HINSTANCE hprevinstance,
  172.                     LPSTR lpcmdline,
  173.                     int ncmdshow)
  174. {
  175. WNDCLASSEX winclass; // this will hold the class we create
  176. HWND       hwnd;     // generic window handle
  177. MSG        msg;      // generic message
  178. HDC        hdc;      // graphics device context
  179. // first fill in the window class stucture
  180. winclass.cbSize         = sizeof(WNDCLASSEX);
  181. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  182.                           CS_HREDRAW | CS_VREDRAW;
  183. winclass.lpfnWndProc    = WindowProc;
  184. winclass.cbClsExtra     = 0;
  185. winclass.cbWndExtra     = 0;
  186. winclass.hInstance      = hinstance;
  187. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  188. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  189. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  190. winclass.lpszMenuName   = NULL;
  191. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  192. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  193. // save hinstance in global
  194. hinstance_app = hinstance;
  195. // register the window class
  196. if (!RegisterClassEx(&winclass))
  197.     return(0);
  198. // create the window
  199. if (!(hwnd = CreateWindowEx(NULL,                  // extended style
  200.                             WINDOW_CLASS_NAME,     // class
  201.                             "T3D Game Console Star Demo", // title
  202.                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  203.                             0,0,      // initial x,y
  204.                             400,300,  // initial width, height
  205.                             NULL,     // handle to parent 
  206.                             NULL,     // handle to menu
  207.                             hinstance,// instance of this application
  208.                             NULL))) // extra creation parms
  209. return(0);
  210. // save main window handle
  211. main_window_handle = hwnd;
  212. // initialize game here
  213. Game_Init();
  214. // enter main event loop
  215. while(TRUE)
  216.     {
  217.     // test if there is a message in queue, if so get it
  218.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  219.        { 
  220.        // test if this is a quit
  221.        if (msg.message == WM_QUIT)
  222.            break;
  223.        // translate any accelerator keys
  224.        TranslateMessage(&msg);
  225.        // send the message to the window proc
  226.        DispatchMessage(&msg);
  227.        } // end if
  228.        // main game processing goes here
  229.        Game_Main();
  230.     } // end while
  231. // closedown game here
  232. Game_Shutdown();
  233. // return to Windows like this
  234. return(msg.wParam);
  235. } // end WinMain
  236. ///

終于看見,類似遊戲的界面了.令我激動不已.

繼續閱讀