Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Attributes | Static Protected Attributes | List of all members
Chesto::Texture Class Reference
Inheritance diagram for Chesto::Texture:
Inheritance graph
[legend]
Collaboration diagram for Chesto::Texture:
Collaboration graph
[legend]

Public Member Functions

void clear (void)
 Reinitialize Texture Resets texture content, size and color. More...
 
bool loadFromSurface (CST_Surface *surface)
 Loads the texture from a surface Returns true if successful. More...
 
bool loadFromCache (std::string &key)
 Loads the texture from caches Returns true if successful. More...
 
bool loadFromSurfaceSaveToCache (std::string &key, CST_Surface *surface)
 Loads the texture from a surface and saves the results in caches Returns true if successful. More...
 
void render (Element *parent)
 Renders the texture. More...
 
void resize (int w, int h)
 Resizes the texture. More...
 
void setScaleMode (TextureScaleMode mode)
 Sets texture scaling mode. More...
 
void getTextureSize (int *w, int *h)
 Return texture's original size. More...
 
TexturesetSize (int w, int h)
 
bool saveTo (std::string &path)
 save this texture to the given file path as a PNG More...
 
void loadPath (std::string &path, bool forceReload=false)
 update and load or reload the texture More...
 
- Public Member Functions inherited from Chesto::Element
virtual bool process (InputEvents *event)
 process any input that is received for this element More...
 
virtual void render (Element *parent)
 display the current state of the display More...
 
bool onTouchDown (InputEvents *event)
 
bool onTouchDrag (InputEvents *event)
 
bool onTouchUp (InputEvents *event)
 
void hide ()
 
void unhide ()
 
void renderBackground (bool fill=true)
 
void addNode (std::unique_ptr< Element > node)
 
void remove (Element *element)
 
void removeAll ()
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
float getEffectiveScale () const
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementsetAbsolute (bool isAbs)
 
template<typename T , typename... Args>
T * createNode (Args &&... args)
 
Elementconstrain (int flags, int padding=0)
 
ElementconstrainToTarget (Element *target, int flags, int padding=0)
 
Elementanimate (int durationIn, std::function< void(float)> onStep, std::function< void()> onFinish)
 
ElementmoveToFront ()
 
ElementsetTouchable (bool touchable)
 
void screenshot (std::string path)
 Take a screenshot of this element and its children, and save it to the given path. More...
 

Static Public Member Functions

static void wipeEntireCache ()
 Wipes the entire texture cache (generally, should only be used to reload entire theme / text) More...
 
static void wipeTextCache ()
 Similar to wipeEntireCache, but only wipes cached text textures (made by TextElement) More...
 

Public Attributes

int cornerRadius = 0
 Rounded corner radius (if >0, will round) More...
 
SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND
 Blend mode to use for this texture. More...
 
- Public Attributes inherited from Chesto::Element
std::function< void()> action = NULL
 the action to call (from binded callback) on touch or button selection https://stackoverflow.com/questions/14189440/c-class-member-callback-simple-examples More...
 
std::function< void(InputEvents *event)> actionWithEvents = NULL
 
std::vector< std::unique_ptr< Element, std::function< void(Element *)> > > elements
 visible GUI child elements of this element More...
 
float scale = 1.0f
 
bool touchable = false
 whether or not this element can be touched (highlights bounds) More...
 
bool dragging = false
 whether or not this element is currently being dragged More...
 
bool needsRedraw = false
 whether or not this element needs the screen redrawn next time it's processed More...
 
int futureRedrawCounter = 0
 whether this element needs a redraw for the next X redraws (decreases each time) (0 is no redraws) More...
 
int lastMouseY = 0
 the last Y, X coordinate of the mouse (from a drag probably) More...
 
int lastMouseX = 0
 
bool hasBackground = false
 
rgb backgroundColor = {0, 0, 0}
 
int backgroundOpacity = 0xff
 
bool isAbsolute = false
 
Elementparent = nullptr
 the parent element (reference only, not owned) More...
 
bool hidden = false
 whether this element should skip rendering or not More...
 
bool isProtected = false
 
int elasticCounter = 0
 how much time is left in an elastic-type flick/scroll set by the last distance traveled in a scroll, and counts down every frame More...
 
int width = 0
 width and height of this element (must be manually set, isn't usually calculated (but is in some cases, like text or images)) More...
 
int height = 0
 
int x = 0
 
int y = 0
 
int xAbs = 0
 
int yAbs = 0
 
double angle = 0
 rotation angle in degrees More...
 
int cornerRadius = 0
 
int tag = 0
 
std::vector< std::unique_ptr< Constraint > > constraints
 
std::vector< std::unique_ptr< Animation > > animations
 
bool useColorMask = false
 whether or not to overlay a color mask on top of this element More...
 
CST_Color maskColor = {0,0,0,0}
 The color to overlay on top. More...
 

Static Public Attributes

static const std::string textElemPrefix = "(TextElement):"
 

Protected Attributes

CST_Texture * mTexture = nullptr
 The actual texture. More...
 
int texW = 0
 The size of the texture. More...
 
int texH = 0
 
CST_Color texFirstPixel = {0,0,0,0}
 The color of the first pixel. More...
 
TextureScaleMode texScaleMode = SCALE_STRETCH
 Texture's scaling mode. More...
 

Static Protected Attributes

static std::unordered_map< std::string, TextureDatatexCache
 Cache previously displayed textures. More...
 

Additional Inherited Members

- Public Types inherited from Chesto::Element
typedef Element super
 
- Protected Member Functions inherited from Chesto::Element
void addStackMember (Element *element)
 

Detailed Description

Definition at line 28 of file Texture.hpp.

Constructor & Destructor Documentation

◆ ~Texture()

Chesto::Texture::~Texture ( )
virtual

Definition at line 9 of file Texture.cpp.

10{
11}

Member Function Documentation

◆ clear()

void Chesto::Texture::clear ( void  )

Reinitialize Texture Resets texture content, size and color.

Definition at line 13 of file Texture.cpp.

14{
15 mTexture = nullptr;
16 texW = 0;
17 texH = 0;
18 texFirstPixel = (CST_Color){0,0,0,0};
19}
int texW
The size of the texture.
Definition: Texture.hpp:93
CST_Color texFirstPixel
The color of the first pixel.
Definition: Texture.hpp:96
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:90

References mTexture, texFirstPixel, and texW.

Referenced by Chesto::TextElement::update().

◆ getTextureSize()

void Chesto::Texture::getTextureSize ( int *  w,
int *  h 
)

Return texture's original size.

Definition at line 230 of file Texture.cpp.

231{
232 if (w)
233 *w = texW;
234 if (h)
235 *h = texH;
236}

References texW.

Referenced by Chesto::TextElement::update().

◆ loadFromCache()

bool Chesto::Texture::loadFromCache ( std::string &  key)

Loads the texture from caches Returns true if successful.

Definition at line 81 of file Texture.cpp.

82{
83 // check if the texture is cached
84 if (texCache.count(key))
85 {
86 TextureData *texData = &texCache[key];
87 mTexture = texData->texture;
88 texFirstPixel = texData->firstPixel;
89 CST_QueryTexture(mTexture, &texW, &texH);
90 return true;
91 }
92
93 return false;
94}
static std::unordered_map< std::string, TextureData > texCache
Cache previously displayed textures.
Definition: Texture.hpp:87

References mTexture, texCache, texFirstPixel, and texW.

Referenced by loadPath(), Chesto::NetImageElement::NetImageElement(), and Chesto::TextElement::update().

◆ loadFromSurface()

bool Chesto::Texture::loadFromSurface ( CST_Surface *  surface)

Loads the texture from a surface Returns true if successful.

Definition at line 54 of file Texture.cpp.

55{
56 if (!surface)
57 return false;
58
59 // will default MainDisplay's renderer if we don't have one in this->renderer
60 CST_Renderer* renderer = getRenderer();
61
62 // try to create a texture from the surface
63 CST_Texture *texture = CST_CreateTextureFromSurface(renderer, surface, true);
64 SDL_SetTextureBlendMode(texture, blendMode);
65 if (!texture)
66 return false;
67
68 // load first pixel color
69 auto pixelcolor = getpixel(surface, 0, 0);
70 CST_GetRGBA(pixelcolor, surface->format, &texFirstPixel);
71
72 // load texture size
73 CST_QueryTexture(texture, &texW, &texH);
74
75 // load texture
76 mTexture = texture;
77
78 return true;
79}
SDL_BlendMode blendMode
Blend mode to use for this texture.
Definition: Texture.hpp:74

References blendMode, mTexture, texFirstPixel, and texW.

Referenced by loadFromSurfaceSaveToCache().

◆ loadFromSurfaceSaveToCache()

bool Chesto::Texture::loadFromSurfaceSaveToCache ( std::string &  key,
CST_Surface *  surface 
)

Loads the texture from a surface and saves the results in caches Returns true if successful.

Definition at line 96 of file Texture.cpp.

97{
98 bool success = loadFromSurface(surface);
99
100 // only save to caches if loading was successful
101 // and the texture isn't already cached
102 if (success && !texCache.count(key))
103 {
104 TextureData texData;
105 texData.texture = mTexture;
106 texData.firstPixel = texFirstPixel;
107 texCache[key] = texData;
108 }
109
110 return success;
111}
bool loadFromSurface(CST_Surface *surface)
Loads the texture from a surface Returns true if successful.
Definition: Texture.cpp:54

References loadFromSurface(), mTexture, texCache, and texFirstPixel.

Referenced by loadPath(), and Chesto::TextElement::update().

◆ loadPath()

void Chesto::Texture::loadPath ( std::string &  path,
bool  forceReload = false 
)

update and load or reload the texture

Definition at line 262 of file Texture.cpp.

262 {
263 // Guard against empty paths
264 if (path.empty()) {
265 width = 0;
266 height = 0;
267 return;
268 }
269
270 if (forceReload || !loadFromCache(path))
271 {
272 CST_Surface *surface = IMG_Load(path.c_str());
273 loadFromSurfaceSaveToCache(path, surface);
274 CST_FreeSurface(surface);
275 }
276
277 width = texW;
278 height = texH;
279}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:132
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
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:81

References loadFromCache(), loadFromSurfaceSaveToCache(), texW, and Chesto::Element::width.

Referenced by Chesto::ImageElement::ImageElement().

◆ render()

void Chesto::Texture::render ( Element parent)
virtual

Renders the texture.

Reimplemented from Chesto::Element.

Definition at line 131 of file Texture.cpp.

132{
133 // update xAbs and yAbs
135
136 if (!mTexture)
137 return;
138
139 if (hidden)
140 return;
141
142 float effectiveScale = getEffectiveScale();
143
144 // rect of element's size (scaled)
145 CST_Rect rect;
146 rect.x = this->xAbs;
147 rect.y = this->yAbs;
148 rect.w = (int)(this->width * effectiveScale);
149 rect.h = (int)(this->height * effectiveScale);
150
151 if (CST_isRectOffscreen(&rect))
152 return;
153
154 CST_Renderer* renderer = getRenderer();
155
156 if (texScaleMode == SCALE_PROPORTIONAL_WITH_BG || texScaleMode == SCALE_PROPORTIONAL_NO_BG)
157 {
158 CST_SetDrawBlend(RootDisplay::renderer, false);
159
160 // draw colored background
161 if (texScaleMode == SCALE_PROPORTIONAL_WITH_BG)
162 {
163 CST_SetDrawColor(renderer, texFirstPixel);
164 auto color = (CST_Color){ texFirstPixel.r, texFirstPixel.g, texFirstPixel.b, 0xFF };
165
166 // if the first pixel is transparent, use the background color
167 if (texFirstPixel.a == 0) {
168 auto r = (Uint8)(backgroundColor.r * 0xff);
169 auto g = (Uint8)(backgroundColor.g * 0xff);
170 auto b = (Uint8)(backgroundColor.b * 0xff);
171 color = (CST_Color){ r, g, b, 0xFF };
172 }
173
174 CST_roundedBoxRGBA(renderer, rect.x, rect.y, rect.x + rect.w, rect.y + rect.h, cornerRadius,
175 color.r, color.g, color.b, 0xFF);
176 }
177
178 // recompute drawing rect
179 if ((width * texH) > (height * texW))
180 {
181 // keep height, scale width
182 rect.h = height;
183 rect.w = (texW * rect.h) / texH;
184 }
185 else
186 {
187 // keep width, scale height
188 rect.w = width;
189 rect.h = (texH * rect.w) / texW;
190 }
191
192 // center the texture
193 rect.x += (width - rect.w) / 2;
194 rect.y += (height - rect.h) / 2;
195 }
196
197 if (angle != 0) {
198 // render the texture with a rotation
199 CST_SetQualityHint("best");
200 CST_RenderCopyRotate(renderer, mTexture, NULL, &rect, this->angle);
201 }
202 else if (useColorMask) {
203 // render the texture with a mask color (only can darken the texture)
204 SDL_SetTextureColorMod(mTexture, maskColor.r, maskColor.g, maskColor.b);
205 CST_RenderCopy(renderer, mTexture, NULL, &rect);
206 SDL_SetTextureColorMod(mTexture, 0xFF, 0xFF, 0xFF);
207 } else {
208 // render the texture normally
209 CST_RenderCopy(renderer, mTexture, NULL, &rect);
210 }
211}
double angle
rotation angle in degrees
Definition: Element.hpp:144
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:89
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:119
CST_Color maskColor
The color to overlay on top.
Definition: Element.hpp:193
Element * parent
the parent element (reference only, not owned)
Definition: Element.hpp:116
bool useColorMask
whether or not to overlay a color mask on top of this element
Definition: Element.hpp:190
int cornerRadius
Rounded corner radius (if >0, will round)
Definition: Texture.hpp:71
TextureScaleMode texScaleMode
Texture's scaling mode.
Definition: Texture.hpp:99

References Chesto::Element::angle, cornerRadius, Chesto::Element::hidden, Chesto::Element::maskColor, mTexture, Chesto::Element::parent, Chesto::Element::render(), texFirstPixel, texScaleMode, texW, Chesto::Element::useColorMask, and Chesto::Element::width.

Referenced by Chesto::NetImageElement::render().

◆ resize()

void Chesto::Texture::resize ( int  w,
int  h 
)

Resizes the texture.

Definition at line 213 of file Texture.cpp.

214{
215 width = w;
216 height = h;
217}

References Chesto::Element::width.

◆ saveTo()

bool Chesto::Texture::saveTo ( std::string &  path)

save this texture to the given file path as a PNG

Definition at line 238 of file Texture.cpp.

239{
240 if (!mTexture)
241 return false;
242
243 // render the texture to one that can be saved (TARGET ACCESS)
244 CST_Texture* target = SDL_CreateTexture(getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, texW, texH);
245 if (!target)
246 return false;
247
248 // set the target texture
249 SDL_SetRenderTarget(getRenderer(), target);
250
251 // render the texture
252 SDL_RenderCopy(getRenderer(), mTexture, NULL, NULL);
253
254 // reset the target texture
255 SDL_SetRenderTarget(getRenderer(), NULL);
256
257
258 // save the surface to the path
259 return CST_SavePNG(target, path.c_str());
260}

References mTexture, and texW.

◆ setScaleMode()

void Chesto::Texture::setScaleMode ( TextureScaleMode  mode)

Sets texture scaling mode.

Definition at line 225 of file Texture.cpp.

226{
227 texScaleMode = mode;
228}

References texScaleMode.

◆ setSize()

Texture * Chesto::Texture::setSize ( int  w,
int  h 
)

Definition at line 219 of file Texture.cpp.

220{
221 this->resize(w, h);
222 return this;
223}
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:213

◆ wipeEntireCache()

void Chesto::Texture::wipeEntireCache ( )
static

Wipes the entire texture cache (generally, should only be used to reload entire theme / text)

Definition at line 113 of file Texture.cpp.

114{
115 texCache.clear();
116}

References texCache.

◆ wipeTextCache()

void Chesto::Texture::wipeTextCache ( )
static

Similar to wipeEntireCache, but only wipes cached text textures (made by TextElement)

Definition at line 118 of file Texture.cpp.

119{
120 for (auto it = texCache.begin(); it != texCache.end(); )
121 {
122 std::string key = it->first;
123 if (key.find(Texture::textElemPrefix) == 0) { // does the key start with our text prefix?
124 it = texCache.erase(it);
125 } else {
126 ++it;
127 }
128 }
129}

References texCache.

Member Data Documentation

◆ blendMode

SDL_BlendMode Chesto::Texture::blendMode = SDL_BLENDMODE_BLEND

Blend mode to use for this texture.

Definition at line 74 of file Texture.hpp.

Referenced by loadFromSurface().

◆ cornerRadius

int Chesto::Texture::cornerRadius = 0

Rounded corner radius (if >0, will round)

Definition at line 71 of file Texture.hpp.

Referenced by render().

◆ mTexture

CST_Texture* Chesto::Texture::mTexture = nullptr
protected

◆ texCache

std::unordered_map< std::string, TextureData > Chesto::Texture::texCache
staticprotected

Cache previously displayed textures.

Definition at line 87 of file Texture.hpp.

Referenced by loadFromCache(), loadFromSurfaceSaveToCache(), wipeEntireCache(), and wipeTextCache().

◆ texFirstPixel

CST_Color Chesto::Texture::texFirstPixel = {0,0,0,0}
protected

The color of the first pixel.

Definition at line 96 of file Texture.hpp.

Referenced by clear(), loadFromCache(), loadFromSurface(), loadFromSurfaceSaveToCache(), and render().

◆ texH

int Chesto::Texture::texH = 0
protected

Definition at line 93 of file Texture.hpp.

◆ texScaleMode

TextureScaleMode Chesto::Texture::texScaleMode = SCALE_STRETCH
protected

Texture's scaling mode.

Definition at line 99 of file Texture.hpp.

Referenced by render(), and setScaleMode().

◆ textElemPrefix

const std::string Chesto::Texture::textElemPrefix = "(TextElement):"
static

Definition at line 77 of file Texture.hpp.

◆ texW

int Chesto::Texture::texW = 0
protected

The documentation for this class was generated from the following files: