TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
TurtleBrainsExpress::Graphics::WebSprite Class Reference

#include <tbx_web_sprite.hpp>

Inheritance diagram for TurtleBrainsExpress::Graphics::WebSprite:
TurtleBrains::Graphics::Graphic

Public Member Functions

 WebSprite (const tbCore::tbString &spriteURL, const tbGraphics::PixelSpace width, const tbGraphics::PixelSpace height, const tbCore::tbString &cacheID="", const bool alwaysFetch=false)
 
virtual ~WebSprite (void)
 
void SetColorUntilLoaded (const tbGraphics::Color &color)
 
void SetOnLoaded (std::function< void(void)> callback)
 
virtual tbGraphics::PixelSpace GetPixelWidth (void) const override
 
virtual tbGraphics::PixelSpace GetPixelHeight (void) const override
 
- Public Member Functions inherited from TurtleBrains::Graphics::Graphic
 Graphic (void)
 
 Graphic (const Graphic &other)
 
virtual ~Graphic (void)
 
bool IsActive (void) const
 
void SetActive (const bool isActive)
 
void Update (const float deltaTime)
 
bool IsVisible (void) const
 
void SetVisible (const bool isVisible)
 
void Render (void) const
 
const tbMath::Vector2GetPosition (void) const
 
void SetPosition (const float x, const float y)
 
void SetPosition (const tbMath::Vector2 &position)
 
const tbMath::Vector2GetOrigin (void) const
 
void SetOrigin (const AnchorLocation &anchor, const tbMath::Vector2 &offset=tbMath::Vector2::Zero())
 
void SetOrigin (const float originX, const float originY)
 
void SetOrigin (const tbMath::Vector2 &origin)
 
float GetDepth (void) const
 
void SetDepth (const float newDepth)
 
const tbMath::Vector2GetScale (void) const
 
void SetScale (const float scale)
 
void SetScale (const tbMath::Vector2 &scale)
 
void SetScale (const float scaleX, const float scaleY)
 
float GetRotation (void) const
 
void SetRotation (const float rotation)
 
void SetScroll (const float scroll)
 
void SetScroll (const float scrollX, const float scrollY)
 
bool IsRelative (void) const
 
const ColorGetColor (void) const
 
virtual void SetColor (const tbGraphics::Color &newColor)
 
float GetWidth (void) const
 
float GetHeight (void) const
 
float GetScaledWidth (void) const
 
float GetScaledHeight (void) const
 
bool UnstableIsPointContained (const tbMath::Vector2 &point) const
 

Static Public Member Functions

static void SetCacheDirectory (const tbCore::tbString &filepath)
 

Protected Member Functions

virtual void OnRender (void) const override
 
- Protected Member Functions inherited from TurtleBrains::Graphics::Graphic
virtual void OnUpdate (const float deltaTime)
 
virtual void ApplyTransform (void) const
 
virtual void PopTransform (void) const
 

Detailed Description

The WebSprite will use TurtleBrains::Network to grab a texture from the web and then display it with an optional default texture to use until it is replaced.

Constructor & Destructor Documentation

TurtleBrainsExpress::Graphics::WebSprite::WebSprite ( const tbCore::tbString spriteURL,
const tbGraphics::PixelSpace  width,
const tbGraphics::PixelSpace  height,
const tbCore::tbString cacheID = "",
const bool  alwaysFetch = false 
)
explicit

Creates a WebSprite image/object by loading from the url. This is done asynchronously and will not be done at time of construction unless using a cacheID that already exists. The size must be specified to know what size is expected. This will create a white box where the image will be placed until it finishes loading. Use SetVisible() or SetColorUntilLoaded() to change that behavior. Once loaded the image will stretch to fit the specified size even if different aspect ratio.

Parameters
spriteURLThe location of the image file. See the TextureManager for a list of supported formats.
widthThe width the image will display at, will stretch to fit.
heightThe height the image will display at, will stretch to fit.
cacheIDCan be empty to never cache the image, always requesting to load it. If used the cacheIDC:.txt must be a unique identifier for the image resource, and is only cached if SetCacheDirectory() was set.
alwaysFetchIf using a cacheID and the image was loaded from cache, this can allow the WebSprite to still make a request anyway; remember the caching system is not smart like a browser.
virtual TurtleBrainsExpress::Graphics::WebSprite::~WebSprite ( void  )
virtual

Destroys all resources used for the WebSprite.

Member Function Documentation

virtual tbGraphics::PixelSpace TurtleBrainsExpress::Graphics::WebSprite::GetPixelHeight ( void  ) const
overridevirtual

Reimplemented from TurtleBrains::Graphics::Graphic.

virtual tbGraphics::PixelSpace TurtleBrainsExpress::Graphics::WebSprite::GetPixelWidth ( void  ) const
overridevirtual

Reimplemented from TurtleBrains::Graphics::Graphic.

virtual void TurtleBrainsExpress::Graphics::WebSprite::OnRender ( void  ) const
overrideprotectedvirtual

This must be overridden by a subclass to actually display the Graphic on the screen. The function will be invoked when Render() is called and the object IsVisible().

Note
OnRender() should not be called directly, even from a subclass. Use Render() which will first check if the Graphic IsVisible() before displaying with OnRender().

Implements TurtleBrains::Graphics::Graphic.

static void TurtleBrainsExpress::Graphics::WebSprite::SetCacheDirectory ( const tbCore::tbString filepath)
static

Set a directory for caching all sprites/images loaded from the web. This cache does not function the same as a browser-cache might instead if it has grabbed the image before and saved in the cache that cached image will be used directly. This cache directory and the cacheID of the image must be set in order for the image to be cached. Each cacheid must be unique, else images may conflict.

Note
By default the cache directory is unset and there will be no images cached, all requests will be made, even if a cacheID is specified.
void TurtleBrainsExpress::Graphics::WebSprite::SetColorUntilLoaded ( const tbGraphics::Color color)

While waiting for the image request, the white box can be changed to a different color which will reset once the image is finished loading. This may not be a perfect representation, but allows it to start a little closer.

Parameters
colorThe color the box should be displayed until loaded.
void TurtleBrainsExpress::Graphics::WebSprite::SetOnLoaded ( std::function< void(void)>  callback)

Set a function callback to be notified when the image finishes loading. This will be called immediately if the WebSprite loaded from cache, even if it is still making another request.

Parameters
callbackThe function to notify.