Back to index

unity  6.0.0
TextureCache.h
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2010, 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
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  * Authored by Gordon Allott <gord.allott@canonical.com>
00018  *             Tim Penhey <tim.penhey@canonical.com>
00019  */
00020 
00021 #ifndef UNITY_TEXTURECACHE_H
00022 #define UNITY_TEXTURECACHE_H
00023 
00024 #include <string>
00025 #include <map>
00026 
00027 #include <Nux/Nux.h>
00028 
00029 #include <sigc++/sigc++.h>
00030 #include <sigc++/trackable.h>
00031 
00032 /* A simple texture cache system, you ask the cache for a texture by id if the
00033  * texture does not exist it calls the callback function you provide it with
00034  * to create the texture, then returns it.  you should remember to ref/unref
00035  * the textures yourself however
00036  */
00037 namespace unity
00038 {
00039 
00040 class TextureCache : public sigc::trackable
00041 {
00042 public:
00043   typedef nux::ObjectPtr<nux::BaseTexture> BaseTexturePtr;
00044 
00045   // id, width, height -> texture
00046   typedef sigc::slot<nux::BaseTexture*, std::string const&, int, int> CreateTextureCallback;
00047 
00048   static TextureCache& GetDefault();
00049 
00050   BaseTexturePtr FindTexture(std::string const& texture_id, int width, int height, CreateTextureCallback callback);
00051 
00052   // Return the current size of the cache.
00053   std::size_t Size() const;
00054 
00055 private:
00056   TextureCache();
00057   ~TextureCache();
00058 
00059   std::string Hash(std::string const& , int width, int height);
00060   // Have the key passed by value not referece, as the key will be a bound
00061   // parameter in the slot passed to the texture on_destroy signal.
00062   void OnDestroyNotify(nux::Trackable* Object, std::string key);
00063 
00064   std::map<std::string, nux::BaseTexture*> cache_;
00065 };
00066 
00067 }
00068 
00069 #endif // TEXTURECACHE_H