现在的位置: 首页 > 综合 > 正文

填充实心多边形(二)

2013年03月24日 ⁄ 综合 ⁄ 共 16375字 ⁄ 字号 评论关闭

 上一篇文章介绍了填充四边形及三角形,那么对于五边形,六边形呢?当然也可以把五边形分解成3个三角形,六边形分解成4个三角形进行填充。推而广之,可以把n边形分解成n-2个三角形来进行填充。这里介绍另一个方法。如图所示。

这是更为一般化的填充(光栅化?)实心多边形方法。找到多边形各顶点中y轴坐标最小的点作为起始点,按顺时针设置各顶点标号,然后从逆时针方向和顺时针方向同时出发,当一条边到达一个终点(另一个顶点)时,重新计算插值,继续下去直到完成光栅化。代码清单如下:

  1. /* 2008/9/6 
  2. 代码功能:光栅化
  3. 多边形的一般方法*/
  4. #define WIN32_LEAN_AND_MEAN   //不使用MFC
  5. #define INITGUID      //使用GUID
  6. #include <windows.h>
  7. #include <windowsx.h>
  8. #include <mmsystem.h> //多媒体API
  9. #include <iostream.h>
  10. #include <conio.h>    //控制台IO支持
  11. #include <stdlib.h>   //声明定义的一些常用标准函数库
  12. #include <malloc.h>   //声明或定义一些内存的函数
  13. #include <memory.h>   //提供了内存操作相关的一些函数及声明
  14. #include <string.h>   //字符串的一些功能
  15. #include <stdarg.h>   //defines ANSI-style macros for variable argument functions
  16. #include <stdio.h>    //efinitions/declarations for standard I/O routines
  17. #include <math.h>     //一些数学方法
  18. #include <io.h>       //declarations for low-level file handling and I/O functions
  19. #include <fcntl.h>    //file control options used by open()
  20. #include <ddraw.h>
  21. //windows类名
  22. #define  WINDOW_CLASS_NAME "WINCLASS1"
  23. //宽,高,色深(像素)
  24. #define  SCREEN_WIDTH   640
  25. #define  SCREEN_HEIGHT   480
  26. #define  SCREEN_BPP     8
  27. #define  MAX_COLORS_PALETTE   256
  28. #define  PI 3.1415926535
  29. typedef unsigned short USHORT;
  30. typedef unsigned short WORD;
  31. typedef unsigned char UCHAR;
  32. typedef unsigned char BYTE;
  33. typedef struct VERTEX2DF_TYP 
  34. {
  35.   float x,y;
  36. }VERTEX2DF,*VERTEX2DF_PTR;
  37. typedef struct POLYGON2D_TYP 
  38. {
  39.   int state;
  40.   int num_vert;
  41.   int x0,y0;
  42.   int xv,yv;
  43.   DWORD color;
  44.   VERTEX2DF* vlist;
  45. }POLYGON2D,*POLYGON2D_PTR;
  46. //GDI文字函数
  47. int Draw_Text_GDI(char* text,int x,int y,int color,LPDIRECTDRAWSURFACE7 lpdds);
  48. //填充表面
  49. int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds,int color);
  50. //旋转图形
  51. int Rotate_Polygon2D(POLYGON2D_PTR poly,int theta);
  52. //填充2D图形
  53. int Draw_Filled_Polygon2D(POLYGON2D_PTR poly,UCHAR* vbuffer,int mempitch);
  54. //设置调色板入口
  55. int Set_Palette_Entry(int color_index,LPPALETTEENTRY color);
  56. //键盘按键宏
  57. #define  KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0)
  58. #define  KEYUP(vk_code)   ((GetAsyncKeyState(vk_code)&0x8000)?0:1)
  59. //初始化结构体
  60. #define  DDRAW_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct));ddstruct.dwSize=sizeof(ddstruct); }
  61. HWND main_window_handle =NULL;
  62. int  window_closed =0;
  63. HINSTANCE hinstance_app =NULL;
  64. LPDIRECTDRAW7 lpdd =NULL;
  65. LPDIRECTDRAWSURFACE7 lpddsprimary =NULL;  //主缓存
  66. LPDIRECTDRAWSURFACE7 lpddsback =NULL;     //后备缓冲
  67. LPDIRECTDRAWPALETTE  lpddpal =NULL;       //调色板接口
  68. LPDIRECTDRAWCLIPPER  lpddclipper =NULL;   //裁剪器
  69. PALETTEENTRY         palette[256];        //调色板
  70. DDSURFACEDESC2       ddsd;
  71. DDBLTFX              ddbltfx;
  72. DDSCAPS2             ddscaps;
  73. /*裁剪区域*/
  74. int min_clip_x = 0,
  75.     max_clip_x =SCREEN_WIDTH-1, 
  76.     min_clip_y = 0,
  77.     max_clip_y = SCREEN_HEIGHT-1;
  78. char buffer[80];
  79. float cos_look[360],
  80.       sin_look[360];
  81. POLYGON2D object;
  82. int Draw_Text_GDI(char* text,int x,int y,int color,LPDIRECTDRAWSURFACE7 lpdds)
  83. {
  84.   HDC xdc;
  85.   if(FAILED(lpdds->GetDC(&xdc)))
  86.     return(0);
  87.   SetTextColor(xdc,RGB(palette[color].peRed,palette[color].peGreen,palette[color].peBlue));
  88.   SetBkMode(xdc,TRANSPARENT);
  89.   TextOut(xdc,x,y,text,strlen(text));
  90.   lpdds->ReleaseDC(xdc);
  91.   return(1);
  92. }
  93. int DDraw_Fill_Surface(LPDIRECTDRAWSURFACE7 lpdds,int color)
  94. {
  95.   DDRAW_INIT_STRUCT(ddbltfx);
  96.   ddbltfx.dwFillColor=color;
  97.   lpdds->Blt(NULL,
  98.              NULL,
  99.              NULL,
  100.              DDBLT_COLORFILL|DDBLT_WAIT,
  101.              &ddbltfx);
  102.   return(1);
  103. }
  104. int Set_Palette_Entry(int color_index,LPPALETTEENTRY color)
  105. {
  106.   lpddpal->SetEntries(0,color_index,1,color);
  107.   memcpy(&palette[1],color,sizeof(PALETTEENTRY));
  108.   return(1);
  109. }
  110. int Rotate_Polygon2D(POLYGON2D_PTR poly, int theta)
  111. {
  112. // this function rotates the local coordinates of the polygon
  113. // test for valid pointer
  114. if (!poly)
  115.    return(0);
  116. // loop and rotate each point, very crude, no lookup!!!
  117. for (int curr_vert = 0; curr_vert < poly->num_vert; curr_vert++)
  118.     {
  119.     // perform rotation
  120.     float xr = (float)poly->vlist[curr_vert].x*cos_look[theta] - 
  121.                     (float)poly->vlist[curr_vert].y*sin_look[theta];
  122.     float yr = (float)poly->vlist[curr_vert].x*sin_look[theta] + 
  123.                     (float)poly->vlist[curr_vert].y*cos_look[theta];
  124.     // store result back
  125.     poly->vlist[curr_vert].x = xr;
  126.     poly->vlist[curr_vert].y = yr;
  127.     } // end for curr_vert
  128. // return success
  129. return(1);
  130. // end Rotate_Polygon2D
  131. int Draw_Filled_Polygon2D(POLYGON2D_PTR poly,UCHAR *vbuffer,int mempitch)
  132. {
  133.   int ydiff1,ydiff2,
  134.       xdiff1,xdiff2,
  135.       start,
  136.       length,
  137.       errorterm1,errorterm2,
  138.       offset1,offset2,
  139.       count1,count2,
  140.       xunit1,xunit2;
  141.   int edgecount = poly->num_vert-1;
  142.   int firstvert=0;
  143.   int min_y=poly->vlist[0].y;
  144.   
  145.   for (int index=1;index<poly->num_vert;index++)
  146.   {/*找到y轴坐标最小的顶点*/
  147.     
  148.     if ((poly->vlist[index].y)<min_y)
  149.     {
  150.       firstvert=index;
  151.       min_y=poly->vlist[index].y;
  152.     }
  153.   }
  154.   int startvert1=firstvert;
  155.   int startvert2=firstvert;
  156.   int xstart1=poly->vlist[startvert1].x+poly->x0;  //逆时针移动的初始位置
  157.   int ystart1=poly->vlist[startvert1].y+poly->y0;
  158.   int xstart2=poly->vlist[startvert2].x+poly->x0;  //顺时针移动的初始位置
  159.   int ystart2=poly->vlist[startvert2].y+poly->y0;
  160.   /*取得逆时针移动遇到的第1个顶点坐标*/
  161.   int endvert1=startvert1-1;
  162.   if (endvert1<0)
  163.     endvert1=poly->num_vert-1;
  164.   int xend1=poly->vlist[endvert1].x+poly->x0;
  165.   int yend1=poly->vlist[endvert1].y+poly->y0;
  166.   
  167.   /*取得顺时针移动遇到的第1个顶点坐标*/
  168.   int endvert2=startvert2+1;
  169.   if(endvert2==(poly->num_vert))
  170.     endvert2=0;
  171.   int xend2=poly->vlist[endvert2].x+poly->x0;
  172.   int yend2=poly->vlist[endvert2].y+poly->y0;
  173.   //绘制并填充多边形
  174.   while (edgecount>0)
  175.   {
  176.     offset1=mempitch*ystart1+xstart1; //左边缘偏移量
  177.     offset2=mempitch*ystart2+xstart2; //右边缘偏移量
  178.     
  179.     //初始化误差额
  180.     errorterm1=0;
  181.     errorterm2=0;
  182.     
  183.     //初始化左边与右边x&y方向的偏移差
  184.     if ((ydiff1=yend1-ystart1)<0)
  185.       ydiff1=-ydiff1;
  186.     if ((ydiff2=yend2-ystart2)<0)
  187.       ydiff2=-ydiff2;
  188.     if ((xdiff1=xend1-xstart1)<0)
  189.     {
  190.       xunit1=-1;
  191.       xdiff1=-xdiff1;
  192.     }
  193.     else
  194.     {
  195.       xunit1=1;
  196.     }
  197.     if ((xdiff2=xend2-xstart2)<0)
  198.     {
  199.       xunit2=-1;
  200.       xdiff2=-xdiff2;
  201.     }
  202.     else
  203.     {
  204.       xunit2=1;
  205.     }
  206.     
  207.     //选择使用哪种情况
  208.     if (xdiff1>ydiff1)
  209.     {/*|k1|<1*/
  210.       if (xdiff2>ydiff2)
  211.       {/*|k2|<1*/
  212.         count1=xdiff1;    //左边X的增量
  213.         count2=xdiff2;    //右边X的增量
  214.         while (count1&&count2)
  215.         {
  216.           while ((errorterm1<xdiff1)&&(count1>0))
  217.           {
  218.             if(count1--)
  219.             {
  220.               offset1+=xunit1;
  221.               xstart1+=xunit1;
  222.             }
  223.             errorterm1+=ydiff1;
  224.             if (errorterm1<xdiff1)
  225.             {
  226.               vbuffer[offset1]=(UCHAR)poly->color;
  227.             }
  228.           }
  229.           errorterm1-=xdiff1;
  230.           while ((errorterm2<xdiff2)&&(count2>0))
  231.           {
  232.             if (count2--)
  233.             {
  234.               offset2+=xunit2;
  235.               xstart2+=xunit2;
  236.             }
  237.             errorterm2+=ydiff2;
  238.             if (errorterm2<xdiff2)
  239.             {
  240.               vbuffer[offset2]=(UCHAR)poly->color;
  241.             }
  242.           }
  243.           errorterm2-=xdiff2;
  244.           length=offset2-offset1;
  245.           if (length<0)
  246.           {
  247.             length=-length;
  248.             start=offset2;
  249.           }
  250.           else
  251.             start=offset1;
  252.           for (int index=start;index<start+length+1;index++)
  253.           {
  254.             vbuffer[index]=(UCHAR)poly->color;
  255.           }
  256.           offset1+=mempitch;
  257.           ystart1++;
  258.           offset2+=mempitch;
  259.           ystart2++;
  260.         }
  261.       }
  262.       else
  263.       {/*|k2|>1*/
  264.         count1=xdiff1;
  265.         count2=ydiff2;
  266.         while (count1&&count2)
  267.         {
  268.           while ((errorterm1<xdiff1)&&(count1>0))
  269.           {
  270.             if(count1--)
  271.             {
  272.               offset1+=xunit1;
  273.               xstart1+=xunit1;
  274.             }
  275.             errorterm1+=ydiff1;
  276.             if (errorterm1<xdiff1)
  277.             {
  278.               vbuffer[offset1]=(UCHAR)poly->color;
  279.             }
  280.           }
  281.           errorterm1-=xdiff1;
  282.           
  283.           errorterm2+=xdiff2;
  284.           if (errorterm2>=ydiff2)
  285.           {
  286.             errorterm2-=ydiff2;
  287.             offset2+=xunit2;
  288.             xstart2+=xunit2;
  289.           }
  290.           count2--;
  291.           length=offset2-offset1;
  292.           if (length<0)
  293.           {
  294.             length=-length;
  295.             start=offset2;
  296.           }
  297.           else
  298.             start=offset1;
  299.           
  300.           for (int index=start;index<start+length+1;index++)
  301.           {
  302.             vbuffer[index]=(UCHAR)poly->color;
  303.           }
  304.           offset1+=mempitch;
  305.           ystart1++;
  306.           offset2+=mempitch;
  307.           ystart2++;
  308.         }
  309.       }
  310.     }
  311.     else
  312.     {/*|k1|>1*/
  313.       if (xdiff2>ydiff2)
  314.       {/*|k2|<1*/
  315.         count1=ydiff1;
  316.         count2=xdiff2;
  317.         while (count1&&count2)
  318.         {
  319.           errorterm1+=xdiff1;
  320.           if (errorterm1>=ydiff1)
  321.           {
  322.             errorterm1-=ydiff1;
  323.             offset1+=xunit1;
  324.             xstart1+=xunit1;
  325.           }
  326.           count1--;
  327.           
  328.           while ((errorterm2<xdiff2)&&(count2>0))
  329.           {
  330.             if (count2--)
  331.             {
  332.               offset2+=xunit2;
  333.               xstart2+=xunit2;
  334.             }
  335.             errorterm2+=ydiff2;
  336.             if (errorterm2<xdiff2)
  337.             {
  338.               vbuffer[offset2]=(UCHAR)poly->color;
  339.             }
  340.         
  341.           }
  342.           errorterm2-=xdiff2;
  343.           length=offset2-offset1;
  344.           if (length<0)
  345.           {
  346.             length=-length;
  347.             start=offset2;
  348.           }
  349.           else
  350.             start=offset1;
  351.           for (int index=start;index<start+length+1;index++)
  352.           {
  353.             vbuffer[index]=(UCHAR)poly->color;
  354.           }
  355.           offset1+=mempitch;
  356.           ystart1++;
  357.           offset2+=mempitch;
  358.           ystart2++;
  359.         }
  360.       }
  361.       else
  362.       {/*|k2|>1*/
  363.         
  364.         count1=ydiff1;
  365.         count2=ydiff2;
  366.         while (count1&&count2)
  367.         {
  368.           errorterm1+=xdiff1;
  369.           if (errorterm1>=ydiff1)
  370.           {
  371.             errorterm1-=ydiff1;
  372.             offset1+=xunit1;
  373.             xstart1+=xunit1;
  374.           }
  375.           count1--;
  376.           errorterm2+=xdiff2;
  377.           if (errorterm2>=ydiff2)
  378.           {
  379.             errorterm2-=ydiff2;
  380.             offset2+=xunit2;
  381.             xstart2+=xunit2;
  382.           }
  383.           count2--;
  384.           length=offset2-offset1;
  385.           if (length<0)
  386.           {
  387.             length=-length;
  388.             start=offset2;
  389.           }
  390.           else
  391.             start=offset1;
  392.           for (int index=start;index<start+length+1;index++)
  393.           {
  394.             vbuffer[index]=(UCHAR)poly->color;
  395.           }
  396.           offset1+=mempitch;
  397.           ystart1++;
  398.           offset2+=mempitch;
  399.           ystart2++;
  400.         }
  401.       }
  402.     }
  403.     if (!count1)
  404.     {/*首先到达逆时针顶点*/
  405.       --edgecount;
  406.       startvert1=endvert1;
  407.       --endvert1;
  408.       if (endvert1<0)
  409.         endvert1=poly->num_vert-1;
  410.       xend1=poly->vlist[endvert1].x+poly->x0;
  411.       yend1=poly->vlist[endvert1].y+poly->y0;
  412.     }
  413.     if (!count2)
  414.     {/*首先到达顺时针顶点*/
  415.       --edgecount;
  416.       startvert2=endvert2;
  417.       ++endvert2;
  418.       if(endvert2==(poly->num_vert))
  419.         endvert2=0;
  420.       xend2=poly->vlist[endvert2].x+poly->x0;
  421.       yend2=poly->vlist[endvert2].y+poly->y0;
  422.     }
  423.     
  424.   }
  425.   
  426.   return(1);
  427. }
  428. LRESULT CALLBACK WindowProc(HWND hwnd,
  429.                             UINT msg,
  430.                             WPARAM wparam,
  431.                             LPARAM lparam)
  432. {
  433. PAINTSTRUCT     ps;     
  434. HDC             hdc;    
  435.        
  436. switch(msg)
  437.     {   
  438.     case WM_CREATE: 
  439.         {
  440.         return(0);
  441.         } break;
  442.    
  443.     case WM_PAINT: 
  444.         {
  445.         hdc = BeginPaint(hwnd,&ps);  
  446.         
  447.         EndPaint(hwnd,&ps);
  448.         return(0);
  449.         } break;
  450.     case WM_DESTROY: 
  451.         {
  452.         PostQuitMessage(0);
  453.         return(0);
  454.         } break;
  455.     default:break;
  456.     }
  457. return (DefWindowProc(hwnd, msg, wparam, lparam));
  458. }
  459. int Game_Init(void *parms=NULL,int num_parms=0)
  460. {
  461.   if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpdd,IID_IDirectDraw7,NULL)))
  462.     return(0);
  463.   if(FAILED(lpdd->SetCooperativeLevel(main_window_handle,
  464.                                       DDSCL_FULLSCREEN|DDSCL_ALLOWMODEX|
  465.                                       DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT)))
  466.     return(0);
  467.   if(FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,0,0)))
  468.     return(0);
  469.   DDRAW_INIT_STRUCT(ddsd);
  470.   ddsd.dwFlags=DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
  471.   ddsd.dwBackBufferCount=1;
  472.   ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|
  473.                         DDSCAPS_FLIP;
  474.   
  475.   if(FAILED(lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)))
  476.     return(0);
  477.   ddsd.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
  478.   if(FAILED(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsback)))
  479.     return(0);
  480.   for (int color=1; color < 255; color++)
  481.     {
  482.     palette[color].peRed   = rand()%256;
  483.     palette[color].peGreen = rand()%256;
  484.     palette[color].peBlue  = rand()%256;
  485.     palette[color].peFlags = PC_NOCOLLAPSE;
  486.     } 
  487. palette[0].peRed     = 0;
  488. palette[0].peGreen   = 0;
  489. palette[0].peBlue    = 0;
  490. palette[0].peFlags   = PC_NOCOLLAPSE;
  491. palette[255].peRed   = 255;
  492. palette[255].peGreen = 255;
  493. palette[255].peBlue  = 255;
  494. palette[255].peFlags = PC_NOCOLLAPSE;
  495. palette[1].peRed     = 0;
  496. palette[1].peGreen   = 16;
  497. palette[1].peBlue    = 0;
  498. palette[1].peFlags   = PC_NOCOLLAPSE;
  499. if(FAILED(lpdd->CreatePalette(DDPCAPS_8BIT|DDPCAPS_ALLOW256|
  500.                               DDPCAPS_INITIALIZE,palette,&lpddpal,NULL)))
  501.     return(0);
  502. if(FAILED(lpddsprimary->SetPalette(lpddpal)))
  503.     return(0);
  504. DDraw_Fill_Surface(lpddsprimary,0);
  505. DDraw_Fill_Surface(lpddsback,0);
  506. //顺时针画出各点!!!
  507. VERTEX2DF object_vertices[4]={-100,-100,100,-100,100,100,-100,100};
  508. object.state=1;
  509. object.num_vert=4;
  510. object.color=1;
  511. object.xv=0;
  512. object.yv=0;
  513. object.x0=SCREEN_WIDTH/2;
  514. object.y0=SCREEN_HEIGHT/2;
  515. object.vlist=new VERTEX2DF[object.num_vert];
  516. for (int index=0;index<object.num_vert;index++)
  517.   object.vlist[index]=object_vertices[index];
  518. for (int ang=0;ang<360;ang++)
  519. {
  520.   float theta=(float)ang*PI/(float)180;
  521.   cos_look[ang]=cos(theta);
  522.   sin_look[ang]=sin(theta);
  523. }
  524. return(1);  
  525. }
  526. int Game_Main(void *parms = NULL, int num_parms = 0)
  527. {
  528.   static int green_inc=4;
  529.   if(window_closed)
  530.     return(0);
  531.   if(KEYDOWN(VK_ESCAPE))
  532.   {
  533.     PostMessage(main_window_handle,WM_CLOSE,0,0);
  534.     window_closed=1;
  535.   }
  536.   DDraw_Fill_Surface(lpddsback,0);
  537.   DDRAW_INIT_STRUCT(ddsd);
  538.   if(FAILED(lpddsback->Lock(NULL,&ddsd,DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR,NULL)))
  539.     return(0);
  540.   Draw_Filled_Polygon2D(object, (UCHAR *)ddsd.lpSurface, ddsd.lPitch);
  541.   Rotate_Polygon2D(object,5);
  542.   if (FAILED(lpddsback->Unlock(NULL)))
  543.    return(0);
  544.   Set_Palette_Entry(1, &palette[1]);
  545.   palette[1].peGreen+=green_inc;
  546.   if (palette[1].peGreen > 255 || palette[1].peGreen < 16)
  547.    {
  548.     green_inc=-green_inc;
  549.     palette[1].peGreen+=green_inc;    
  550.    } 
  551. while(FAILED(lpddsprimary->Flip(NULL,DDFLIP_WAIT)));
  552.   
  553. Sleep(33);
  554. return(1);
  555. }
  556. int Game_Shutdown(void *parms = NULL, int num_parms = 0)
  557. {/*清除资源*/
  558. if (lpddpal)
  559.    {
  560.    lpddpal->Release();
  561.    lpddpal = NULL;
  562.    }
  563. if(lpddsback)
  564.   {
  565.   lpddsback->Release();
  566.   lpddsback=NULL;
  567.   }
  568. if (lpddsprimary)
  569.    {
  570.    lpddsprimary->Release();
  571.    lpddsprimary = NULL;
  572.    } 
  573. if (lpdd)
  574.    {
  575.    lpdd->Release();
  576.    lpdd = NULL;
  577.    } 
  578. return(1);
  579. }
  580. int WINAPI WinMain( HINSTANCE hinstance,
  581.                     HINSTANCE hprevinstance,
  582.                     LPSTR lpcmdline,
  583.                     int ncmdshow)
  584. {
  585. WNDCLASSEX winclass; 
  586. HWND       hwnd;    
  587. MSG        msg;      
  588. //HDC        hdc;    
  589. winclass.cbSize         = sizeof(WNDCLASSEX);
  590. winclass.style          = CS_DBLCLKS | CS_OWNDC | 
  591.                           CS_HREDRAW | CS_VREDRAW;
  592. winclass.lpfnWndProc    = WindowProc;
  593. winclass.cbClsExtra     = 0;
  594. winclass.cbWndExtra     = 0;
  595. winclass.hInstance      = hinstance;
  596. winclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  597. winclass.hCursor        = LoadCursor(NULL, IDC_ARROW); 
  598. winclass.hbrBackground  = (HBRUSH)GetStockObject(BLACK_BRUSH);
  599. winclass.lpszMenuName   = NULL;
  600. winclass.lpszClassName  = WINDOW_CLASS_NAME;
  601. winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);
  602. hinstance_app = hinstance;
  603. if (!RegisterClassEx(&winclass))
  604.     return(0);
  605. if (!(hwnd = CreateWindowEx(NULL,                  
  606.                             WINDOW_CLASS_NAME,    
  607.                             "DirectDraw 8-Bit Line Drawing Demo"
  608.                             WS_POPUP | WS_VISIBLE,
  609.                             0,0,    
  610.                             SCREEN_WIDTH,SCREEN_HEIGHT,  
  611.                             NULL,     
  612.                             NULL,  
  613.                             hinstance,
  614.                             NULL))) 
  615. return(0);
  616. main_window_handle = hwnd;
  617. Game_Init();
  618. while(TRUE)
  619.     {
  620.   
  621.     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  622.        { 
  623.        if (msg.message == WM_QUIT)
  624.            break;
  625.     
  626.        TranslateMessage(&msg);
  627.        DispatchMessage(&msg);
  628.        }
  629.      Game_Main();
  630.        
  631.     } 
  632. Game_Shutdown();
  633. return(msg.wParam);
  634. }

一定要注意调用光栅化函数时传入的参数为顺时针在笛卡尔坐标系中设置的各多边形坐标!!!

抱歉!评论已关闭.