高分求解入门问题!!!!!!!!!!!
为什么无法创建 主表面?
错误信息:
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; //这是后台缓存
if ( FAILED( lpddsPrimary->GetAttachedSurface( &ddsd.ddsCaps, &lpDDSBuffer )) )
return FALSE; //在这里返回false!!!怎么回事?
错误信息:
lpddsPrimary->GetAttachedSurface( &ddsd.ddsCaps, &lpDDSBuffer ) CXX0052: 错误:成员函数不存在
代码:
#include "Main.h"
// G L O B A L S _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
// Windows
HINSTANCE hMainInstance;
HWND hMainWindow;
// DirectDraw
LPDIRECTDRAW7 lpdd7;
LPDIRECTDRAWSURFACE7 lpDDSPrimary;
LPDIRECTDRAWSURFACE7 lpDDSBack; // 存放背景图的页面的指针
LPDIRECTDRAWSURFACE7 lpDDSBuffer; // DirectDraw后台缓存的指针
DDSURFACEDESC2 ddsd;
int Initialize(void)
{
if (DirectDrawCreateEx (NULL, (void **)&lpdd7, IID_IDirectDraw7, NULL) != DD_OK )
return(FALSE);
if (lpdd7->SetCooperativeLevel(hMainWindow,DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN)!=DD_OK)
return(TRUE);
if (lpdd7->SetDisplayMode(640,480,32,0,DDSDM_STANDARDVGAMODE)!=DD_OK)
if (lpdd7->SetDisplayMode(640,480,24,0,DDSDM_STANDARDVGAMODE)!=DD_OK)
return(FALSE);
// create our primary surface
memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
ddsd.dwSize=sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS|DDSD_BACKBUFFERCOUNT; //有后台缓存
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1; //一个后台缓存
if((lpdd7 ->CreateSurface(&ddsd , &lpDDSPrimary , NULL)) != DD_OK)
return FALSE;
MessageBox(hMainWindow, "Primary surface OK!", "Notice", MB_OK | MB_ICONINFORMATION);
ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; //这是后台缓存
if ( FAILED( lpDDSPrimary->GetAttachedSurface( &ddsd.ddsCaps, &lpDDSBuffer )) )
return FALSE; ///////////////////////////在这里报错!!!
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; //这是离屏页面
if ( FAILED( lpdd7->CreateSurface( &ddsd, &lpDDSBack, NULL )) )
return FALSE; //创建放背景图的页面
//如还有别的页面可在此处继续创建
if ( LoadBitmapResource(lpDDSBack,0,0,hMainInstance)==false )
MessageBox(hMainWindow, "加载图片失败!","Error",MB_OK | MB_ICONEXCLAMATION);
lpDDSPrimary->Flip(lpDDSBack,DDFLIP_WAIT);
// we're ready to rock
MessageBox(hMainWindow, "Initialization OK!\nYou can start loading the resources! :)", "Notice", MB_OK | MB_ICONINFORMATION);
return(TRUE);
}
///////////////////////////////////////////////////////////////
// SHUTDOWN
// Release the objects we created.
///////////////////////////////////////////////////////////////
void Shutdown(void)
{
if (lpDDSPrimary)
{
// kill the primary surface!
lpDDSPrimary->Release();
lpDDSPrimary = NULL;
}
if (lpdd7)
{
// blow away DirectDraw!
lpdd7->Release();
lpdd7 = NULL;
}
// destroy the window!
CloseWindow(hMainWindow);
// assassinate COM! :)
CoUninitialize();
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
if(!InitWindow(hInstance,nCmdShow)) return false;
MSG msg;
// initialize, test for success
if( Initialize() )
{
for(;;)
{
if(PeekMessage(&msg, NULL, 0, 0 ,PM_REMOVE))
{
if(msg.message==WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
else
MessageBox(hMainWindow, "Initialization failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
// shut everything down
Shutdown();
//MessageBox(NULL, "Shutdown OK!", "Notice", MB_OK | MB_ICONINFORMATION);
// give Windows the shutdown code
return(msg.wParam);
}
bool InitWindow(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX sampleClass;
HWND hwnd;
sampleClass.cbSize=sizeof(WNDCLASSEX);
sampleClass.style= CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
sampleClass.lpfnWndProc= MsgHandler;
sampleClass.cbClsExtra= 0;
sampleClass.cbWndExtra= 0;
sampleClass.hInstance=hInstance;
sampleClass.hIcon= LoadIcon(NULL,IDI_WINLOGO);
sampleClass.hCursor= LoadCursor(NULL, IDC_ARROW);
sampleClass.hbrBackground= (HBRUSH)GetStockObject(BLACK_BRUSH);
sampleClass.lpszMenuName= NULL;
sampleClass.lpszClassName= "FrameWork";
sampleClass.hIconSm= LoadIcon(NULL, IDI_WINLOGO);
RegisterClassEx(&sampleClass);
if(!(hwnd=CreateWindowEx(NULL,
"FrameWork",
"My application framework",
WS_POPUP| WS_MAXIMIZE,0,0,
GetSystemMetrics( SM_CXSCREEN ),
GetSystemMetrics( SM_CYSCREEN ),
NULL,
NULL,
hInstance,
NULL))) return false;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
//为全局变量赋值
hMainInstance=hInstance;
hMainWindow=hwnd;
return true;
}
int LoadBitmapResource(LPDIRECTDRAWSURFACE7 lpdds, int xDest, int yDest, HINSTANCE hInstance)
{
HDC hSrcDC; // source DC - memory device context
HDC hDestDC; // destination DC - surface device context
HBITMAP hbitmap; // handle to the bitmap resource
BITMAP bmp; // structure for bitmap info
int nHeight, nWidth; // bitmap dimensions
// first load the bitmap resource
if ((hbitmap = (HBITMAP)LoadImage(hInstance, "test.bmp",
IMAGE_BITMAP , 0, 0,
LR_CREATEDIBSECTION | LR_LOADFROMFILE)) == NULL)
return(FALSE);
// create a DC for the bitmap to use
if ((hSrcDC = CreateCompatibleDC(NULL)) == NULL)
return(FALSE);
// select the bitmap into the DC
if (SelectObject(hSrcDC, hbitmap) == NULL)
{
DeleteDC(hSrcDC);
return(FALSE);
}
// get image dimensions
if (GetObject(hbitmap, sizeof(BITMAP), &bmp) == 0)
{
DeleteDC(hSrcDC);
return(FALSE);
}
nWidth = bmp.bmWidth;
nHeight = bmp.bmHeight;
// retrieve surface DC
if (FAILED(lpdds->GetDC(&hDestDC)))
{
DeleteDC(hSrcDC);
return(FALSE);
}
// copy image from one DC to the other
if (BitBlt(hDestDC, xDest, yDest, nWidth, nHeight, hSrcDC, 0, 0,
SRCCOPY) == NULL)
{
lpdds->ReleaseDC(hDestDC);
DeleteDC(hSrcDC);
return(FALSE);
}
// kill the device contexts
lpdds->ReleaseDC(hDestDC);
DeleteDC(hSrcDC);
// return success
return(TRUE);
}
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch( msg )
{
case WM_KEYDOWN:
switch ( wParam )
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}