dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / application / UiBuilder / UIDirect3DTextureCanvas.cpp
blob9565952202906e60e0cf071d8e34e4a24ee2a117
1 #include "FirstUiBuilder.h"
2 #include "UIDirect3DTextureCanvas.h"
4 #include "UIDirect3DPrimaryCanvas.h"
6 #include <math.h>
8 UIDirect3DTextureCanvas::UIDirect3DTextureCanvas( const UISize &Size, const bool InSystemMemory ) :
9 UIDirectDrawCanvas( Size ),
10 mHasShader (false)
12 mRenderCanvas = 0;
13 mInSystemMemory = InSystemMemory;
16 //----------------------------------------------------------------------
18 UIDirect3DTextureCanvas::~UIDirect3DTextureCanvas()
20 SetRenderCanvas( 0 );
23 //----------------------------------------------------------------------
25 bool UIDirect3DTextureCanvas::IsA( const UITypeID Type ) const
27 return (Type == TUIDirect3DTextureCanvas) || UIDirectDrawCanvas::IsA( Type );
30 //----------------------------------------------------------------------
32 UIBaseObject *UIDirect3DTextureCanvas::Clone( void ) const
34 return 0;
37 //----------------------------------------------------------------------
39 void UIDirect3DTextureCanvas::SetRenderCanvas( UIDirect3DPrimaryCanvas *NewRenderCanvas )
41 if( NewRenderCanvas )
42 NewRenderCanvas->Attach( this );
44 if( mRenderCanvas )
45 mRenderCanvas->Detach( this );
47 mRenderCanvas = NewRenderCanvas;
50 //----------------------------------------------------------------------
52 UIDirect3DPrimaryCanvas *UIDirect3DTextureCanvas::GetRenderCanvas( void ) const
54 return mRenderCanvas;
57 //----------------------------------------------------------------------
59 void UIDirect3DTextureCanvas::NotifyRenderCanvasChanged( void )
61 DestoryAll();
64 //----------------------------------------------------------------------
66 bool UIDirect3DTextureCanvas::Generate( void ) const
68 assert( mRenderCanvas );
70 if( !mSurface )
72 LPDIRECTDRAWSURFACE7 CreatedSurface;
73 DDSURFACEDESC2 ddsd = { sizeof( ddsd ) };
74 HRESULT hr;
76 // Round texture size up to a power of two
77 const_cast<UIDirect3DTextureCanvas *>( this )->mSize.x = (long)(pow( 2.0, ceil( log( (double) mSize.x ) / log( 2.0 ) ) ));
78 const_cast<UIDirect3DTextureCanvas *>( this )->mSize.y = (long)(pow( 2.0, ceil( log( (double) mSize.y ) / log( 2.0 ) ) ));
80 // Make texture a square
81 if( mSize.x < mSize.y )
82 const_cast<UIDirect3DTextureCanvas *>( this )->mSize.x = mSize.y;
83 else
84 const_cast<UIDirect3DTextureCanvas *>( this )->mSize.y = mSize.x;
86 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT;
87 ddsd.dwWidth = GetWidth();
88 ddsd.dwHeight = GetHeight();
90 mRenderCanvas->GetPreferedPixelFormat( &ddsd.ddpfPixelFormat );
92 if( mInSystemMemory )
93 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY;
94 else
95 ddsd.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
97 ddsd.ddsCaps.dwCaps2 = 0;
99 hr = gDirectDraw->CreateSurface( &ddsd, &CreatedSurface, 0 );
101 if( FAILED( hr ) )
102 return false;
104 const_cast<UIDirect3DTextureCanvas *>( this )->SetSurface( CreatedSurface );
106 else if( mSurface->IsLost() )
108 if( FAILED( mSurface->Restore() ) )
109 return false;
112 if( !UICanvas::Generate() )
114 if( mSurface )
115 mSurface->Release();
117 const_cast<UIDirect3DTextureCanvas *>( this )->mSurface = 0;
118 return false;
121 return true;
124 //----------------------------------------------------------------------
126 bool UIDirect3DTextureCanvas::CreateFromSurfaceDescription( LPDDSURFACEDESC2 pddsd )
128 SetSurface( 0 );
130 LPDIRECTDRAWSURFACE7 CreatedSurface;
131 HRESULT hr;
133 hr = gDirectDraw->CreateSurface( pddsd, &CreatedSurface, 0 );
135 if( FAILED( hr ) )
136 return false;
138 SetSurface( CreatedSurface );
139 return true;
142 //----------------------------------------------------------------------
144 void UIDirect3DTextureCanvas::SetHasShader (bool b)
146 mHasShader = b;
149 //----------------------------------------------------------------------