بسم الله الرحمن الرحيم عندي مشكلة لم أكتشفها في كود DIRECTX
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
BOOL SetupWCE(HINSTANCE hInstance,int nShowCmd);
LRESULT CALLBACK WndProc(HWND hwnd,int message,WPARAM wParam,LPARAM lParam);
BOOL LoopMessage();
void initD3D(HWND hwnd);
BOOL Render(float timeDalta);
void Cleanup();
void Setup();
IDirect3D9* d3d9;
IDirect3DDevice9* Device = 0;
IDirect3DVertexBuffer9* VB = 0;
D3DXMATRIX p;
#define DEMOFVF ( D3DFVF_XYZ | D3DFVF_DIFFUSE )
struct Vertex
{
public:
Vertex(){x = 0;y = 0;z = 0;}
Vertex(float _x,float _y,float _z,D3DCOLOR _color)
{x = _x;y = _y;z = _z;color = _color;}
float x,y,z;
D3DCOLOR color;
};
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbClsExtra = 0;
wc.style = 0;
wc.lpszClassName = "main";
wc.lpszMenuName = NULL;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.hInstance = hInstance;
wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(12);
wc.cbWndExtra = 0;
RegisterClassEx(&wc);
HWND hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,"main","DirectX",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,250,300,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
initD3D(hwnd);
MSG msg;
ZeroMemory(&msg,sizeof(MSG));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
int b= 0;
Render(b);
}
}
Cleanup();
return msg.wParam;
}
void initD3D(HWND hwnd)
{
d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = 480;
d3dpp.BackBufferHeight = 640;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3d9->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&Device);
Setup();
}
BOOL Render(float timeDalta)
{
Device->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,0xffffffff,0.1f,0);
////
D3DXMatrixTranslation(&p,0.0f,0.0f,0.0f);
Device->SetTransform(D3DTS_WORLD,&p);
Device->BeginScene();
Device->SetStreamSource(0,VB,0,sizeof(Vertex));
Device->SetFVF(DEMOFVF);
Device->DrawPrimitive( D3DPT_TRIANGLELIST,0,1);
Device->EndScene();
Device->Present(0,0,0,0);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd,int message,WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wParam,lParam);
break;
}
return 0;
}
void Cleanup()
{
Device->Release();
}
void Setup()
{
Device->CreateVertexBuffer(sizeof(Vertex) * 3,D3DUSAGE_WRITEONLY,DEMOFVF,D3DPOOL_MANAGED,&VB,0);
Vertex* vertex = new Vertex[3];
VOID* pVoid;
VB->Lock(0,0,(void**)&pVoid,0);
vertex[0] = Vertex(0,0,0,D3DCOLOR_XRGB(255,0,0));
vertex[1] = Vertex(10,0,0,D3DCOLOR_XRGB(0,255,0));
vertex[2] = Vertex(0,10,0,D3DCOLOR_XRGB(0,0,255));
memcpy(pVoid,vertex, sizeof(Vertex)*3);
VB->Unlock();
D3DXVECTOR3 position(0,0,10);
D3DXVECTOR3 look(0,0,0);
D3DXVECTOR3 up(0.0f,1.0f,0.0f);
D3DXMATRIX v;
D3DXMatrixLookAtLH(&v,&position,&look,&up);
Device->SetTransform(D3DTS_VIEW,&v);
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj,D3DX_PI * 0.5,(float)480/(float)640,0.1f,100000.0f);
Device->SetTransform(D3DTS_PROJECTION,&proj);
Device->SetRenderState(D3DRS_LIGHTING, FALSE);
}