18Uint32 getpixel(CST_Surface *surface,
int x,
int y)
20 int bpp = surface->format->BytesPerPixel;
22 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
35 if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
36 return p[0] << 16 | p[1] << 8 | p[2];
38 return p[0] | p[1] << 8 | p[2] << 16;
56 CST_Renderer* renderer = getRenderer();
59 CST_Texture *texture = CST_CreateTextureFromSurface(renderer, surface,
true);
64 auto pixelcolor = getpixel(surface, 0, 0);
68 CST_QueryTexture(texture, &
texW, &texH);
123 rect.w = this->
width;
124 rect.h = this->height;
126 if (CST_isRectOffscreen(&rect))
129 CST_Renderer* renderer = getRenderer();
133 CST_SetDrawBlend(RootDisplay::renderer,
false);
141 color = (CST_Color){0xFF,0xFF,0xFF,0xFF};
143 CST_SetDrawColor(renderer, color);
144 CST_FillRect(renderer, &rect);
151 rect.w = (
texW * rect.h) / texH;
157 rect.h = (texH * rect.w) /
texW;
161 rect.x += (
width - rect.w) / 2;
162 rect.y += (height - rect.h) / 2;
167 CST_SetQualityHint(
"best");
168 CST_RenderCopyRotate(renderer,
mTexture, NULL, &rect, this->
angle);
173 CST_RenderCopy(renderer,
mTexture, NULL, &rect);
174 SDL_SetTextureColorMod(
mTexture, 0xFF, 0xFF, 0xFF);
177 CST_RenderCopy(renderer,
mTexture, NULL, &rect);
187Texture* Texture::setSize(
int w,
int h)
212 CST_Texture* target = SDL_CreateTexture(getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET,
texW, texH);
217 SDL_SetRenderTarget(getRenderer(), target);
220 SDL_RenderCopy(getRenderer(),
mTexture, NULL, NULL);
223 SDL_SetRenderTarget(getRenderer(), NULL);
227 return CST_SavePNG(target, path.c_str());
233 CST_Surface *surface = IMG_Load(path.c_str());
235 CST_FreeSurface(surface);
bool hidden
whether this element should skip rendering or not
virtual void render(Element *parent)
display the current state of the display
bool useColorMask
whether or not to overlay a color mask on top of this element
double angle
rotation angle in degrees
CST_Color maskColor
The color to overlay on top.
int width
width and height of this element (must be manually set, isn't usually calculated (but is in some case...
Element * parent
the parent element (can sometimes be null if it isn't set)
void getTextureSize(int *w, int *h)
Return texture's original size.
bool loadFromSurfaceSaveToCache(std::string &key, CST_Surface *surface)
Loads the texture from a surface and saves the results in caches Returns true if successful.
static std::unordered_map< std::string, TextureData > texCache
Cache previously displayed textures.
CST_Color texFirstPixel
The color of the first pixel.
int texW
The size of the texture.
void resize(int w, int h)
Resizes the texture.
bool loadFromCache(std::string &key)
Loads the texture from caches Returns true if successful.
void clear(void)
Reinitialize Texture Resets texture content, size and color.
void render(Element *parent)
Renders the texture.
bool saveTo(std::string &path)
save this texture to the given file path as a PNG
void setScaleMode(TextureScaleMode mode)
Sets texture scaling mode.
bool loadFromSurface(CST_Surface *surface)
Loads the texture from a surface Returns true if successful.
TextureScaleMode texScaleMode
Texture's scaling mode.
void loadPath(std::string &path, bool forceReload=false)
update and load or reload the texture
CST_Texture * mTexture
The actual texture.