游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3278|回复: 4

DirectDraw窗口绘制的问题。【求助】

[复制链接]

2

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2006-4-20 11:22:00 | 显示全部楼层 |阅读模式
是《windows游戏编程大师技巧》上的一个例子,在窗口中绘制象素。其中的一段代码如下:

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

DDSURFACEDESC2 ddsd;    // directdraw surface description
RECT           client;  // used to hold client rectangle

// 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


// get the window's client rectangle in screen coordinates
GetWindowRect(main_window_handle, &client);

// initialize structure
DDRAW_INIT_STRUCT(ddsd);

// lock the primary surface
lpddsprimary->Lock(NULL,&ddsd,
              DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

// get video pointer to primary surface
// cast to VOID * since we don't know what we are
// dealing with yet
UCHAR *primary_buffer = (UCHAR *)ddsd.lpSurface;      

// what is the color depth?
if (pixel_format == 32)
   {
    // draw 10 random pixels in 32 bit mode
    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;
        } // end for index
    } // end if 24 bit

else
if (pixel_format == 24)
   {
    // draw 10 random pixels in 24 bit mode (very rare???)
    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;
        } // end for index
    } // end if 24 bit
else

if (pixel_format == 16)
    {
    // draw 10 random pixels in 16 bit mode
    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 = _RGB16BIT565(rand()%256, rand()%256, rand()%256);
        *((USHORT *)(primary_buffer + x*2 + y*ddsd.lPitch)) = color;
        } // end for index
    } // end if 16 bit
else
    {// assume 8 bits per pixel
    // draw 10 random pixels in 8 bit mode
    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;
        } // end for index
    } // end else

// unlock primary buffer
if (FAILED(lpddsprimary->Unlock(NULL)))
   return(0);


// wait a sec
Sleep(1);

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

} // end Game_Main

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

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

DDPIXELFORMAT ddpixelformat; // hold the pixel format

// create IDirectDraw interface 7.0 object and test for error
if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
   return(0);

// set cooperation to full screen
if (FAILED(lpdd->SetCooperativeLevel(main_window_handle, DDSCL_NORMAL)))
   return(0);

// clear ddsd and set size
DDRAW_INIT_STRUCT(ddsd);

// enable valid fields
ddsd.dwFlags = DDSD_CAPS;

// request primary surface
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

// create the primary surface
if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
   return(0);

// get pixel format

// clean out the structure and set it up
DDRAW_INIT_STRUCT(ddpixelformat);

// get the pixel format
lpddsprimary->GetPixelFormat(&ddpixelformat);

// set global pixel format
pixel_format = ddpixelformat.dwRGBBitCount;

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

} // end Game_Init


请问// get the window's client rectangle in screen coordinates
GetWindowRect(main_window_handle, &client);
是如何将client和DD表面关联的,我换成GetClientRect()却无法做出同样的结果,为什么?

18

主题

573

帖子

573

积分

高级会员

Rank: 4

积分
573
发表于 2006-4-20 14:03:00 | 显示全部楼层

Re:DirectDraw窗口绘制的问题。【求助】

我还是不太明白你的”关联“是什么意思。我来解释一下吧!
程序工作在窗口模式下时,整个的桌面的大小就是你的PrimarySurface的大小,使用GetWindowRect就是获得你的窗口在桌面上的位置(当然,相对于桌面来说),只是在绘图时需要参考一下你的窗口的位置,再画在窗口所在位置的PrimarySurface的相应位置上。如果使用GetClientRect,你所得到的只是窗口的Client区域的Rect大小,而不是相对于屏幕的大小,OK?
还是仔细看看程序吧!就会明白的!

86

主题

2251

帖子

2386

积分

金牌会员

Rank: 6Rank: 6

积分
2386
QQ
发表于 2006-4-20 14:15:00 | 显示全部楼层

Re:DirectDraw窗口绘制的问题。【求助】

GetWindowRect()   得到的是在屏幕坐标系下的RECT;(即以屏幕左上角为原点)
GetClientRect()   得到的是在客户区坐标系下的RECT;   (即以所在窗口左上角为原点)
ScreenToClient()   就是把屏幕坐标系下的RECT坐标转换为客户区坐标系下的RECT坐标。

GetWindowRect和DD没太大关系吧,关联是什么意思?

2

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2006-4-20 18:14:00 | 显示全部楼层

Re: Re:DirectDraw窗口绘制的问题。【求助】

谢谢楼上两位大哥啦。ORZ

18

主题

47

帖子

49

积分

注册会员

Rank: 2

积分
49
发表于 2006-4-23 12:38:00 | 显示全部楼层

Re:DirectDraw窗口绘制的问题。【求助】

谁能告诉我《windows游戏编程大师技巧》的光盘里的源代码怎么运行啊,我用VC++6.0打开后不能运行!还有就是这些代码都编写到那里进去啊??我是个初学者!!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-24 07:04

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表