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
69 SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
70
71protected:
73 static std::unordered_map<std::string, TextureData> texCache;
74
76 CST_Texture* mTexture = nullptr;
77
79 int texW = 0, texH = 0;
80
82 CST_Color texFirstPixel = {0,0,0,0};
83
85 TextureScaleMode texScaleMode = SCALE_STRETCH;
86};
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:107
void getTextureSize(int *w, int *h)
Return texture's original size.
Definition: Texture.cpp:199
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:92
static std::unordered_map< std::string, TextureData > texCache
Cache previously displayed textures.
Definition: Texture.hpp:73
SDL_BlendMode blendMode
Blend mode to use for this texture.
Definition: Texture.hpp:69
CST_Color texFirstPixel
The color of the first pixel.
Definition: Texture.hpp:82
int cornerRadius
Rounded corner radius (if >0, will round)
Definition: Texture.hpp:66
int texW
The size of the texture.
Definition: Texture.hpp:79
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:182
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:77
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:109
bool saveTo(std::string &path)
save this texture to the given file path as a PNG
Definition: Texture.cpp:207
void setScaleMode(TextureScaleMode mode)
Sets texture scaling mode.
Definition: Texture.cpp:194
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:85
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
Definition: Texture.cpp:231
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:76