Mercurial > LightClone
annotate LightClone/Source/GuiImage.cpp @ 25:eae13b04b06f
Working on Gui drag and drop
author | koryspansel <koryspansel@bendbroadband.com> |
---|---|
date | Fri, 16 Sep 2011 13:11:35 -0700 |
parents | a785b0aaf004 |
children | 3a63df04f3c0 |
rev | line source |
---|---|
18 | 1 /* |
2 * GuiImage | |
3 */ | |
4 | |
5 #include "GuiImage.h" | |
6 #include "VertexTypes.h" | |
7 | |
8 /* | |
9 * GuiImage | |
10 */ | |
11 GuiImage::GuiImage() : pEffect(NULL), pVertexBuffer(NULL), pTexture(NULL) | |
12 { | |
13 } | |
14 | |
15 /* | |
16 * Initialize | |
17 */ | |
18 ErrorCode GuiImage::Initialize(ResourceManager* pManager) | |
19 { | |
20 pResourceManager = pManager; | |
21 | |
22 ErrorCode eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect); | |
23 if(eCode == Error_Success) | |
24 { | |
25 eCode = pResourceManager->CreateVertexBuffer(6 * sizeof(Vertex::Quad), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer); | |
26 if(eCode == Error_Success) | |
27 { | |
28 eCode = SetupVertexBuffer(); | |
29 } | |
30 } | |
31 | |
32 return eCode; | |
33 } | |
34 | |
35 /* | |
36 * Terminate | |
37 */ | |
38 void GuiImage::Terminate() | |
39 { | |
40 if(pTexture) | |
41 { | |
42 pTexture->Release(); | |
43 pTexture = NULL; | |
44 } | |
45 | |
46 if(pVertexBuffer) | |
47 { | |
48 pVertexBuffer->Release(); | |
49 pVertexBuffer = NULL; | |
50 } | |
51 | |
52 if(pEffect) | |
53 { | |
54 pEffect->Release(); | |
55 pEffect = NULL; | |
56 } | |
57 } | |
58 | |
59 /* | |
60 * Update | |
61 */ | |
62 void GuiImage::Update(float fElapsed) | |
63 { | |
64 } | |
65 | |
66 /* | |
67 * Render | |
68 */ | |
69 void GuiImage::Render(RenderContext& kContext, Camera& kCamera) | |
70 { | |
23 | 71 if(bVisible) |
18 | 72 { |
23 | 73 if(pTexture) |
74 { | |
75 uint32 nPasses = 0; | |
18 | 76 |
23 | 77 const float fOffsetX = -0.5f * ScreenSizeX; |
78 const float fOffsetY = +0.5f * ScreenSizeY; | |
18 | 79 |
23 | 80 const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f); |
81 | |
82 kContext.ApplyCameraToEffect(kCamera, pEffect); | |
18 | 83 |
23 | 84 D3DXMATRIX kScale; |
85 D3DXMatrixScaling(&kScale, kDimensions.x, kDimensions.y, 1.0f); | |
18 | 86 |
23 | 87 const D3DXVECTOR2& kLocation = GetPosition(); |
18 | 88 |
23 | 89 D3DXMATRIX kTranslate; |
90 D3DXMatrixTranslation(&kTranslate, fOffsetX + kLocation.x + 0.5f, fOffsetY - kLocation.y + 0.5f, 0.0f); | |
18 | 91 |
23 | 92 D3DXMATRIX kWorldMatrix; |
93 D3DXMatrixMultiply(&kWorldMatrix, &kScale, &kTranslate); | |
94 | |
95 pEffect->SetMatrix(pEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix); | |
96 pEffect->SetVector(pEffect->GetParameterByName(NULL, "kColor"), &kColorVector); | |
97 pEffect->SetTexture(pEffect->GetParameterByName(NULL, "kTexture"), pTexture); | |
18 | 98 |
23 | 99 pEffect->SetTechnique(pEffect->GetTechnique(0)); |
100 pEffect->Begin(&nPasses, 0); | |
101 pEffect->BeginPass(0); | |
102 | |
103 kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace); | |
21
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
104 |
23 | 105 pEffect->EndPass(); |
106 pEffect->End(); | |
107 } | |
18 | 108 |
23 | 109 GuiElement::Render(kContext, kCamera); |
18 | 110 } |
111 } | |
112 | |
113 /* | |
114 * SetTexture | |
115 */ | |
21
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
116 ErrorCode GuiImage::SetTexture(const char* pName, bool bResize) |
18 | 117 { |
118 if(pTexture) | |
119 { | |
120 pTexture->Release(); | |
121 pTexture = NULL; | |
122 } | |
123 | |
21
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
124 ErrorCode eCode = pResourceManager->CreateTextureFromFile(pName, &pTexture); |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
125 if(eCode == Error_Success) |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
126 { |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
127 if(bResize) |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
128 { |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
129 D3DSURFACE_DESC kDescriptor; |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
130 pTexture->GetLevelDesc(0, &kDescriptor); |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
131 |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
132 kDimensions.x = (float)kDescriptor.Width; |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
133 kDimensions.y = (float)kDescriptor.Height; |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
134 } |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
135 } |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
136 |
b4dc5d674e22
Added GUI event system and some support data structures
koryspansel
parents:
18
diff
changeset
|
137 return eCode; |
18 | 138 } |
139 | |
140 /* | |
141 * SetupVertexBuffer | |
142 */ | |
143 ErrorCode GuiImage::SetupVertexBuffer() | |
144 { | |
145 Vertex::Quad* pVertices = NULL; | |
146 | |
147 HRESULT hResult = pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD); | |
148 if(FAILED(hResult)) | |
149 { | |
150 return Error_Fail; | |
151 } | |
152 | |
153 pVertices[0] = Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f); | |
154 pVertices[1] = Vertex::Quad(+0.0f, +0.0f, 1.0f, 0.0f, 0.0f); | |
155 pVertices[2] = Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f); | |
156 pVertices[3] = Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f); | |
157 pVertices[4] = Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f); | |
158 pVertices[5] = Vertex::Quad(+1.0f, -1.0f, 1.0f, 1.0f, 1.0f); | |
159 | |
160 pVertexBuffer->Unlock(); | |
161 | |
162 return Error_Success; | |
163 } |