Colobot
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
modelmanager.h
1 // * This file is part of the COLOBOT source code
2 // * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
3 // * Copyright (C) 2012-2014, Polish Portal of Colobot (PPC)
4 // *
5 // * This program is free software: you can redistribute it and/or modify
6 // * it under the terms of the GNU General Public License as published by
7 // * the Free Software Foundation, either version 3 of the License, or
8 // * (at your option) any later version.
9 // *
10 // * This program is distributed in the hope that it will be useful,
11 // * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // * GNU General Public License for more details.
14 // *
15 // * You should have received a copy of the GNU General Public License
16 // * along with this program. If not, see http://www.gnu.org/licenses/.
17 
18 #pragma once
19 
20 #include "common/singleton.h"
21 
23 
24 #include <string>
25 #include <vector>
26 #include <map>
27 
28 namespace Gfx {
29 
30 class CEngine;
31 class CModelFile;
32 
52 class CModelManager : public CSingleton<CModelManager>
53 {
54 public:
55  CModelManager(CEngine* engine);
56  ~CModelManager();
57 
59  bool LoadModel(const std::string& fileName, bool mirrored);
60 
62  bool AddModelReference(const std::string& fileName, bool mirrored, int objRank);
63 
65  bool AddModelCopy(const std::string& fileName, bool mirrored, int objRank);
66 
68  bool IsModelLoaded(const std::string& fileName, bool mirrored);
69 
71  int GetModelBaseObjRank(const std::string& fileName, bool mirrored);
72 
74  void DeleteAllModelCopies();
75 
77  void UnloadModel(const std::string& fileName, bool mirrored);
79  void UnloadAllModels();
80 
81 protected:
83  float GetHeight(std::vector<ModelTriangle>& triangles, Math::Vector pos);
84 
86  void Mirror(std::vector<ModelTriangle>& triangles);
87 
88 private:
89  struct ModelInfo
90  {
91  std::vector<ModelTriangle> triangles;
92  int baseObjRank;
93  };
94  struct FileInfo
95  {
96  std::string fileName;
97  bool mirrored;
98 
99  inline FileInfo(const std::string& _fileName, bool _mirrored)
100  : fileName(_fileName)
101  , mirrored(_mirrored)
102  {}
103 
104  inline bool operator<(const FileInfo& other) const
105  {
106  int compare = fileName.compare(other.fileName);
107  if (compare < 0)
108  return true;
109  if (compare > 0)
110  return false;
111 
112  return !mirrored && mirrored != other.mirrored;
113  }
114  };
115  std::map<FileInfo, ModelInfo> m_models;
116  std::vector<int> m_copiesBaseRanks;
117  CEngine* m_engine;
118 };
119 
120 } // namespace Gfx
121 
CSingleton base class for singletons.
float GetHeight(std::vector< ModelTriangle > &triangles, Math::Vector pos)
Returns the height of model – closest point to X and Z coords of pos.
Definition: modelmanager.cpp:201
Definition: singleton.h:27
void DeleteAllModelCopies()
Deletes all copied objects.
Definition: modelmanager.cpp:154
bool LoadModel(const std::string &fileName, bool mirrored)
Loads a model from given file.
Definition: modelmanager.cpp:42
bool IsModelLoaded(const std::string &fileName, bool mirrored)
Returns true if given model is loaded.
Definition: modelmanager.cpp:140
Model loading - CModelFile class (aka modfile)
int GetModelBaseObjRank(const std::string &fileName, bool mirrored)
Returns the rank of base engine object of given loaded model.
Definition: modelmanager.cpp:145
bool AddModelCopy(const std::string &fileName, bool mirrored, int objRank)
Adds an instance of model to the given object rank as a copy (copied base object) ...
Definition: modelmanager.cpp:120
void UnloadAllModels()
Unloads all models.
Definition: modelmanager.cpp:175
Manager for static models.
Definition: modelmanager.h:52
The graphics engine.
Definition: engine.h:682
3D (3x1) vector
Definition: vector.h:49
void UnloadModel(const std::string &fileName, bool mirrored)
Unloads the given model.
Definition: modelmanager.cpp:164
void Mirror(std::vector< ModelTriangle > &triangles)
Mirrors the model along the Z axis.
Definition: modelmanager.cpp:183
bool AddModelReference(const std::string &fileName, bool mirrored, int objRank)
Adds an instance of model to the given object rank as a reference to base object. ...
Definition: modelmanager.cpp:104