SuperTuxKart
shader_files_manager.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2016 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 SERVER_ONLY
19 
20 #ifndef HEADER_SHADER_FILES_MANAGER_HPP
21 #define HEADER_SHADER_FILES_MANAGER_HPP
22 
23 #include "graphics/gl_headers.hpp"
24 #include "utils/log.hpp"
25 #include "utils/no_copy.hpp"
26 #include "utils/singleton.hpp"
27 
28 #include <algorithm>
29 #include <cassert>
30 #include <string>
31 #include <memory>
32 #include <unordered_map>
33 
34 class ShaderFilesManager : public Singleton<ShaderFilesManager>, NoCopy
35 {
36 private:
37  typedef std::shared_ptr<GLuint> SharedShader;
42  std::unordered_map<std::string, SharedShader> m_shader_files_loaded;
43 
44  // ------------------------------------------------------------------------
45  const std::string& getHeader();
46  // ------------------------------------------------------------------------
47  void readFile(const std::string& file, std::ostringstream& code,
48  bool not_header = true);
49  // ------------------------------------------------------------------------
50  SharedShader addShaderFile(const std::string& full_path, unsigned type);
51 
52 public:
53  // ------------------------------------------------------------------------
55  // ------------------------------------------------------------------------
57  {
58  removeAllShaderFiles();
59  }
60  // ------------------------------------------------------------------------
61  void removeAllShaderFiles()
62  {
63  removeUnusedShaderFiles();
64  if (!m_shader_files_loaded.empty())
65  {
66 #ifdef DEBUG
67  Log::error("ShaderFilesManager", "Some shader file > 1 ref_count");
68 #endif
69  m_shader_files_loaded.clear();
70  }
71  }
72  // ------------------------------------------------------------------------
73  void removeUnusedShaderFiles()
74  {
75  for (auto it = m_shader_files_loaded.begin();
76  it != m_shader_files_loaded.end();)
77  {
78  if (it->second.use_count() == 1 || !it->second)
79  {
80  it = m_shader_files_loaded.erase(it);
81  }
82  else
83  {
84  it++;
85  }
86  }
87  }
88  // ------------------------------------------------------------------------
89  SharedShader loadShader(const std::string& full_path, unsigned type);
90  // ------------------------------------------------------------------------
91  SharedShader getShaderFile(const std::string& file, unsigned type);
92 
93 }; // ShaderFilesManager
94 
95 #endif
96 
97 #endif // !SERVER_ONLY
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Definition: shader_files_manager.hpp:35
SharedShader loadShader(const std::string &full_path, unsigned type)
Loads a single shader.
Definition: shader_files_manager.cpp:131
const std::string & getHeader()
Returns a string with the content of header.txt (which contains basic shader defines).
Definition: shader_files_manager.cpp:41
SharedShader addShaderFile(const std::string &full_path, unsigned type)
Loads a single shader file, and add it to the loaded (cached) list.
Definition: shader_files_manager.cpp:267
SharedShader getShaderFile(const std::string &file, unsigned type)
Get a shader file.
Definition: shader_files_manager.cpp:287
std::unordered_map< std::string, SharedShader > m_shader_files_loaded
Map from a filename in full path to a shader indentifier.
Definition: shader_files_manager.hpp:42
Definition: singleton.hpp:87