Chesto 0.9
A declarative and element-based library for creating GUIs on homebrew'd consoles
Public Member Functions | Public Attributes | Protected Attributes | Static Protected Attributes | List of all members
Texture Class Reference
Inheritance diagram for Texture:
Inheritance graph
[legend]
Collaboration diagram for 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 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 append (Element *element)
 
void remove (Element *element)
 
void removeAll (bool moveToTrash=false)
 
void position (int x, int y)
 position the element More...
 
void recalcPosition (Element *parent)
 
CST_Rect getBounds ()
 
CST_Renderer * getRenderer ()
 
void wipeAll (bool delSelf=false)
 
Elementchild (Element *child)
 
ElementsetPosition (int x, int y)
 
ElementsetAction (std::function< void()> func)
 
ElementcenterHorizontallyIn (Element *parent)
 
ElementcenterVerticallyIn (Element *parent)
 
ElementcenterIn (Element *parent)
 
ElementsetAbsolute (bool isAbs)
 
Elementconstrain (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...
 

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 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< 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}
 
bool isAbsolute = false
 
Elementparent = NULL
 the parent element (can sometimes be null if it isn't set) 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 xOff = 0
 
int yOff = 0
 
int cornerRadius = 0
 
std::vector< Constraint * > constraints
 
std::vector< 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...
 

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 Element
typedef Element super
 

Detailed Description

Definition at line 23 of file Texture.hpp.

Constructor & Destructor Documentation

◆ ~Texture()

Texture::~Texture ( )
virtual

Definition at line 5 of file Texture.cpp.

6{
7}

Member Function Documentation

◆ clear()

void Texture::clear ( void  )

Reinitialize Texture Resets texture content, size and color.

Definition at line 9 of file Texture.cpp.

10{
11 mTexture = nullptr;
12 texW = 0;
13 texH = 0;
14 texFirstPixel = (CST_Color){0,0,0,0};
15}
CST_Color texFirstPixel
The color of the first pixel.
Definition: Texture.hpp:82
int texW
The size of the texture.
Definition: Texture.hpp:79
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:76

References mTexture, texFirstPixel, and texW.

Referenced by TextElement::update().

◆ getTextureSize()

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

Return texture's original size.

Definition at line 199 of file Texture.cpp.

200{
201 if (w)
202 *w = texW;
203 if (h)
204 *h = texH;
205}

References texW.

Referenced by TextElement::update().

◆ loadFromCache()

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

Loads the texture from caches Returns true if successful.

Definition at line 77 of file Texture.cpp.

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

References mTexture, texCache, texFirstPixel, and texW.

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

◆ loadFromSurface()

bool Texture::loadFromSurface ( CST_Surface *  surface)

Loads the texture from a surface Returns true if successful.

Definition at line 50 of file Texture.cpp.

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

References blendMode, mTexture, texFirstPixel, and texW.

Referenced by loadFromSurfaceSaveToCache().

◆ loadFromSurfaceSaveToCache()

bool 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 92 of file Texture.cpp.

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

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

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

◆ loadPath()

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

update and load or reload the texture

Definition at line 231 of file Texture.cpp.

231 {
232 if (forceReload || !loadFromCache(path))
233 {
234 CST_Surface *surface = IMG_Load(path.c_str());
235 loadFromSurfaceSaveToCache(path, surface);
236 CST_FreeSurface(surface);
237 }
238
239 width = texW;
240 height = texH;
241}
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Definition: Element.hpp:120
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
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
Definition: Texture.cpp:77

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

Referenced by ImageElement::ImageElement(), and Button::process().

◆ render()

void Texture::render ( Element parent)
virtual

Renders the texture.

Reimplemented from Element.

Definition at line 109 of file Texture.cpp.

110{
111 // update xAbs and yAbs
113
114 if (!mTexture)
115 return;
116
117 if (hidden)
118 return;
119
120 // rect of element's size
121 CST_Rect rect;
122 rect.x = this->xAbs;
123 rect.y = this->yAbs;
124 rect.w = this->width;
125 rect.h = this->height;
126
127 if (CST_isRectOffscreen(&rect))
128 return;
129
130 CST_Renderer* renderer = getRenderer();
131
132 if (texScaleMode == SCALE_PROPORTIONAL_WITH_BG)
133 {
134 CST_SetDrawBlend(RootDisplay::renderer, false);
135
136 // draw colored background
137 CST_SetDrawColor(renderer, texFirstPixel);
138 auto color = (CST_Color){texFirstPixel.r, texFirstPixel.g, texFirstPixel.b, 0xFF};
139
140 // if the first pixel is transparent, use white
141 if (texFirstPixel.a == 0)
142 color = (CST_Color){0xFF,0xFF,0xFF,0xFF};
143
144 CST_SetDrawColor(renderer, color);
145 CST_FillRect(renderer, &rect);
146
147 // recompute drawing rect
148 if ((width * texH) > (height * texW))
149 {
150 // keep height, scale width
151 rect.h = height;
152 rect.w = (texW * rect.h) / texH;
153 }
154 else
155 {
156 // keep width, scale height
157 rect.w = width;
158 rect.h = (texH * rect.w) / texW;
159 }
160
161 // center the texture
162 rect.x += (width - rect.w) / 2;
163 rect.y += (height - rect.h) / 2;
164 }
165
166 if (angle != 0) {
167 // render the texture with a rotation
168 CST_SetQualityHint("best");
169 CST_RenderCopyRotate(renderer, mTexture, NULL, &rect, this->angle);
170 }
171 else if (useColorMask) {
172 // render the texture with a mask color (only can darken the texture)
173 SDL_SetTextureColorMod(mTexture, maskColor.r, maskColor.g, maskColor.b);
174 CST_RenderCopy(renderer, mTexture, NULL, &rect);
175 SDL_SetTextureColorMod(mTexture, 0xFF, 0xFF, 0xFF);
176 } else {
177 // render the texture normally
178 CST_RenderCopy(renderer, mTexture, NULL, &rect);
179 }
180}
bool hidden
whether this element should skip rendering or not
Definition: Element.hpp:107
virtual void render(Element *parent)
display the current state of the display
Definition: Element.cpp:60
bool useColorMask
whether or not to overlay a color mask on top of this element
Definition: Element.hpp:177
double angle
rotation angle in degrees
Definition: Element.hpp:130
CST_Color maskColor
The color to overlay on top.
Definition: Element.hpp:180
Element * parent
the parent element (can sometimes be null if it isn't set)
Definition: Element.hpp:104
TextureScaleMode texScaleMode
Texture's scaling mode.
Definition: Texture.hpp:85

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

Referenced by NetImageElement::render().

◆ resize()

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

Resizes the texture.

Definition at line 182 of file Texture.cpp.

183{
184 width = w;
185 height = h;
186}

References Element::width.

Referenced by Button::process().

◆ saveTo()

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

save this texture to the given file path as a PNG

Definition at line 207 of file Texture.cpp.

208{
209 if (!mTexture)
210 return false;
211
212 // render the texture to one that can be saved (TARGET ACCESS)
213 CST_Texture* target = SDL_CreateTexture(getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, texW, texH);
214 if (!target)
215 return false;
216
217 // set the target texture
218 SDL_SetRenderTarget(getRenderer(), target);
219
220 // render the texture
221 SDL_RenderCopy(getRenderer(), mTexture, NULL, NULL);
222
223 // reset the target texture
224 SDL_SetRenderTarget(getRenderer(), NULL);
225
226
227 // save the surface to the path
228 return CST_SavePNG(target, path.c_str());
229}

References mTexture, and texW.

◆ setScaleMode()

void Texture::setScaleMode ( TextureScaleMode  mode)

Sets texture scaling mode.

Definition at line 194 of file Texture.cpp.

195{
196 texScaleMode = mode;
197}

References texScaleMode.

◆ setSize()

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

Definition at line 188 of file Texture.cpp.

189{
190 this->resize(w, h);
191 return this;
192}
void resize(int w, int h)
Resizes the texture.
Definition: Texture.cpp:182

Member Data Documentation

◆ blendMode

SDL_BlendMode Texture::blendMode = SDL_BLENDMODE_BLEND

Blend mode to use for this texture.

Definition at line 69 of file Texture.hpp.

Referenced by loadFromSurface().

◆ cornerRadius

int Texture::cornerRadius = 0

Rounded corner radius (if >0, will round)

Definition at line 66 of file Texture.hpp.

◆ mTexture

CST_Texture* Texture::mTexture = nullptr
protected

The actual texture.

Definition at line 76 of file Texture.hpp.

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

◆ texCache

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

Cache previously displayed textures.

Definition at line 73 of file Texture.hpp.

Referenced by loadFromCache(), and loadFromSurfaceSaveToCache().

◆ texFirstPixel

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

The color of the first pixel.

Definition at line 82 of file Texture.hpp.

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

◆ texH

int Texture::texH = 0
protected

Definition at line 79 of file Texture.hpp.

◆ texScaleMode

TextureScaleMode Texture::texScaleMode = SCALE_STRETCH
protected

Texture's scaling mode.

Definition at line 85 of file Texture.hpp.

Referenced by render(), and setScaleMode().

◆ texW

int Texture::texW = 0
protected

The size of the texture.

Definition at line 79 of file Texture.hpp.

Referenced by clear(), getTextureSize(), loadFromCache(), loadFromSurface(), loadPath(), NetImageElement::NetImageElement(), render(), and saveTo().


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