KeosTexture.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 "KeosTexture.h"
00022 #include "KeosTextureBase.h"
00023 #include "KeosMediaManager.h"
00024 #include "KeosResourceManager.h"
00025 #include "KeosRenderSystem.h"
00026 #include "KeosImage.h"
00027 #include "KeosPixelUtils.h"
00028 #include "KeosLogger.h"
00029 #include "KeosMath.h"
00030 
00031 namespace Keos
00032 {
00033   //=======================================================================
00034   // CTexture implementation
00035   //=======================================================================
00036 
00037   //-----------------------------------------------------------------------
00038   void CTexture::Create(const TVector2I& Size, TPixelFormat Format, ulong nFlags, const String& strName)
00039   {
00040     // Creaion of the texture
00041     CreateFromImage(CImage(Size, Format), Format, nFlags, strName);
00042   }
00043 
00044   //-----------------------------------------------------------------------
00045   void CTexture::CreateFromFile(const String& strFilename, TPixelFormat Format, ulong nFlags)
00046   {
00047     // We look if the texture was not already loaded
00048     m_Texture = CResourceManager::Instance().Get<ITextureBase>(strFilename);
00049 
00050     if (!m_Texture)
00051     {
00052       // Load of the image
00053       CSmartPtr<CImage> Image = CMediaManager::Instance().LoadMediaFromFile<CImage>(strFilename);
00054 
00055       // Creation of the texture
00056       Load(*Image, Format, nFlags, strFilename);
00057     }
00058   }
00059 
00060   //-----------------------------------------------------------------------
00061   void CTexture::CreateFromImage(const CImage& Image, TPixelFormat Format, ulong nFlags, const String& strName)
00062   {
00063     // We look if the texture was not already loaded
00064     m_Texture = CResourceManager::Instance().Get<ITextureBase>(strName);
00065 
00066     if (!m_Texture)
00067       Load(Image, Format, nFlags, strName);
00068   }
00069 
00070   //-----------------------------------------------------------------------
00071   void CTexture::Load(const CImage& Pixels, TPixelFormat Format, ulong nFlags, const String& strName)
00072   {
00073     // If the format is compressed and that the compression is not supported, we pass in a standard format (PXF_A8R8G8B8)
00074     if (FormatCompressed(Format) && !Renderer->GetCapabilities()->HasCapability(CAP_TEXTURE_COMPRESSION_DXT))
00075     {
00076       Format = PXF_A8R8G8B8;
00077       ILogger::Log("%s %s (texture : \"%s\")",
00078                    "WARNING - Chosen compressed format, but not supported by the render system.\n",
00079                    "The used format will be PXF_A8R8G8B8. ",
00080                    strName.c_str());
00081     }
00082 
00083     // If the system requires dimensions in powers of 2 and if it is not the case, we modify these
00084     TVector2I Size(CMath::NearestPowerOfTwo(Pixels.GetSize().x), CMath::NearestPowerOfTwo(Pixels.GetSize().y));
00085     if ((Size != Pixels.GetSize()) && !Renderer->GetCapabilities()->HasCapability(CAP_NON_POWER_OF_2_TEXTURES))
00086     {
00087       ILogger::Log("%s %s (%dx%d -> %dx%d)",
00088                    "WARNING - Dimensions of texture non-powers of 2, but not supported by the render system.\n",
00089                    "The dimensions will be adjusted. ",
00090                    Pixels.GetSize().x, Pixels.GetSize().y, Size.x, Size.y);
00091     }
00092     else
00093     {
00094       Size = Pixels.GetSize();
00095     }
00096 
00097     try
00098     {
00099       // Creation of the texture
00100       m_Texture = Renderer->CreateTexture(Size, Format, nFlags);
00101     }
00102     catch (const std::exception& E)
00103     {
00104       LOADINGFAILED_EXCEPT(strName, E.what(), "CTexture::Load");
00105     }
00106 
00107     // Addition in the resources
00108     if (strName != "")
00109       CResourceManager::Instance().Add(strName, m_Texture);
00110 
00111     // If the format is compressed we change the format of the copy system of the texture (PXF_A8R8G8B8)
00112     if (FormatCompressed(Format))
00113       GetPixels() = CImage(GetSize(), PXF_A8R8G8B8);
00114 
00115     // Copy of the pixels
00116     GetPixels().CopyImage(Pixels);
00117     Update();
00118   }
00119 
00120   //-----------------------------------------------------------------------
00121   void CTexture::SaveToFile(const String& strFilename) const
00122   {
00123     CMediaManager::Instance().SaveMediaToFile(&m_Texture->m_Data, strFilename);
00124   }
00125 
00126   //-----------------------------------------------------------------------
00127   void CTexture::Update(const CRectangle& Rect)
00128   {
00129     if (Rect.Width() == -1)
00130       m_Texture->Update(CRectangle(0, 0, GetSize().x, GetSize().y));
00131     else
00132       m_Texture->Update(Rect);
00133   }
00134 
00135   //-----------------------------------------------------------------------
00136   CImage& CTexture::GetPixels()
00137   {
00138     return m_Texture->m_Data;
00139   }
00140 
00141   //-----------------------------------------------------------------------
00142   const TVector2I& CTexture::GetSize() const
00143   {
00144     return m_Texture->m_Size;
00145   }
00146 
00147   //-----------------------------------------------------------------------
00148   TPixelFormat CTexture::GetFormat() const
00149   {
00150     return m_Texture->m_Format;
00151   }
00152 
00153   //-----------------------------------------------------------------------
00154   const String& CTexture::GetName() const
00155   {
00156     return m_Texture->GetName();
00157   }
00158 
00159   //-----------------------------------------------------------------------
00160   const ITextureBase* CTexture::GetTextureBase() const
00161   {
00162     return m_Texture;
00163   }
00164 
00165   //-----------------------------------------------------------------------
00166   bool CTexture::operator ==(const CTexture& Texture) const
00167   {
00168     return m_Texture == Texture.m_Texture;
00169   }
00170 
00171   //-----------------------------------------------------------------------
00172   bool CTexture::operator !=(const CTexture& Texture) const
00173   {
00174     return !(*this == Texture);
00175   }
00176 
00177 } // namespace Keos

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