Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / Game Design & Programming / Why CreateOffscreenPlainSurface go error?? Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
yzw

January 24, 2005, 02:26 AM

I write a program,as follow:
//------------------------------------------

#include
#include
#include

HWND hWnd;
LPDIRECT3D9 pD3D;
LPDIRECT3DDEVICE9 pd3dDevice;
LPDIRECT3DSURFACE9 pSurface;

int InitWindow(HINSTANCE hInstance);
int InitD3D();
int SetupSurface();
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
void render();
void CleanUp();

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
MSG msg;

if(!InitWindow(hInstance))
{
MessageBox(NULL,"InitWindow Failed!","Error",MB_OK);
return 0;
}

ZeroMemory(&msg,sizeof(msg));
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
render();
}
}

return msg.wParam;
}

int InitWindow(HINSTANCE hInstance)
{
WNDCLASSEX wc;

wc.cbClsExtra=0;
wc.cbSize=sizeof(WNDCLASSEX);
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(0);
wc.hCursor=LoadCursor(NULL,IDC_ARROW);
wc.hIcon=0;
wc.hIconSm=0;
wc.hInstance=hInstance;
wc.lpfnWndProc=WndProc;
wc.lpszClassName="LoadBMP";
wc.lpszMenuName=NULL;
wc.style=CS_HREDRAW|CS_VREDRAW;

if(!RegisterClassEx(&wc))
{
MessageBox(NULL,"Can't register window class!","Error",MB_OK);
return 0;
}


hWnd=CreateWindow("LoadBMP", //window class name
"LoadBMP", //window name
WS_OVERLAPPEDWINDOW, //window style
CW_USEDEFAULT, //InitPosition-X
CW_USEDEFAULT, //InitPosition-Y
800, //weight
600, //height
NULL, //Parent Window
NULL, //hMenu
hInstance, //HINSTANCE
NULL); //pointer to window-creation data

if(hWnd==NULL)
{
MessageBox(NULL,"Create Window Failed!","Error",MB_OK);
return 0;
}


if(!InitD3D())
{
MessageBox(NULL,"InitD3D Failed!","Error",MB_OK);
return 0;
}


ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);


return 1;
}

int InitD3D()
{
D3DPRESENT_PARAMETERS pd3dpp;

pD3D=NULL;
pd3dDevice=NULL;


pD3D=Direct3DCreate9(D3D_SDK_VERSION);
if(pD3D==NULL)
{
MessageBox(NULL,"Error when calling Direct3DCreae9(...)!","Error",MB_OK);
return 0;
}


ZeroMemory(&pd3dpp,sizeof(D3DPRESENT_PARAMETERS));
pd3dpp.Windowed=TRUE;
pd3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD;
pd3dpp.BackBufferWidth=800;
pd3dpp.BackBufferHeight=600;
pd3dpp.BackBufferCount=1;
pd3dpp.BackBufferFormat=D3DFMT_X8R8G8B8;
pd3dpp.hDeviceWindow=hWnd;
if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, //Adapter
D3DDEVTYPE_HAL, //D3DDEVTYPE
hWnd, //hFocusWindow
D3DCREATE_SOFTWARE_VERTEXPROCESSING, //D3DCREATE BehaviorFlag
&pd3dpp, //pointer of D3DPRESENT_PARAMETERS
&pd3dDevice))) //pointer of LPDIRECT3DDEVICE9
{
MessageBox(NULL,"Error when calling CreateDevice(...)!","Error",MB_OK);
return 0;
}

//setup surface
if(!SetupSurface())
{
return 0;
}

return 1;
}

int SetupSurface()
{
HRESULT hResult;
D3DXIMAGE_INFO Info;

pSurface=NULL;


D3DXGetImageInfoFromFile("test.bmp", //pSrcFile
&Info); //D3DXIMAGE_INFO

hResult=pd3dDevice->CreateOffscreenPlainSurface(Info.Width, //Width
Info.Height, //Height
Info.Format, //D3DFORMAT
D3DPOOL_DEFAULT, //D3DPOOL
&pSurface, //pSurface
NULL); //pHandle
if(FAILED(hResult))
{
MessageBox(NULL,"Error when calling CreateOffscreenPlainSurface(...)!","",MB_OK);
return 0;
}

//Load a bitmap file
hResult=D3DXLoadSurfaceFromFile(pSurface, //pDestSurface
NULL, //pDestPalette
NULL, //pDestRect
"test.bmp", //pSrcFile
NULL, //pSrcRect
D3DX_FILTER_NONE, //filter
0, //ColorKey
NULL); //pSrcInfo

return 1;
}

void render()
{
IDirect3DSurface9* backbuffer=NULL;

if(pd3dDevice==NULL)
{
return;
}


pd3dDevice->Clear(0, //Number of rectangles in array of pRects
NULL, //pRects
D3DCLEAR_TARGET, //D3DCLEAR Flags
D3DCOLOR_XRGB(0,0,0), //color
1.0f, //Z
0); //Stencil


pd3dDevice->GetBackBuffer(0,
0,
D3DBACKBUFFER_TYPE_MONO,
&backbuffer);


pd3dDevice->StretchRect(pSurface, //pSourceSurface
NULL, //pSourceRect
backbuffer, //pDestSurface
NULL, //pDestRect
D3DTEXF_NONE); //Filter

pd3dDevice->Present(NULL, //pSourceRect=NULL unless ....
NULL, //pDestRect=NULL unless....
NULL, //hDestWindowOverride
NULL); //pDirtyRegion
}

void CleanUp()
{
if(pd3dDevice!=NULL)
{
pd3dDevice->Release();
}
if(pD3D!=NULL)
{
pD3D->Release();
}
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
CleanUp();
PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd,message,wParam,lParam);
}

//----------------------------------------------------
I can compile and run it.But,it does't display a bitmap!Why??
Thanks for your help!!

 
This thread contains 1 message.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.