Item Sprite

Renders an item’s inventory icon from its 3D model, the way the game does: light the model from the fixed inventory light, project it through the item’s own camera angles onto a 36 by 32 canvas, then outline it and drop a shadow behind it.

Example:

sprite := TItemSprite.Create(CacheReader, itemDef);
if sprite.Valid then
  sprite.ToImage().Show();

TItemSprite

Renders item inventory icons from 3D models.


TItemSprite.Create

function TItemSprite.Create(const itemDef: TItemDefinition): TItemSprite; static;
function TItemSprite.Create(const archive: TArchive; id: Integer): TItemSprite; static; overload;
function TItemSprite.Create(id: Integer): TItemSprite; static; overload;

Loads the item’s inventory model and lights it. Sets TItemSprite.Valid to False if the model archive/data can’t be loaded.

Noted items are only drawn properly by the overloads that take an item id. Those link the item to its note paper with TItemDefinition.LinkNote and render every animation frame of the unnoted item into TItemSprite.NotePixels, which every image is then drawn with on top of the paper. A noted item whose paper or unnoted item can’t be rendered is not valid.

Lighting depends only on the model and the item, not on how the icon is drawn, so it is done once here rather than again by every TItemSprite.ToImage or TItemSprite.BorderImage.


TItemSprite.Light

procedure TItemSprite.Light();

Lights the item’s model the way the client lights inventory models - see TLitModel.Light.


TItemSprite.FrameCount

function TItemSprite.FrameCount(): Integer;

How many different icons the item’s animated textures cycle through, 1 for an item that doesn’t animate. Any frame given to TItemSprite.ToImage wraps around at this.

Every animated texture on the model and on the item a note is of has to be back where it started at once, so this is the smallest count all their own frame counts divide into.

Example:

sprite := TItemSprite.Create(6570);
for frame := 0 to sprite.FrameCount() - 1 do
  sprite.ToImage(False, 0, frame).Show();

TItemSprite.Project

procedure TItemSprite.Project(selected: Boolean = False; noted: Boolean = False; frame: Integer = 0);

Sets up the 36x32 rasterizer and projects and draws the model through the item’s calibrated 2D camera parameters.

selected pushes the camera back to 1.04 times the item’s Zoom2D, which is what the game does for the item you currently have selected so its thicker outline still fits inside the icon.

noted pushes it back to 1.5 times instead, the smaller icon the game draws on top of note paper. It takes priority over selected.

frame is which animation frame textured faces are drawn with, see TItemSprite.FrameCount.


TItemSprite.ToImage

function TItemSprite.ToImage(selected: Boolean = False; shadowColor: Integer = 0; frame: Integer = 0): TImage;

Projects and rasterizes the model lit by TItemSprite.Create and returns the resulting 36x32 icon.

shadowColor can be set by the user, items use $FF333333 most of the time except when the bank is open, in which case $FF302020 is used instead. 0 which is the default means no shadow will be drawn.

frame picks the animation frame of an item with animated textures, see TItemSprite.FrameCount.

Colours are given the way ColorUtils.Palette writes one - alpha, red, green and blue - because they go straight into the rasterizer’s buffer. The near-black outline is not quite zero, since zero is what marks a pixel as untouched.

img := sprite.ToImage();
img.Show();
Sleep(3000);
img := sprite.ToImage(True, 0);
img.Show();

TItemSprite.BorderImage

function TItemSprite.BorderImage(selected: Boolean = False; frame: Integer = 0): TImage;

The item’s outline on its own, without the item or a shadow behind it.

Renders the icon exactly as TItemSprite.ToImage does and then returns only the pixels TSpritePixels.DrawBorder laid down, so the outline sits at the same place in the 36x32 canvas as it does in the full icon.

img := sprite.BorderImage();
img.Show();

TItemSprite.MaskImage

function TItemSprite.MaskImage(selected: Boolean = False; frame: Integer = 0): TImage;

The item’s outline on its own filled white.

img := sprite.MaskImage();
img.Show();

TItemSprite.GetCRC

function TItemSprite.GetCRC(img: TImage; top: Integer = 9): UInt32; static;
function TItemSprite.GetBorderCRC(img: TImage; top: Integer = 9): UInt32; static;

Hashes an item icon from TItemSprite.ToImage, leaving out the first top rows, where the game draws stack numbers over the icon.

GetCRC hashes the pixels themselves. GetBorderCRC hashes only where the icon’s outline is: the points of the $010000 border colour, in the order TImage.FindColor returns them. Every item drawn with the same outline shares a border hash, however it is coloured, and an icon with no outline below top gives 0.

Example:

img := TItemSprite.Create(4151).ToImage();
WriteLn TItemSprite.GetCRC(img), ' ', TItemSprite.GetBorderCRC(img);