# Texture Loader Builds the pixels textured faces are drawn with, and animates them. A texture definition only names the sprite a texture is made from. The texels themselves are that sprite laid out on a 128 by 128 grid with the client's brightness curve applied to it, and they are built the first time a texture is asked for and kept until the brightness changes. Some textures scroll, such as lava or water. Their animation is not a series of stored images but the base texels shifted a little further along every client tick, so any frame can be built directly from the base texels. Frame `0` is the texture as it is stored. - - - ## TextureLoader.Load ```pascal procedure TTextureLoader.Load(); ``` Reads every texture definition, caching the texture index first. Does nothing once loaded. Definitions are stored at their own id, so an id that has no texture leaves an empty definition behind, one with no sprite to build texels from. - - - ## TextureLoader.Exists ```pascal function TTextureLoader.Exists(id: Integer): Boolean; ``` Whether `id` is a texture with a sprite to draw it from. - - - ## TextureLoader.BuildTexels ```pascal function TTextureLoader.BuildTexels(id: Integer): TIntegerArray; ``` The unanimated texels of texture `id`, as `SIZE` by `SIZE` red, green and blue values with no alpha, or an empty array if its sprite can't be read. The sprite is placed on its full canvas and every colour is bent by the current brightness the way the client bends a texture's palette, which is not quite the same as the brightness the colour palette is built with, since texture colours are already red, green and blue. A texel that is `0` is a hole in the texture. A 64 by 64 sprite is doubled up to fill the grid. - - - ## TextureLoader.FrameCount ```pascal function TTextureLoader.FrameCount(id: Integer): Integer; ``` How many distinct frames texture `id` animates through before it repeats, `1` if it doesn't move. Each tick moves the texture `AnimationSpeed` texels along a grid that wraps every `SIZE` texels, so it lines up with itself again after `SIZE` divided by the largest number both are divisible by. - - - ## TextureLoader.CombineFrameCounts ```pascal function TTextureLoader.CombineFrameCounts(a, b: Integer): Integer; static; ``` How many frames two animations take to both be back where they started, which is the smallest number both `a` and `b` divide into. - - - ## TextureLoader.Animate ```pascal function TTextureLoader.Animate(const texels: TIntegerArray; direction, speed, frame: Integer): TIntegerArray; static; ``` `texels` shifted as far as `frame` ticks of animation move them. Directions `1` and `3` scroll the texture along its rows, `1` one way and `3` the other, by whole rows at a time. Directions `2` and `4` scroll along its columns. Anything else doesn't move. - - - ## TextureLoader.Texels ```pascal function TTextureLoader.Texels(id: Integer; frame: Integer = 0): TIntegerArray; ``` The texels of texture `id` at animation `frame`, or an empty array if the texture doesn't exist or its sprite can't be read. Frames past {ref}`TTextureLoader.FrameCount` wrap back around. Texels are built once per brightness. Only the last frame asked for is kept for each texture, which is all a renderer drawing one frame at a time needs. Example: ```pascal texels := TextureLoader.Texels(40, 3); ``` - - - ## TextureLoader.AverageColor ```pascal function TTextureLoader.AverageColor(id: Integer): Integer; ``` The average colour of texture `id` as `$RRGGBB`, or `-1` if it has no texels. Holes in the texture don't count towards it. It is what a textured face looks like from far enough away, which is all something that only needs a face's colour, rather than its pattern, cares about. Worked out once per texture and brightness.