1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#pragma warn -com.
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
if (NULL == (objet_D3D = Direct3DCreate9(D3D_SDK_VERSION))) // création d'un périphérique D3Dx9
{
MessageDlg("Erreur lors de la création du périphérique.", mtError, TMsgDlgButtons() << mbOK, 0);
}
if (objet_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm) == D3DERR_INVALIDCALL) // création d'un récupérateur de résolution
{
MessageDlg("Impossible de créer l'objet associé au périphérique 3D qui récupère la résolution en cours.", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // objet permettant le paramétrage du périphérique D3Dx9
d3dpp.BackBufferWidth = 300;
d3dpp.BackBufferHeight = 300;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferCount = 1;
formatdepixel = D3DFMT_A8R8G8B8;
d3dpp.BackBufferFormat = formatdepixel;
if (objet_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1->Handle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &objet_periph3D) != D3D_OK) // et maintenant on cré l'objet direct3D
{
formatdepixel = D3DFMT_R8G8B8;
d3dpp.BackBufferFormat = formatdepixel;
if (objet_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1->Handle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &objet_periph3D) != D3D_OK)
{
formatdepixel = D3DFMT_R5G6B5;
d3dpp.BackBufferFormat = formatdepixel;
if (objet_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Form1->Handle, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &objet_periph3D) != D3D_OK)
{
MessageDlg("Impossible de créer l'objet associé au périphérique 3D (résolution incorrecte).", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (objet_D3D != NULL) // on détruit les périphérique D3Dx9
{
objet_D3D->Release();
}
if (objet_periph3D != NULL)
{
objet_periph3D->Release();
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
objet_periph3D->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0, 0); // on efface le back buffer en le remplissant de pixel de couleur noir
objet_periph3D->BeginScene(); // mise en place du verrou sur le back buffer pour modification
objet_periph3D->EndScene(); // fin d'écriture dans le back buffer (cela enlève le vérou)
objet_periph3D->Present(NULL, NULL, NULL, NULL); // affiche le back buffer à l'écran
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
CUSTOMVERTEX g_Vertices[] =
{
{150, 50, 0.5, 1, 0xffff0000,},
{ 250, 250, 0.5, 1, 0xff00ff00,},
{ 50, 250, 0.5, 1, 0xff00ffff,},
};
if (objet_periph3D->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &VERBUFF, NULL) != D3D_OK)
{
MessageDlg("Impossible de créer le vertex buffer.", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
void *pVertices;
if (VERBUFF->Lock(0, sizeof(g_Vertices), &pVertices, 0) == D3DERR_INVALIDCALL)
{
MessageDlg("Impossible de bloquer le vertex buffer.", mtError, TMsgDlgButtons() << mbOK, 0);
return;
}
memcpy(pVertices, g_Vertices, sizeof(g_Vertices));
VERBUFF->Unlock();
objet_periph3D->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1, 0);
objet_periph3D->BeginScene();
objet_periph3D->SetStreamSource(0, VERBUFF, sizeof(CUSTOMVERTEX), sizeof(CUSTOMVERTEX));
objet_periph3D->SetFVF(D3DFVF_CUSTOMVERTEX);
objet_periph3D->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
objet_periph3D->EndScene();
objet_periph3D->Present(NULL, NULL, NULL, NULL);
}
//--------------------------------------------------------------------------- |