只需要在ddraw中找到一个兼容DC,就可以使用,使用完之后再释放。
函数实现为
int Draw_Text_GDI( char * text, int x, int y, COLORREF color, LPDIRECTDRAWSURFACE7 lpdds )
{
HDC xdc;
lpdds->GetDC( & xdc );
SetTextColor( xdc, color );
SetBkMode( xdc, TRANSPARENT );
TextOut( xdc, x, y, text, strlen( text ) );
lpdds->ReleaseDC( xdc );
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
int x = rand() % SCREEN_WIDTH;
int y = rand() % SCREEN_HEIGHT;
Draw_Text_GDI( "hello",x, y, RGB( rand() % 256, rand() % 256, rand() % 256 ), lpddsprimary );
// 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
开始封装,加上成员函数
int DDRAW_Interface::Draw_Text_GDI(char * text, int x,int y, COLORREF color, LPDIRECTDRAWSURFACE7 lpdds )
{
HDC xdc;
lpdds->GetDC( & xdc );
SetTextColor( xdc, color );
SetBkMode( xdc, TRANSPARENT );
TextOut( xdc, x, y, text, strlen( text ) );
lpdds->ReleaseDC( xdc );
return 1;
}但是出来后都是白色,在game_main()中改为
ddraw->Draw_Text_GDI( "hello",x, y, RGB( 100,200, 130 ));也是白色的,奇怪了。
备注:
后来发现书上说,RGB是24位结构,于是在引擎中,改了#define SCREEN_BPP 16或者24,颜色正常了,分辨率还得调一下,不过这倒是不关键了,