Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# 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
    00042     //-----------------------------------------------------------------------
    00043     CD3D9Texture::~CD3D9Texture()
    00044     {
    00045
    00046     }
    00047
    00048     //-----------------------------------------------------------------------
    00049     IDirect3DTexture9* CD3D9Texture::GetD3D9Texture() const
    00050     {
    00051         return m_pTexture;
    00052     }
    00053
    00054     //-----------------------------------------------------------------------
    00055     void CD3D9Texture::Update(const CRectangle& Rect)
    00056     {
    00057         Assert(CRectangle(0, 0, m_Size.x, m_Size.y).Intersects(Rect) == INT_IN);
    00058
    00059         // The format is the same --> simple copy
    00060         if (m_Format == m_Data.GetFormat())
    00061         {
    00062             // Lock the texture
    00063             D3DLOCKED_RECT LockedRect;
    00064             RECT Lock = {Rect.Left(), Rect.Top(), Rect.Right(), Rect.Bottom()};
    00065             m_pTexture->LockRect(0, &LockedRect, &Lock, 0);
    00066
    00067             // Copy pixels
    00068             UpdateSurface(LockedRect, Rect);
    00069
    00070             // Unlock the texture
    00071             m_pTexture->UnlockRect(0);
    00072         }
    00073         else
    00074         {
    00075             // Get the device
    00076             LPDIRECT3DDEVICE9 Device = NULL;
    00077             m_pTexture->GetDevice(&Device);
    00078
    00079             // Create a texture in memory
    00080             LPDIRECT3DSURFACE9 Src = NULL;
    00081             if (FAILED(Device->CreateOffscreenPlainSurface(Rect.Width(), Rect.Height(), CD3D9Enum::Get(m_Data.GetFormat()), D3DPOOL_SYSTEMMEM, &Src, NULL)))
    00082                 D3D9_EXCEPT("Error in fonction CreateOffscreenPlainSurface", "CD3D9Texture::Update");
    00083
    00084             // Lock the temp texture
    00085             D3DLOCKED_RECT LockedRect;
    00086             Src->LockRect(&LockedRect, NULL, 0);
    00087
    00088             // Copy pixels
    00089             UpdateSurface(LockedRect, Rect);
    00090
    00091             // Unlock the temp texture
    00092             Src->UnlockRect();
    00093
    00094             // Get the level 0 surface of the texture
    00095             LPDIRECT3DSURFACE9 Dest = NULL;
    00096             m_pTexture->GetSurfaceLevel(0, &Dest);
    00097
    00098             // Copy Src surface on Dest Surface (with format conversion)
    00099             RECT DestRect = {Rect.Left(), Rect.Top(), Rect.Right(), Rect.Bottom()};
    00100             if (FAILED(D3DXLoadSurfaceFromSurface(Dest, NULL, &DestRect, Src, NULL, NULL, D3DX_DEFAULT, 0)))
    00101                 D3D9_EXCEPT("Error in fonction D3DXLoadSurfaceFromSurface", "CD3D9Texture::Update");
    00102         }
    00103
    00104         // Mipmapping levels generation
    00105         if (m_bHasMipmaps)
    00106         {
    00107             if (m_bAutoMipmaps)
    00108                 m_pTexture->GenerateMipSubLevels();
    00109             else
    00110                 D3DXFilterTexture(m_pTexture, NULL, D3DX_DEFAULT, D3DX_DEFAULT);
    00111         }
    00112     }
    00113
    00114     void CD3D9Texture::UpdateSurface(const D3DLOCKED_RECT& LockedRect, const CRectangle& Rect)
    00115     {
    00116         // Get A pointer on source and destination pixels
    00117         unsigned char* DestPix = reinterpret_cast<unsigned char*>(LockedRect.pBits);
    00118         const unsigned char* SrcPix = m_Data.GetData() + (Rect.Left() + Rect.Top() * m_Size.x) * GetBytesPerPixel(m_Data.GetFormat());
    00119
    00120         // Copy pixels on the surface
    00121         unsigned int Bpp = GetBytesPerPixel(m_Data.GetFormat());
    00122         for (int i = 0; i < Rect.Height(); ++i)
    00123         {
    00124             std::copy(SrcPix, SrcPix + Rect.Width() * Bpp, DestPix);
    00125             SrcPix  += m_Size.x * Bpp;
    00126             DestPix += LockedRect.Pitch;
    00127         }
    00128     }
    00129 } // namespace Keos