|
|
是一个简单的粒子系统的程序,后面的几个函数我放到另外一个程序,只改设备接口,能运行,可见不是函数的问题,课主体我检查很多遍了,为什么还是有错,麻烦各位帮我看看!!谢谢!!
#include<d3d9.h>
#include<d3dx9.h>
#include<windows.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"winmm.lib")
//
// Globals
//
HINSTANCE hInst; // application instance
HWND wndHandle; // application window handle
LPDIRECT3D9 pD3D = 0;
LPDIRECT3DDEVICE9 pd3dDevice = 0;
LPD3DXMESH mesh;
const int Width=640;
const int Height=480;
LPDIRECT3DVERTEXBUFFER9 VB;
bool initWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool initDirect3D();
void render(void);
struct Vertex
{
float x,y,z;
Vertex(float _x,float _y,float _z)
{
x=_x;y=_y;z=_z;
}
};
#define FVF D3DFVF_XYZ
//
// Framework functions
//
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
if (!initWindow(hInstance))
{
MessageBox(NULL, "Unable to create window", "ERROR", MB_OK);
return false;
}
if (!initDirect3D())
{
MessageBox(NULL, "Unable to init Direct3D", "ERROR", MB_OK);
return false;
}
// Main message loop:
MSG msg;
ZeroMemory( &msg, sizeof(msg) );
while( msg.message!=WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
render();
}
// release the device and the direct3D object
if( pd3dDevice != NULL)
pd3dDevice->Release();
if( pD3D != NULL)
pD3D->Release();
return (int) msg.wParam;
}
/*********************************************************************
* initWindow
*********************************************************************/
bool initWindow(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "DirectXExample";
wcex.hIconSm = 0;
RegisterClassEx(&wcex);
// create the window
wndHandle = CreateWindow("DirectXExample",
"DirectXExample",
WS_OVERLAPPEDWINDOW,
0,
0,
640,
480,
NULL,
NULL,
hInstance,
NULL);
if (!wndHandle)
return false;
ShowWindow(wndHandle, SW_SHOW);
UpdateWindow(wndHandle);
return true;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
/*********************************************************************
* initDirect3D
* initializes direct3D
*********************************************************************/
bool initDirect3D()
{
pD3D = NULL;
pd3dDevice = NULL;
// create the directX object
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
// fill the presentation parameters structure
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.hDeviceWindow = wndHandle;
d3dpp.EnableAutoDepthStencil=true;
d3dpp.AutoDepthStencilFormat=D3DFMT_D24S8;
// create a default directx device
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wndHandle,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &pd3dDevice ) ) )
{
return false;
}
return true;
}
bool Setup()
{
pd3dDevice->CreateVertexBuffer(3*sizeof(Vertex),D3DUSAGE_WRITEONLY,FVF,D3DPOOL_MANAGED,&VB,0);
Vertex* v;
VB->Lock(0,0,(void**)&v,0);
v[0]=Vertex(0.0f,1.0f,5.0f);
v[1]=Vertex(1.0f,0.0f,5.0f);
v[2]=Vertex(-1.0f,0.0f,5.0f);
VB->Unlock();
D3DXCreateTeapot(pd3dDevice,&mesh,0);
//
// Set camera.
//
D3DXVECTOR3 pos(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,
&pos,
&target,
&up);
pd3dDevice->SetTransform(D3DTS_VIEW, &V);
//
// Set projection matrix.
//
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
0.0f,
1000.0f);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
return true;
}
void render(void)
{
D3DXMATRIX m;
D3DXMatrixTranslation(&m,1,1,1);
pd3dDevice->SetTransform(D3DTS_WORLD,&m);
pd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
pd3dDevice->BeginScene();
pd3dDevice->SetStreamSource(0,VB,0,sizeof(Vertex));
pd3dDevice->SetFVF(FVF);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
mesh->DrawSubset(0);
pd3dDevice->EndScene();
pd3dDevice-> resent(0, 0, 0, 0);
} |
|