diff --git a/asset.h b/asset.h new file mode 100644 index 0000000..575f284 --- /dev/null +++ b/asset.h @@ -0,0 +1,12 @@ +#ifndef ASSET_H +#define ASSET_H + +#include + +namespace vb01{ + struct Asset{ + std::string path; + }; +} + +#endif diff --git a/assetManager.cpp b/assetManager.cpp new file mode 100644 index 0000000..0df8b43 --- /dev/null +++ b/assetManager.cpp @@ -0,0 +1,37 @@ +#include "assetManager.h" +#include "imageReader.h" +#include "util.h" + +#include + +using namespace std; + +namespace vb01{ + AssetManager *assetManager = nullptr; + + AssetManager* AssetManager::getSingleton(){ + if(!assetManager) + assetManager = new AssetManager(); + + return assetManager; + } + + void AssetManager::load(string path){ + if(getAsset(path)) + return; + + string format = path.substr(path.find_last_of(".") + 1, string::npos); + vector imageFormats = vector{"png", "jpg", "jpeg"}; + + if(*find(imageFormats.begin(), imageFormats.end(), format) == format) + assets.push_back(ImageReader::getSingleton()->readImage(path, true)); + } + + Asset* AssetManager::getAsset(string path){ + for(Asset *a : assets) + if(a->path == path) + return a; + + return nullptr; + } +} diff --git a/assetManager.h b/assetManager.h new file mode 100644 index 0000000..454f353 --- /dev/null +++ b/assetManager.h @@ -0,0 +1,20 @@ +#ifndef ASSET_MANAGER_H +#define ASSET_MANAGER_H + +#include "asset.h" + +#include + +namespace vb01{ + class AssetManager{ + public: + static AssetManager* getSingleton(); + void load(std::string); + Asset* getAsset(std::string); + private: + AssetManager(){} + std::vector assets; + }; +} + +#endif diff --git a/fontAsset.h b/fontAsset.h new file mode 100644 index 0000000..6812e15 --- /dev/null +++ b/fontAsset.h @@ -0,0 +1,12 @@ +#ifndef FONT_ASSET_H +#define FONT_ASSET_H + +#include "asset.h" + +namespace vb01{ + struct FontAsset : public Asset{ + u32 **glyphImages; + }; +} + +#endif diff --git a/imageAsset.h b/imageAsset.h new file mode 100644 index 0000000..b7c8c14 --- /dev/null +++ b/imageAsset.h @@ -0,0 +1,17 @@ +#ifndef IMAGE_ASSET_H +#define IMAGE_ASSET_H + +#include "asset.h" + +namespace vb01{ + struct ImageAsset : public Asset{ + ImageAsset(std::string path, u8 *image){ + this->path = path; + this->image = image; + } + + u8 *image; + }; +} + +#endif diff --git a/imageReader.cpp b/imageReader.cpp new file mode 100644 index 0000000..82594f7 --- /dev/null +++ b/imageReader.cpp @@ -0,0 +1,22 @@ +#include "imageReader.h" + +#include "stb_image.h" + +using namespace std; + +namespace vb01{ + ImageReader *imageReader = nullptr; + + ImageReader* ImageReader::getSingleton(){ + if(!imageReader) + imageReader = new ImageReader(); + + return imageReader; + } + + ImageAsset* ImageReader::readImage(string path, bool flip){ + stbi_set_flip_vertically_on_load(flip); + u8 *data = stbi_load(path.c_str(), nullptr, nullptr, nullptr, 0); + return new ImageAsset(path, data); + } +} diff --git a/imageReader.h b/imageReader.h new file mode 100644 index 0000000..8975413 --- /dev/null +++ b/imageReader.h @@ -0,0 +1,17 @@ +#ifndef IMAGE_READER_H +#define IMAGE_READER_H + +#include "util.h" +#include "imageAsset.h" + +namespace vb01{ + class ImageReader{ + public: + static ImageReader* getSingleton(); + ImageAsset* readImage(std::string, bool); + private: + ImageReader(){} + }; +} + +#endif diff --git a/modelAsset.h b/modelAsset.h new file mode 100644 index 0000000..22bfcda --- /dev/null +++ b/modelAsset.h @@ -0,0 +1,14 @@ +#ifndef MODEL_ASSET_H +#define MODEL_ASSET_H + +#include "asset.h" + +namespace vb01{ + class Node; + + struct ModelAsset : public Asset{ + Node *rootNode; + }; +} + +#endif diff --git a/textAsset.h b/textAsset.h new file mode 100644 index 0000000..33be2bb --- /dev/null +++ b/textAsset.h @@ -0,0 +1,12 @@ +#ifndef TEXT_ASSET_H +#define TEXT_ASSET_H + +#include "asset.h" + +namespace vb01{ + struct TextAsset : public Asset{ + std::string text; + }; +} + +#endif diff --git a/texture.cpp b/texture.cpp index 244cce1..51409a4 100755 --- a/texture.cpp +++ b/texture.cpp @@ -5,6 +5,8 @@ #include FT_FREETYPE_H #include "texture.h" +#include "assetManager.h" +#include "imageAsset.h" #include "glad.h" #include @@ -75,13 +77,10 @@ namespace vb01{ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - stbi_set_flip_vertically_on_load(flip); + u8 *image = ((ImageAsset*)AssetManager::getSingleton()->getAsset(paths[i]))->image; - data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0); - - glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, png ? GL_RGBA : GL_RGB, width, height, 0, png ? GL_RGBA : GL_RGB, GL_UNSIGNED_BYTE, image); glGenerateMipmap(GL_TEXTURE_2D); - stbi_image_free(data); } } @@ -98,29 +97,17 @@ namespace vb01{ glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_CUBE_MAP, texture[0]); - if(flip) - stbi_set_flip_vertically_on_load(flip); - for(int i = 0; i < 6; i++){ if(!depth){ if(fromFile){ - data = stbi_load(paths[i].c_str(), &width, &height, &numChannels, 0); - - if(data){ - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, data); - stbi_image_free(data); - } - else{ - cout << "Failed to read cubemap texture " << i << endl; - exit(-1); - } + u8 *image = ((ImageAsset*)AssetManager::getSingleton()->getAsset(paths[i]))->image; + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_BYTE, image); } else glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, width, 0, GL_RGB, GL_UNSIGNED_INT, NULL); } - else{ + else glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, width, width, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); - } } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); diff --git a/texture.h b/texture.h index 95ddc74..51cfe58 100755 --- a/texture.h +++ b/texture.h @@ -32,9 +32,8 @@ namespace vb01{ bool cubemap = false; u32 *texture = nullptr; s64 lastUpdateTime = 0; - int width, height, numChannels, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1; + int width, height, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1; float mixRatio; - u8 *data; std::string *paths = nullptr; void createCubemap(bool, bool, bool);