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.    
  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. void Erase_Stars(void)
  106. {
  107. // this function erases all the stars
  108. for (int index=0; index < NUM_STARS; index++)
  109.     SetPixel(global_dc, stars[index].x, stars[index].y, RGB(0,0,0));
  110. // end Erase_Stars
  111. void Draw_Stars()
  112. {
  113. // this function draws all the stars
  114. for (int index=0; index < NUM_STARS; index++)
  115.     SetPixel(global_dc, stars[index].x, stars[index].y, stars[index].col);
  116. // end Draw_Stars
  117. void Move_Stars(void)
  118. {
  119. // this function moves all the stars and wraps them around the 
  120. // screen boundaries
  121. for (int index=0; index < NUM_STARS; index++)
  122.     {
  123.     // move the star and test for edge
  124.     stars[index].x+=stars[index].vel;
  125.     if (stars[index].x >= WINDOW_WIDTH)
  126.         stars[index].x -= WINDOW_WIDTH;
  127.     
  128.     } // end for index
  129. // end Move_Stars
  130. int Game_Main(void *parms = NULL, int num_parms = 0)
  131. {
  132. // this is the main loop of the game, do all your processing
  133. // here
  134. // get the time
  135. DWORD start_time = GetTickCount();
  136. // erase the stars
  137. Erase_Stars();
  138. // move the stars
  139. Move_Stars();
  140. // draw the stars
  141. Draw_Stars();
  142. // lock to 30 fps
  143. while((start_time - GetTickCount() < 33));
  144. // for now test if user is hitting ESC and send WM_CLOSE
  145. if (KEYDOWN(VK_ESCAPE))
  146.    SendMessage(main_window_handle,WM_CLOSE,0,0);
  147. // return success or failure or your own return code here
  148. return(1);
  149. // end Game_Main
  150. int Game_Init(void *parms = NULL, int num_parms = 0)
  151. {
  152. // this is called once after the initial window is created and
  153. // before the main event loop is entered, do all your initialization
  154. // here
  155. // first get the dc to the window
  156. global_dc = GetDC(main_window_handle);
  157. // initialize the star field here
  158. Init_Stars();
  159. // return success or failure or your own return code here
  160. return(1);
  161. // end Game_Init
  162. /
  163. int Game_Shutdown(void *parms = NULL, int num_parms = 0)
  164. {
  165. // this is called after the game is exited and the main event
  166. // loop while is exited, do all you cleanup and shutdown here
  167. // release the global dc
  168. ReleaseDC(main_window_handle, global_dc);
  169. // return success or failure or your own return code here
  170. return(1);
  171. // end Game_Shutdown
  172. // WINMAIN 
  173. int WINAPI WinMain( HINSTANCE hinstance,
  174.                     HINSTANCE hprevinstance,
  175.                     LPSTR lpcmdline,
  176.                     int ncmdshow)
  177. {
  178. WNDCLASSEX winclass; // this will hold the class we create
  179. HWND       hwnd;     // generic window handle
  180. MSG        msg;      // generic message
  181. HDC        hdc;      // graphics device context
  182. // first fill in the window class stucture
  183. winclass.cbSize         = sizeof(WNDCLASSEX);
  184. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  185.                           CS_HREDRAW | CS_VREDRAW;
  186. winclass.lpfnWndProc    = WindowProc;
  187. winclass.cbClsExtra     = 0;
  188. winclass.cbWndExtra     = 0;
  189. winclass.hInstance      = hinstance;
  190. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  191. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  192. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  193. winclass.lpszMenuName   = NULL;
  194. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  195. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  196. // save hinstance in global
  197. hinstance_app = hinstance;
  198. // register the window class
  199. if (!RegisterClassEx(&winclass))
  200.     return(0);
  201. // create the window
  202. if (!(hwnd = CreateWindowEx(NULL,                  // extended style
  203.                             WINDOW_CLASS_NAME,     // class
  204.                             "T3D Game Console Star Demo"// title
  205.                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  206.                             0,0,      // initial x,y
  207.                             400,300,  // initial width, height
  208.                             NULL,     // handle to parent 
  209.                             NULL,     // handle to menu
  210.                             hinstance,// instance of this application
  211.                             NULL))) // extra creation parms
  212. return(0);
  213. // save main window handle
  214. main_window_handle = hwnd;
  215. // initialize game here
  216. Game_Init();
  217. // enter main event loop
  218. while(TRUE)
  219.     {
  220.     // test if there is a message in queue, if so get it
  221.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  222.        { 
  223.        // test if this is a quit
  224.        if (msg.message == WM_QUIT)
  225.            break;
  226.     
  227.        // translate any accelerator keys
  228.        TranslateMessage(&msg);
  229.        // send the message to the window proc
  230.        DispatchMessage(&msg);
  231.        } // end if
  232.     
  233.        // main game processing goes here
  234.        Game_Main();
  235.        
  236.     } // end while
  237. // closedown game here
  238. Game_Shutdown();
  239. // return to Windows like this
  240. return(msg.wParam);
  241. // end WinMain
  242. ///

 

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值