|
Posted on 2008-09-26 17:51 没画完的画 阅读(1535) 评论(1) 编辑 收藏 引用 所属分类: DirectX
第1集中,DirectDraw 画出来的界面是全屏的,想把它改成窗口的
我把原先在调用 SetCooperativeLevel() 时 DDSCL_FULLSCREEN 参数改成 DDSCL_NORMAL
if (FAILED(lpDDraw->SetCooperativeLevel(g_hwnd, DDSCL_NORMAL )))
结果后面的
CreateSurface() 就调用失败了,返回的错误是 DDERR_INVALIDCAPS
Google 了一下,意外地找到几个URL
另外也找到了答案,代码改动如下:
/**//** * 设置参数 */ if (FAILED(lpDDraw->SetCooperativeLevel(g_hwnd, DDSCL_NORMAL ))) { MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"), TEXT("Wrong!"), MB_OK); return(0); }
/**//** * 创建一个表层 */ DDSURFACEDESC ddsd; LPDIRECTDRAWSURFACE lpDDSPrimary; ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS ; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ddsd.dwBackBufferCount = 1; ddrval = lpDDraw->CreateSurface(&ddsd, &lpDDSPrimary, NULL); if (FAILED(ddrval)) { MessageBox(NULL, "Failed to CreateSurface", NULL, MB_OK); return FALSE; }
全部代码:
/**//** * 创建一个 DirectDraw 对象 */ LPDIRECTDRAW _lpDD = NULL; HRESULT ddrval = DirectDrawCreate(NULL, &_lpDD, NULL); if (FAILED(ddrval)) { MessageBox(NULL, "Failed to DirectDrawCreate", NULL, MB_OK); return FALSE; } LPDIRECTDRAW lpDDraw = NULL; if(FAILED(_lpDD->QueryInterface(IID_IDirectDraw, (LPVOID *)&lpDDraw))) { MessageBox(NULL,TEXT("DirectDraw QueryInterface error!"), TEXT("Wrong!"), MB_OK); return(0); } /**//** * 设置执行模式 */ if (FAILED(lpDDraw->SetCooperativeLevel(g_hwnd, DDSCL_NORMAL ))) { MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"), TEXT("Wrong!"), MB_OK); return(0); } /**//** * 创建一个表层 */ DDSURFACEDESC ddsd; LPDIRECTDRAWSURFACE lpDDSPrimary; ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS ; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ddsd.dwBackBufferCount = 1; ddrval = lpDDraw->CreateSurface(&ddsd, &lpDDSPrimary, NULL); if (FAILED(ddrval)) { MessageBox(NULL, "Failed to CreateSurface", NULL, MB_OK); return FALSE; } HDC hdc; if(lpDDSPrimary->GetDC(&hdc) == DD_OK) { SetBkColor(hdc, RGB(0,0,255)); SetTextColor( hdc,RGB(255,255,0 ) ); TextOut( hdc, 0, 0, "sxFrontMsg", lstrlen("szFrontMsg")); lpDDSPrimary->ReleaseDC(hdc); }
*-*!!!!
虽然不像之前全屏黑色,这次是“窗口模式”,DDSCL_NORMAL 或 DDSCL_FULLSCREEN 跟调用 CreateSurface() 时的参数有联系,
但怎么 TextOut 还是在屏幕的左上角?????
听说有一个 Clipper 的对象,就是一个“剪切区域”,通过这个对象可以防止画图画出指定的范围
修改代码如下:
/**//** * 设置执行模式 */ if (FAILED(lpDDraw->SetCooperativeLevel(g_hwnd, DDSCL_NORMAL ))) { MessageBox(NULL,TEXT("DirectDraw SetCooperativeLevel error!"), TEXT("Wrong!"), MB_OK); return(0); } /**//** * 创建一个表层 */ DDSURFACEDESC ddsd; LPDIRECTDRAWSURFACE lpDDSPrimary; ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS ; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; ddsd.dwBackBufferCount = 1; ddrval = lpDDraw->CreateSurface(&ddsd, &lpDDSPrimary, NULL); if (FAILED(ddrval)) { MessageBox(NULL, "Failed to CreateSurface", NULL, MB_OK); return FALSE; } LPDIRECTDRAWCLIPPER clipper; if(lpDDraw->CreateClipper(0, &clipper, NULL) != DD_OK) { MessageBox(NULL, "Failed to GreateClipper", NULL, MB_OK); return FALSE; } if( lpDDSPrimary->SetClipper(clipper) != DD_OK ) { MessageBox(NULL, "Failed to SetClipper", NULL, MB_OK); return FALSE; } clipper->SetHWnd(0, g_hwnd); HDC hdc; if(lpDDSPrimary->GetDC(&hdc) != DD_OK) { MessageBox(NULL, "Failed to GetDC", NULL, MB_OK); return FALSE; }
SetBkColor(hdc, RGB(0,0,255)); SetTextColor( hdc,RGB(255,255,0 ) ); TextOut( hdc, 0, 0, "sxFrontMsg", lstrlen("szFrontMsg")); lpDDSPrimary->ReleaseDC(hdc);
*-*!!!! TextOut 出来的东西依然显示在左上角!!!!
突然觉得好饿!!!,吃完东西再研究~~
Feedback
# re: 初探DirectDraw 第2集 回复 更多评论
2008-09-29 17:27 by
ddraw在dx9里面已经取消了吧?
|