Glimmr Simple Graphics Window

version 1/100805 by Erik Temple

  • Home page
  • Beginning
  • Previous



  • Example: * Ramrod - This simple example illustrates the use of GSGW on its own, without any of the other Glimmr extensions. The example includes a drawing rule that centers an image (specified using the "current image" variable) in the graphics-window. If the image is too large to fit in the window, it will be scaled down.

    Note that this example requires an image to function. The image referred to in the example can be downloaded, along with all of the other images used in Glimmr examples, from http://code.google.com/p/glimmr-i7x/downloads/list.

    Also note that this example includes I6 inclusions. Due to a bug in Inform, these do not transfer properly when using the paste button. One-line inclusions might look like this after being pasted to your source code:

        (--) glk_request_timer_events({T}); -)

    Delete the first -) for each inclusion and things should work fine. Corrected:

        (- glk_request_timer_events({T}); -)

    Multi-line I6 inclusions should probably be copied directly from the source of the extension into your project.

        "Ramrod"

        Boston 1775 is a room.

        Include Flexible Windows by Jon Ingold.
        Include Glimmr Simple Graphics Window by Erik Temple.

        The position of the graphics-window is g-placeabove. The back-colour of the graphics-window is g-Black.

        When play begins:
            open up the graphics-window.
        
        Figure of Gunnery is the file "Revolution.jpg".

        After looking:
            follow the window-drawing rules for the graphics-window.
        

    You may insert only the code below to use the window-drawing rule in your own code. Just remember to delete the sentence "The current image is Figure of Gunnery". You will also need to remember to include an appropriate regime for updating the window. If you are using an image for each location, for example, it will probably be best to update the window in an "after looking" rule (as in this example). To update the window, use the phrase "follow the window-drawing rules for the graphics-window".

    The code for image display and so on (everything after the window-drawing rule) is borrowed from Glimmr Drawing Commands.
        
        The current image is a figure name variable. The current image is Figure of Gunnery.
        
        A window-drawing rule for the graphics-window (this is the scaled image display rule):
            let win_width be the width of the graphics-window;
            let win_height be the height of the graphics-window;
            let img_width be the image-width of the current image;
            let img_height be the image-height of the current image;
            let scale_factor be 100;
            let width_factor be (win_width * 100) / img_width;
            let height_factor be (win_height * 100) / img_height;
            if width_factor is greater than height_factor:
                let scale_factor be height_factor;
            otherwise:
                 let scale_factor be width_factor;
            let img_width be (img_width * scale_factor) / 100;
            let img_height be (img_height * scale_factor) / 100;
            let offset_width be the greater of 0 or (win_width - img_width) / 2;
            let offset_height be the greater of 0 or (win_height - img_height) / 2;
            display the current image in the graphics-window at (offset_width) by (offset_height) with dimensions (img_width) by (img_height).

        [After printing the banner text:
        say "[line break]This exceedingly dull example, for the Glimmr Simple Graphics Window extension, illustrates how to write a simple graphics window rule in I7. The rule that scales and centers the image is as follows:[paragraph break] A window-drawing rule for the graphics-window (this is the scaled image display rule):[line break] let win_width be the width of the graphics-window;[line break] let win_height be the height of the graphics-window;[line break] let img_width be the image-width of the current image;[line break] let img_height be the image-height of the current image;[line break] let scale_factor be 100;[line break] let width_factor be (win_width * 100) / img_width;[line break] let height_factor be (win_height * 100) / img_height;[line break] if width_factor is greater than height_factor:[line break] let scale_factor be height_factor;[line break] otherwise:[line break] let scale_factor be width_factor;[line break] let img_width be (img_width * scale_factor) / 100;[line break] let img_height be (img_height * scale_factor) / 100;[line break] let offset_width be the greater of 0 or (win_width - img_width) / 2;[line break] let offset_height be the greater of 0 or (win_height - img_height) / 2;[line break] display the current image in the graphics-window at (offset_width) by (offset_height) with dimensions (img_width) by (img_height).[paragraph break]"]
        
        To display (ID - a figure name) in (win - a g-window) at (x1 - a number) by/x (y1 - a number) with size/dimensions (width - a number) by/x (height - a number):
            (- DrawImageScaled({ID}, {win}, {x1}, {y1}, {width}, {height}); -)
        

        Include (-

        [DrawImageScaled ID win x y image_x image_y ;
            if (win.ref_number) {
                glk_image_draw_scaled(win.ref_number, ResourceIDsOfFigures-->ID, x, y, image_x, image_y);
            }
        ];

        -).

        To decide which number is the greater/max of/-- (X - a number) or (Y - a number):
            if Y > X, decide on Y;
            decide on X.
        
        To decide what number is the image-width of (img - a figure name):
            (- FindImageWidth({img}) -)
        
        To decide what number is the image-height of (img - a figure name):
            (- FindImageHeight({img}) -)
        

        Include (-

        [ FindImageWidth img result img_width;
            result = glk_image_get_info(ResourceIDsOfFigures-->img, gg_arguments, gg_arguments+WORDSIZE);
         img_width = gg_arguments-->0;
            return img_width;
        ];

        [ FindImageHeight img result img_height;
            result = glk_image_get_info(ResourceIDsOfFigures-->img, gg_arguments, gg_arguments+WORDSIZE);
         img_height = gg_arguments-->1;
            return img_height;
        ];

        -)