游戏编程中的人工智能技术【2-1】

  

HDC hdc = GetDC(hwnd);//客户区 hdc = GetWindowDC(hwnd) 或者 hdc = GetDC(NULL);

hdc = ps.hdc; //这个是一个RECT里的dc

ReleaseDC(hWnd,hdc);

1。 Lines-----------------------------------------------------------------------------------------------------------------------------------------------

LRESULT CALLBACK WindowProc (HWND   hwnd,
							 UINT   msg,
							 WPARAM wParam,
							 LPARAM lParam)
{
	/************************************************************************/
	/* 这里定义了两个静态整型变量来保存窗口客户区的尺寸。这样,显示图形就能够在用户改变窗口尺寸的时候进行相应的缩放                                                                     */
	/************************************************************************/
	static int cxClient, cyClient;

	switch (msg)
	{
	case WM_CREATE:
		{
			RECT rect;
			GetClientRect(hwnd, &rect);
			cxClient = rect.right;
			cyClient = rect.bottom;
		}

		break;
	case WM_SIZE:
		{
                   cxClient = LOWORD(lParam);
		   cyClient = HIWORD(lParam);
		}
        break;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;

			BeginPaint (hwnd, &ps);

			//**this is where we do any drawing to the screen**
			const int NumLinesPerSide = 10;
			int       yStep           = cyClient/NumLinesPerSide;
			int       xStep           = cxClient/NumLinesPerSide;
			//Draw Lines
			for(int mult=1; mult<NumLinesPerSide; ++mult){
				//bottom left
				MoveToEx(ps.hdc, xStep*mult, 0, 0);
				LineTo(ps.hdc, 0, cyClient-yStep*mult);
                //top right
				MoveToEx(ps.hdc, xStep*mult, cyClient, 0);
				LineTo(ps.hdc, cxClient, cyClient-yStep*mult);
                //bottom right
				MoveToEx(ps.hdc, xStep*mult, 0,0);
				LineTo(ps.hdc, cxClient, yStep*mult);
                //top left
				MoveToEx(ps.hdc, xStep*mult, cyClient, 0);
				LineTo(ps.hdc, 0, yStep*mult);
			}
			EndPaint (hwnd, &ps);
		}

		break;

	case WM_DESTROY:
		{
			// kill the application, this sends a WM_QUIT message  
			PostQuitMessage (0);
		}

		break;


	}//end switch

	//this is where all the messages not specifically handled by our 
	//winproc are sent to be processed
	return DefWindowProc (hwnd, msg, wParam, lParam);
}
 
2。 lines2 ---------------------------------------------------------------------------------------------------------------
LRESULT CALLBACK WindowProc (HWND   hwnd,
							 UINT   msg,
							 WPARAM wParam,
							 LPARAM lParam)
{
	/************************************************************************/
	/* 这里定义了两个静态整型变量来保存窗口客户区的尺寸。这样,显示图形就能够在用户改变窗口尺寸的时候进行相应的缩放                                                                     */
	/************************************************************************/
	static int cxClient, cyClient;
	static HPEN BluePen   = CreatePen(PS_SOLID, 1, RGB(0,0,255));
	static HPEN GreenPen  = CreatePen(PS_SOLID, 1, RGB(255,0,0));
	static HPEN RedPen    = CreatePen(PS_SOLID, 1, RGB(0,255,0));
	static HPEN PurplePen = CreatePen(PS_SOLID, 1, RGB(255,0,255));
	static HPEN OldPen    = NULL;
	switch (msg)
	{
	case WM_CREATE:
		{
			RECT rect;
			GetClientRect(hwnd, &rect);
			cxClient = rect.right;
			cyClient = rect.bottom;
		}

		break;
	case WM_SIZE:
		{
           cxClient = LOWORD(lParam);
		   cyClient = HIWORD(lParam);
		}
        break;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;

			BeginPaint (hwnd, &ps);
            OldPen = (HPEN)SelectObject(ps.hdc, RedPen);
			//**this is where we do any drawing to the screen**
			const int NumLinesPerSide = 10;
			int       yStep           = cyClient/NumLinesPerSide;
			int       xStep           = cxClient/NumLinesPerSide;
			//Draw Lines
			for(int mult=1; mult<NumLinesPerSide; ++mult){
				//bottom left
				SelectObject(ps.hdc, RedPen);
				MoveToEx(ps.hdc, xStep*mult, 0, 0);
				LineTo(ps.hdc, 0, cyClient-yStep*mult);
                //top right
				SelectObject(ps.hdc, BluePen);
				MoveToEx(ps.hdc, xStep*mult, cyClient, 0);
				LineTo(ps.hdc, cxClient, cyClient-yStep*mult);
                //bottom right
				SelectObject(ps.hdc, GreenPen);
				MoveToEx(ps.hdc, xStep*mult, 0,0);
				LineTo(ps.hdc, cxClient, yStep*mult);
                //top left
				SelectObject(ps.hdc, PurplePen);
				MoveToEx(ps.hdc, xStep*mult, cyClient, 0);
				LineTo(ps.hdc, 0, yStep*mult);
			}
			SelectObject(ps.hdc, OldPen);
			EndPaint (hwnd, &ps);
		}

		break;

	case WM_DESTROY:
		{
			// kill the application, this sends a WM_QUIT message  
			DeleteObject(RedPen);
			DeleteObject(BluePen);
			DeleteObject(GreenPen);
			DeleteObject(PurplePen);
			DeleteObject(OldPen);
			PostQuitMessage (0);
		}

		break;


	}//end switch

	//this is where all the messages not specifically handled by our 
	//winproc are sent to be processed
	return DefWindowProc (hwnd, msg, wParam, lParam);
}
3。 Polygon--------------------------------------------------------------------------------------------------------

LRESULT CALLBACK WindowProc (HWND   hwnd,
                             UINT   msg,
                             WPARAM wParam,
                             LPARAM lParam)
{
    //create some pens to use for drawing
    static HPEN BluePen  = CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
    static HPEN OldPen   = NULL;

    //create a solid brush
    static HBRUSH RedBrush = CreateSolidBrush(RGB(255, 0, 0));
    static HBRUSH OldBrush = NULL;
    //these hold the dimensions of the client window area
      static int cxClient, cyClient;

    //this will hold the vertices of the polygons we create
    static POINT verts[NUM_VERTS];

    static int iNumVerts = NUM_VERTS;
    switch (msg)
    {
        //A WM_CREATE msg is sent when your application window is first
        //created
    case WM_CREATE:
      {
         //to get get the size of the client window first we need  to create
         //a RECT and then ask Windows to fill in our RECT structure with
         //the client window size. Then we assign to cxClient and cyClient
         //accordingly
               RECT rect;

               GetClientRect(hwnd, &rect);

               cxClient = rect.right;
               cyClient = rect.bottom;

         //seed random number generator
         srand((unsigned) time(NULL));

         //now lets create some random vertices
         for (int v=0; v<iNumVerts; ++v)
         {
           verts[v].x = RandInt(0, cxClient);
           verts[v].y = RandInt(0, cyClient);
         }

      }

      break;

    case WM_KEYUP:
      {
        switch(wParam)
        {
        case VK_SPACE:
          {
            //create some new points for our polygon
            //now lets create some random vertices
            for (int v=0; v<iNumVerts; ++v)
            {
              verts[v].x = RandInt(0, cxClient);
              verts[v].y = RandInt(0, cyClient);
            }

             //refresh the display so we can see our
              //new polygon
              InvalidateRect(hwnd, NULL, TRUE);
              UpdateWindow(hwnd);
          }
          break;

        case VK_ESCAPE:
          {
            PostQuitMessage(0);
          }
          break;
        }
      }
    case WM_PAINT:
      {
                  PAINTSTRUCT ps;
         BeginPaint (hwnd, &ps);
         //first select a pen to draw with and store a copy
         //of the pen we are swapping it with
         OldPen = (HPEN)SelectObject(ps.hdc, BluePen);

         //do the same for our brush
         OldBrush = (HBRUSH)SelectObject(ps.hdc, RedBrush);

         //draw the polygon
         Polygon(ps.hdc, verts, iNumVerts);
         //replace the original pen
         SelectObject(ps.hdc, OldPen);
         //and brush
         SelectObject(ps.hdc, OldBrush);
         EndPaint (hwnd, &ps);
      }

      break;

    //has the user resized the client area?
        case WM_SIZE:
          {
        //if so we need to update our variables so that any drawing
        //we do using cxClient and cyClient is scaled accordingly
              cxClient = LOWORD(lParam);
              cyClient = HIWORD(lParam);

        //create a new polygon
        for (int v=0; v<iNumVerts; ++v)
        {
           verts[v].x = RandInt(0, cxClient);
           verts[v].y = RandInt(0, cyClient);
        }
      }

      break;
         case WM_DESTROY:
             {
                 //delete the pens       
         DeleteObject(BluePen);
         DeleteObject(OldPen);

         //and the brushes
         DeleteObject(RedBrush);
         DeleteObject(OldBrush);
         // kill the application, this sends a WM_QUIT message 
                 PostQuitMessage (0);
             }

       break;

     }//end switch

     //this is where all the messages not specifically handled by our
         //winproc are sent to be processed
         return DefWindowProc (hwnd, msg, wParam, lParam);
}

 

出自: 《游戏编程中的人工智能技术》

你可能感兴趣的:(游戏,技术,职场,人工智能,休闲)