3 * Copyright (c) 2007-2010 SlimDX Group
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "../ComObject.h"
29 #include "../math/Color4.h"
30 #include "../math/Vector3.h"
36 #include "Direct3D9Exception.h"
38 using namespace System;
39 using namespace System::Drawing;
45 Sprite::Sprite( SlimDX::Direct3D9::Device^ device )
49 HRESULT hr = D3DXCreateSprite( device->InternalPointer, &sprite );
51 if( RECORD_D3D9( hr ).IsFailure )
52 throw gcnew Direct3D9Exception( Result::Last );
57 Result Sprite::Begin( SpriteFlags flags )
59 HRESULT hr = InternalPointer->Begin( static_cast<DWORD>( flags ) );
60 return RECORD_D3D9( hr );
65 HRESULT hr = InternalPointer->End();
66 return RECORD_D3D9( hr );
69 Result Sprite::Flush()
71 HRESULT hr = InternalPointer->Flush();
72 return RECORD_D3D9( hr );
75 Result Sprite::OnLostDevice()
77 HRESULT hr = InternalPointer->OnLostDevice();
78 return RECORD_D3D9( hr );
81 Result Sprite::OnResetDevice()
83 HRESULT hr = InternalPointer->OnResetDevice();
84 return RECORD_D3D9( hr );
87 SlimDX::Direct3D9::Device^ Sprite::Device::get()
89 IDirect3DDevice9* device;
91 HRESULT hr = InternalPointer->GetDevice( &device );
93 if( RECORD_D3D9( hr ).IsFailure )
96 return SlimDX::Direct3D9::Device::FromPointer( device );
99 Matrix Sprite::Transform::get()
103 HRESULT hr = InternalPointer->GetTransform( reinterpret_cast<D3DXMATRIX*>( &result ) );
109 void Sprite::Transform::set( Matrix value )
111 HRESULT hr = InternalPointer->SetTransform( reinterpret_cast<const D3DXMATRIX*>( &value ) );
115 Result Sprite::SetWorldViewLH( Matrix world, Matrix view )
117 HRESULT hr = InternalPointer->SetWorldViewLH( reinterpret_cast<const D3DXMATRIX*>( &world ), reinterpret_cast<const D3DXMATRIX*>( &view ) );
118 return RECORD_D3D9( hr );
121 Result Sprite::SetWorldViewRH( Matrix world, Matrix view )
123 HRESULT hr = InternalPointer->SetWorldViewRH( reinterpret_cast<const D3DXMATRIX*>( &world ), reinterpret_cast<const D3DXMATRIX*>( &view ) );
124 return RECORD_D3D9( hr );
127 Result Sprite::Draw( Texture^ texture, Nullable<System::Drawing::Rectangle> sourceRect, Nullable<Vector3> center, Nullable<Vector3> position, Color4 color )
131 if(sourceRect.HasValue)
133 System::Drawing::Rectangle rectValue = sourceRect.Value;
134 Utilities::ConvertRect(rectValue, rect);
138 const D3DXVECTOR3* centerPtr = 0;
140 if( center.HasValue )
142 centerValue = center.Value;
143 centerPtr = reinterpret_cast<const D3DXVECTOR3*>( ¢erValue );
146 const D3DXVECTOR3* positionPtr = 0;
147 Vector3 positionValue;
148 if( position.HasValue )
150 positionValue = position.Value;
151 positionPtr = reinterpret_cast<const D3DXVECTOR3*>( &positionValue );
154 HRESULT hr = InternalPointer->Draw( texture->InternalPointer, rectPtr, centerPtr, positionPtr, color.ToArgb() );
155 return RECORD_D3D9( hr );
158 Result Sprite::Draw( Texture^ texture, Nullable<System::Drawing::Rectangle> sourceRect, Color4 color )
162 if(sourceRect.HasValue)
164 System::Drawing::Rectangle rectValue = sourceRect.Value;
165 Utilities::ConvertRect(rectValue, rect);
169 HRESULT hr = InternalPointer->Draw( texture->InternalPointer, rectPtr, NULL, NULL, color.ToArgb() );
170 return RECORD_D3D9( hr );
173 Result Sprite::Draw( Texture^ texture, Nullable<Vector3> center, Nullable<Vector3> position, Color4 color )
175 const D3DXVECTOR3* centerPtr = 0;
177 if( center.HasValue )
179 centerValue = center.Value;
180 centerPtr = reinterpret_cast<const D3DXVECTOR3*>( ¢erValue );
183 const D3DXVECTOR3* positionPtr = 0;
184 Vector3 positionValue;
185 if( position.HasValue )
187 positionValue = position.Value;
188 positionPtr = reinterpret_cast<const D3DXVECTOR3*>( &positionValue );
191 HRESULT hr = InternalPointer->Draw( texture->InternalPointer, NULL, centerPtr, positionPtr, color.ToArgb() );
192 return RECORD_D3D9( hr );
195 Result Sprite::Draw( Texture^ texture, Color4 color )
197 HRESULT hr = InternalPointer->Draw( texture->InternalPointer, NULL, NULL, NULL, color.ToArgb() );
198 return RECORD_D3D9( hr );