7-1,16色像素绘制(2013年2月25日-26日)

来源:互联网 发布:万达电商 网络总裁不 编辑:程序博客网 时间:2024/05/17 21:42

16位就是RGB代替调色板,分为R5G6B5R5G5B5两种,DEMO运行结果如下

首先设置显示模式

Lpdd->SetDisplayMode( 640, 480, 16, 0, 0 );
 绘制像素分四个步骤。

1,锁定表面

2,建立16RGB

3,写像素

4,解锁主表面

 定义16位的两种格式

#define _RGB16BIT555( r,g, b ) ( ( b & 31) + ( ( g & 31) << 5 ) + ( r & 31 ) << 10 )

#define _RGB16BIT565( r,g, b ) ( ( b & 31) + ( ( g & 63) << 5 ) + ( r & 31 ) << 11 )

根据位数不同,决定是否用调色板格式,

在窗口模式下,

if ( m_dwBPP == 8)

              {

                   for ( int index = 0; index < 10; index ++)

                   {

                       palette[index].peFlags =palette[index+246].peFlags =PC_EXPLICIT;

 

                   }

                   // create the palette object

                   if (FAILED(m_lpdd->CreatePalette(DDPCAPS_8BIT |DDPCAPS_INITIALIZE,

                       palette,&m_lpddpal,NULL)))

                   {

                       // error

                       return(0);

                   } // end if

              }

非窗口模式

     if ( m_dwBPP == 8)

         {

              if (FAILED(m_lpdd->CreatePalette(DDPCAPS_8BIT |DDPCAPS_ALLOW256 |

                   DDPCAPS_INITIALIZE,

                   palette,&m_lpddpal,NULL)))

              {

                   // error

                   return(0);

              } // end if

         }

     在16位格式中,绘制

void DDRAW_Interface::Plot_Pixel16(intx, int y, int red,int green, int blue, USHORT *video_buffer, intlpitch16 )

{

     USHORT pixel = _RGB16BIT565( red, green, blue );

 

     video_buffer[x+y*lpitch16 ]  = pixel;

 

}

并且返回位数值,来判断如何绘制

DWORD DDRAW_Interface::getdwBPP()

{

     return m_dwBPP;

}

在绘制中,

switch( ddraw->getdwBPP())

     {

     case 8:

         {

              int mempitch        = (int)ddsd.lPitch;

              UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

 

              // plot 1000 random pixels with random colors on the

              // primary surface, they will be instantly visible

              for (intindex=0; index < 1000;index++)

              {

                   // select random position and color for 640x480x8

                   UCHAR color = rand()%256;

                   int x = rand()%640;

                   int y = rand()%480;

 

                   // plot the pixel

                   video_buffer[x+y*mempitch] =color;       

 

              } // end for index

 

         }

         break;

 

     case 16:

         {

              int lpitch16 = ( int ) ( ddsd.lPitch >> 1 );

              USHORT * video_buffer = ( USHORT * )ddsd.lpSurface;

 

              for ( int index = 0; index < 1000; index ++ )

              {

                   int red = rand() % 256;

                   int green = rand() % 256;

                   int blue = rand() % 256;

                   int x = rand() % 640;

                   int y = rand() % 480;

 

                   ddraw->Plot_Pixel16(x, y, red, green, blue, video_buffer, lpitch16 );

              }

         }

         break;

 

     default:

         break;

 

     }

再仔细分析下,按照T3DLIB引擎,设置一个锁定主表面和解锁主表面,但是牵涉到8位和16位的绘制,故暂时不进行在MAIN()函数里调用,可以先调用UNLOCK()

函数原型如下:

void DDRAW_Interface::DDraw_Lock_PrimarySurface()

{

     DDSURFACEDESC2        ddsd;   

     DDRAW_INIT_STRUCT( ddsd );

     m_lpddsprimary->Lock(NULL, & ddsd,DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR,NULL );

}

 

void DDRAW_Interface::DDraw_Unlock_PrimarySurface()

{

     m_lpddsprimary->Unlock(NULL );

}

MAIN函数里,

 

     ddraw->DDraw_Unlock_PrimarySurface();

备注:

T3DLIB里面函数还很复杂,涉及面较多,可以逐次进行,不急不忙。

原创粉丝点击