KeosD3D9Texture.cpp

Go to the documentation of this file.
00001 /*
00002  * This source file is part of KEOS (Free 3D Engine)
00003  * For the latest info, see http://www.keosengine.org/
00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
00005  *
00006  * This program is free software; you can redistribute it and/or modify it under
00007  * the terms of the GNU Lesser General Public License as published by the Free Software
00008  * Foundation; either version 2 of the License, or (at your option) any later
00009  * version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but WITHOUT
00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public License along with
00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00018  * http://www.gnu.org/copyleft/lesser.txt.
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   // CD3D9Texture implementation
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     // The format is the same --> simple copy
00056     if (m_Format == m_Data.GetFormat())
00057     {
00058       // Lock the texture
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       // Copy pixels
00064       UpdateSurface(LockedRect, Rect);
00065 
00066       // Unlock the texture
00067       m_pTexture->UnlockRect(0);
00068     }
00069     else
00070     {
00071       // Get the device
00072       LPDIRECT3DDEVICE9 Device = NULL;
00073       m_pTexture->GetDevice(&Device);
00074 
00075       // Create a texture in memory
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       // Lock the temp texture
00081       D3DLOCKED_RECT LockedRect;
00082       Src->LockRect(&LockedRect, NULL, 0);
00083 
00084       // Copy pixels
00085       UpdateSurface(LockedRect, Rect);
00086 
00087       // Unlock the temp texture
00088       Src->UnlockRect();
00089 
00090       // Get the level 0 surface of the texture
00091       LPDIRECT3DSURFACE9 Dest = NULL;
00092       m_pTexture->GetSurfaceLevel(0, &Dest);
00093 
00094       // Copy Src surface on Dest Surface (with format conversion)
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     // Mipmapping levels generation
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     // Get A pointer on source and destination pixels
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     // Copy pixels on the surface
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 } // namespace Keos

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1