Glimmr Canvas-Based Drawing

version 2/101030 by Erik Temple

  • Home page
  • Beginning
  • Previous
  • Next



  • Part - Sprites

    [A sprite is an image (defined by a PNG or other image file) that has the capacity to be placed independently of other elements.]

    A sprite is a kind of g-element.

    The specification of a sprite is "A sprite, per general computing terminology, is an independent image that is integrated into a larger composition. To use an image file as a sprite element, we must first declare the external image file (can be in PNG or JPEG format) as a figure in I7. The figure must then be assigned to the sprite by setting the 'image-ID' property of the sprite object to the figure name. Like other g-elements, sprites have a coordinate pair, the origin property, that determines where in the canvas coordinate system the upper-left corner of the sprite will be displayed. Sprites can be scaled if desired, using the 'scaling factor' property. This scaling factor is calculated before the canvas is scaled to fit the window; in other words, the scaling factor property of the sprite scales it in relation to the rest of the canvas. If the asymmetrical scaling use option is set, it will be necessary to set both the 'x-scaling factor' and the 'y-scaling factor' of the sprite; these can, of course, be set to different values to allow the sprite to be 'stretched' to an aspect ratio other than the original aspect ratio."

    A sprite has a figure name called image-ID. The image-ID property translates into I6 as "image_ID". The image-ID of a sprite is usually Figure of Null.

    [The sprite-x and sprite-y refer to the scaled dimensions of the image. Authors should not need to set them directly.]
    A sprite has a number called the sprite-x. The sprite-x is usually 0.
    A sprite has a number called the sprite-y. The sprite-y is usually 0.

    To display/draw the/-- sprite --/element (ID - a g-element) in (g - a g-window) at coordinates (off_x - a number) by/x/and (off_y - a number) with dimensions (image_x - a number) by/x/and (image_y - a number):
        (- DrawSprite({ID}, {g}, {off_x}, {off_y}, {image_x}, {image_y}); -)


    Include (-

    [DrawSprite ID g off_x off_y image_x image_y ;
        if (g.ref_number) {
        #ifdef ASYM_SCALING;
            if (g.scaling_factor~= (+ Divisor +) || ID.x_factor~= (+ Divisor +) || ID.y_factor~= (+ Divisor +) ) {!internal representation of 1.0000
        #ifnot;
            if (g.scaling_factor~= (+ Divisor +) || ID.scaling_factor~= (+ Divisor +) ) {!internal representation of 1.0000
        #endif;
                glk_image_draw_scaled(g.ref_number, ResourceIDsOfFigures-->ID.image_ID, off_x, off_y, image_x, image_y);
            }
            else {
                glk_image_draw(g.ref_number, ResourceIDsOfFigures-->ID.image_ID, off_x, off_y);
            }
        }
    ];

    -).