src/render/rTexture.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023   
00024 ***************************************************************************
00025 
00026 */
00027 
00028 #ifndef ArmageTron_TEXTURE_H
00029 #define ArmageTron_TEXTURE_H
00030 
00031 #include "tString.h"
00032 #include "tResourceManager.h"
00033 #include "tDirectories.h"
00034 #include "tList.h"
00035 #include "rGL.h"
00036 #include "rGLuintObject.h"
00037 #include <list>
00038 
00039 struct SDL_Surface;
00040 
00042 class rTextureGroups
00043 {
00044 public:
00046     enum Group
00047     {
00048         TEX_FLOOR=0,
00049         TEX_WALL=1,
00050         TEX_OBJ=2,
00051         TEX_FONT=3,
00052         TEX_GROUPS
00053     };
00054 
00055     static int TextureMode[TEX_GROUPS];                 
00056     static char const * TextureGroupDescription[TEX_GROUPS];   
00057 };
00058 
00060 class rSurface
00061 {
00062 public:
00063     explicit rSurface( char const * fileName, tPath const *path = &tDirectories::Data() );       
00064     ~rSurface();                                      
00065     rSurface( rSurface const & other );               
00066     rSurface & operator = ( rSurface const & other ); 
00067 protected:
00068     rSurface();                             
00069     void Init();                            
00070     void Clear();                           
00071     void Create( char const * fileName, tPath const *path = &tDirectories::Data() );   
00072     void Create( SDL_Surface * surface );   
00073 
00074 private:
00075     // attributes
00076     SDL_Surface * surface_;                  
00077     GLenum format_;                          
00078 
00079     void CopyFrom( rSurface const & other ); 
00080 
00081     // accessors
00082 public:
00083     inline SDL_Surface * GetSurface( void ) const;                           
00084     inline rSurface const & GetSurface( SDL_Surface * & surface ) const; 
00085     inline GLenum const & GetFormat( void ) const;                           
00086     inline rSurface const & GetFormat( GLenum & format ) const;          
00087 protected:
00088     inline rSurface & SetSurface( SDL_Surface * surface );                   
00089     inline rSurface & SetFormat( GLenum const & format );                    
00090 private:
00091 };
00092 
00093 // ******************************************************************************************
00094 
00096 class rITexture
00097 {
00098 public:
00099     virtual ~rITexture() = 0;                
00100 
00101     static void UnloadAll();                 
00102     static void LoadAll();                   
00103 
00104     inline void Select(bool enforce=false);  
00105     inline void Unload();                    
00106 protected:
00107     rITexture(); 
00108 
00109     virtual void OnSelect(bool enforce) = 0; 
00110     virtual void OnUnload() = 0;             
00111 
00112 private:
00113     static tList<rITexture> s_textures_;     
00114     int id_;                                 
00115 
00116     // disable copying
00117     rITexture( rITexture const & );
00118     rITexture & operator = ( rITexture const & );
00119 };
00120 
00121 // ******************************************************************************************
00122 
00124 class rISurfaceTexture: public rITexture
00125 {
00126 public:
00127     rISurfaceTexture(int group, bool repx=0, bool repy=0,
00128                      bool storeAlpha=false);        
00129     virtual ~rISurfaceTexture();                    
00130 
00131     bool Loaded(){ return textureModeLast_ >= 0; }  
00132 
00134     static bool storageHack_;
00135 protected:
00136     virtual void ProcessImage(SDL_Surface *);       
00137 
00138     void Upload( rSurface & surface );              
00139 
00140     virtual void OnSelect(bool enforce);            
00141     virtual void OnSelect()=0;                      
00142     virtual void OnUnload();                        
00143 
00144     void StoreAlpha();                              
00145 
00146     static bool s_reportErrors_;                    
00147 private:
00148 
00149     int group_;             
00150 
00151     int textureModeLast_;   
00152 
00153     rGLuintObjectTexture tint_;   
00154     bool repx_,repy_;             
00155     bool storeAlpha_;             
00156 };
00157 
00158 // ******************************************************************************************
00159 
00161 class rFileTexture: public rISurfaceTexture
00162 {
00163 public:
00164     rFileTexture(int group, char const * fileName, bool repx=0, bool repy=0,
00165                  bool storeAlpha=false, tPath const *path = &tDirectories::Data());    
00166     virtual ~rFileTexture();                
00167 
00168 protected:
00169     virtual void OnSelect();                
00170 private:
00171     tString fileName_;                      
00172     tPath const *path_;
00173 
00174 public:
00175     inline tString const & GetFileName( void ) const;                     
00176     inline rFileTexture const & GetFileName( tString & fileName ) const;  
00177 protected:
00178 private:
00179     inline rFileTexture & SetFileName( tString const & fileName );            
00180 };
00181 
00182 class rResourceTextureWrapper;
00183 
00187 class rResourceTexture
00188 {
00189     class InternalTex;
00190     friend class InternalTex;
00191     class InternalTex : public rFileTexture
00192     {
00193     public:
00194         int use_;
00195         tResourcePath path_;
00196 
00197         InternalTex(tResourcePath const &path);
00198 
00199         void Release();
00200         void Use() {++use_;}
00201     };
00202     typedef InternalTex tex_t; // don't ask me why this is needed. InteralTex just won't cut it in some places
00203     typedef std::list<InternalTex *> texlist_t;
00204     static texlist_t textures;
00205     InternalTex *tex_;
00206 
00207     bool repx_, repy_;
00208 public:
00210     rResourceTexture() : tex_(0) {}
00212     rResourceTexture(tResourcePath const &path, bool repx=false, bool repy=false);
00213 
00215     rResourceTexture(rResourceTexture const &other) : tex_(other.tex_) {if(tex_) tex_->Use();}
00217     rResourceTexture &operator=(rResourceTexture const &other);
00219     ~rResourceTexture() {if(tex_) tex_->Release();}
00221     bool Valid() {return tex_;}
00223         void Select();
00226     tResourcePath const &Path() {return tex_->path_;}
00227 };
00228 
00229 // ******************************************************************************************
00230 
00232 class rSurfaceTexture: public rISurfaceTexture
00233 {
00234 public:
00235     rSurfaceTexture(int group, rSurface const & surface, bool repx=0, bool repy=0,
00236                     bool storeAlpha=false );    
00237     virtual ~rSurfaceTexture();                 
00238 
00239 protected:
00240     virtual void OnSelect();                    
00241 private:
00242     rSurface const & surface_;                  
00243 
00244 public:
00245     inline const rSurface & GetSurface( void ) const; 
00246 protected:
00247 private:
00248 };
00249 
00250 // ******************************************************************************************
00251 // *
00252 // *    GetSurface
00253 // *
00254 // ******************************************************************************************
00258 // ******************************************************************************************
00259 
00260 SDL_Surface * rSurface::GetSurface( void ) const
00261 {
00262     return this->surface_;
00263 }
00264 
00265 // ******************************************************************************************
00266 // *
00267 // *    GetSurface
00268 // *
00269 // ******************************************************************************************
00274 // ******************************************************************************************
00275 
00276 rSurface const & rSurface::GetSurface( SDL_Surface * & surface ) const
00277 {
00278     surface = this->surface_;
00279     return *this;
00280 }
00281 
00282 // ******************************************************************************************
00283 // *
00284 // *    SetSurface
00285 // *
00286 // ******************************************************************************************
00291 // ******************************************************************************************
00292 
00293 rSurface & rSurface::SetSurface( SDL_Surface * surface )
00294 {
00295     this->surface_ = surface;
00296     return *this;
00297 }
00298 
00299 // ******************************************************************************************
00300 // *
00301 // *    GetFormat
00302 // *
00303 // ******************************************************************************************
00307 // ******************************************************************************************
00308 
00309 GLenum const & rSurface::GetFormat( void ) const
00310 {
00311     return this->format_;
00312 }
00313 
00314 // ******************************************************************************************
00315 // *
00316 // *    GetFormat
00317 // *
00318 // ******************************************************************************************
00323 // ******************************************************************************************
00324 
00325 rSurface const & rSurface::GetFormat( GLenum & format ) const
00326 {
00327     format = this->format_;
00328     return *this;
00329 }
00330 
00331 // ******************************************************************************************
00332 // *
00333 // *    SetFormat
00334 // *
00335 // ******************************************************************************************
00340 // ******************************************************************************************
00341 
00342 rSurface & rSurface::SetFormat( GLenum const & format )
00343 {
00344     this->format_ = format;
00345     return *this;
00346 }
00347 
00348 // ******************************************************************************************
00349 // *
00350 // *    Select
00351 // *
00352 // ******************************************************************************************
00356 // ******************************************************************************************
00357 
00358 void rITexture::Select( bool enforce )
00359 {
00360     this->OnSelect( enforce );
00361 }
00362 
00363 // ******************************************************************************************
00364 // *
00365 // *    Unload
00366 // *
00367 // ******************************************************************************************
00370 // ******************************************************************************************
00371 
00372 void rITexture::Unload()
00373 {
00374     this->OnUnload();
00375 }
00376 
00377 // ****************************************************************************
00378 // *
00379 // *    GetFileName
00380 // *
00381 // ****************************************************************************
00385 // ****************************************************************************
00386 
00387 tString const & rFileTexture::GetFileName( void ) const
00388 {
00389     return this->fileName_;
00390 }
00391 
00392 // ****************************************************************************
00393 // *
00394 // *    GetFileName
00395 // *
00396 // ****************************************************************************
00401 // ****************************************************************************
00402 
00403 rFileTexture const & rFileTexture::GetFileName( tString & fileName ) const
00404 {
00405     fileName = this->fileName_;
00406     return *this;
00407 }
00408 
00409 // ****************************************************************************
00410 // *
00411 // *    SetFileName
00412 // *
00413 // ****************************************************************************
00418 // ****************************************************************************
00419 
00420 rFileTexture & rFileTexture::SetFileName( tString const & fileName )
00421 {
00422     this->fileName_ = fileName;
00423     return *this;
00424 }
00425 
00426 // ****************************************************************************
00427 // *
00428 // *    GetSurface
00429 // *
00430 // ****************************************************************************
00434 // ****************************************************************************
00435 
00436 const rSurface & rSurfaceTexture::GetSurface( void ) const
00437 {
00438     return this->surface_;
00439 }
00440 
00441 #endif

Generated on Sat Mar 15 22:55:53 2008 for Armagetron Advanced by  doxygen 1.5.4