2016-07-28 17:01:57 +02:00

124 lines
3.3 KiB
C++

#ifndef IMAGE
#define IMAGE
#include <string>
#include <vector>
#include <unordered_map>
class Texture;
struct Image
{
int width;
int height;
int depth;
std::string m_name;
std::vector<unsigned char> pixels;
Image() : width(0), height(0), depth(8) {}
Image(const std::string &name, int myDepth, int myWidth, int myHeight, float frequency, float amplitude);
void allocate(int size)
{ pixels.resize(size); }
/*
* PNG serialization
*/
Image(const std::string &pngFile);
Image(const unsigned char *pngPtr, size_t size);
bool save(const std::string &filename);
bool save(std::vector<unsigned char> &out);
};
class TexturePack
{
private:
struct TexImage
{
std::string name;
Texture* tex;
Image* img;
TexImage() : tex(NULL), img(NULL) {}
};
std::string m_name;
std::unordered_map<std::string, TexImage> m_images;
bool allocatedImg;
bool allocatedTex;
public:
/**
* @brief creates a new empty texture pack
* the name must be a valid string for a filename :
* (no symbols except underscore, avoid spaces, avoid special characters)
*/
TexturePack(std::string name = "texture_pack") :
m_name(name), allocatedImg(false), allocatedTex(false) {}
/**
* @brief rename renames the texture pack
* the name must be a valid string for a filename :
* (no symbols except underscore, avoid spaces, avoid special characters)
*/
void rename(const std::string &name) { m_name = name; }
/**
* @brief addImage adds an image to the pack, if an image of this name already exists in the pack, it will be replaced.
*/
void addImage(Image* img);
/**
* @brief removeImage removes the image named "name" from the pack if it exists.
*/
void removeImage(const std::string &name);
/**
* @brief addImage adds an image from an OpenGL texture and names it with the provided name.
* @param downloadFromGL if this is false, the Image will not be downloaded from the texture, only the texture will be added.
* this does not support cubemap or array textures (only the first face will be added)
*/
void addImage(Texture* tex, const std::string &name, bool downloadFromGL = true);
/**
* @brief initGL creates an OpenGL texture for every Image in the pack.
*/
void initGL();
/**
* @brief initDestroyImages creates an OpenGL texture for every Image in the pack, and then deletes all Images.
*/
void initDestroyImages();
/**
* @brief getImagesFromGL downloads the images from the opengl texture memory for all available textures
*/
void getImagesFromGL();
/**
* @brief destroyGL destroys all the OpenGL textures of the pack.
*/
void destroyGL();
/**
* @brief loads a texture pack from a folder
* the texture pack name will be the folder name
* every png file in the folder will be loaded in the pack
* and named from the file name minus its extension
*/
bool load(const std::string &folderPath);
/**
* @brief saves the texture pack as png images in the specified existing folder
* @return true if serialisation succeeded
*/
bool save(const std::string &folderPath);
};
#endif // IMAGE