-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloDirect3DForm.cpp
More file actions
99 lines (97 loc) · 2.79 KB
/
HelloDirect3DForm.cpp
File metadata and controls
99 lines (97 loc) · 2.79 KB
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "HelloDirect3DForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
nRedColor = 0;
if (!InitializeDirect3D())
{
Application->MessageBox(_T("Error Loading DirectX"), _T("Error"));
}
Application->OnIdle = ApplicationIDLE;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
RenderScene();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
DestroyDirect3D();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationIDLE(TObject *Sender, bool &bFlag)
{
Invalidate();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMEraseBkgnd(Winapi::Messages::TWMEraseBkgnd &Message)
{
// Option A: Simply prevent flicker (do nothing, return true)
Message.Result = 1;
// Option B: Custom background drawing
// HDC dc = Message.DC;
// // Perform GDI drawing here using dc...
// Message.Result = 1; // Tell Windows we handled it
}
//---------------------------------------------------------------------------
bool TForm1::InitializeDirect3D()
{
D3DDISPLAYMODE d3ddm;
HRESULT hr;
ZeroMemory(&d3dpp, sizeof(d3dpp));
ZeroMemory(&d3ddm, sizeof(d3ddm));
m_d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if (m_d3d9 == nullptr) return FALSE;
hr = m_d3d9->GetAdapterDisplayMode(0, &d3ddm);
if (FAILED(hr)) return FALSE;
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
hr = m_d3d9->CreateDevice
(
0,
D3DDEVTYPE_HAL,
Handle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&m_d3d_Device
);
if (FAILED(hr)) return false;
return true;
}
//---------------------------------------------------------------------------
void TForm1::DestroyDirect3D()
{
m_d3d_Device.Release();
m_d3d9.Release();
}
//---------------------------------------------------------------------------
void TForm1::RenderScene()
{
if (m_d3d9 == nullptr) return;
if (m_d3d_Device == nullptr) return;
m_d3d_Device->Clear
(
0,
nullptr,
D3DCLEAR_TARGET,
D3DCOLOR_XRGB(nRedColor++, 0, 0),
0, 0
);
m_d3d_Device->BeginScene();
m_d3d_Device->EndScene();
m_d3d_Device->Present(nullptr, nullptr, 0, nullptr);
}