OK,现在马不停蹄,结束这个第7章,拖延的时间真长,有6个月了,汗。。。
这个是上次的应用,加上逻辑判断如何画点,并用GetWindowRect()是客户区,实际上这不对,应该是GetClientRect(),果然不对,只能是说把图画上了。
代码如下
DDSURFACEDESC2 ddsd;
RECT client;
GetWindowRect( main_window_handle, & client );
DDRAW_INIT_STRUCT( ddsd );
lpddsprimary->Lock( NULL, & ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL );
UCHAR * primary_buffer = ( UCHAR * ) ddsd.lpSurface;
if( pixel_format == 32 )
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
DWORD color = _RGB32BIT( 0, rand() % 256, rand() % 256, rand() % 256 );
* ( ( DWORD * ) ( primary_buffer + x * 4 + y * ddsd.lPitch ) ) = color;
}
}
else
if( pixel_format == 24 )
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
( ( primary_buffer + x * 3 + y * ddsd.lPitch ) )[0] = rand() % 256 ;
( ( primary_buffer + x * 3 + y * ddsd.lPitch ) )[1] = rand() % 256 ;
( ( primary_buffer + x * 3 + y * ddsd.lPitch ) )[2] = rand() % 256 ;
}
}
else
if( pixel_format == 16 )
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
USHORT color = _RGB32BIT( 0, rand() % 256, rand() % 256, rand() % 256 );
* ( ( USHORT * ) ( primary_buffer + x * 4 + y * ddsd.lPitch ) ) = color;
}
}
else
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
UCHAR color = rand() % 256;
primary_buffer[x + y * ddsd.lPitch] = color;
}
}
lpddsprimary->Unlock( NULL );
Sleep( 1 );
结果如下图所示
![](https://img-blog.csdn.net/20130826211948578?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGlyZWN0eDNkX2JlZ2lubmVy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
但是当封装后,
DDSURFACEDESC2 ddsd;
RECT client;
GetWindowRect( main_window_handle, & client );
DDRAW_INIT_STRUCT( ddsd );
ddraw->getPrimarySurface()->Lock( NULL, & ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL );
UCHAR * primary_buffer = ( UCHAR * ) ddsd.lpSurface;
if( ddraw->getPixelFormat() == 32 )
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
DWORD color = _RGB32BIT( 0, rand() % 256, rand() % 256, rand() % 256 );
* ( ( DWORD * ) ( primary_buffer + x * 4 + y * ddsd.lPitch ) ) = color;
}
}
else
if( ddraw->getPixelFormat() == 24 )
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
( ( primary_buffer + x * 3 + y * ddsd.lPitch ) )[0] = rand() % 256 ;
( ( primary_buffer + x * 3 + y * ddsd.lPitch ) )[1] = rand() % 256 ;
( ( primary_buffer + x * 3 + y * ddsd.lPitch ) )[2] = rand() % 256 ;
}
}
else
if( ddraw->getPixelFormat() == 16 )
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
USHORT color = _RGB32BIT( 0, rand() % 256, rand() % 256, rand() % 256 );
* ( ( USHORT * ) ( primary_buffer + x * 4 + y * ddsd.lPitch ) ) = color;
}
}
else
{
for( int index = 0; index < 10; index ++ )
{
int x = rand() % ( client.right - client.left ) + client.left;
int y = rand() % ( client.bottom - client.top ) + client.top;
UCHAR color = rand() % 256;
primary_buffer[x + y * ddsd.lPitch] = color;
}
}
ddraw->getPrimarySurface()->Unlock( NULL );
Sleep( 1 );
8位模式时却不对,只占了左侧一部分。
![](https://img-blog.csdn.net/20130826212055500?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGlyZWN0eDNkX2JlZ2lubmVy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
但是改为全屏,却是可以的,邪门了
![](https://img-blog.csdn.net/20130826212158796?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGlyZWN0eDNkX2JlZ2lubmVy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
接下来,就是查找实际的客户区,或许与这个有关系。
这里在T3DCONSOLE2中的main()函数里面了。
加上以下代码后,窗口根本显示不出来了,废了,
if( WINDOWED_APP )
{
RECT window_rect = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
AdjustWindowRectEx( & window_rect, GetWindowStyle( main_window_handle ), GetMenu( main_window_handle ) != NULL,
GetWindowExStyle( main_window_handle ) );
// window_client_x0 = -window_rect.left;
// window_client_y0 = -window_rect.top;
MoveWindow( main_window_handle, CW_USEDEFAULT, CW_USEDEFAULT, window_rect.right - window_rect.left,
window_rect.bottom - window_rect.top, TRUE );
ShowWindow( main_window_handle, SW_SHOW );
}
加了个窗口的裁剪器 LPDIRECTDRAWCLIPPER m_lpddclipperwin;
if( m_bWindowed == true )
{
m_lpdd->CreateClipper( 0, & m_lpddclipperwin, NULL );
m_lpddclipperwin->SetHWnd( 0, m_hWnd );
m_lpddsprimary->SetClipper( m_lpddclipperwin );
}
终于OK了,因为书上的代码不对,而光盘上的代码才是正确的
![](https://img-blog.csdn.net/20130826212247390?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZGlyZWN0eDNkX2JlZ2lubmVy/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
正确代码如下:
// resize the window so that client is really width x height
if (WINDOWED_APP)
{
// now resize the window, so the client area is the actual size requested
// since there may be borders and controls if this is going to be a windowed app
// if the app is not windowed then it won't matter
RECT window_rect = {0,0,WINDOW_WIDTH-1,WINDOW_HEIGHT-1};
// make the call to adjust window_rect
AdjustWindowRectEx(&window_rect,
GetWindowStyle(main_window_handle),
GetMenu(main_window_handle) != NULL,
GetWindowExStyle(main_window_handle));
// save the global client offsets, they are needed in DDraw_Flip()
window_client_x0 = -window_rect.left;
window_client_y0 = -window_rect.top;
// now resize the window with a call to MoveWindow()
MoveWindow(main_window_handle,
0, // x position
0, // y position
window_rect.right - window_rect.left, // width
window_rect.bottom - window_rect.top, // height
FALSE);
// show the window, so there's no garbage on first render
ShowWindow(main_window_handle, SW_SHOW);
} // end if windowed
不禁感慨,窗口确实要小心测试。