1 #include "FirstUiBuilder.h"
2 #include "UIDirect3DPrimaryCanvas.h"
4 #include "UIDirect3DTextureCanvas.h"
9 extern LPDIRECTDRAW7 gDirectDraw
;
10 extern LPDIRECT3D7 gDirect3D
;
11 LPDIRECTDRAWSURFACE7 gPrimarySurface
= 0;
13 namespace UIDirect3DPrimaryCanvasNamespace
15 void updateRenderState (LPDIRECT3DDEVICE7 device
, const D3DRENDERSTATETYPE state
, const DWORD newValue
)
18 if (device
->GetRenderState (state
, &oldValue
) == D3D_OK
&& oldValue
!= newValue
)
19 device
->SetRenderState (state
, newValue
);
22 void updateTextureStageState (LPDIRECT3DDEVICE7 device
, const DWORD stage
, const D3DTEXTURESTAGESTATETYPE type
, const DWORD newValue
)
25 if (device
->GetTextureStageState (stage
, type
, &oldValue
) == D3D_OK
&& oldValue
!= newValue
)
26 device
->SetTextureStageState (stage
, type
, newValue
);
30 using namespace UIDirect3DPrimaryCanvasNamespace
;
32 UIDirect3DPrimaryCanvas::UIDirect3DPrimaryCanvas( const UISize
&size
, HWND hwnd
, bool fullscreen
) :
39 mIsFullscreen
= fullscreen
;
42 mBackBufferSurface
= 0;
46 mShowTriangles
= false;
48 memset( &mPreferedTexturePixelFormat
, 0, sizeof( mPreferedTexturePixelFormat
) );
51 UIDirect3DPrimaryCanvas::~UIDirect3DPrimaryCanvas()
56 bool UIDirect3DPrimaryCanvas::IsA( const UITypeID Type
) const
58 return (Type
== TUIDirect3DPrimaryCanvas
) || UICanvas::IsA( Type
);
61 UIBaseObject
*UIDirect3DPrimaryCanvas::Clone( void ) const
66 void UIDirect3DPrimaryCanvas::Attach( UIBaseObject
*o
)
68 UICanvas::Attach( o
);
70 if( o
&& o
->IsA( TUIDirect3DTextureCanvas
) )
71 mAttachedTextures
.insert( reinterpret_cast<UIDirect3DTextureCanvas
*>(o
) );
74 void UIDirect3DPrimaryCanvas::Detach( UIBaseObject
*o
)
76 if( o
&& o
->IsA( TUIDirect3DTextureCanvas
) )
78 assert( mAttachedTextures
.find( reinterpret_cast<UIDirect3DTextureCanvas
*>(o
) ) != mAttachedTextures
.end() );
79 mAttachedTextures
.erase( mAttachedTextures
.find( reinterpret_cast<UIDirect3DTextureCanvas
*>(o
) ) );
82 UICanvas::Detach( o
);
85 void UIDirect3DPrimaryCanvas::DestroyAll( void )
95 mPrimarySurface
->Release();
99 if( mBackBufferSurface
)
101 mBackBufferSurface
->Release();
102 mBackBufferSurface
= 0;
107 mRenderDevice
->Release();
112 void UIDirect3DPrimaryCanvas::SetSize( const UISize
&NewSize
)
114 UICanvas::SetSize( NewSize
);
118 //----------------------------------------------------------------------
120 void UIDirect3DPrimaryCanvas::ShowTriangles( bool ShowTriangles
)
122 mShowTriangles
= ShowTriangles
;
125 //----------------------------------------------------------------------
127 void UIDirect3DPrimaryCanvas::ShowShaders (bool b
)
132 //----------------------------------------------------------------------
134 long UIDirect3DPrimaryCanvas::GetTriangleCount( void ) const
136 return mTriangleCount
;
139 bool UIDirect3DPrimaryCanvas::Prepare( void ) const
141 return const_cast<UIDirect3DPrimaryCanvas
*>( this )->Generate();
144 bool UIDirect3DPrimaryCanvas::Generate( void )
146 if( mPrimarySurface
)
148 if( mPrimarySurface
&& mPrimarySurface
->IsLost() )
150 HRESULT hr
= mPrimarySurface
->Restore();
156 if( mBackBufferSurface
&& mBackBufferSurface
->IsLost() )
158 HRESULT hr
= mBackBufferSurface
->Restore();
168 DDSURFACEDESC2 ddsd
= { sizeof( ddsd
) };
173 ddsd
.dwFlags
= DDSD_CAPS
| DDSD_BACKBUFFERCOUNT
;
174 ddsd
.ddsCaps
.dwCaps
= DDSCAPS_3DDEVICE
| DDSCAPS_COMPLEX
| DDSCAPS_FLIP
| DDSCAPS_PRIMARYSURFACE
;
175 ddsd
.dwBackBufferCount
= 1;
177 hr
= gDirectDraw
->CreateSurface( &ddsd
, &mPrimarySurface
, 0 );
185 ddsd
.dwFlags
= DDSD_CAPS
;
186 ddsd
.ddsCaps
.dwCaps
= DDSCAPS_PRIMARYSURFACE
;
188 hr
= gDirectDraw
->CreateSurface( &ddsd
, &mPrimarySurface
, 0 );
192 if( hr
== DDERR_PRIMARYSURFACEALREADYEXISTS
)
194 mPrimarySurface
= gPrimarySurface
;
195 mPrimarySurface
->AddRef();
202 gPrimarySurface
= mPrimarySurface
;
205 hr
= gDirectDraw
->CreateClipper( 0, &mClipper
, 0 );
210 hr
= mClipper
->SetHWnd( 0, mWindow
);
216 ddsd
.dwFlags
= DDSD_CAPS
| DDSD_WIDTH
| DDSD_HEIGHT
;
217 ddsd
.dwWidth
= GetWidth();
218 ddsd
.dwHeight
= GetHeight();
219 ddsd
.ddsCaps
.dwCaps
= DDSCAPS_3DDEVICE
| DDSCAPS_OFFSCREENPLAIN
;
220 ddsd
.ddsCaps
.dwCaps2
= 0;
222 hr
= gDirectDraw
->CreateSurface( &ddsd
, &mBackBufferSurface
, 0 );
228 static const GUID
*DevicePreference
[] =
230 &IID_IDirect3DTnLHalDevice
,
231 &IID_IDirect3DHALDevice
,
232 &IID_IDirect3DRGBDevice
,
235 for( int i
= 0; i
< (sizeof( DevicePreference
) / sizeof( *DevicePreference
)); ++i
)
237 DDPIXELFORMAT OldPreferedTexturePixelFormat
;
239 hr
= gDirect3D
->CreateDevice( *DevicePreference
[i
], mIsFullscreen
? mPrimarySurface
: mBackBufferSurface
, &mRenderDevice
);
248 vp
.dwWidth
= GetWidth();
249 vp
.dwHeight
= GetHeight();
253 hr
= mRenderDevice
->SetViewport( &vp
);
258 updateRenderState( mRenderDevice
, D3DRENDERSTATE_LIGHTING
, FALSE
);
259 updateRenderState( mRenderDevice
, D3DRENDERSTATE_CLIPPING
, FALSE
);
260 updateRenderState( mRenderDevice
, D3DRENDERSTATE_CULLMODE
, D3DCULL_NONE
);
261 updateRenderState( mRenderDevice
, D3DRENDERSTATE_COLORVERTEX
, FALSE
);
262 updateRenderState( mRenderDevice
, D3DRENDERSTATE_ZFUNC
, D3DCMP_ALWAYS
);
263 updateRenderState( mRenderDevice
, D3DRENDERSTATE_LOCALVIEWER
, FALSE
);
264 updateRenderState( mRenderDevice
, D3DRENDERSTATE_ALPHABLENDENABLE
, TRUE
);
265 updateRenderState( mRenderDevice
, D3DRENDERSTATE_SRCBLEND
, D3DBLEND_SRCALPHA
);
266 updateRenderState( mRenderDevice
, D3DRENDERSTATE_DESTBLEND
, D3DBLEND_INVSRCALPHA
);
268 updateTextureStageState( mRenderDevice
, 0, D3DTSS_MAGFILTER
, D3DTFG_POINT
);
269 updateTextureStageState( mRenderDevice
, 0, D3DTSS_MINFILTER
, D3DTFG_POINT
);
271 updateTextureStageState( mRenderDevice
, 0, D3DTSS_COLOROP
, D3DTOP_MODULATE
);
272 updateTextureStageState( mRenderDevice
, 0, D3DTSS_COLORARG1
, D3DTA_TEXTURE
);
273 updateTextureStageState( mRenderDevice
, 0, D3DTSS_COLORARG2
, D3DTA_DIFFUSE
);
274 updateTextureStageState( mRenderDevice
, 0, D3DTSS_ALPHAOP
, D3DTOP_MODULATE
);
275 updateTextureStageState( mRenderDevice
, 0, D3DTSS_ALPHAARG1
, D3DTA_TEXTURE
);
276 updateTextureStageState( mRenderDevice
, 0, D3DTSS_ALPHAARG2
, D3DTA_DIFFUSE
);
278 updateTextureStageState( mRenderDevice
, 0, D3DTSS_ADDRESSU
, D3DTADDRESS_WRAP
);
279 updateTextureStageState( mRenderDevice
, 0, D3DTSS_ADDRESSV
, D3DTADDRESS_WRAP
);
281 OldPreferedTexturePixelFormat
= mPreferedTexturePixelFormat
;
282 ZeroMemory( &mPreferedTexturePixelFormat
, sizeof(mPreferedTexturePixelFormat
) );
283 hr
= mRenderDevice
->EnumTextureFormats( D3DEnumPixelFormatsStaticCallback
, this );
288 if( memcmp( &OldPreferedTexturePixelFormat
, &mPreferedTexturePixelFormat
, sizeof( mPreferedTexturePixelFormat
) ) )
290 UIDirect3DTextureCanvasSet::iterator i
;
292 for( i
= mAttachedTextures
.begin(); i
!= mAttachedTextures
.end(); ++i
)
293 (*i
)->NotifyRenderCanvasChanged();
311 if( mPrimarySurface
)
313 mPrimarySurface
->Release();
317 if( mBackBufferSurface
)
319 mBackBufferSurface
->Release();
320 mBackBufferSurface
= 0;
325 mRenderDevice
->Release();
333 void UIDirect3DPrimaryCanvas::ClearTo( const UIColor
&c
, const UIRect
& rc
)
335 //-----------------------------------------------------------------
336 //-- don't count triangles for clearing
340 UICanvas::ClearTo (c
, rc
);
343 void UIDirect3DPrimaryCanvas::EnableFiltering( bool NewValue
)
347 updateTextureStageState( mRenderDevice
, 0, D3DTSS_MAGFILTER
, D3DTFG_LINEAR
);
348 updateTextureStageState( mRenderDevice
, 0, D3DTSS_MINFILTER
, D3DTFG_LINEAR
);
352 updateTextureStageState( mRenderDevice
, 0, D3DTSS_MAGFILTER
, D3DTFG_POINT
);
353 updateTextureStageState( mRenderDevice
, 0, D3DTSS_MINFILTER
, D3DTFG_POINT
);
357 //----------------------------------------------------------------------
359 // Verts are passed in in this order:
368 void UIDirect3DPrimaryCanvas::RenderQuad( const UICanvas
* const src
, const UIFloatPoint VerticesIn
[4], const UIFloatPoint UVs
[4] )
370 static D3DTLVERTEX D3DVertices
[4] =
372 { 0, 0, 0, 1, 0, 0, 0, 0 },
373 { 0, 0, 0, 1, 0, 0, 0, 0 },
374 { 0, 0, 0, 1, 0, 0, 0, 0 },
375 { 0, 0, 0, 1, 0, 0, 0, 0 },
378 const UIDirect3DTextureCanvas
* SourceCanvas
= 0;
382 assert( src
->IsA( TUIDirect3DTextureCanvas
) );
384 SourceCanvas
= static_cast<const UIDirect3DTextureCanvas
*>(src
);
386 // if( mCurrentTexture != SourceCanvas->GetSurface() )
388 mCurrentTexture
= SourceCanvas
->GetSurface();
389 mRenderDevice
->SetTexture( 0, mCurrentTexture
);
393 mRenderDevice
->SetTexture( 0, 0 );
395 DWORD dwVertexColor
= mState
.Color
.FormatRGBA();
397 UIFloatPoint
const * Vertices
= VerticesIn
;
400 UIFloatPoint NewVertices
[4];
401 Deform(Vertices
, NewVertices
, 4);
402 Vertices
= NewVertices
;
405 // Copy data to vertex buffer, offseting geometry
406 // so texture centers lie on pixel centers
407 D3DVertices
[0].sx
= Vertices
[0].x
- 0.5f
;
408 D3DVertices
[0].sy
= Vertices
[0].y
- 0.5f
;
409 D3DVertices
[0].tu
= UVs
[0].x
;
410 D3DVertices
[0].tv
= UVs
[0].y
;
411 D3DVertices
[0].color
= dwVertexColor
;
412 D3DVertices
[1].sx
= Vertices
[1].x
- 0.5f
;
413 D3DVertices
[1].sy
= Vertices
[1].y
- 0.5f
;
414 D3DVertices
[1].tu
= UVs
[1].x
;
415 D3DVertices
[1].tv
= UVs
[1].y
;
416 D3DVertices
[1].color
= dwVertexColor
;
417 D3DVertices
[2].sx
= Vertices
[2].x
- 0.5f
;
418 D3DVertices
[2].sy
= Vertices
[2].y
- 0.5f
;
419 D3DVertices
[2].tu
= UVs
[2].x
;
420 D3DVertices
[2].tv
= UVs
[2].y
;
421 D3DVertices
[2].color
= dwVertexColor
;
422 D3DVertices
[3].sx
= Vertices
[3].x
- 0.5f
;
423 D3DVertices
[3].sy
= Vertices
[3].y
- 0.5f
;
424 D3DVertices
[3].tu
= UVs
[3].x
;
425 D3DVertices
[3].tv
= UVs
[3].y
;
426 D3DVertices
[3].color
= dwVertexColor
;
430 UI_IGNORE_RETURN (mRenderDevice
->DrawPrimitive( D3DPT_TRIANGLESTRIP
, D3DFVF_TLVERTEX
, &D3DVertices
, 4, 0 ));
436 static_cast<long>(Vertices
[0].x
), static_cast<long>(Vertices
[0].y
),
437 static_cast<long>(Vertices
[1].x
), static_cast<long>(Vertices
[1].y
),
438 static_cast<long>(Vertices
[2].x
), static_cast<long>(Vertices
[2].y
),
439 static_cast<long>(Vertices
[3].x
), static_cast<long>(Vertices
[3].y
)));
442 if (mShowShaders
&& SourceCanvas
&& SourceCanvas
->GetHasShader ())
444 mShaderQuads
.push_back(
446 static_cast<long>(Vertices
[0].x
), static_cast<long>(Vertices
[0].y
),
447 static_cast<long>(Vertices
[1].x
), static_cast<long>(Vertices
[1].y
),
448 static_cast<long>(Vertices
[2].x
), static_cast<long>(Vertices
[2].y
),
449 static_cast<long>(Vertices
[3].x
), static_cast<long>(Vertices
[3].y
)));
454 //----------------------------------------------------------------------
456 // Verts are passed in in this order:
465 void UIDirect3DPrimaryCanvas::RenderQuad( const UICanvas
* const src
, const UIFloatPoint VerticesIn
[4], const UIFloatPoint UVs
[4], const UIColor Colors
[4] )
467 static D3DTLVERTEX D3DVertices
[4] =
469 { 0, 0, 0, 1, 0, 0, 0, 0 },
470 { 0, 0, 0, 1, 0, 0, 0, 0 },
471 { 0, 0, 0, 1, 0, 0, 0, 0 },
472 { 0, 0, 0, 1, 0, 0, 0, 0 },
475 const UIDirect3DTextureCanvas
* SourceCanvas
= 0;
479 assert( src
->IsA( TUIDirect3DTextureCanvas
) );
481 SourceCanvas
= static_cast<const UIDirect3DTextureCanvas
*>(src
);
483 // if( mCurrentTexture != SourceCanvas->GetSurface() )
485 mCurrentTexture
= SourceCanvas
->GetSurface();
486 mRenderDevice
->SetTexture( 0, mCurrentTexture
);
490 mRenderDevice
->SetTexture( 0, 0 );
492 UIFloatPoint
const * Vertices
= VerticesIn
;
495 UIFloatPoint NewVertices
[4];
496 Deform(Vertices
, NewVertices
, 4);
497 Vertices
= NewVertices
;
500 // Copy data to vertex buffer, offseting geometry
501 // so texture centers lie on pixel centers
502 D3DVertices
[0].sx
= Vertices
[0].x
- 0.5f
;
503 D3DVertices
[0].sy
= Vertices
[0].y
- 0.5f
;
504 D3DVertices
[0].tu
= UVs
[0].x
;
505 D3DVertices
[0].tv
= UVs
[0].y
;
506 D3DVertices
[0].color
= Colors
[0].FormatRGBA();
507 D3DVertices
[1].sx
= Vertices
[1].x
- 0.5f
;
508 D3DVertices
[1].sy
= Vertices
[1].y
- 0.5f
;
509 D3DVertices
[1].tu
= UVs
[1].x
;
510 D3DVertices
[1].tv
= UVs
[1].y
;
511 D3DVertices
[1].color
= Colors
[1].FormatRGBA();
512 D3DVertices
[2].sx
= Vertices
[2].x
- 0.5f
;
513 D3DVertices
[2].sy
= Vertices
[2].y
- 0.5f
;
514 D3DVertices
[2].tu
= UVs
[2].x
;
515 D3DVertices
[2].tv
= UVs
[2].y
;
516 D3DVertices
[2].color
= Colors
[2].FormatRGBA();
517 D3DVertices
[3].sx
= Vertices
[3].x
- 0.5f
;
518 D3DVertices
[3].sy
= Vertices
[3].y
- 0.5f
;
519 D3DVertices
[3].tu
= UVs
[3].x
;
520 D3DVertices
[3].tv
= UVs
[3].y
;
521 D3DVertices
[3].color
= Colors
[3].FormatRGBA();
525 UI_IGNORE_RETURN (mRenderDevice
->DrawPrimitive( D3DPT_TRIANGLESTRIP
, D3DFVF_TLVERTEX
, &D3DVertices
, 4, 0 ));
531 static_cast<long>(Vertices
[0].x
), static_cast<long>(Vertices
[0].y
),
532 static_cast<long>(Vertices
[1].x
), static_cast<long>(Vertices
[1].y
),
533 static_cast<long>(Vertices
[2].x
), static_cast<long>(Vertices
[2].y
),
534 static_cast<long>(Vertices
[3].x
), static_cast<long>(Vertices
[3].y
)));
537 if (mShowShaders
&& SourceCanvas
&& SourceCanvas
->GetHasShader ())
539 mShaderQuads
.push_back(
541 static_cast<long>(Vertices
[0].x
), static_cast<long>(Vertices
[0].y
),
542 static_cast<long>(Vertices
[1].x
), static_cast<long>(Vertices
[1].y
),
543 static_cast<long>(Vertices
[2].x
), static_cast<long>(Vertices
[2].y
),
544 static_cast<long>(Vertices
[3].x
), static_cast<long>(Vertices
[3].y
)));
548 //----------------------------------------------------------------------
550 void UIDirect3DPrimaryCanvas::BltFrom( const UICanvas
* const src
, const UIPoint
&Source
, const UIPoint
&Destination
, const UISize
&Size
)
553 static_cast<const UIDirect3DTextureCanvas
*>( src
)->Prepare();
557 UICanvas::BltFrom( src
, Source
, Destination
, Size
);
560 //----------------------------------------------------------------------
562 void UIDirect3DPrimaryCanvas::BltFromNoScaleOrRotate( const UICanvas
* const srcarg
, const UIPoint
&Source
, const UIPoint
&Destination
, const UISize
&Size
)
564 BltFrom( srcarg
, Source
, Destination
, Size
);
568 //----------------------------------------------------------------------
570 void UIDirect3DPrimaryCanvas::RenderLines (const UICanvas
* const , int numLines
, const UILine
* lines
, const UILine
* )
572 const DWORD dwVertexColor
= mState
.Color
.FormatRGBA();
574 mRenderDevice
->SetTexture( 0, 0 );
577 for (int i
= 0; i
< numLines
; ++i
)
579 const UILine
& theLine
= lines
[i
];
581 static D3DTLVERTEX Vertices
[2] =
583 { 0, 0, 0, 1, 0, 0, 0, 0 },
584 { 0, 0, 0, 1, 0, 0, 0, 0 },
589 p
= Transform( theLine
.p1
);
590 Vertices
[0].sx
= p
.x
;
591 Vertices
[0].sy
= p
.y
;
592 Vertices
[0].color
= dwVertexColor
;
594 p
= Transform( theLine
.p2
);
595 Vertices
[1].sx
= p
.x
;
596 Vertices
[1].sy
= p
.y
;
597 Vertices
[1].color
= dwVertexColor
;
601 UI_IGNORE_RETURN (mRenderDevice
->DrawPrimitive( D3DPT_LINELIST
, D3DFVF_TLVERTEX
, &Vertices
, 2, 0 ));
605 //----------------------------------------------------------------------
607 void UIDirect3DPrimaryCanvas::RenderLines(const UICanvas
* const , int numLines
, const UILine
* lines
, const UIFloatPoint
* uvs
, const UIColor
* colors
)
609 mRenderDevice
->SetTexture( 0, 0 );
612 static D3DTLVERTEX Vertices
[2];
613 static UIFloatPoint p
;
615 for (int i
= 0; i
< numLines
; ++i
)
617 const UILine
& theLine
= lines
[i
];
620 Vertices
[0].sx
= theLine
.p1
.x
;
621 Vertices
[0].sy
= theLine
.p1
.y
;
622 Vertices
[0].dvTU
= (uvs
+ index
)->x
;
623 Vertices
[0].dvTV
= (uvs
+ index
)->y
;
624 Vertices
[0].color
= (colors
+ index
)->FormatRGBA();
626 Vertices
[1].sx
= theLine
.p2
.x
;
627 Vertices
[1].sy
= theLine
.p2
.y
;
628 Vertices
[1].dvTU
= (uvs
+ index
+ 1)->x
;
629 Vertices
[1].dvTV
= (uvs
+ index
+ 1)->y
;
630 Vertices
[1].color
= (colors
+ index
+ 1)->FormatRGBA();
634 UI_IGNORE_RETURN (mRenderDevice
->DrawPrimitive( D3DPT_LINELIST
, D3DFVF_TLVERTEX
, &Vertices
, 2, 0 ));
638 //----------------------------------------------------------------------
640 void UIDirect3DPrimaryCanvas::RenderTriangles (const UICanvas
* const src
, int numTris
, const UITriangle
* tris
, const UITriangle
* uvs
)
642 const DWORD dwVertexColor
= mState
.Color
.FormatRGBA();
646 assert( src
->IsA( TUIDirect3DTextureCanvas
) );
648 const UIDirect3DTextureCanvas
* const SourceCanvas
= static_cast<const UIDirect3DTextureCanvas
*>(src
);
649 mCurrentTexture
= SourceCanvas
->GetSurface();
650 mRenderDevice
->SetTexture( 0, mCurrentTexture
);
653 mRenderDevice
->SetTexture( 0, 0 );
655 for (int i
= 0; i
< numTris
; ++i
)
657 const UITriangle
& theTri
= tris
[i
];
658 const UITriangle
& theUvs
= uvs
[i
];
660 static D3DTLVERTEX Vertices
[3] =
662 { 0, 0, 0, 1, 0, 0, 0, 0 },
663 { 0, 0, 0, 1, 0, 0, 0, 0 },
664 { 0, 0, 0, 1, 0, 0, 0, 0 }
672 Vertices
[0].sx
= p
.x
;
673 Vertices
[0].sy
= p
.y
;
674 Vertices
[0].tu
= uv
.x
;
675 Vertices
[0].tv
= uv
.y
;
676 Vertices
[0].color
= dwVertexColor
;
680 Vertices
[1].sx
= p
.x
;
681 Vertices
[1].sy
= p
.y
;
682 Vertices
[1].tu
= uv
.x
;
683 Vertices
[1].tv
= uv
.y
;
684 Vertices
[1].color
= dwVertexColor
;
688 Vertices
[2].sx
= p
.x
;
689 Vertices
[2].sy
= p
.y
;
690 Vertices
[2].tu
= uv
.x
;
691 Vertices
[2].tv
= uv
.y
;
692 Vertices
[2].color
= dwVertexColor
;
700 static_cast<long>(Vertices
[0].sx
), static_cast<long>(Vertices
[0].sy
),
701 static_cast<long>(Vertices
[1].sx
), static_cast<long>(Vertices
[1].sy
),
702 static_cast<long>(Vertices
[2].sx
), static_cast<long>(Vertices
[2].sy
),
703 static_cast<long>(Vertices
[0].sx
), static_cast<long>(Vertices
[0].sy
)));
706 UI_IGNORE_RETURN (mRenderDevice
->DrawPrimitive( D3DPT_TRIANGLELIST
, D3DFVF_TLVERTEX
, &Vertices
, 3, 0 ));
710 //----------------------------------------------------------------------
712 bool UIDirect3DPrimaryCanvas::BeginRendering( void )
714 UICanvas::BeginRendering();
720 if( !Prepare() || !mRenderDevice
)
723 hr
= mPrimarySurface
->SetClipper( mClipper
);
728 hr
= mRenderDevice
->BeginScene();
738 //----------------------------------------------------------------------
740 void UIDirect3DPrimaryCanvas::EndRendering( void )
742 UICanvas::EndRendering();
744 assert( mRenderDevice
);
746 mRenderDevice
->SetTexture( 0, 0 );
748 mRenderDevice
->GetRenderState( D3DRENDERSTATE_FILLMODE
, &dwOldState
);
750 if( mShowTriangles
&& !mQuads
.empty ())
752 D3DTLVERTEX Vertices
[4];
754 updateRenderState( mRenderDevice
, D3DRENDERSTATE_FILLMODE
, D3DFILL_WIREFRAME
);
756 for( int i
= 0; i
< 4; ++i
)
758 Vertices
[i
].sz
= 0.0;
759 Vertices
[i
].rhw
= 1.0;
760 Vertices
[i
].color
= 0xFFFFFFFF;
763 for( UIQuadList::iterator CurrentQuad
= mQuads
.begin(); CurrentQuad
!= mQuads
.end(); ++CurrentQuad
)
765 const UIQuad
& q
= *CurrentQuad
;
768 Vertices
[0].sx
= static_cast<float>(q
.p1
.x
);
769 Vertices
[0].sy
= static_cast<float>(q
.p1
.y
);
772 Vertices
[1].sx
= static_cast<float>(q
.p2
.x
);
773 Vertices
[1].sy
= static_cast<float>(q
.p2
.y
);
776 Vertices
[2].sx
= static_cast<float>(q
.p3
.x
);
777 Vertices
[2].sy
= static_cast<float>(q
.p3
.y
);
780 Vertices
[3].sx
= static_cast<float>(q
.p4
.x
);
781 Vertices
[3].sy
= static_cast<float>(q
.p4
.y
);
783 mRenderDevice
->DrawPrimitive( D3DPT_TRIANGLESTRIP
, D3DFVF_TLVERTEX
, &Vertices
, 4, 0 );
788 //----------------------------------------------------------------------
790 if(!mShaderQuads
.empty ())
792 D3DTLVERTEX Vertices
[5];
794 updateRenderState( mRenderDevice
, D3DRENDERSTATE_FILLMODE
, D3DFILL_WIREFRAME
);
796 for( int i
= 0; i
< 4; ++i
)
798 Vertices
[i
].sz
= 0.0;
799 Vertices
[i
].rhw
= 1.0;
800 Vertices
[i
].color
= 0xaaFFaa00;
803 for( UIQuadList::iterator CurrentQuad
= mShaderQuads
.begin(); CurrentQuad
!= mShaderQuads
.end(); ++CurrentQuad
)
805 const UIQuad
& q
= *CurrentQuad
;
808 Vertices
[0].sx
= static_cast<float>(q
.p1
.x
);
809 Vertices
[0].sy
= static_cast<float>(q
.p1
.y
);
812 Vertices
[1].sx
= static_cast<float>(q
.p2
.x
);
813 Vertices
[1].sy
= static_cast<float>(q
.p2
.y
);
816 Vertices
[2].sx
= static_cast<float>(q
.p4
.x
);
817 Vertices
[2].sy
= static_cast<float>(q
.p4
.y
);
820 Vertices
[3].sx
= static_cast<float>(q
.p3
.x
);
821 Vertices
[3].sy
= static_cast<float>(q
.p3
.y
);
824 Vertices
[4] = Vertices
[0];
826 mRenderDevice
->DrawPrimitive( D3DPT_LINESTRIP
, D3DFVF_TLVERTEX
, &Vertices
, 5, 0 );
828 mShaderQuads
.clear();
831 updateRenderState( mRenderDevice
, D3DRENDERSTATE_FILLMODE
, dwOldState
);
833 mRenderDevice
->EndScene();
836 //----------------------------------------------------------------------
838 void UIDirect3DPrimaryCanvas::Flip( void )
842 assert( mPrimarySurface
);
843 assert( mIsFullscreen
|| mBackBufferSurface
);
846 hr
= mPrimarySurface
->Flip( 0, DDFLIP_WAIT
);
856 ClientToScreen( mWindow
, &theOrigin
);
858 rcDest
.left
= theOrigin
.x
;
859 rcDest
.top
= theOrigin
.y
;
860 rcDest
.right
= theOrigin
.x
+ GetWidth();
861 rcDest
.bottom
= theOrigin
.y
+ GetHeight();
865 rcSrc
.right
= GetWidth();
866 rcSrc
.bottom
= GetHeight();
868 hr
= mPrimarySurface
->Blt( &rcDest
, mBackBufferSurface
, &rcSrc
, DDBLT_WAIT
, 0 );
872 void UIDirect3DPrimaryCanvas::GetPreferedPixelFormat( LPDDPIXELFORMAT lpddpf
)
874 *lpddpf
= mPreferedTexturePixelFormat
;
877 HRESULT
UIDirect3DPrimaryCanvas::D3DEnumPixelFormats( LPDDPIXELFORMAT lppf
)
879 // We need surfaces with both RGB and alpha components
880 if( !(lppf
->dwFlags
& DDPF_RGB
) || !(lppf
->dwFlags
& DDPF_ALPHAPIXELS
) )
881 return D3DENUMRET_OK
;
883 // Alpha must be at least 8 bits
884 if( lppf
->dwAlphaBitDepth
< 8 )
885 return D3DENUMRET_OK
;
887 // RGB must be at least 16 bits
888 if( lppf
->dwRGBBitCount
< 16 )
889 return D3DENUMRET_OK
;
891 // If this is the first pixel format that meets these criteria use it for now
892 if( mPreferedTexturePixelFormat
.dwSize
== 0 )
894 mPreferedTexturePixelFormat
= *lppf
;
895 return D3DENUMRET_OK
;
898 if( lppf
->dwRGBBitCount
>= mPreferedTexturePixelFormat
.dwRGBBitCount
)
899 mPreferedTexturePixelFormat
= *lppf
;
901 return D3DENUMRET_OK
;
904 HRESULT CALLBACK
UIDirect3DPrimaryCanvas::D3DEnumPixelFormatsStaticCallback( LPDDPIXELFORMAT lppf
, LPVOID arg
)
906 return reinterpret_cast<UIDirect3DPrimaryCanvas
*>(arg
)->D3DEnumPixelFormats( lppf
);
909 HWND
UIDirect3DPrimaryCanvas::GetWindow( void )
914 bool UIDirect3DPrimaryCanvas::MatchPixelFormat( LPDDPIXELFORMAT FormatToMatch
, LPDDPIXELFORMAT BestMatch
)
918 mBestMatchFound
= false;
919 mFormatToMatch
= *FormatToMatch
;
921 hr
= mRenderDevice
->EnumTextureFormats( D3DEnumPixelFormatsForBestMatchStaticCallback
, this );
923 if( FAILED( hr
) || !mBestMatchFound
)
926 *BestMatch
= mBestMatchPixelFormat
;
930 HRESULT
UIDirect3DPrimaryCanvas::D3DEnumPixelFormatsForBestMatch( LPDDPIXELFORMAT lppf
)
932 bool UseThisFormat
= false;
934 if( mFormatToMatch
.dwFlags
& DDPF_FOURCC
)
936 if( ( lppf
->dwFlags
& DDPF_FOURCC
) && (mFormatToMatch
.dwFourCC
== lppf
->dwFourCC
) )
937 UseThisFormat
= true;
941 if( (mFormatToMatch
.dwFlags
& DDPF_ALPHAPIXELS
) == (lppf
->dwFlags
& DDPF_ALPHAPIXELS
) )
943 if( mFormatToMatch
.dwRGBBitCount
== lppf
->dwRGBBitCount
)
944 UseThisFormat
= true;
950 mBestMatchPixelFormat
= *lppf
;
951 mBestMatchFound
= true;
952 return D3DENUMRET_CANCEL
;
955 return D3DENUMRET_OK
;
958 HRESULT CALLBACK
UIDirect3DPrimaryCanvas::D3DEnumPixelFormatsForBestMatchStaticCallback( LPDDPIXELFORMAT lppf
, LPVOID arg
)
960 return reinterpret_cast<UIDirect3DPrimaryCanvas
*>(arg
)->D3DEnumPixelFormatsForBestMatch( lppf
);
963 //----------------------------------------------------------------------
965 bool UIDirect3DPrimaryCanvas::GetShowShaders () const