96 lines
2.4 KiB
C++
96 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include "render/OpenGlExtensions.h"
|
|
#include "utils/Utils.h"
|
|
#include <memory>
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
|
|
#if defined __EMSCRIPTEN__ || defined __ANDROID__
|
|
#define PNG_ENABLED
|
|
#endif
|
|
|
|
namespace FRG
|
|
{
|
|
|
|
struct TextureDataStruct {
|
|
size_t width;
|
|
size_t height;
|
|
std::vector<char> data;
|
|
|
|
enum Format {
|
|
R8, // Для шрифтов (GL_RED)
|
|
RGB, // BS_24BIT
|
|
RGBA // BS_32BIT
|
|
};
|
|
|
|
|
|
enum MipmapType {
|
|
NONE,
|
|
GENERATE
|
|
};
|
|
|
|
Format format = RGBA;
|
|
MipmapType mipmap = GENERATE;
|
|
};
|
|
|
|
class Texture
|
|
{
|
|
size_t width = 0;
|
|
size_t height = 0;
|
|
GLuint texID = 0;
|
|
|
|
public:
|
|
|
|
Texture(const TextureDataStruct& texData);
|
|
|
|
//Cubemap texture:
|
|
Texture(const std::array<TextureDataStruct, 6>& texDataArray);
|
|
|
|
~Texture();
|
|
|
|
GLuint getTexID();
|
|
|
|
size_t getWidth();
|
|
size_t getHeight();
|
|
|
|
|
|
};
|
|
|
|
TextureDataStruct CreateTextureDataFromBmp24(const std::string& fullFileName, const std::string& ZIPFileName="");
|
|
TextureDataStruct CreateTextureDataFromBmp32(const std::string& fullFileName, const std::string& ZIPFileName="");
|
|
#ifdef PNG_ENABLED
|
|
TextureDataStruct CreateTextureDataFromPng(const std::string& fullFileName, const std::string& ZIPFileName = "");
|
|
#endif
|
|
|
|
bool IsPowerOf2(size_t n);
|
|
size_t NextPowerOf2(size_t n);
|
|
TextureDataStruct ResizeTextureDataToPOT(const TextureDataStruct& texData);
|
|
|
|
class TextureManager
|
|
{
|
|
public:
|
|
std::shared_ptr<Texture> LoadFromBmp24(const std::string& fileName, const std::string& zipFile = "", bool resizeToPOT = false);
|
|
std::shared_ptr<Texture> LoadFromBmp32(const std::string& fileName, const std::string& zipFile = "", bool resizeToPOT = false);
|
|
#ifdef PNG_ENABLED
|
|
std::shared_ptr<Texture> LoadFromPng(const std::string& fileName, const std::string& zipFile = "", bool resizeToPOT = false);
|
|
#endif
|
|
|
|
void Unload(const std::string& fileName, const std::string& zipFile = "");
|
|
void UnloadByPrefix(const std::string& prefix);
|
|
void UnloadAll();
|
|
|
|
// Marks every texture cached so far as resident, so UnloadNonResident() keeps it.
|
|
void MarkAllResident();
|
|
// Frees cached textures nothing else uses any more (only the cache holds them),
|
|
// except resident ones. Call after releasing the objects that used them.
|
|
void UnloadUnused();
|
|
|
|
private:
|
|
std::unordered_map<std::string, std::shared_ptr<Texture>> textureMap;
|
|
std::unordered_set<std::string> residentKeys;
|
|
static std::string MakeKey(const std::string& fileName, const std::string& zipFile);
|
|
};
|
|
|
|
}
|