Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

ImageFileTexture.cpp

Go to the documentation of this file.
00001 
00002 /*
00003     TEDDY - General graphics application library
00004     Copyright (C) 1999-2002  Timo Suoranta
00005     tksuoran@cc.helsinki.fi
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2.1 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     $Id: ImageFileTexture.cpp,v 1.4 2002/01/11 14:34:59 tksuoran Exp $
00022 */
00023 
00024 
00025 #include "Teddy/TeddyConfig.h"
00026 #include "Teddy/Maths/Vector2.h"
00027 #include "Teddy/Graphics/convgltex.h"
00028 #include "Teddy/Graphics/ImageFileTexture.h"
00029 #include "Teddy/Graphics/Device.h"
00030 #include "Teddy/Graphics/View.h"
00031 #include "Teddy/SysSupport/Messages.h"
00032 using namespace Teddy::Maths;
00033 using namespace Teddy::SysSupport;
00034 
00035 
00036 namespace Teddy    {
00037 namespace Graphics {
00038 
00039 
00040 #ifdef HAVE_LIB_SDL_IMAGE
00041 # include "SDL_image.h"
00042 # if defined( _MSC_VER )
00043 #  if defined( _DEBUG ) 
00044 #   pragma comment (lib, "SDL_imageD.lib")
00045 #  else
00046 #   pragma comment (lib, "SDL_image.lib")
00047 #  endif
00048 # endif
00049 #endif
00050 
00051 
00052 static Uint32 getpixel( SDL_Surface *surface, int x, int y );
00053 
00054 
00055 /*static*/ const unsigned long ImageFileTexture::TEXTURE_NO_SCALE   = 1;
00056 /*static*/ const unsigned long ImageFileTexture::TEXTURE_MODE_COLOR = 1;
00057 /*static*/ const unsigned long ImageFileTexture::TEXTURE_MODE_ALPHA = 2;
00058 
00059 
00061 ImageFileTexture::ImageFileTexture( const std::string &fname, int mode, int flags ):Texture( fname){
00062 #ifdef HAVE_LIB_SDL_IMAGE
00063     View::check();
00064     if( fname.length() > 0 ){
00065         SDL_Surface *sdl_surface = load_gl_texture( fname.c_str() );  // IMG_Load( fname );
00066         if( sdl_surface != NULL ){
00067             copySdlSurface( sdl_surface, mode, flags );
00068 //          SDL_FreeSurface( sdl_surface );
00069             this->is_good = true;
00070         }else{
00071             emsg( M_MAT, "Could not load texture file %s", fname.c_str() );
00072         }
00073     }else{
00074         emsg( M_MAT, "Bad empty filename" );
00075     }
00076 #else
00077     emsg( M_MAT, "ImageFileTexture requires SDL_image, which was not available" );
00078 #endif
00079 }
00080 
00081 
00083 void ImageFileTexture::copySdlSurface( SDL_Surface *surface, int mode, int flags ){
00084     View::check();
00085     size = Vector2( surface->w, surface->h );
00086     int format   = 0;
00087 
00088     switch( surface->format->BitsPerPixel ){
00089     case  8: break;
00090     case 16: break;
00091     case 24: format = FORMAT_RGB;  break;
00092     case 32: format = FORMAT_RGBA; break;
00093     default: break;
00094     }
00095 
00096     setWrap  ( WRAP_REPEAT, WRAP_REPEAT );
00097     View::check();
00098     setFilter( FILTER_LINEAR );
00099 //  setFilter( FILTER_NEAREST );
00100     View::check();
00101     setEnv   ( ENV_MODULATE );
00102     View::check();
00103     putData( (unsigned char*)surface->pixels, size, format, 0 );
00104 //  putData( (unsigned char*)surface->pixels, width, height, format, TX_BLIT_NO_SCALE );
00105     View::check();
00106 }
00107 
00108 
00109 };  //  namespace Materials
00110 };  //  namespace Teddy
00111