SimpleGL  1.1.0
A framework for platform independent rendering
manager_vk.cpp
Go to the documentation of this file.
1 
9 
10 #include <fstream>
11 #include <map>
12 #include <vector>
13 
14 #define STB_IMAGE_IMPLEMENTATION
15 #include <stb_image.h>
16 
17 #include <SimpleGL/logging.hpp>
18 
19 namespace sgl {
20 
21  void ShaderModuleManager::remove(std::string name) {
22  /* Dispose shader module */
23  ShaderModule* shaderModule = get(name);
24  delete shaderModule;
25 
26  /* Call base method */
27  Manager::remove(name);
28  }
29 
31  /* Dispose all shaders */
32  for (map<string, ShaderModule*>::iterator it = objects.begin(); it != objects.end(); it++) {
33  ShaderModule* shaderModule = it->second;
34  delete shaderModule;
35  }
36 
37  /* Call base method */
39  }
40 
41  ShaderModule* ShaderModuleManager::loadShaderModule(std::string path, std::string name, ShaderStageBit stageBit) {
42  /* Opening file stream */
43  ifstream fileStream(path, ios::ate | ios::binary);
44  if (fileStream.is_open()) {
45  /* Determine file size */
46  size_t fileSize = fileStream.tellg();
47  vector<uint8_t> fileBuffer(fileSize);
48 
49  /* Read binary file */
50  fileStream.seekg(0);
51  fileStream.read(reinterpret_cast<char*> (fileBuffer.data()), fileSize);
52 
53  /* Close file */
54  fileStream.close();
55 
56  /* Create shader module */
57  ShaderModule* shaderModule = new ShaderModule(fileBuffer, stageBit);
58 
59  /* Store shader module and return it */
60  put(name, shaderModule);
61  return shaderModule;
62  } else {
63  Logger::logError("Could not open file " + path);
64  return new ShaderModule(vector<uint8_t>(), stageBit);
65  }
66  }
67 
68  void TextureImageManager::remove(std::string name) {
69  /* Dispose texture image */
70  TextureImage* textureImage = get(name);
71  delete textureImage;
72 
73  /* Call base method */
74  Manager::remove(name);
75  }
76 
78  /* Dispose all texture images */
79  for (map<string, TextureImage*>::iterator it = objects.begin(); it != objects.end(); it++) {
80  TextureImage* textureImage = it->second;
81  delete textureImage;
82  }
83 
84  /* Call base method */
86  }
87 
88  TextureImage* TextureImageManager::load(std::string path, std::string name) {
89  /* Load bitmap file and create image */
90  stbi_set_flip_vertically_on_load(true);
91  int32_t width, height, components;
92  stbi_uc* pixels = stbi_load(path.c_str(), &width, &height, &components, 0);
93 
94  /* Generate Texture image */
95  TextureImage* textureImage = new TextureImage(width, height);
96 
97  /* Upload texture data */
98  textureImage->upload(vector<uint8_t>(pixels, pixels + components * width * height));
99  stbi_image_free(pixels);
100 
101  /* Put texture in storage and return it */
102  put(name, textureImage);
103  return textureImage;
104  }
105 }
std::map< std::string, ShaderModule * > objects
Contains the objects to manage.
Definition: manager.hpp:28
ShaderModule * loadShaderModule(std::string path, std::string name, ShaderStageBit stageBit)
Loads a shader module from a binary file and stores it.
Definition: manager_vk.cpp:41
void remove(std::string name) override
Removes and disposes a shader module from the storage.
Definition: manager_vk.cpp:21
static void logError(std::string message)
Logs an error string to cerr.
Definition: logging.cpp:24
void clear() override
Removes all texture images and disposes them.
Definition: manager_vk.cpp:77
Generic namespace for the SimpleGL framework.
Definition: application.hpp:18
void remove(std::string name) override
Removes and disposes a texture image from the storage.
Definition: manager_vk.cpp:68
void put(std::string name, ShaderModule *value)
Stores a object with specified name.
virtual void clear()
Removes all objects and disposes them.
Definition: manager.tcc:47
This class wraps a Vulkan texture image.
virtual void remove(std::string name)
Removes and disposes an object from the storage.
Definition: manager.tcc:42
ShaderStageBit
This enum stores supported shader stage flag bit.
TextureImage * load(std::string path, std::string name)
Loads a texture from file and stores it.
Definition: manager_vk.cpp:88
This class wraps a Vulkan shader module.
void clear() override
Removes all shader modules and disposes them.
Definition: manager_vk.cpp:30