00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00035
00036
00037
00038 void CTexture::Create(const TVector2I& Size, TPixelFormat Format, ulong nFlags, const String& strName)
00039 {
00040
00041 CreateFromImage(CImage(Size, Format), Format, nFlags, strName);
00042 }
00043
00044
00045 void CTexture::CreateFromFile(const String& strFilename, TPixelFormat Format, ulong nFlags)
00046 {
00047
00048 m_Texture = CResourceManager::Instance().Get<ITextureBase>(strFilename);
00049
00050 if (!m_Texture)
00051 {
00052
00053 CSmartPtr<CImage> Image = CMediaManager::Instance().LoadMediaFromFile<CImage>(strFilename);
00054
00055
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
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
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
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
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
00108 if (strName != "")
00109 CResourceManager::Instance().Add(strName, m_Texture);
00110
00111
00112 if (FormatCompressed(Format))
00113 GetPixels() = CImage(GetSize(), PXF_A8R8G8B8);
00114
00115
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 }