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

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