codice:
#include <windows.h>
#include <d3dx9.h>
#pragma comment (lib,"d3d9.lib")
#pragma comment (lib,"d3dx9.lib")
#define CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
typedef struct _vertex_
{
float x,y,z;
WORD Colore;
} Vertex;
void Render();
void Chiudi();
HRESULT Init3D(HWND hWnd);
bool InitVertex();
void Posiziona();
LPDIRECT3D9 g_pD3D = NULL;
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVX = NULL;
const char NomeClasse[] = "Finestra";
Vertex Vertici[] =
{
{ 150.0f, 50.0f, 0.5f, D3DCOLOR_ARGB(0,255,123,214), },
{ 250.0f, 250.0f, 0.5f, D3DCOLOR_ARGB(0,124,255,120), },
{ 50.0f, 250.0f, 0.5f, D3DCOLOR_ARGB(0,0,184,255), },
};
LRESULT CALLBACK WndProc (HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch (Msg)
{
case WM_CLOSE:
Chiudi();
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
Render();
ValidateRect(hWnd,NULL);
break;
}
return DefWindowProc(hWnd,Msg,wParam,lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
NomeClasse, NULL };
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( NomeClasse, "D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
if( SUCCEEDED (Init3D(hWnd) ) )
{
InitVertex();
ShowWindow(hWnd,SW_SHOWDEFAULT);
UpdateWindow(hWnd);
MessageBox(NULL,"Gnam",NULL,MB_OK);
}
else
{
MessageBox(NULL,"Impossibile inizializzare il Device","Errore",MB_OK);
}
MSG Msg;
while(GetMessage(&Msg,NULL,0,0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
HRESULT Init3D (HWND hWnd)
{
if ((g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.BackBufferCount = 1;
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_pd3dDevice)))
{
MessageBox(NULL,"Impossibile creare il Device","Adso",MB_OK);
return E_FAIL;
}
return S_OK;
}
bool InitVertex()
{
if(FAILED(g_pd3dDevice->CreateVertexBuffer(3 * sizeof(Vertex),0,
CUSTOMVERTEX,D3DPOOL_MANAGED,&g_pVX,NULL)))
{
MessageBox(NULL,"Errore nella creazione del buffer dei vertici","Adso",MB_OK);
return false;
}
VOID *PVertex;
g_pVX->Lock(0,sizeof(Vertex),(void**)&PVertex,NULL);
memcpy(PVertex,Vertici,sizeof(Vertici));
g_pVX->Unlock();
return true;
}
void Chiudi()
{
if (g_pVX != NULL)
g_pVX->Release();
if (g_pd3dDevice != NULL)
g_pd3dDevice->Release();
if (g_pD3D != NULL)
g_pD3D->Release();
}
void Render()
{
if (NULL == g_pd3dDevice)
return;
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(110,211,123), 1.0f, 0 );
g_pd3dDevice->BeginScene();
g_pd3dDevice->SetStreamSource(0,g_pVX,0,sizeof(Vertex));
g_pd3dDevice->SetFVF(CUSTOMVERTEX);
Posiziona();
g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
g_pd3dDevice->EndScene();
g_pd3dDevice->Present(NULL,NULL,NULL,NULL);
}
void Posiziona()
{
D3DXMATRIX FinPos;
D3DXMatrixIdentity(&FinPos);
D3DXMatrixTranslation(&FinPos,10.0f,20.0f,50.0f);
g_pd3dDevice->SetTransform(D3DTS_WORLD,&FinPos);
}
Lo riporto anche qui nel caso vi possa andare meglio...