Graphical Window Sprites

version 1 by Erik Temple

  • Home page
  • Beginning
  • Previous
  • Next



  • Section: Defining Sprites

    Sprites are subclasses of the "thing" kind in Inform. We define them like so:

        Mario is a sprite. The sprite-identity is Figure of Mario. The sprite coordinates are {58, 45}. Mario is display-active. The display-layer is 4. The sprite-scaling factor is 0.7500.

    This defines a sprite named Mario that will display an image (Figure of Mario) at the coordinates (58,45). The coordinates refer to the coordinates of the background (not the window--see the Concepts section above) where the upper-left corner of the sprite image will be placed. All sprites will also be automatically adjusted by the active drawing rule; depending on which of the built-in drawing rules are used (see below), sprites will follow the scaling and centering of the background. The coordinates can be changed at any time:

        Now the sprite-coordinates of Mario are {12, 17}.

    However, the change will not be reflected on-screen until the graphics window is updated (see below).

    The adjective "display-active" tells Inform that Mario should be drawn to the graphics window the next time it is updated. By default, sprites are display-inactive, meaning that they will not be drawn until we set them as display-active. We do this simply by writing:

        Now Luigi is display-active
        Now Mario is display-inactive

    The display-layer property tells Inform in what order to draw the sprite, in reference to other display-active sprites. Sprites on display-layer 1 are drawn first, those on layer 2 second, and so on. There can be any number of display-layers, but by default all sprites are set to draw on layer 1. The display-layer can be changed like this:

        Now the display-layer of Mario is 3

    The sprite-scaling factor is the amount by which the sprite should be scaled when it is drawn, expressed as a number with four-digit precision. It is mandatory to include all four decimals. In the example above, Mario will be reduced 75% (expressed as a ratio: 0.7500) in relation to the background image. Thus, Mario's image will be drawn so that one of Mario's pixels equals only 0.75 of one pixel of the background image. By default, the sprite-scaling factor is 1.000, or 100%. (The sprite-scaling factor affects only the size of the sprite, not the coordinates of its placement.)

    The sprite-scaling factor lets us make adjustments to the size of our sprites without remaking them in our graphics program, but it is likely more useful for simulating depth--images that are intended to be seen as farther away can be drawn at a smaller scale, then scaled up as we approach them. When combined with the display-layer feature, we have the tools we need to procedurally generate landscapes based on the distance to objects and the ways they overlap or occlude one another in our field of vision.

    One of the most useful features of sprites is that we can define subclasses of sprites and give them their own defaults:

        An occluder is a kind of sprite. An occluder is usually display-active. The display-layer of an occluder is usually 2.

    Now we can refer to occluders as a class anywhere in our source, including in special rules to control their display.

    Similarly, sprites can be "attached" to other items in the game. To assign a sprite to every object in the game, for example, we could write:

        An object has a sprite called the thing-sprite.

    This opens a number of options to us, such as:

        To activate the (N - a thing) sprite:
            Now the thing-sprite of N is display-active

    And now we can simply type "Activate the sword sprite" in our source to mark it for display in the graphics window.

    Finally, note that, in most cases, it will likely be easier and more readable to define sprites using tables rather than in-line.