TextureScale
Introduction
The TextureScale property can be used to size a Sprite according to its Texture and texture coordiantes. If a Sprite which is displaying its full texture is using a TextureScale of 1, then the Sprite's width will match its Texture's width, and the Sprite's height will match the Texture's Height. A TextureScale of 1 is the most common value, and this is the default for Sprites created in a 2D Entity in the FlatRedBall Editor. TextureScale works by changing a Sprite's Width and Height properties according to a Sprite's Texture and texture coordinate values. Therefore, whenever a Sprite's Texture or texture coordinates change, the Sprite's Width or Height may change if the Sprite is using a TextureScale. If TextureScale of 0, the Sprite will ignore this value and use its existing Width and Height values instead.
When does TextureScale change Width and Height values values?
The TextureScale value changes the Width and Height values according to the Texture size. The Width and Height of a Sprite will change when the following occur (assuming TextureScale greater than 0):
Setting TextureScale
Setting Texture
When an animation frame changes (if the Sprite is using AnimationChains)
TextureScale can overwrite Scale/Width/Height values
Whenever TextureScale is not 0, it will overwrite Width/Height values when any of the values mentioned above change. For example:
Code Example
The following example creates two Sprites. Their TextureScale is set to the same value. Therefore, their sizes relative to each other is the same as the sizes of their Textures relative to each other.
TextureScale and Power of Two
Some older video cards cards expect that all textures have a power of two width and height. In some cases, textures may even be re-sized to fit this requirement. For example, if the current graphics card requires a power of two, an image that is 100X300 may be re-sized to a texture that is 128X512 when loaded in the game. The resizing always increases the resolution of images. This can cause problems if your code uses TextureScale for Sprites which are displaying textures which are not a power of two. The reason is because you may see different behavior on different machines. Using the example above, if you were to load the 100X300 image and display it with a Sprite using a TextureScale of 1, then you would see the following results: On a machine that doesn't resize:
On a machine that does resize:
The solution to this problem is to make sure all of your images have power of two width and height.
Animations "resetting" Scale
If you are using an Animation on your Sprite, and you are modifying the scale of your Sprite, then you must do one of two things:
Change TextureScale instead of Width/Height (or ScaleX/ScaleY)
-- or --
Set TextureScale to 0, then manually adjust Width/Height (or ScaleX/ScaleY)
Let's consider an example to see why this is the case. For this example, let's consider a Sprite that is displaying an animation. For simplicity purposes, we'll assume that each frame in the animation is displaying a 32x32 image. So our code may look something like this:
Last updated