游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3090|回复: 8

DirectDraw 初级问题

[复制链接]

1

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2004-7-20 13:24:00 | 显示全部楼层 |阅读模式
我编译的很简单的入门级别的  DirectDraw程序

在MFC下 使用IDirectDraw7  建立一个简单的换页链

将一副位图载入到备用缓冲     再BltFast到  后台缓冲

再Flip到主页面      

为什么我的程序只能运行1次(在重启电脑后)     再运行就 出错了
我明明都释放了啊  

请教各位高手啊

16

主题

158

帖子

168

积分

注册会员

Rank: 2

积分
168
发表于 2004-7-20 13:52:00 | 显示全部楼层

Re:DirectDraw 初级问题

MFC下的?

3

主题

17

帖子

17

积分

新手上路

Rank: 1

积分
17
发表于 2004-7-20 19:05:00 | 显示全部楼层

Re:DirectDraw 初级问题

关键的程序代码是?

32

主题

377

帖子

378

积分

中级会员

Rank: 3Rank: 3

积分
378
发表于 2004-7-20 20:41:00 | 显示全部楼层

Re:DirectDraw 初级问题

贴出来看看吧

4

主题

178

帖子

180

积分

注册会员

Rank: 2

积分
180
发表于 2004-7-20 21:01:00 | 显示全部楼层

Re:DirectDraw 初级问题

MFC下的我也写过,很烦的,经常出错,还是在地层下写,错了,知道在什么地方改.MFC错了,大家都没法啊.

1

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2004-7-20 21:48:00 | 显示全部楼层

Re:DirectDraw 初级问题

BOOL CMainFrame:rawInitial()
{//初始化DirectDraw
       
        //**Initialize the OLE/COM library
        if(FAILED(CoInitialize(NULL)))
        {
              Msg("OLE/COM library initial failed err");
              return false;
        }
        //create a direct draw object and interface
        HRESULT result=CoCreateInstance(CLSID_DirectDraw,NULL,CLSCTX_ALL,
                                                                IID_IDirectDraw7,(void**)&m_pIDraw);
        //CoCreateInstance返回一个指向新的DirectDraw对象的IDirectDraw7接口的指针
        if(result!=DD_OK)
    {
        Msg("CoCreateInstance failed err=%d",result);
        return FALSE;
    }
       
        result=m_pIDraw->Initialize((GUID*)NULL);
        if(result!=DD_OK)
        {
        Msg("DDraw initail failed err=%d",result);
        return FALSE;
    }       
       
    result=m_pIDraw->SetCooperativeLevel(m_hWnd,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN|DDSCL_ALLOWREBOOT);
    if(result!=DD_OK)
    {
        Msg("SetCooperativeLevel failed err=%d",result);
        return FALSE;
    }
   
       
        result = m_pIDraw->SetDisplayMode(640,480,8,0,0);//设定屏幕显示模式               
                         
   
    if(result!=DD_OK)
    {
        Msg("SetDisplayMode failed err=%d",result);
        return FALSE;
    }
   

   //check capabilites
    DDCAPS ddcaps;
    ddcaps.dwSize=sizeof(DDCAPS);
    result=m_pIDraw->GetCaps(&ddcaps,NULL);
    if(result!=DD_OK)
    {
        Msg("GetCaps failed err=%d",result);
        return FALSE;
    }
    if(ddcaps.dwCaps&DDCAPS_NOHARDWARE)
    {
        Msg("No hardware support at all");
    }
       
        

   DDSURFACEDESC2 ddsd;
    ::ZeroMemory(&ddsd,sizeof(DDSURFACEDESC2));
    ddsd.dwSize=sizeof(DDSURFACEDESC2);
    ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
    ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
    ddsd.dwBackBufferCount=1;        //简单换页链
    result=m_pIDraw->CreateSurface(&ddsd,&m_pIMainSurface,NULL);  //前台主页面
    if(result!=DD_OK)
    {
        Msg("Create MainSurface failed err=%d",result);
        return FALSE;
    }

   

    DDSCAPS2 ddscaps;       
        ::ZeroMemory(&ddscaps,sizeof(DDSCAPS2));
    ddscaps.dwCaps=DDSCAPS_BACKBUFFER;
    result=m_pIMainSurface->GetAttachedSurface(&ddscaps,&m_pIFlipSurface);  //后台缓冲
    if(result!=DD_OK)
    {
        Msg("GetAttachedsurface failed err=%d",result);
        return FALSE;
    }
       

        //create memorySurface to load bitmap
        //should be in Physical Mem not in VM
        ::ZeroMemory(&ddsd,sizeof(DDSURFACEDESC2));
        ddsd.dwSize=sizeof(ddsd);
        ddsd.dwFlags=DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH;
        ddsd.ddsCaps.dwCaps=DDSCAPS_OFFSCREENPLAIN;        ddsd.dwWidth=256;
        ddsd.dwHeight=256;//图片的大小
        result=m_pIDraw->CreateSurface(&ddsd,&m_pIMemorySurface,NULL);        //后备页面
        if(result!=DD_OK)
    {
        Msg("Create MemorySurface failed err=%d",result);
        return FALSE;
    }
               
    return TRUE;
}

1

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2004-7-20 21:53:00 | 显示全部楼层

Re: DirectDraw 初级问题

HRESULT CMainFrame::BitmapToMemory()
{
        HRESULT                result;
       
        CBitmap                bitmap;       
        CBitmap                *pOldImageBMP;                   
        CDC                        srcDC,destDC;

        //faint why always fail!  main Problem maybe here               
       
        result=m_pIMemorySurface->Restore();//这里为调试用 其实没用
        if (result != DD_OK)
        {
                Msg("memory restore error %d",result);
                return result;
        }
       
        result=m_pIMemorySurface->GetDC(&destDC.m_hDC);
        //这句代码在第二次运行时就出错了
       
        if (result != DD_OK)
        {//为调试用   result一般为-200205327
                       
                if(result==DDERR_DCALREADYCREATED)
                        MessageBox("DC have not already created");
                else if(result==DDERR_GENERIC)
                        MessageBox("generic error");
                else if(result==DDERR_INVALIDOBJECT)
                        MessageBox("invalid object");
                else if(result==DDERR_INVALIDPARAMS )
                        MessageBox("invalid params");
                else if(result==DDERR_INVALIDSURFACETYPE )
                        MessageBox("invalid surface type");
                else if(result==DDERR_SURFACELOST )
                        MessageBox("suface lost");
                else if(result==DDERR_UNSUPPORTED )
                        MessageBox("unsupported");
                else if(result==DDERR_WASSTILLDRAWING )
                        MessageBox("was still drawing");
                else
                {
                        Msg("strange faint %d",result);
               
                }
                return result;  
        }


                
        srcDC.CreateCompatibleDC(&destDC);
        bitmap.LoadBitmap(IDB_BITMAP);//位图资源
       
        pOldImageBMP = srcDC.SelectObject(&bitmap);       
        BitBlt(destDC.m_hDC, 0, 0, 256, 256, srcDC.m_hDC, 0, 0, SRCCOPY);
        srcDC.SelectObject(pOldImageBMP);

         

        // 清理、恢复
        m_pIMemorySurface->ReleaseDC(destDC.m_hDC);
        bitmap.DeleteObject();
       
        return DD_OK;
}abc

1

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2004-7-20 22:00:00 | 显示全部楼层

Re: DirectDraw 初级问题

全部文件(工作目录)在附件中

多谢各位大侠帮忙啊 [em13] [em13] [em13]

sf_200472022045.zip

44.56 KB, 下载次数:

0

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2004-7-21 01:27:00 | 显示全部楼层

Re: DirectDraw 初级问题

你是不是按照某书上的例题写的??如果是的,请你在仔细看看书,不要太急,初学都比较急,祝你成功!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-4 00:21

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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