Colobot
modelmanager.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 #include "common/singleton.h"
23 
25 
26 #include <string>
27 #include <vector>
28 #include <map>
29 
30 namespace Gfx {
31 
32 class CEngine;
33 class CModelFile;
34 
54 class CModelManager : public CSingleton<CModelManager>
55 {
56 public:
57  CModelManager(CEngine* engine);
58  ~CModelManager();
59 
61  bool LoadModel(const std::string& fileName, bool mirrored);
62 
64  bool AddModelReference(const std::string& fileName, bool mirrored, int objRank);
65 
67  bool AddModelCopy(const std::string& fileName, bool mirrored, int objRank);
68 
70  bool IsModelLoaded(const std::string& fileName, bool mirrored);
71 
73  int GetModelBaseObjRank(const std::string& fileName, bool mirrored);
74 
76  void DeleteAllModelCopies();
77 
79  void UnloadModel(const std::string& fileName, bool mirrored);
81  void UnloadAllModels();
82 
83 protected:
85  float GetHeight(std::vector<ModelTriangle>& triangles, Math::Vector pos);
86 
88  void Mirror(std::vector<ModelTriangle>& triangles);
89 
90 private:
91  struct ModelInfo
92  {
93  std::vector<ModelTriangle> triangles;
94  int baseObjRank;
95  };
96  struct FileInfo
97  {
98  std::string fileName;
99  bool mirrored;
100 
101  inline FileInfo(const std::string& _fileName, bool _mirrored)
102  : fileName(_fileName)
103  , mirrored(_mirrored)
104  {}
105 
106  inline bool operator<(const FileInfo& other) const
107  {
108  int compare = fileName.compare(other.fileName);
109  if (compare < 0)
110  return true;
111  if (compare > 0)
112  return false;
113 
114  return !mirrored && mirrored != other.mirrored;
115  }
116  };
117  std::map<FileInfo, ModelInfo> m_models;
118  std::vector<int> m_copiesBaseRanks;
119  CEngine* m_engine;
120 };
121 
122 } // namespace Gfx
123 
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:30
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:43
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
Namespace for (new) graphics code.
Definition: app.h:49
Manager for static models.
Definition: modelmanager.h:54
The graphics engine.
Definition: engine.h:684
3D (3x1) vector
Definition: vector.h:52
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