2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "GUITextureD3D.h"
23 #include "windowing/WindowingFactory.h"
27 CGUITextureD3D::CGUITextureD3D(float posX, float posY, float width, float height, const CTextureInfo &texture)
28 : CGUITextureBase(posX, posY, width, height, texture)
32 void CGUITextureD3D::Begin(color_t color)
35 CBaseTexture* texture = m_texture.m_textures[m_currentFrame];
36 LPDIRECT3DDEVICE9 p3DDevice = g_Windowing.Get3DDevice();
40 m_diffuse.m_textures[0]->LoadToGPU();
41 // Set state to render the image
42 texture->BindToUnit(unit);
43 p3DDevice->SetTextureStageState( unit, D3DTSS_COLOROP , D3DTOP_MODULATE );
44 p3DDevice->SetTextureStageState( unit, D3DTSS_COLORARG1, D3DTA_TEXTURE );
45 p3DDevice->SetTextureStageState( unit, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
46 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAOP , D3DTOP_MODULATE );
47 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
48 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
53 m_diffuse.m_textures[0]->BindToUnit(1);
54 p3DDevice->SetTextureStageState( unit, D3DTSS_COLORARG1, D3DTA_TEXTURE );
55 p3DDevice->SetTextureStageState( unit, D3DTSS_COLORARG2, D3DTA_CURRENT );
56 p3DDevice->SetTextureStageState( unit, D3DTSS_COLOROP , D3DTOP_MODULATE );
57 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
58 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAARG2, D3DTA_CURRENT );
59 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAOP , D3DTOP_MODULATE );
63 if(g_Windowing.UseLimitedColor())
65 m_col = D3DCOLOR_RGBA(GET_R(color) * (235 - 16) / 255
66 , GET_G(color) * (235 - 16) / 255
67 , GET_B(color) * (235 - 16) / 255
69 p3DDevice->SetTextureStageState( unit, D3DTSS_COLOROP , D3DTOP_ADD );
70 p3DDevice->SetTextureStageState( unit, D3DTSS_COLORARG1, D3DTA_CURRENT) ;
71 p3DDevice->SetRenderState( D3DRS_TEXTUREFACTOR, D3DCOLOR_RGBA(16,16,16, 0) );
72 p3DDevice->SetTextureStageState( unit, D3DTSS_COLORARG2, D3DTA_TFACTOR );
78 p3DDevice->SetTextureStageState( unit, D3DTSS_COLOROP, D3DTOP_DISABLE);
79 p3DDevice->SetTextureStageState( unit, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
81 p3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
82 p3DDevice->SetRenderState( D3DRS_ALPHAREF, 0 );
83 p3DDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
84 p3DDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
85 p3DDevice->SetRenderState( D3DRS_FOGENABLE, FALSE );
86 p3DDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
87 p3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
88 p3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
89 p3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
90 p3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
91 p3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
92 p3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
94 p3DDevice->SetFVF( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2 );
97 void CGUITextureD3D::End()
99 // unset the texture and palette or the texture caching crashes because the runtime still has a reference
100 g_Windowing.Get3DDevice()->SetTexture( 0, NULL );
101 if (m_diffuse.size())
102 g_Windowing.Get3DDevice()->SetTexture( 1, NULL );
105 void CGUITextureD3D::Draw(float *x, float *y, float *z, const CRect &texture, const CRect &diffuse, int orientation)
107 struct CUSTOMVERTEX {
110 FLOAT tu, tv; // Texture coordinates
114 // D3D aligns to half pixel boundaries
115 for (int i = 0; i < 4; i++)
121 CUSTOMVERTEX verts[4];
122 verts[0].x = x[0]; verts[0].y = y[0]; verts[0].z = z[0];
123 verts[0].tu = texture.x1; verts[0].tv = texture.y1;
124 verts[0].tu2 = diffuse.x1; verts[0].tv2 = diffuse.y1;
125 verts[0].color = m_col;
127 verts[1].x = x[1]; verts[1].y = y[1]; verts[1].z = z[1];
130 verts[1].tu = texture.x1;
131 verts[1].tv = texture.y2;
135 verts[1].tu = texture.x2;
136 verts[1].tv = texture.y1;
138 if (m_info.orientation & 4)
140 verts[1].tu2 = diffuse.x1;
141 verts[1].tv2 = diffuse.y2;
145 verts[1].tu2 = diffuse.x2;
146 verts[1].tv2 = diffuse.y1;
148 verts[1].color = m_col;
150 verts[2].x = x[2]; verts[2].y = y[2]; verts[2].z = z[2];
151 verts[2].tu = texture.x2; verts[2].tv = texture.y2;
152 verts[2].tu2 = diffuse.x2; verts[2].tv2 = diffuse.y2;
153 verts[2].color = m_col;
155 verts[3].x = x[3]; verts[3].y = y[3]; verts[3].z = z[3];
158 verts[3].tu = texture.x2;
159 verts[3].tv = texture.y1;
163 verts[3].tu = texture.x1;
164 verts[3].tv = texture.y2;
166 if (m_info.orientation & 4)
168 verts[3].tu2 = diffuse.x2;
169 verts[3].tv2 = diffuse.y1;
173 verts[3].tu2 = diffuse.x1;
174 verts[3].tv2 = diffuse.y2;
176 verts[3].color = m_col;
178 g_Windowing.Get3DDevice()->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, verts, sizeof(CUSTOMVERTEX));
181 void CGUITextureD3D::DrawQuad(const CRect &rect, color_t color, CBaseTexture *texture, const CRect *texCoords)
183 struct CUSTOMVERTEX {
186 FLOAT tu, tv; // Texture coordinates
190 LPDIRECT3DDEVICE9 p3DDevice = g_Windowing.Get3DDevice();
194 texture->LoadToGPU();
195 texture->BindToUnit(0);
196 // Set state to render the image
197 p3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
198 p3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
199 p3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
200 p3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
201 p3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
202 p3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
203 p3DDevice->SetTextureStageState( 1, D3DTSS_COLOROP, D3DTOP_DISABLE);
204 p3DDevice->SetTextureStageState( 1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
207 p3DDevice->SetTexture( 0, NULL );
209 p3DDevice->SetRenderState( D3DRS_ALPHATESTENABLE, TRUE );
210 p3DDevice->SetRenderState( D3DRS_ALPHAREF, 0 );
211 p3DDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );
212 p3DDevice->SetRenderState( D3DRS_ZENABLE, FALSE );
213 p3DDevice->SetRenderState( D3DRS_FOGENABLE, FALSE );
214 p3DDevice->SetRenderState( D3DRS_FOGTABLEMODE, D3DFOG_NONE );
215 p3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID );
216 p3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
217 p3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
218 p3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
219 p3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
220 p3DDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
222 p3DDevice->SetFVF( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX2 );
224 CRect coords = texCoords ? *texCoords : CRect(0.0f, 0.0f, 1.0f, 1.0f);
225 CUSTOMVERTEX verts[4] = {
226 { rect.x1 - 0.5f, rect.y1 - 0.5f, 0, color, coords.x1, coords.y1, 0, 0 },
227 { rect.x2 - 0.5f, rect.y1 - 0.5f, 0, color, coords.x2, coords.y1, 0, 0 },
228 { rect.x2 - 0.5f, rect.y2 - 0.5f, 0, color, coords.x2, coords.y2, 0, 0 },
229 { rect.x1 - 0.5f, rect.y2 - 0.5f, 0, color, coords.x1, coords.y2, 0, 0 },
231 p3DDevice->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, verts, sizeof(CUSTOMVERTEX));
234 p3DDevice->SetTexture( 0, NULL );