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
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
1 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
2 * GuiImage
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
3 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
4
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
5 #include "GuiImage.h"
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
6 #include "VertexTypes.h"
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
7
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
8 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
9 * GuiImage
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
10 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
11 GuiImage::GuiImage() : pEffect(NULL), pVertexBuffer(NULL), pTexture(NULL)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
12 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
13 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
14
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
15 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
16 * Initialize
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
17 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
18 ErrorCode GuiImage::Initialize(ResourceManager* pManager)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
19 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
20 pResourceManager = pManager;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
21
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
22 ErrorCode eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
23 if(eCode == Error_Success)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
24 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
25 eCode = pResourceManager->CreateVertexBuffer(6 * sizeof(Vertex::Quad), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
26 if(eCode == Error_Success)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
27 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
28 eCode = SetupVertexBuffer();
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
29 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
30 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
31
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
32 return eCode;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
33 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
34
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
35 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
36 * Terminate
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
37 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
38 void GuiImage::Terminate()
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
39 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
40 if(pTexture)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
41 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
42 pTexture->Release();
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
43 pTexture = NULL;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
44 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
45
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
46 if(pVertexBuffer)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
47 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
48 pVertexBuffer->Release();
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
49 pVertexBuffer = NULL;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
50 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
51
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
52 if(pEffect)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
53 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
54 pEffect->Release();
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
55 pEffect = NULL;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
56 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
57 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
58
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
59 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
60 * Update
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
61 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
62 void GuiImage::Update(float fElapsed)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
63 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
64 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
65
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
66 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
67 * Render
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
68 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
69 void GuiImage::Render(RenderContext& kContext, Camera& kCamera)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
70 {
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
71 if(bVisible)
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
72 {
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
73 if(pTexture)
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
74 {
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
75 uint32 nPasses = 0;
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
76
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
77 const float fOffsetX = -0.5f * ScreenSizeX;
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
78 const float fOffsetY = +0.5f * ScreenSizeY;
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
79
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
80 const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
81
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
82 kContext.ApplyCameraToEffect(kCamera, pEffect);
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
83
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
84 D3DXMATRIX kScale;
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
85 D3DXMatrixScaling(&kScale, kDimensions.x, kDimensions.y, 1.0f);
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
86
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
87 const D3DXVECTOR2& kLocation = GetPosition();
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
88
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
89 D3DXMATRIX kTranslate;
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
90 D3DXMatrixTranslation(&kTranslate, fOffsetX + kLocation.x + 0.5f, fOffsetY - kLocation.y + 0.5f, 0.0f);
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
91
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
92 D3DXMATRIX kWorldMatrix;
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
93 D3DXMatrixMultiply(&kWorldMatrix, &kScale, &kTranslate);
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
94
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
95 pEffect->SetMatrix(pEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
96 pEffect->SetVector(pEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
97 pEffect->SetTexture(pEffect->GetParameterByName(NULL, "kTexture"), pTexture);
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
98
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
99 pEffect->SetTechnique(pEffect->GetTechnique(0));
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
100 pEffect->Begin(&nPasses, 0);
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
101 pEffect->BeginPass(0);
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
102
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
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
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
105 pEffect->EndPass();
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
106 pEffect->End();
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
107 }
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
108
23
a785b0aaf004 More work on the Gui system
koryspansel
parents: 21
diff changeset
109 GuiElement::Render(kContext, kCamera);
18
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
110 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
111 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
112
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
113 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
114 * SetTexture
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
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
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
117 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
118 if(pTexture)
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
119 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
120 pTexture->Release();
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
121 pTexture = NULL;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
122 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
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
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
138 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
139
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
140 /*
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
141 * SetupVertexBuffer
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
142 */
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
143 ErrorCode GuiImage::SetupVertexBuffer()
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
144 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
145 Vertex::Quad* pVertices = NULL;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
146
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
147 HRESULT hResult = pVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
148 if(FAILED(hResult))
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
149 {
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
150 return Error_Fail;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
151 }
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
152
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
153 pVertices[0] = Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
154 pVertices[1] = Vertex::Quad(+0.0f, +0.0f, 1.0f, 0.0f, 0.0f);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
155 pVertices[2] = Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
156 pVertices[3] = Vertex::Quad(+0.0f, -1.0f, 1.0f, 0.0f, 1.0f);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
157 pVertices[4] = Vertex::Quad(+1.0f, +0.0f, 1.0f, 1.0f, 0.0f);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
158 pVertices[5] = Vertex::Quad(+1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
159
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
160 pVertexBuffer->Unlock();
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
161
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
162 return Error_Success;
33cb6979ac51 More work on GUI system
koryspansel
parents:
diff changeset
163 }