1 | #pragma once
|
---|
2 |
|
---|
3 | #include <d3d9.h>
|
---|
4 |
|
---|
5 | class CDXTexture
|
---|
6 | {
|
---|
7 | friend class CTextureManager;
|
---|
8 |
|
---|
9 | protected:
|
---|
10 | LPDIRECT3DDEVICE9 m_Dev;
|
---|
11 | LPDIRECT3DSURFACE9 m_pBackBuffer;
|
---|
12 | IDirect3DSurface9* m_OrigDepthStencil;
|
---|
13 | int m_RTindex;
|
---|
14 |
|
---|
15 | D3DVIEWPORT9 m_OldViewport;
|
---|
16 | int m_Width, m_Height;
|
---|
17 | int m_ColorStage;
|
---|
18 |
|
---|
19 | char m_Name[512];
|
---|
20 |
|
---|
21 | public:
|
---|
22 | IDirect3DTexture9* m_Tex;
|
---|
23 | IDirect3DSurface9* m_Stencil;
|
---|
24 |
|
---|
25 | void SetStage(int color)
|
---|
26 | { m_ColorStage = color; }
|
---|
27 |
|
---|
28 | static void SetLinearFilters(LPDIRECT3DDEVICE9 dev, DWORD stage) {
|
---|
29 | dev->SetSamplerState(stage, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
|
---|
30 | dev->SetSamplerState(stage, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
|
---|
31 | dev->SetSamplerState(stage, D3DSAMP_MAXANISOTROPY, 2);
|
---|
32 | dev->SetSamplerState(stage, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
|
---|
33 | }
|
---|
34 | static void SetPointFilters(LPDIRECT3DDEVICE9 dev, DWORD stage) {
|
---|
35 | dev->SetSamplerState(stage, D3DSAMP_MAGFILTER, D3DTEXF_NONE);
|
---|
36 | dev->SetSamplerState(stage, D3DSAMP_MINFILTER, D3DTEXF_NONE);
|
---|
37 | dev->SetSamplerState(stage, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
|
---|
38 | }
|
---|
39 |
|
---|
40 | inline void CopyFromBackBuffer()
|
---|
41 | {
|
---|
42 | LPDIRECT3DSURFACE9 backBuffer;
|
---|
43 | m_Dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
|
---|
44 | m_Dev->StretchRect(backBuffer, NULL, this->GetSurface(0), NULL, D3DTEXF_POINT);
|
---|
45 | SAFE_RELEASE(backBuffer);
|
---|
46 | }
|
---|
47 |
|
---|
48 | inline void CopyToBackBuffer()
|
---|
49 | {
|
---|
50 | LPDIRECT3DSURFACE9 backBuffer;
|
---|
51 | m_Dev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
|
---|
52 | m_Dev->StretchRect(this->GetSurface(0), NULL, backBuffer, NULL, D3DTEXF_POINT);
|
---|
53 | SAFE_RELEASE(backBuffer);
|
---|
54 | }
|
---|
55 | virtual bool InitTex(LPDIRECT3DDEVICE9 dev, char* name, char* tex_name, D3DFORMAT format = D3DFMT_UNKNOWN);
|
---|
56 | virtual bool InitTex(LPDIRECT3DDEVICE9 dev, char* tex_name, int width, int height, D3DFORMAT format = D3DFMT_A8R8G8B8, bool mipmapped = false);
|
---|
57 |
|
---|
58 |
|
---|
59 | CDXTexture();
|
---|
60 | ~CDXTexture(void);
|
---|
61 | virtual inline void Bind()
|
---|
62 | { m_Dev->SetTexture(m_ColorStage, m_Tex); }
|
---|
63 | virtual inline void Bind(int stage)
|
---|
64 | { m_Dev->SetTexture(stage, m_Tex); }
|
---|
65 | virtual inline void BindVertexSampler(int stage)
|
---|
66 | { m_Dev->SetTexture(D3DVERTEXTEXTURESAMPLER0 + stage, m_Tex); }
|
---|
67 |
|
---|
68 | inline void BindColor()
|
---|
69 | { m_Dev->SetTexture(m_ColorStage, m_Tex); }
|
---|
70 |
|
---|
71 | inline virtual void BindOther() {}
|
---|
72 |
|
---|
73 | LPDIRECT3DSURFACE9 GetSurface(int level)
|
---|
74 | {
|
---|
75 | LPDIRECT3DSURFACE9 surface;
|
---|
76 | m_Tex->GetSurfaceLevel(level, &surface);
|
---|
77 | return surface;
|
---|
78 | }
|
---|
79 |
|
---|
80 | inline void StartRender(bool depth = true, int RTindex = 0)
|
---|
81 | {
|
---|
82 | m_RTindex = RTindex;
|
---|
83 | // set new, funky viewport
|
---|
84 | D3DVIEWPORT9 newViewport;
|
---|
85 | newViewport.X = 0;
|
---|
86 | newViewport.Y = 0;
|
---|
87 | newViewport.Width = m_Width;
|
---|
88 | newViewport.Height = m_Height;
|
---|
89 | newViewport.MinZ = 0.0f;
|
---|
90 | newViewport.MaxZ = 1.0f;
|
---|
91 |
|
---|
92 | if (m_RTindex == 0) {
|
---|
93 | m_Dev->GetViewport(&m_OldViewport);
|
---|
94 | m_Dev->SetViewport(&newViewport);
|
---|
95 | m_Dev->GetRenderTarget(0, &m_pBackBuffer);
|
---|
96 | m_Dev->GetDepthStencilSurface(&m_OrigDepthStencil);
|
---|
97 | if (depth)
|
---|
98 | m_Dev->SetDepthStencilSurface(m_Stencil);
|
---|
99 | }
|
---|
100 |
|
---|
101 | LPDIRECT3DSURFACE9 surface;
|
---|
102 | m_Tex->GetSurfaceLevel(0, &surface);
|
---|
103 | m_Dev->SetRenderTarget(m_RTindex, surface);
|
---|
104 |
|
---|
105 | SAFE_RELEASE(surface);
|
---|
106 | }
|
---|
107 |
|
---|
108 | inline void EndRender()
|
---|
109 | {
|
---|
110 | if (m_RTindex == 0){
|
---|
111 | m_Dev->SetRenderTarget(0, m_pBackBuffer);
|
---|
112 | m_Dev->SetDepthStencilSurface(m_OrigDepthStencil);
|
---|
113 | m_Dev->SetViewport(&m_OldViewport);
|
---|
114 | SAFE_RELEASE(m_pBackBuffer);
|
---|
115 | SAFE_RELEASE(m_OrigDepthStencil);
|
---|
116 | }
|
---|
117 | else
|
---|
118 | m_Dev->SetRenderTarget(m_RTindex, NULL);
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | void CopyToName(char* name);
|
---|
123 | };
|
---|