Bubba-3D  0.9.0
Awesome game engine!
ResourceManager.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include "ResourceManager.h"
18 #include <sstream>
19 #include <Logger.h>
20 #include <ShaderProgram.h>
21 #include <shader/VertexShader.h>
22 #include <shader/FragmentShader.h>
23 #include "objects/Chunk.h"
24 
25 std::map<std::string, ShaderProgram> ResourceManager::shaders;
26 std::map<std::string, Texture> ResourceManager::textures;
27 std::map<std::string, Mesh> ResourceManager::meshes;
28 
29 void ResourceManager::loadShader(const std::string &vertexShader, const std::string &fragmentShader, std::string name){
30  ShaderProgram shaderProgram;
31  shaderProgram.loadShader(new VertexShader(vertexShader), new FragmentShader(fragmentShader));
32  shaders.insert(std::pair<std::string, ShaderProgram>(name, shaderProgram));
33 }
34 
35 ShaderProgram* ResourceManager::getShader(std::string name) {
36  return getItemFromMap(&shaders, name);
37 }
38 
39 Texture* ResourceManager::loadAndFetchTexture(const std::string &fileName) {
40  try {
41  return getTexture(fileName);
42  } catch (std::invalid_argument exception) {
43  loadTexture(fileName);
44  return getTexture(fileName);
45  }
46 }
47 
48 void ResourceManager::loadTexture(const std::string &fileName) {
49  Texture texture;
50  texture.loadTexture(fileName);
51  textures.insert(std::pair<std::string, Texture>(fileName, texture));
52 }
53 
54 Texture* ResourceManager::getTexture(std::string fileName) {
55  return getItemFromMap(&textures, fileName);
56 }
57 
58 
59 Mesh* ResourceManager::loadAndFetchMesh(const std::string &fileName){
60  try {
61  return getMesh(fileName);
62  } catch (std::invalid_argument exception) {
63  loadMesh(fileName);
64  return getMesh(fileName);
65  }
66 }
67 
68 void ResourceManager::loadMesh(const std::string &fileName){
69  Mesh mesh;
70  mesh.loadMesh(fileName);
71  meshes.insert(std::pair<std::string, Mesh>(fileName, mesh));
72 }
73 
74 Mesh* ResourceManager::getMesh(std::string fileName)
75 {
76  return getItemFromMap(&meshes, fileName);
77 }
78 
79 template<typename Type>
80 Type* ResourceManager::getItemFromMap(std::map<std::string, Type> *map, std::string id) {
81  typename std::map<std::string, Type>::iterator it = map->find(id);
82  if( it != map->end()) {
83  return &it->second;
84  } else {
85  std::stringstream errorMessage;
86  errorMessage << id << " hasn't been loaded into ResourceManager before fetched";
87  throw std::invalid_argument(errorMessage.str());
88  }
89 }
90 
91 
92 
93 
94 
95 
Compiles Vertex Shaders.
Definition: VertexShader.h:37
Compiles Fragment Shaders.
Definition: Mesh.h:45
Class for maintaining OpenGL shader programs.
Definition: ShaderProgram.h:39
void loadShader(IShader *vertexShader, IShader *fragmentShader)