PCG-FPS  v2 m0.22.4
Simple First Person Shooter with Procedurally Generated Level
ModelCache.h
Go to the documentation of this file.
1 #ifndef SKR_FPS2_RENDERER_OPENGL_MODELCACHE_H
2 #define SKR_FPS2_RENDERER_OPENGL_MODELCACHE_H
3 
4 #include "..\..\pch.h"
5 #include <assert.h>
6 
7 #include "Model.h"
8 
9 namespace skr
10 {
11 namespace fps2
12 {
13 namespace Renderer
14 {
15 namespace OpenGL
16 {
21  class ModelCache
22  {
23  public:
27  {
28  static ModelCache instance;
29  return instance;
30  }
31 
36  size_t AddModel(Model model)
37  {
38  size_t id;
39  if (!CheckIfModelAlreadyAdded(model._path, id))
40  {
41  id = _nextModelId;
42  _modelCache.insert({ id, std::move(model) });
43  _nextModelId++;
44  return id;
45  }
46  else
47  {
48  return id;
49  }
50  }
51 
59  size_t AddModel(std::string modelDir, glm::vec3 scale = glm::vec3(1.0f), float yoffset = 0.0f, ShaderType shadertype = ShaderType::Main)
60  {
61  size_t id;
62  if (!CheckIfModelAlreadyAdded(modelDir, id))
63  {
64  Model model(modelDir, scale, yoffset, shadertype);
65  return AddModel(std::move(model));
66  }
67  else
68  {
69  return id;
70  }
71  }
72 
76  Model& GetModel(size_t id)
77  {
78  // TODO bounds check?
79  return _modelCache[id];
80  }
81 
82  private:
86  : _nextModelId(0)
87  {};
88 
90  ModelCache(const ModelCache&) = delete;
91 
93  ModelCache operator=(const ModelCache&) = delete;
94 
96  std::unordered_map<size_t, Model> _modelCache;
97 
99  size_t _nextModelId = 1;
100 
105  bool CheckIfModelAlreadyAdded(std::string path, size_t& id)
106  {
107  for (auto& m : _modelCache)
108  {
109  #ifdef _DEBUG
110  assert(!m.second._path.empty()); // model path string is not allowed to be empty empty
111  #endif // _DEBUG
112 
113  if (m.second._path == path)
114  {
115  id = m.first;
116  return true;
117  }
118  }
119 
120  return false;
121  }
122  };
123 
124 }
125 }
126 }
127 }
128 
129 #endif // !SKR_FPS2_RENDERER_OPENGL_MODELCACHE_H
skr::fps2::Renderer::OpenGL::ShaderType::Main
@ Main
main shader
skr::fps2::Renderer::OpenGL::ModelCache::operator=
ModelCache operator=(const ModelCache &)=delete
copy assignment operator is deleted due to singleton pattern
skr::fps2::Renderer::OpenGL::ModelCache::ModelCache
ModelCache(const ModelCache &)=delete
copy constructor is deleted due to singleton pattern
skr::fps2::Renderer::OpenGL::ModelCache::_nextModelId
size_t _nextModelId
id of the next model
Definition: ModelCache.h:99
skr::fps2::Renderer::OpenGL::ModelCache
manages loaded models in a central place Implements Scott Meyers' singleton pattern currently,...
Definition: ModelCache.h:22
skr::fps2::Renderer::OpenGL::Model
holds information of a model
Definition: Model.h:24
skr::fps2::Renderer::OpenGL::ModelCache::_modelCache
std::unordered_map< size_t, Model > _modelCache
map containing models and there ids as key
Definition: ModelCache.h:96
skr::fps2::Renderer::OpenGL::ModelCache::GetInstance
static ModelCache & GetInstance()
gets only instance of ModelCache Implements Scott Meyers' singleton pattern
Definition: ModelCache.h:26
skr::fps2::Renderer::OpenGL::ModelCache::GetModel
Model & GetModel(size_t id)
get reference to model of specified id
Definition: ModelCache.h:76
Model.h
skr::fps2::Renderer::OpenGL::ShaderType
ShaderType
shader type identifies. Used as keys in map keeping shaders
Definition: Shader.h:18
skr::fps2::Renderer::OpenGL::ModelCache::CheckIfModelAlreadyAdded
bool CheckIfModelAlreadyAdded(std::string path, size_t &id)
checks if a given model is already present in the cache, based on its path
Definition: ModelCache.h:105
skr::fps2::Renderer::OpenGL::ModelCache::AddModel
size_t AddModel(Model model)
add model to cache if model is already added, return its id. If not, model is added and its new id is...
Definition: ModelCache.h:36
skr
Definition: AssetPath.h:10
skr::fps2::Renderer::OpenGL::Model::_path
std::string _path
path to directory where model file is loaded from
Definition: Model.h:29
skr::fps2::Renderer::OpenGL::ModelCache::ModelCache
ModelCache()
default constructor default constructor is needed since OpenGL Renderer Object is a global object,...
Definition: ModelCache.h:85
skr::fps2::Renderer::OpenGL::ModelCache::AddModel
size_t AddModel(std::string modelDir, glm::vec3 scale=glm::vec3(1.0f), float yoffset=0.0f, ShaderType shadertype=ShaderType::Main)
add model to cache If model is already in cache, skip loading and return id of model....
Definition: ModelCache.h:59