7const std::string Texture::textElemPrefix =
"(TextElement):";
22Uint32 getpixel(CST_Surface *surface,
int x,
int y)
24 int bpp = surface->format->BytesPerPixel;
26 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
39 if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
40 return p[0] << 16 | p[1] << 8 | p[2];
42 return p[0] | p[1] << 8 | p[2] << 16;
60 CST_Renderer* renderer = getRenderer();
63 CST_Texture *texture = CST_CreateTextureFromSurface(renderer, surface,
true);
64 SDL_SetTextureBlendMode(texture,
blendMode);
69 auto pixelcolor = getpixel(surface, 0, 0);
73 CST_QueryTexture(texture, &
texW, &texH);
102 if (success && !
texCache.count(key))
122 std::string key = it->first;
123 if (key.find(Texture::textElemPrefix) == 0) {
142 float effectiveScale = getEffectiveScale();
148 rect.w = (int)(this->
width * effectiveScale);
149 rect.h = (int)(this->height * effectiveScale);
151 if (CST_isRectOffscreen(&rect))
154 CST_Renderer* renderer = getRenderer();
158 CST_SetDrawBlend(RootDisplay::renderer,
false);
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 };
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);
183 rect.w = (
texW * rect.h) / texH;
189 rect.h = (texH * rect.w) /
texW;
193 rect.x += (
width - rect.w) / 2;
194 rect.y += (height - rect.h) / 2;
199 CST_SetQualityHint(
"best");
200 CST_RenderCopyRotate(renderer,
mTexture, NULL, &rect, this->
angle);
205 CST_RenderCopy(renderer,
mTexture, NULL, &rect);
206 SDL_SetTextureColorMod(
mTexture, 0xFF, 0xFF, 0xFF);
209 CST_RenderCopy(renderer,
mTexture, NULL, &rect);
219Texture* Texture::setSize(
int w,
int h)
244 CST_Texture* target = SDL_CreateTexture(getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
texW, texH);
249 SDL_SetRenderTarget(getRenderer(), target);
252 SDL_RenderCopy(getRenderer(),
mTexture, NULL, NULL);
255 SDL_SetRenderTarget(getRenderer(), NULL);
259 return CST_SavePNG(target, path.c_str());
272 CST_Surface *surface = IMG_Load(path.c_str());
274 CST_FreeSurface(surface);
double angle
rotation angle in degrees
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
virtual void render(Element *parent)
display the current state of the display
bool hidden
whether this element should skip rendering or not
CST_Color maskColor
The color to overlay on top.
Element * parent
the parent element (reference only, not owned)
bool useColorMask
whether or not to overlay a color mask on top of this element
int cornerRadius
Rounded corner radius (if >0, will round)
static void wipeEntireCache()
Wipes the entire texture cache (generally, should only be used to reload entire theme / text)
TextureScaleMode texScaleMode
Texture's scaling mode.
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
void resize(int w, int h)
Resizes the texture.
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
void clear(void)
Reinitialize Texture Resets texture content, size and color.
void setScaleMode(TextureScaleMode mode)
Sets texture scaling mode.
static void wipeTextCache()
Similar to wipeEntireCache, but only wipes cached text textures (made by TextElement)
bool loadFromSurface(CST_Surface *surface)
Loads the texture from a surface Returns true if successful.
bool saveTo(std::string &path)
save this texture to the given file path as a PNG
int texW
The size of the texture.
static std::unordered_map< std::string, TextureData > texCache
Cache previously displayed textures.
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
SDL_BlendMode blendMode
Blend mode to use for this texture.
CST_Color texFirstPixel
The color of the first pixel.
void render(Element *parent)
Renders the texture.
void getTextureSize(int *w, int *h)
Return texture's original size.
CST_Texture * mTexture
The actual texture.