|

楼主 |
发表于 2010-3-6 14:04:00
|
显示全部楼层
Re:求教: 在DX中创建立方体时 运行时出现花屏是怎么回事
代码是:
头文件里是:GAME.H
#include <d3d9.h>
#include <d3dx9.h>
#include <windows.h>
class Game
{
public:
Game();
~Game();
bool InitInstance(HWND hWnd);
bool setup();
void Render();
bool ShutDown();
private:
LPDIRECT3D9 m_pD3D;
LPDIRECT3DDEVICE9 m_pD3DDevice;
IDirect3DVertexBuffer9* VB;
IDirect3DIndexBuffer9* IB;
};
Game::Game()
{
m_pD3D = NULL;
m_pD3DDevice = NULL;
}
Game::~Game()
{
IB->Release();
VB->Release();
if(m_pD3DDevice)
m_pD3DDevice->Release();
if(m_pD3D)
m_pD3D->Release();
}
struct Vertex
{
Vertex(){}
Vertex(float x,float y,float z)
{
_x=x;_y=y;_z=y;
}
float _x,_y,_z;
static const DWORD FVF=D3DFVF_XYZ;
} ;
bool Game::InitInstance(HWND hWnd)
{
D3DPRESENT_PARAMETERS PresentParams;
m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!m_pD3D)
{
MessageBox (NULL, TEXT("Can Not Create m_pD3D"),TEXT("Error"), MB_ICONERROR);
return 0;
}
ZeroMemory( & resentParams, sizeof(PresentParams) );
PresentParams.BackBufferHeight = 800;
PresentParams.BackBufferWidth = 600;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
PresentParams.Windowed = true;
PresentParams.BackBufferCount = 1;
PresentParams.hDeviceWindow = hWnd;
PresentParams.BackBufferFormat=D3DFMT_R5G6B5;
if(!(m_pD3D->CreateDevice( 0,
D3DDEVTYPE_HAL , hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&PresentParams,
&m_pD3DDevice)))
{
//MessageBox (NULL, TEXT("Can Not Create the DirectX3D Device!"),TEXT("Error"), MB_ICONERROR);
return 0;
}
return 0;
}
bool Game::setup()
{
const int width=800;
const int height=600;
m_pD3DDevice ->CreateVertexBuffer(8*sizeof(Vertex),
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&VB,
0);
m_pD3DDevice ->CreateIndexBuffer(36*sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&IB,
0);
Vertex* v;
VB->Lock(0,0,(void**)&v,0);
v[0]=Vertex(-1.0f,-1.0f,-1.0f);
v[1]=Vertex(-1.0f,1.0f,-1.0f);
v[2]=Vertex(1.0f,1.0f,-1.0f);
v[3]=Vertex(1.0f,-1.0f,-1.0f);
v[4]=Vertex(-1.0f,-1.0f,1.0f);
v[5]=Vertex(-1.0f,1.0f,1.0f);
v[6]=Vertex(1.0f,1.0f,1.0f);
v[7]=Vertex(1.0f,-1.0f,1.0f);
VB->Unlock();
WORD* ib=0;
IB->Lock(0,0,(void**)&ib,0);
ib[0]=0;ib[1]=1;ib[2]=2;
ib[3]=0;ib[4]=2;ib[5]=3;
ib[6]=4;ib[7]=6;ib[8]=5;
ib[9]=4;ib[10]=7;ib[11]=0;
ib[12]=4;ib[13]=5;ib[14]=1;
ib[15]=4;ib[16]=1;ib[17]=0;
ib[18]=3;ib[19]=2;ib[20]=6;
ib[21]=3;ib[22]=6;ib[23]=7;
ib[24]=1;ib[25]=5;ib[26]=6;
ib[27]=1;ib[28]=6;ib[29]=2;
ib[30]=4;ib[31]=0;ib[32]=3;
ib[33]=4;ib[34]=3;ib[35]=7;
IB->Unlock();
D3DXVECTOR3 position(0.0f,0.0f,-5.0f);
D3DXVECTOR3 target(0.0f,0.0f,0.0f);
D3DXVECTOR3 up(0.0f,1.0f,0.0f);
D3DXMATRIX V;
D3DXMatrixLookAtLH(&V,&position,&target,&up);
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,
D3DX_PI*0.5f,
(float)width/(float)height,
1.0f,1000.0f);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&proj);
m_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);
return true;
}
void Game::Render()
{
/* D3DXMATRIX Rx,Ry;
D3DXMatrixRotationX(&Rx,3.14/4.0f);
float y=0.0f;
for(y;y<=6.28f ;y+=0.2f)
{
D3DXMatrixRotationY(&Ry,y);
}
D3DXMATRIX p=Rx*Ry;
m_pD3DDevice->SetTransform(D3DTS_WORLD,&p);*/
if(NULL==m_pD3D)
return;
m_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0);
// if(SUCCEEDED(g_pd3dDevice->BeginScene()))
// {
// g_pd3dDevice->EndScene();
// }
m_pD3DDevice ->BeginScene();
m_pD3DDevice->SetStreamSource(0,VB,0,sizeof(Vertex));
m_pD3DDevice->SetIndices(IB);
m_pD3DDevice->SetFVF(Vertex::FVF);
m_pD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,8,0,12);
m_pD3DDevice->EndScene();
m_pD3DDevice->Present(NULL,NULL,NULL,NULL);
}
///////////////////////////////////////////////////////////////////////////////
game.CPP里
#include "Game.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
Game m_game;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int iCmdShow)
{
USHORT Width = 800;
USHORT Height = 600;
static TCHAR szAppName[] = TEXT("DEMO");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT("Can Not Register Windows Class."),szAppName, MB_ICONERROR);
return 0;
}
hWnd= CreateWindow( szAppName,
TEXT ("DEMO"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
Width,
Height,
NULL,
NULL,
hInstance,
NULL );
m_game.InitInstance(hWnd);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (1)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT) break;
TranslateMessage( &msg );
DispatchMessage ( &msg );
}
else
{
//
}
}
return 0;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
break;
case WM_PAINT:
if(!m_game.setup())
return 0;
m_game.Render();
default:
return DefWindowProc( hWnd, message, wParam, lParam);
}
return 0;
} |
|