src/video/sdl/sdl_texture.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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 3 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, see <http://www.gnu.org/licenses/>.
00016 
00017 #ifndef HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
00018 #define HEADER_SUPERTUX_VIDEO_SDL_TEXTURE_HPP
00019 
00020 #include <algorithm>
00021 #include <config.h>
00022 
00023 #include <SDL.h>
00024 
00025 #include "video/color.hpp"
00026 #include "video/texture.hpp"
00027 
00028 class SDLTexture : public Texture
00029 {
00030 protected:
00031   SDL_Surface *texture;
00032   //unsigned int width;
00033   //unsigned int height;
00034 
00035   struct ColorCache
00036   {
00037     static const int HASHED_BITS = 3;
00038     static const int CACHE_SIZE = 1 << (HASHED_BITS * 3);
00039 
00040     static void ref(SDL_Surface *surface)
00041     {
00042       if(surface)
00043       {
00044         surface->refcount++;
00045       }
00046     }
00047 
00048     static int hash(const Color &color)
00049     {
00050       return
00051         ((int) (color.red * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1) * 2) |
00052         ((int) (color.green * ((1 << HASHED_BITS) - 1)) << (HASHED_BITS - 1)) |
00053         ((int) (color.blue * ((1 << HASHED_BITS) - 1)) << 0);
00054     }
00055 
00056     SDL_Surface *data[CACHE_SIZE];
00057 
00058     ColorCache()
00059     {
00060       memset(data, 0, CACHE_SIZE * sizeof(SDL_Surface *));
00061     }
00062 
00063     ColorCache(const ColorCache&);
00064 
00065     ~ColorCache()
00066     {
00067       std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
00068     }
00069 
00070     ColorCache& operator=(const ColorCache &other)
00071     {
00072       if (this != &other)
00073       {
00074         std::for_each(other.data, other.data + CACHE_SIZE, ref);
00075         std::for_each(data, data + CACHE_SIZE, SDL_FreeSurface);
00076         memcpy(data, other.data, CACHE_SIZE * sizeof(SDL_Surface *));
00077       }
00078       return *this;
00079     }
00080 
00081     SDL_Surface *&operator [] (const Color &color)
00082     {
00083       return data[hash(color)];
00084     }
00085   };
00086   //typedef std::map<Color, SDL_Surface *> ColorCache;
00087   ColorCache cache[NUM_EFFECTS];
00088 
00089 public:
00090   SDLTexture(SDL_Surface* sdlsurface);
00091   virtual ~SDLTexture();
00092 
00093   SDL_Surface *get_transform(const Color &color, DrawingEffect effect);
00094 
00095   SDL_Surface *get_texture() const
00096   {
00097     return texture;
00098   }
00099 
00100   unsigned int get_texture_width() const
00101   {
00102     return texture->w;
00103   }
00104 
00105   unsigned int get_texture_height() const
00106   {
00107     return texture->h;
00108   }
00109 
00110   unsigned int get_image_width() const
00111   {
00112     return texture->w;
00113   }
00114 
00115   unsigned int get_image_height() const
00116   {
00117     return texture->h;
00118   }
00119 
00120   /*unsigned int get_texture_width() const
00121     {
00122     return width;
00123     }
00124 
00125     unsigned int get_texture_height() const
00126     {
00127     return height;
00128     }
00129 
00130     unsigned int get_image_width() const
00131     {
00132     return width;
00133     }
00134 
00135     unsigned int get_image_height() const
00136     {
00137     return height;
00138     }*/
00139 
00140 private:
00141   SDLTexture(const SDLTexture&);
00142   SDLTexture& operator=(const SDLTexture&);
00143 };
00144 
00145 #endif
00146 
00147 /* EOF */

Generated on Mon Jun 9 03:38:24 2014 for SuperTux by  doxygen 1.5.1