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

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(int x, int y, int red, int green, int blue, USHORT * video_buffer, int lpitch16 )

{

     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 (int index=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里面函数还很复杂,涉及面较多,可以逐次进行,不急不忙。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值