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

  1. // DEMO4_1.CPP  - Pixel plotting demo
  2. // INCLUDES ///
  3. #define WIN32_LEAN_AND_MEAN  // just say no to MFC
  4. #include <windows.h>   // include all the windows headers
  5. #include <windowsx.h>  // include useful macros
  6. #include <mmsystem.h>  // very important and include WINMM.LIB too!
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. // DEFINES 
  11. // defines for windows 
  12. #define WINDOW_CLASS_NAME "WINCLASS1"
  13. #define WINDOW_WIDTH  400
  14. #define WINDOW_HEIGHT 300
  15. // MACROS /
  16. #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
  17. #define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
  18. // GLOBALS 
  19. HWND      main_window_handle = NULL; // globally track main window
  20. HINSTANCE hinstance_app      = NULL; // globally track hinstance
  21. char buffer[80];                     // general printing buffer
  22. // FUNCTIONS //
  23. LRESULT CALLBACK WindowProc(HWND hwnd, 
  24.                             UINT msg, 
  25.                             WPARAM wparam, 
  26.                             LPARAM lparam)
  27. {
  28. // this is the main message handler of the system
  29. PAINTSTRUCT     ps;     // used in WM_PAINT
  30. HDC             hdc;    // handle to a device context
  31. char buffer[80];        // used to print strings
  32. // what is the message 
  33. switch(msg)
  34.     {   
  35.     case WM_CREATE: 
  36.         {
  37.         // do initialization stuff here
  38.         // return success
  39.         return(0);
  40.         } break;
  41.    
  42.     case WM_PAINT: 
  43.         {
  44.         // simply validate the window 
  45.         hdc = BeginPaint(hwnd,&ps);  
  46.         
  47.         // end painting
  48.         EndPaint(hwnd,&ps);
  49.         // return success
  50.         return(0);
  51.         } break;
  52.     case WM_DESTROY: 
  53.         {
  54.         // kill the application, this sends a WM_QUIT message 
  55.         PostQuitMessage(0);
  56.         // return success
  57.         return(0);
  58.         } break;
  59.     default:break;
  60.     } // end switch
  61. // process any messages that we didn't take care of 
  62. return (DefWindowProc(hwnd, msg, wparam, lparam));
  63. // end WinProc
  64. // WINMAIN 
  65. int WINAPI WinMain( HINSTANCE hinstance,
  66.                     HINSTANCE hprevinstance,
  67.                     LPSTR lpcmdline,
  68.                     int ncmdshow)
  69. {
  70. WNDCLASSEX winclass; // this will hold the class we create
  71. HWND       hwnd;     // generic window handle
  72. MSG        msg;      // generic message
  73. HDC        hdc;      // graphics device context
  74. // first fill in the window class stucture
  75. winclass.cbSize         = sizeof(WNDCLASSEX);
  76. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  77.                           CS_HREDRAW | CS_VREDRAW;
  78. winclass.lpfnWndProc    = WindowProc;
  79. winclass.cbClsExtra     = 0;
  80. winclass.cbWndExtra     = 0;
  81. winclass.hInstance      = hinstance;
  82. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  83. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  84. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  85. winclass.lpszMenuName   = NULL;
  86. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  87. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  88. // save hinstance in global
  89. hinstance_app = hinstance;
  90. // register the window class
  91. if (!RegisterClassEx(&winclass))
  92.     return(0);
  93. // create the window
  94. if (!(hwnd = CreateWindowEx(NULL,                // extended style
  95.                             WINDOW_CLASS_NAME,   // class
  96.                             "Pixel Plotting Demo"// title
  97.                             WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  98.                             0,0,      // initial x,y
  99.                             WINDOW_WIDTH, // initial width
  100.                             WINDOW_HEIGHT,// initial height
  101.                             NULL,     // handle to parent 
  102.                             NULL,     // handle to menu
  103.                             hinstance,// instance of this application
  104.                             NULL))) // extra creation parms
  105. return(0);
  106. // save main window handle
  107. main_window_handle = hwnd;
  108. // enter main event loop, but this time we use PeekMessage()
  109. // instead of GetMessage() to retrieve messages
  110. while(TRUE)
  111.     {
  112.     // test if there is a message in queue, if so get it
  113.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  114.        { 
  115.         // test if this is a quit
  116.            if (msg.message == WM_QUIT)
  117.                            break;
  118.     
  119.         // translate any accelerator keys
  120.              TranslateMessage(&msg);
  121.         // send the message to the window proc
  122.               DispatchMessage(&msg);
  123.        } // end if
  124.      // draw some pixels each cycle
  125.      // get the dc for the window
  126.      hdc = GetDC(hwnd);
  127.      // draw 1000 pixels
  128.      for (int index=0; index < 1000; index++)
  129.          {
  130.          // get random position
  131.          int x = rand()%400;
  132.          int y = rand()%300;
  133.          COLORREF color = RGB(rand()%255,rand()%255,rand()%255);
  134.          SetPixel(hdc, x,y, color);
  135.          } // end for index
  136.       // release the dc
  137.       ReleaseDC(hwnd, hdc);
  138.     
  139.        // main game processing goes here
  140.        if (KEYDOWN(VK_ESCAPE))
  141.           SendMessage(hwnd, WM_CLOSE, 0,0);
  142.        
  143.     } // end while
  144. // return to Windows like this
  145. return(msg.wParam);
  146. // end WinMain
  147. ///

要注意的是与一般WIN32程序的框架不同点.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值