00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "KeosD3D9Texture.h"
00022 #include "KeosD3D9Enum.h"
00023 #include "KeosD3D9RenderSystem.h"
00024 #include "KeosD3D9Exception.h"
00025 #include "KeosPixelUtils.h"
00026 #include "KeosRectangle.h"
00027
00028 namespace Keos
00029 {
00030
00031
00032
00033
00034
00035 CD3D9Texture::CD3D9Texture(const TVector2I& Size, TPixelFormat Format, bool bHasMipmaps, bool bAutoMipmaps, IDirect3DTexture9* pTexture) :
00036 ITextureBase(Size, Format, bHasMipmaps, bAutoMipmaps),
00037 m_pTexture (pTexture)
00038 {}
00039
00040
00041 CD3D9Texture::~CD3D9Texture()
00042 {}
00043
00044
00045 IDirect3DTexture9* CD3D9Texture::GetD3D9Texture() const
00046 {
00047 return m_pTexture;
00048 }
00049
00050
00051 void CD3D9Texture::Update(const CRectangle& Rect)
00052 {
00053 Assert(CRectangle(0, 0, m_Size.x, m_Size.y).Intersects(Rect) == INT_IN);
00054
00055
00056 if (m_Format == m_Data.GetFormat())
00057 {
00058
00059 D3DLOCKED_RECT LockedRect;
00060 RECT Lock = {Rect.Left(), Rect.Top(), Rect.Right(), Rect.Bottom()};
00061 m_pTexture->LockRect(0, &LockedRect, &Lock, 0);
00062
00063
00064 UpdateSurface(LockedRect, Rect);
00065
00066
00067 m_pTexture->UnlockRect(0);
00068 }
00069 else
00070 {
00071
00072 LPDIRECT3DDEVICE9 Device = NULL;
00073 m_pTexture->GetDevice(&Device);
00074
00075
00076 LPDIRECT3DSURFACE9 Src = NULL;
00077 if (FAILED(Device->CreateOffscreenPlainSurface(Rect.Width(), Rect.Height(), CD3D9Enum::Get(m_Data.GetFormat()), D3DPOOL_SYSTEMMEM, &Src, NULL)))
00078 D3D9_EXCEPT("Error in fonction CreateOffscreenPlainSurface", "CD3D9Texture::Update");
00079
00080
00081 D3DLOCKED_RECT LockedRect;
00082 Src->LockRect(&LockedRect, NULL, 0);
00083
00084
00085 UpdateSurface(LockedRect, Rect);
00086
00087
00088 Src->UnlockRect();
00089
00090
00091 LPDIRECT3DSURFACE9 Dest = NULL;
00092 m_pTexture->GetSurfaceLevel(0, &Dest);
00093
00094
00095 RECT DestRect = {Rect.Left(), Rect.Top(), Rect.Right(), Rect.Bottom()};
00096 if (FAILED(D3DXLoadSurfaceFromSurface(Dest, NULL, &DestRect, Src, NULL, NULL, D3DX_DEFAULT, 0)))
00097 D3D9_EXCEPT("Error in fonction D3DXLoadSurfaceFromSurface", "CD3D9Texture::Update");
00098 }
00099
00100
00101 if (m_bHasMipmaps)
00102 {
00103 if (m_bAutoMipmaps)
00104 m_pTexture->GenerateMipSubLevels();
00105 else
00106 D3DXFilterTexture(m_pTexture, NULL, D3DX_DEFAULT, D3DX_DEFAULT);
00107 }
00108 }
00109
00110 void CD3D9Texture::UpdateSurface(const D3DLOCKED_RECT& LockedRect, const CRectangle& Rect)
00111 {
00112
00113 unsigned char* DestPix = reinterpret_cast<unsigned char*>(LockedRect.pBits);
00114 const unsigned char* SrcPix = m_Data.GetData() + (Rect.Left() + Rect.Top() * m_Size.x) * GetBytesPerPixel(m_Data.GetFormat());
00115
00116
00117 unsigned int Bpp = GetBytesPerPixel(m_Data.GetFormat());
00118 for (int i = 0; i < Rect.Height(); ++i)
00119 {
00120 std::copy(SrcPix, SrcPix + Rect.Width() * Bpp, DestPix);
00121 SrcPix += m_Size.x * Bpp;
00122 DestPix += LockedRect.Pitch;
00123 }
00124 }
00125 }