您的位置:首页 > 其它

2013年8月19日星期一(demo7_16,色彩旋转)

2013-08-19 17:06 375 查看
这一节也并不难,上周在另外一个机子上调试过了,原理是以相邻的色彩项或者寄存器的集合作为参数,然后以循环的方式移动它们的一种处理,这样使物体看起来像在移动,或者模拟流水。

我觉得原理很重要,从色彩color[c1]旋转到色彩color[c2];
Temp = color[c1];
For( int index = c1; index < c2; index ++ )
Color[c1] = color[c1+1];
Color[index] = temp;

设定一个函数
int Rotate_Colors(
int start_index, int end_index )
{
int colors = end_index - start_index;
PALETTEENTRY work_pal[MAX_COLORS_PALETTE];
lpddpal->GetEntries( 0, start_index, colors, work_pal );
lpddpal->SetEntries( 0, start_index + 1,colors - 1, work_pal );
lpddpal->SetEntries( 0, start_index, 1, & work_pal[colors - 1] );
lpddpal->GetEntries( 0, 0, MAX_COLORS_PALETTE, palette );

return ( 1 );
}
在game_init()中,加载位图兵设置调色板,
{
。。。。。。
// load the 8-bit image
if (!Load_Bitmap_File(&bitmap,"waterfall8.bmp"))
return(0);

// load it's palette into directdraw
if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))
return(0);

// clean the surface
DDraw_Fill_Surface(lpddsprimary,0);



}
在Game_Main()中,旋转133至143的调色板项。
{
。。。。。。。。。。。
Rotate_Colors( 133, 143 );
}

下一步根据DEMO编写C++引擎,
加入一个成员函数
int Rotate_Colors(
int start_index, int end_index );
成员变量的实现如下:
int DDRAW_Interface::Rotate_Colors(
int start_index, int end_index )
{
int colors = end_index - start_index;
PALETTEENTRY work_pal[MAX_COLORS_PALETTE];
m_lpddpal->GetEntries( 0, start_index, colors, work_pal );
m_lpddpal->SetEntries( 0, start_index + 1,colors - 1, work_pal );
m_lpddpal->SetEntries( 0, start_index, 1, & work_pal[colors - 1] );
m_lpddpal->GetEntries( 0, 0, MAX_COLORS_PALETTE, palette );

return ( 1 );


}

在game_main()中

int Game_Main(void *parms = NULL,
int num_parms = 0)
{
// this is the main loop of the game, do all your processing
// here

// make sure this isn't executed again
if (window_closed)
return(0);

// for now test if user is hitting ESC and send WM_CLOSE
if (KEYDOWN(VK_ESCAPE))
{
PostMessage(main_window_handle,WM_CLOSE,0,0);
window_closed = 1;
} // end if

ddraw->DDraw_Lock_Primary_Surface();
// get video pointer to primary surfce
UCHAR *primary_buffer = (UCHAR *)ddraw->getPrimarybuffer();


// test if memory is linear
if (ddraw->getPrimarylpitch() == SCREEN_WIDTH)
{
// copy memory from double buffer to primary buffer
memcpy((void *)primary_buffer, (void *)bitmap.buffer, SCREEN_WIDTH*SCREEN_HEIGHT);
} // end if
else
{ // non-linear

// make copy of source and destination addresses
UCHAR *dest_ptr = primary_buffer;
UCHAR *src_ptr = bitmap.buffer;

// memory is non-linear, copy line by line
for (int y=0; y < SCREEN_HEIGHT; y++)
{
// copy line
memcpy((void *)dest_ptr, (void *)src_ptr, SCREEN_WIDTH);

// advance pointers to next line
dest_ptr+=ddraw->getPrimarylpitch();
src_ptr +=SCREEN_WIDTH;
} // end for

} // end else
ddraw->DDraw_Unlock_Primary_Surface();
// animate the lights
ddraw->Rotate_Colors( 133, 143 );

// wait a sec
Sleep(33);

// do nothing -- look at pretty picture

// return success or failure or your own return code here
return(1);

} // end Game_Main

在game_init()中

int Game_Init(void *parms = NULL,
int num_parms = 0)
{
// this is called once after the initial window is created and
// before the main event loop is entered, do all your initialization
// here

// load the 8-bit image
if (!ddraw->Load_Bitmap_File(&bitmap,"waterfall8.bmp"))
return(0);

// load it's palette into directdraw
if (FAILED(ddraw->getlpddpal()->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))
return(0);



// return success or failure or your own return code here
return(1);
}

/////////////////////////////////////////////////////////////

结果OK。
现在,再查看下T3DLIB中的东西。把调色板的几个其它函数加上去吧。

//改变调色板某一颜色
int DDRAW_Interface::Set_Palette_Entry(
int color_index, LPPALETTEENTRY color )
{
m_lpddpal->SetEntries( 0, color_index, 1, color );
memcpy( & palette[color_index], color,
sizeof( PALETTEENTRY ) );
return ( 1 );
}
//从当前调色板获得一个调色板项
int DDRAW_Interface::Get_Palette_Entry(
int color_index, LPPALETTEENTRY color )
{
memcpy( color, & palette[color_index],
sizeof( PALETTEENTRY ) );
}

//将发送的调色板数据保存在磁盘中的一个ASCII文件中。
int DDRAW_Interface::Save_Palette_To_File(
char * filename, LPPALETTEENTRY palette )
{
FILE * fp_file;
if( ( fp_file = fopen( filename,
"w" ) ) == NULL )
return ( 0 );

for(
int index = 0; index < MAX_COLORS_PALETTE; index++ )
{
fprintf( fp_file, "\n%d %d %d %d", palette[index].peRed,

palette[index].peGreen,
palette[index].peBlue,
palette[index].peFlags );
}
fclose( fp_file );
return ( 1 );
}

//将硬件调色板扫描到sav_palette中,一边进行操作或将它保存到磁盘
int DDRAW_Interface::Set_Palette( LPPALETTEENTRY sav_palette )
{
memcpy( sav_palette, palette, MAX_COLORS_PALETTE *
sizeof( PALETTEENTRY ) );
return ( 1 );
}

,OK,调色板的弄完了,先到这里吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: