-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirect3D.cpp
More file actions
86 lines (72 loc) · 2.42 KB
/
Direct3D.cpp
File metadata and controls
86 lines (72 loc) · 2.42 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
#include "Direct3D.h"
// Create Direct3D and the Direct3D Device
void Direct3D::Initialize(GameWindow & gw)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.Windowed = gw.Windowed;
d3dpp.BackBufferWidth = gw.Width;
d3dpp.BackBufferHeight = gw.Height;
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
gw.hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
D3DXCreateSprite(d3ddev, &d3dspt);
}
// Close the Device and Direct3D
void Direct3D::Close()
{
d3ddev->Release();
d3d->Release();
}
// Start rendering
void Direct3D::StartRender()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3dspt->Begin(D3DXSPRITE_ALPHABLEND);
}
// Stop rendering
void Direct3D::EndRender()
{
d3dspt->End();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
// Load a graphic into a Sprite object
void Direct3D::LoadSprite(Sprite & sprite, LPCTSTR File, int width, int height, int cols, int rows)
{
D3DXCreateTextureFromFileEx(d3ddev,
File,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
NULL,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DCOLOR_XRGB(255, 0, 255),
NULL,
NULL,
& (sprite.tex));
sprite.width = width;
sprite.height = height;
sprite.cols = cols;
sprite.rows = rows;
}
// Draw a frame from a Sprite object
void Direct3D::DrawSprite(Sprite & sprite, int Frame, float x, float y, float z)
{
RECT FrameBox;
FrameBox.left = (Frame % sprite.cols) * sprite.width;
FrameBox.top = (Frame / sprite.cols) * sprite.height;
FrameBox.right = FrameBox.left + sprite.width;
FrameBox.bottom = FrameBox.top + sprite.height;
D3DXVECTOR3 position(x, y, z);
d3dspt->Draw(sprite.tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));
}