1 //  SuperTuxKart - a fun racing game with go-kart
2 //  Copyright (C) 2017 SuperTuxKart-Team
3 //
4 //  This program is free software; you can redistribute it and/or
5 //  modify it under the terms of the GNU General Public License
6 //  as published by the Free Software Foundation; either version 3
7 //  of the License, or (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program; if not, write to the Free Software
16 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 
18 #ifndef HEADER_STK_TEX_MANAGER_HPP
19 #define HEADER_STK_TEX_MANAGER_HPP
20 
21 #include "graphics/gl_headers.hpp"
22 #include "utils/no_copy.hpp"
23 #include "utils/singleton.hpp"
24 
25 #include "irrString.h"
26 #include "ITexture.h"
27 
28 #include <cassert>
29 #include <string>
30 #include <unordered_map>
31 
32 class STKTexture;
33 namespace irr
34 {
35     namespace video { class SColor; }
36 }
37 
38 struct TexConfig
39 {
40      bool m_srgb;
41      bool m_premul_alpha;
42      bool m_mesh_tex;
43      bool m_set_material;
44      bool m_colorization_mask;
45      bool m_normal_map;
TexConfigTexConfig46      TexConfig(bool srgb = false, bool premul_alpha = false,
47                bool mesh_tex = true, bool set_material = false,
48                bool color_mask = false, bool normal_map = false)
49      {
50          m_srgb = srgb;
51          m_premul_alpha = premul_alpha;
52          m_mesh_tex = mesh_tex;
53          m_set_material = set_material;
54          m_colorization_mask = color_mask;
55          m_normal_map = normal_map;
56      }
57 };
58 
59 class STKTexManager : public Singleton<STKTexManager>, NoCopy
60 {
61 private:
62     std::unordered_map<std::string, STKTexture*> m_all_textures;
63 
64     /** Additional details to be shown in case that a texture is not found.
65      *  This is used to specify details like: "while loading kart '...'" */
66     std::string m_texture_error_message;
67 
68     // ------------------------------------------------------------------------
69     STKTexture* findTextureInFileSystem(const std::string& filename,
70                                         std::string* full_path);
71 public:
72     // ------------------------------------------------------------------------
STKTexManager()73     STKTexManager() {}
74     // ------------------------------------------------------------------------
75     ~STKTexManager();
76     // ------------------------------------------------------------------------
77     irr::video::ITexture* getTexture(const std::string& path,
78                                      TexConfig* tc = NULL,
79                                      bool no_upload = false,
80                                      bool create_if_unfound = true);
81     // ------------------------------------------------------------------------
82     irr::video::ITexture* addTexture(STKTexture* texture);
83     // ------------------------------------------------------------------------
84     void removeTexture(STKTexture* texture, bool remove_all = false);
85     // ------------------------------------------------------------------------
86     int dumpTextureUsage();
87     // ------------------------------------------------------------------------
88     /** Returns the currently defined texture error message, which is used
89      *  by event_handler.cpp to print additional info about irrlicht
90      *  internal errors or warnings. If no error message is currently
91      *  defined, the error message is "".
92      */
getTextureErrorMessage()93     const std::string &getTextureErrorMessage()
94     {
95         return m_texture_error_message;
96     }   // getTextureErrorMessage
97     // ------------------------------------------------------------------------
98     void setTextureErrorMessage(const std::string &error,
99                                 const std::string &detail="");
100     // ------------------------------------------------------------------------
101     /** Disables the texture error message again.
102      */
unsetTextureErrorMessage()103     void unsetTextureErrorMessage()           { m_texture_error_message = ""; }
104     // ------------------------------------------------------------------------
105     /** Convenience function that loads a texture with default parameters
106      *  but includes an error message.
107      *  \param filename File name of the texture to load.
108      *  \param error Error message, potentially with a '%' which will be
109      *               replaced with detail.
110      *  \param detail String to replace a '%' in the error message.
111      */
getTexture(const std::string & filename,const std::string & error_message,const std::string & detail="")112     irr::video::ITexture* getTexture(const std::string &filename,
113                                      const std::string &error_message,
114                                      const std::string &detail="")
115     {
116         setTextureErrorMessage(error_message, detail);
117         irr::video::ITexture *tex = getTexture(filename);
118         unsetTextureErrorMessage();
119         return tex;
120     }   // getTexture
121     // ------------------------------------------------------------------------
122     /** Convenience function that loads a texture with default parameters
123      *  but includes an error message.
124      *  \param filename File name of the texture to load.
125      *  \param error Error message, potentially with a '%' which will be
126      *               replaced with detail.
127      *  \param detail String to replace a '%' in the error message.
128      */
getTexture(const std::string & filename,char * error_message,char * detail=NULL)129     irr::video::ITexture* getTexture(const std::string &filename,
130                                      char *error_message,
131                                      char *detail = NULL)
132     {
133         if (!detail)
134             return getTexture(filename, std::string(error_message),
135                               std::string(""));
136 
137         return getTexture(filename, std::string(error_message),
138                           std::string(detail));
139     }   // getTexture
140 
141 };   // STKTexManager
142 
143 #endif
144