dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / application / UiBuilder / UIDirect3DPrimaryCanvas.cpp
blob934d15c2ecfd57962c88f02eadaed9dbfe48a449
1 #include "FirstUiBuilder.h"
2 #include "UIDirect3DPrimaryCanvas.h"
4 #include "UIDirect3DTextureCanvas.h"
5 #include "UIUtils.h"
7 #include <cmath>
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)
17 DWORD oldValue;
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)
24 DWORD oldValue;
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 ) :
33 UICanvas( size ),
34 mQuads (),
35 mShaderQuads (),
36 mShowShaders (true)
38 mWindow = hwnd;
39 mIsFullscreen = fullscreen;
41 mPrimarySurface = 0;
42 mBackBufferSurface = 0;
43 mRenderDevice = 0;
44 mClipper = 0;
45 mCurrentTexture = 0;
46 mShowTriangles = false;
48 memset( &mPreferedTexturePixelFormat, 0, sizeof( mPreferedTexturePixelFormat ) );
51 UIDirect3DPrimaryCanvas::~UIDirect3DPrimaryCanvas()
53 DestroyAll();
56 bool UIDirect3DPrimaryCanvas::IsA( const UITypeID Type ) const
58 return (Type == TUIDirect3DPrimaryCanvas) || UICanvas::IsA( Type );
61 UIBaseObject *UIDirect3DPrimaryCanvas::Clone( void ) const
63 return 0;
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 )
87 if( mClipper )
89 mClipper->Release();
90 mClipper = 0;
93 if( mPrimarySurface )
95 mPrimarySurface->Release();
96 mPrimarySurface = 0;
99 if( mBackBufferSurface )
101 mBackBufferSurface->Release();
102 mBackBufferSurface = 0;
105 if( mRenderDevice )
107 mRenderDevice->Release();
108 mRenderDevice = 0;
112 void UIDirect3DPrimaryCanvas::SetSize( const UISize &NewSize )
114 UICanvas::SetSize( NewSize );
115 DestroyAll();
118 //----------------------------------------------------------------------
120 void UIDirect3DPrimaryCanvas::ShowTriangles( bool ShowTriangles )
122 mShowTriangles = ShowTriangles;
125 //----------------------------------------------------------------------
127 void UIDirect3DPrimaryCanvas::ShowShaders (bool b)
129 mShowShaders = 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();
152 if( FAILED( hr ) )
153 return false;
156 if( mBackBufferSurface && mBackBufferSurface->IsLost() )
158 HRESULT hr = mBackBufferSurface->Restore();
160 if( FAILED( hr ) )
161 return false;
163 return true;
168 DDSURFACEDESC2 ddsd = { sizeof( ddsd ) };
169 HRESULT hr;
171 if( mIsFullscreen )
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 );
179 if( FAILED( hr ) )
180 throw hr;
182 else
184 // Primary surface
185 ddsd.dwFlags = DDSD_CAPS;
186 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
188 hr = gDirectDraw->CreateSurface( &ddsd, &mPrimarySurface, 0 );
190 if( FAILED( hr ) )
192 if( hr == DDERR_PRIMARYSURFACEALREADYEXISTS )
194 mPrimarySurface = gPrimarySurface;
195 mPrimarySurface->AddRef();
197 else
198 throw hr;
200 else
202 gPrimarySurface = mPrimarySurface;
205 hr = gDirectDraw->CreateClipper( 0, &mClipper, 0 );
207 if( FAILED( hr ) )
208 throw hr;
210 hr = mClipper->SetHWnd( 0, mWindow );
212 if( FAILED( hr ) )
213 throw hr;
215 // Back buffer
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 );
224 if( FAILED( hr ) )
225 throw hr;
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 );
241 if( FAILED(hr) )
242 continue;
244 D3DVIEWPORT7 vp;
246 vp.dwX = 0;
247 vp.dwY = 0;
248 vp.dwWidth = GetWidth();
249 vp.dwHeight = GetHeight();
250 vp.dvMinZ = 0.0f;
251 vp.dvMaxZ = 1.0f;
253 hr = mRenderDevice->SetViewport( &vp );
255 if( FAILED(hr) )
256 throw hr;
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 );
285 if( FAILED(hr) )
286 throw hr;
288 if( memcmp( &OldPreferedTexturePixelFormat, &mPreferedTexturePixelFormat, sizeof( mPreferedTexturePixelFormat ) ) )
290 UIDirect3DTextureCanvasSet::iterator i;
292 for( i = mAttachedTextures.begin(); i != mAttachedTextures.end(); ++i )
293 (*i)->NotifyRenderCanvasChanged();
296 return true;
299 throw hr;
301 catch( HRESULT hr )
303 hr = hr;
305 if( mClipper )
307 mClipper->Release();
308 mClipper = 0;
311 if( mPrimarySurface )
313 mPrimarySurface->Release();
314 mPrimarySurface = 0;
317 if( mBackBufferSurface )
319 mBackBufferSurface->Release();
320 mBackBufferSurface = 0;
323 if( mRenderDevice )
325 mRenderDevice->Release();
326 mRenderDevice = 0;
330 return false;
333 void UIDirect3DPrimaryCanvas::ClearTo( const UIColor &c, const UIRect & rc)
335 //-----------------------------------------------------------------
336 //-- don't count triangles for clearing
338 mTriangleCount -= 2;
340 UICanvas::ClearTo (c, rc);
343 void UIDirect3DPrimaryCanvas::EnableFiltering( bool NewValue )
345 if( NewValue )
347 updateTextureStageState( mRenderDevice, 0, D3DTSS_MAGFILTER, D3DTFG_LINEAR );
348 updateTextureStageState( mRenderDevice, 0, D3DTSS_MINFILTER, D3DTFG_LINEAR );
350 else
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:
360 // 0-----1
361 // | /|
362 // | / |
363 // | / |
364 // | / |
365 // |/ |
366 // 2-----3
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;
380 if( src )
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 );
392 else
393 mRenderDevice->SetTexture( 0, 0 );
395 DWORD dwVertexColor = mState.Color.FormatRGBA();
397 UIFloatPoint const * Vertices = VerticesIn;
398 if (IsDeforming())
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;
428 mTriangleCount += 2;
430 UI_IGNORE_RETURN (mRenderDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, D3DFVF_TLVERTEX, &D3DVertices, 4, 0 ));
432 if (mShowTriangles)
434 mQuads.push_back(
435 UIQuad(
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(
445 UIQuad(
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:
457 // 0-----1
458 // | /|
459 // | / |
460 // | / |
461 // | / |
462 // |/ |
463 // 2-----3
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;
477 if( src )
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 );
489 else
490 mRenderDevice->SetTexture( 0, 0 );
492 UIFloatPoint const * Vertices = VerticesIn;
493 if (IsDeforming())
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();
523 mTriangleCount += 2;
525 UI_IGNORE_RETURN (mRenderDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, D3DFVF_TLVERTEX, &D3DVertices, 4, 0 ));
527 if (mShowTriangles)
529 mQuads.push_back(
530 UIQuad(
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(
540 UIQuad(
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 )
552 if( src )
553 static_cast<const UIDirect3DTextureCanvas *>( src )->Prepare();
555 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 );
575 mCurrentTexture = 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 },
587 UIFloatPoint p;
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;
599 mTriangleCount += 1;
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 );
610 mCurrentTexture = 0;
612 static D3DTLVERTEX Vertices[2];
613 static UIFloatPoint p;
615 for (int i = 0; i < numLines; ++i)
617 const UILine & theLine = lines [i];
618 int index = i * 2;
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();
632 mTriangleCount += 1;
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();
644 if( src )
646 assert( src->IsA( TUIDirect3DTextureCanvas ) );
648 const UIDirect3DTextureCanvas * const SourceCanvas = static_cast<const UIDirect3DTextureCanvas *>(src);
649 mCurrentTexture = SourceCanvas->GetSurface();
650 mRenderDevice->SetTexture( 0, mCurrentTexture );
652 else
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 }
667 UIFloatPoint p;
668 UIFloatPoint uv;
670 p = theTri.p1;
671 uv = theUvs.p1;
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;
678 p = theTri.p2;
679 uv = theUvs.p2;
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;
686 p = theTri.p3;
687 uv = theUvs.p3;
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;
694 ++mTriangleCount;
696 if (mShowTriangles)
698 mQuads.push_back(
699 UIQuad(
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();
716 HRESULT hr;
718 mCurrentTexture = 0;
720 if( !Prepare() || !mRenderDevice )
721 return false;
723 hr = mPrimarySurface->SetClipper( mClipper );
725 if( FAILED( hr ) )
726 return false;
728 hr = mRenderDevice->BeginScene();
730 if( FAILED(hr) )
731 return false;
733 mTriangleCount = 0;
735 return true;
738 //----------------------------------------------------------------------
740 void UIDirect3DPrimaryCanvas::EndRendering( void )
742 UICanvas::EndRendering();
744 assert( mRenderDevice );
746 mRenderDevice->SetTexture( 0, 0 );
747 DWORD dwOldState;
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;
767 // Top Left
768 Vertices[0].sx = static_cast<float>(q.p1.x);
769 Vertices[0].sy = static_cast<float>(q.p1.y);
771 // Top Right
772 Vertices[1].sx = static_cast<float>(q.p2.x);
773 Vertices[1].sy = static_cast<float>(q.p2.y);
775 // Bottom Left
776 Vertices[2].sx = static_cast<float>(q.p3.x);
777 Vertices[2].sy = static_cast<float>(q.p3.y);
779 // Bottom Right
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 );
785 mQuads.clear();
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;
807 // Top Left
808 Vertices[0].sx = static_cast<float>(q.p1.x);
809 Vertices[0].sy = static_cast<float>(q.p1.y);
811 // Top Right
812 Vertices[1].sx = static_cast<float>(q.p2.x);
813 Vertices[1].sy = static_cast<float>(q.p2.y);
815 // Bottom Right
816 Vertices[2].sx = static_cast<float>(q.p4.x);
817 Vertices[2].sy = static_cast<float>(q.p4.y);
819 // Bottom Left
820 Vertices[3].sx = static_cast<float>(q.p3.x);
821 Vertices[3].sy = static_cast<float>(q.p3.y);
823 // Top Left
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 )
840 HRESULT hr;
842 assert( mPrimarySurface );
843 assert( mIsFullscreen || mBackBufferSurface );
845 if( mIsFullscreen )
846 hr = mPrimarySurface->Flip( 0, DDFLIP_WAIT );
847 else
849 POINT theOrigin;
850 RECT rcDest;
851 RECT rcSrc;
853 theOrigin.x = 0;
854 theOrigin.y = 0;
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();
863 rcSrc.left = 0;
864 rcSrc.top = 0;
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 )
911 return mWindow;
914 bool UIDirect3DPrimaryCanvas::MatchPixelFormat( LPDDPIXELFORMAT FormatToMatch, LPDDPIXELFORMAT BestMatch )
916 HRESULT hr;
918 mBestMatchFound = false;
919 mFormatToMatch = *FormatToMatch;
921 hr = mRenderDevice->EnumTextureFormats( D3DEnumPixelFormatsForBestMatchStaticCallback, this );
923 if( FAILED( hr ) || !mBestMatchFound )
924 return false;
926 *BestMatch = mBestMatchPixelFormat;
927 return true;
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;
939 else
941 if( (mFormatToMatch.dwFlags & DDPF_ALPHAPIXELS) == (lppf->dwFlags & DDPF_ALPHAPIXELS) )
943 if( mFormatToMatch.dwRGBBitCount == lppf->dwRGBBitCount )
944 UseThisFormat = true;
948 if( UseThisFormat )
950 mBestMatchPixelFormat = *lppf;
951 mBestMatchFound = true;
952 return D3DENUMRET_CANCEL;
954 else
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
967 return mShowShaders;