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#include <map>
8
9namespace Chesto {
10
11enum TextureScaleMode
12{
14 SCALE_STRETCH,
15
17 SCALE_PROPORTIONAL_WITH_BG,
18
19 SCALE_PROPORTIONAL_NO_BG,
20};
21
23{
24 CST_Texture* texture;
25 CST_Color firstPixel;
26};
27
28class Texture : public Element
29{
30public:
31 virtual ~Texture();
32
35 void clear(void);
36
39 bool loadFromSurface(CST_Surface *surface);
40
43 bool loadFromCache(std::string &key);
44
47 bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface);
48
50 void render(Element* parent);
51
53 void resize(int w, int h);
54
56 void setScaleMode(TextureScaleMode mode);
57
59 void getTextureSize(int *w, int *h);
60
61 // chainables
62 Texture* setSize(int w, int h);
63
65 bool saveTo(std::string& path);
66
68 void loadPath(std::string& path, bool forceReload = false);
69
71 int cornerRadius = 0;
72
74 SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
75
76 // the prefix to keys to identify our text elements within our cache
77 static const std::string textElemPrefix;
78
80 static void wipeEntireCache();
81
83 static void wipeTextCache();
84
85protected:
87 static std::unordered_map<std::string, TextureData> texCache;
88
90 CST_Texture* mTexture = nullptr;
91
93 int texW = 0, texH = 0;
94
96 CST_Color texFirstPixel = {0,0,0,0};
97
99 TextureScaleMode texScaleMode = SCALE_STRETCH;
100};
101
102} // namespace Chesto
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116
int cornerRadius
Rounded corner radius (if >0, will round)
Definition: Texture.hpp:71
static void wipeEntireCache()
Wipes the entire texture cache (generally, should only be used to reload entire theme / text)
Definition: Texture.cpp:113
TextureScaleMode texScaleMode
Texture's scaling mode.
Definition: Texture.hpp:99
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:96
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:213
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
Definition: Texture.cpp:262
void clear(void)
Reinitialize Texture Resets texture content, size and color.
Definition: Texture.cpp:13
void setScaleMode(TextureScaleMode mode)
Sets texture scaling mode.
Definition: Texture.cpp:225
static void wipeTextCache()
Similar to wipeEntireCache, but only wipes cached text textures (made by TextElement)
Definition: Texture.cpp:118
bool loadFromSurface(CST_Surface *surface)
Loads the texture from a surface Returns true if successful.
Definition: Texture.cpp:54
bool saveTo(std::string &path)
save this texture to the given file path as a PNG
Definition: Texture.cpp:238
int texW
The size of the texture.
Definition: Texture.hpp:93
static std::unordered_map< std::string, TextureData > texCache
Cache previously displayed textures.
Definition: Texture.hpp:87
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:81
SDL_BlendMode blendMode
Blend mode to use for this texture.
Definition: Texture.hpp:74
CST_Color texFirstPixel
The color of the first pixel.
Definition: Texture.hpp:96
void render(Element *parent)
Renders the texture.
Definition: Texture.cpp:131
void getTextureSize(int *w, int *h)
Return texture's original size.
Definition: Texture.cpp:230
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:90