Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Texture.hpp
1#pragma once
2
3#include <unordered_map>
4#include <string>
5#include "RootDisplay.hpp"
6#include "Element.hpp"
7
8enum TextureScaleMode
9{
11 SCALE_STRETCH,
12
14 SCALE_PROPORTIONAL_WITH_BG,
15};
16
18{
19 CST_Texture* texture;
20 CST_Color firstPixel;
21};
22
23class Texture : public Element
24{
25public:
26 virtual ~Texture();
27
30 void clear(void);
31
34 bool loadFromSurface(CST_Surface *surface);
35
38 bool loadFromCache(std::string &key);
39
42 bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface);
43
45 void render(Element* parent);
46
48 void resize(int w, int h);
49
51 void setScaleMode(TextureScaleMode mode);
52
54 void getTextureSize(int *w, int *h);
55
56 // chainables
57 Texture* setSize(int w, int h);
58
60 bool saveTo(std::string& path);
61
63 void loadPath(std::string& path, bool forceReload = false);
64
66 int cornerRadius = 0;
67
68protected:
70 static std::unordered_map<std::string, TextureData> texCache;
71
73 CST_Texture* mTexture = nullptr;
74
76 int texW = 0, texH = 0;
77
79 CST_Color texFirstPixel = {0,0,0,0};
80
82 TextureScaleMode texScaleMode = SCALE_STRETCH;
83};
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:102
void getTextureSize(int *w, int *h)
Return texture's original size.
Definition: Texture.cpp:198
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
Definition: Texture.cpp:91
static std::unordered_map< std::string, TextureData > texCache
Cache previously displayed textures.
Definition: Texture.hpp:70
CST_Color texFirstPixel
The color of the first pixel.
Definition: Texture.hpp:79
int cornerRadius
Rounded corner radius (if >0, will round)
Definition: Texture.hpp:66
int texW
The size of the texture.
Definition: Texture.hpp:76
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:181
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:76
void clear(void)
Reinitialize Texture Resets texture content, size and color.
Definition: Texture.cpp:9
void render(Element *parent)
Renders the texture.
Definition: Texture.cpp:108
bool saveTo(std::string &path)
save this texture to the given file path as a PNG
Definition: Texture.cpp:206
void setScaleMode(TextureScaleMode mode)
Sets texture scaling mode.
Definition: Texture.cpp:193
bool loadFromSurface(CST_Surface *surface)
Loads the texture from a surface Returns true if successful.
Definition: Texture.cpp:50
TextureScaleMode texScaleMode
Texture's scaling mode.
Definition: Texture.hpp:82
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
Definition: Texture.cpp:230
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:73