00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
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  const unsigned long ImageFileTexture::TEXTURE_NO_SCALE   = 1;
00056  const unsigned long ImageFileTexture::TEXTURE_MODE_COLOR = 1;
00057  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() );  
00066         if( sdl_surface != NULL ){
00067             copySdlSurface( sdl_surface, mode, flags );
00068 
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 
00100     View::check();
00101     setEnv   ( ENV_MODULATE );
00102     View::check();
00103     putData( (unsigned char*)surface->pixels, size, format, 0 );
00104 
00105     View::check();
00106 }
00107 
00108 
00109 };  
00110 };  
00111