Texture.cpp

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2002-2006  Ryan Nunn and The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "pent_include.h"
00020 #include "Texture.h"
00021 #include "TextureBitmap.h"
00022 #include "TextureTarga.h"
00023 #include "TexturePNG.h"
00024 
00025 #include <cstring>
00026 
00027 //
00028 // Base Clear Func
00029 //
00030 bool Texture::Clear()
00031 {
00032         // Temporary fix to prevent us from freeing a RenderSurface's memory
00033         if (format != TEX_FMT_NATIVE)
00034                 delete [] buffer;
00035         buffer = 0;
00036                 
00037         return true;
00038 }
00039 
00040 //
00041 // Destructor
00042 //
00043 Texture::~Texture()
00044 {
00045         Clear();
00046 }
00047 
00048 //
00049 // Helper Macro for texture type detection
00050 //
00051 #define TRY_TYPE(TextureType)                           \
00052 tex = new TextureType();                                        \
00053 /* If read failed, delete the texture. */       \
00054 if (!tex->Read(ds)) {                                           \
00055         delete tex;                                                             \
00056         tex = 0;                                                                \
00057 }                                                                                       \
00058 else {                                                                          \
00059         /* Worked so return it */                               \
00060         return tex;                                                             \
00061 }
00062 
00063 //
00064 // Create a texture from a Data Source
00065 // (filename is used to help detection of type)
00066 //
00067 Texture * Texture::Create(IDataSource *ds, const char *filename)
00068 {
00069         Texture *tex;
00070 
00071         if (filename) {
00072                 // Looks like it's a PNG
00073                 if (std::strstr(filename, ".png")) {
00074                         TRY_TYPE(TexturePNG);
00075                 }
00076                 // Looks like it's a BMP
00077                 if (std::strstr(filename, ".bmp")) {
00078                         TRY_TYPE(TextureBitmap);
00079                 }
00080                 // Looks like it's a TGA
00081                 if (std::strstr(filename, ".tga")) {
00082                         TRY_TYPE(TextureTarga);
00083                 }
00084         }
00085 
00086         // Now go through each type 1 by 1
00087         TRY_TYPE(TexturePNG);
00088         TRY_TYPE(TextureBitmap);
00089         TRY_TYPE(TextureTarga);
00090 
00091         // Couldn't find it
00092         return 0;
00093 }

Generated on Fri Jul 27 22:27:41 2007 for pentagram by  doxygen 1.4.7