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...
 
- 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
 
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:79
int texW
The size of the texture.
Definition: Texture.hpp:76
CST_Texture * mTexture
The actual texture.
Definition: Texture.hpp:73

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

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

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

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

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 if (!texture)
61 return false;
62
63 // load first pixel color
64 auto pixelcolor = getpixel(surface, 0, 0);
65 CST_GetRGBA(pixelcolor, surface->format, &texFirstPixel);
66
67 // load texture size
68 CST_QueryTexture(texture, &texW, &texH);
69
70 // load texture
71 mTexture = texture;
72
73 return true;
74}

References 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 91 of file Texture.cpp.

92{
93 bool success = loadFromSurface(surface);
94
95 // only save to caches if loading was successful
96 // and the texture isn't already cached
97 if (success && !texCache.count(key))
98 {
99 TextureData texData;
100 texData.texture = mTexture;
101 texData.firstPixel = texFirstPixel;
102 texCache[key] = texData;
103 }
104
105 return success;
106}
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 230 of file Texture.cpp.

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

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

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

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

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

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

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

References mTexture, and texW.

◆ setScaleMode()

void Texture::setScaleMode ( TextureScaleMode  mode)

Sets texture scaling mode.

Definition at line 193 of file Texture.cpp.

194{
195 texScaleMode = mode;
196}

References texScaleMode.

◆ setSize()

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

Definition at line 187 of file Texture.cpp.

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

Member Data Documentation

◆ 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 73 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 70 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 79 of file Texture.hpp.

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

◆ texH

int Texture::texH = 0
protected

Definition at line 76 of file Texture.hpp.

◆ texScaleMode

TextureScaleMode Texture::texScaleMode = SCALE_STRETCH
protected

Texture's scaling mode.

Definition at line 82 of file Texture.hpp.

Referenced by render(), and setScaleMode().

◆ texW

int Texture::texW = 0
protected

The size of the texture.

Definition at line 76 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: