Flexible Windows

version 13/130803 by Jon Ingold

  • Home page
  • Beginning
  • Previous



  • Example: ** Inventory Window and Picture - A more complex example, that also provides an image panel above the inventory.

        "Inventory Window and Picture"

        The Study is a room. In the study is an old oak desk. On the desk is a Parker pen, a letter, an envelope and twenty dollars.

        Include Flexible Windows by Jon Ingold.

        The side-window is a text-buffer g-window spawned by the main-window.

        The measurement of the side-window is 30.

        A window-drawing rule for the side-window (this is the construct inventory rule):
            move focus to side-window, clearing the window;
            try taking inventory;
            return to main screen.

        Every turn when the side-window is g-present: follow the window-drawing rules for the side-window.

    Now we set up and open the graphics-window. Note that we don't need to open the inventory window, since it will be called into being by the graphics window that it has spawned.

        The graphics-window is a graphics g-window spawned by the side-window.

        When play begins:
            open up the graphics-window.

    This is enough to set up the graphics panel. Now let's give it some images.

        The current image is a figure-name that varies.

        Figure 1 is the file "letter.jpg". Figure 2 is the file "scraps.jpg".

        Carry out examining the letter:
            depict Figure 1;

        Instead of attacking the letter:
            remove the letter from play;
            depict Figure 2;
            say "You tear the letter to shreds." instead.

        To depict (f - a figure-name):
            now the current image is f;
            follow the window-drawing rules for the graphics-window.

    Finally, here's the rule and routines to actually get the picture to display.
        
        Window-drawing rule for the graphics-window (this is the draw scaled image rule):
            if graphics-window is g-unpresent, rule fails;
            clear the graphics-window;
            draw scaled copy of current image in graphics-window.

        To draw scaled copy of (f - a figure-name) in (g - a g-window):
            (- DrawScaled({f}, {g}); -).

    What follows is some I6 code for handling the glulx imagery. Note that you may need to copy and paste the I6 code directly from this page rather than use the paste button.

        Include (-

            ! Doing scaling calculations in I6 lets us handle bigger numbers

            [ GetImageSize curimg index result;
                result = glk_image_get_info( ResourceIDsOfFigures-->curimg, gg_arguments, gg_arguments+WORDSIZE);
                return gg_arguments-->index;
            ];

            [ DrawScaled figure g w_total h_total graph_height graph_width w_offset h_offset;
            graph_height = WindowSize(g, 1);
            graph_width = gg_arguments-->0;
            w_total = GetImageSize(figure, 0);
            h_total = gg_arguments-->1;
        
            if (graph_height - h_total < 0) ! if the image won't fit, find the scaling factor
            {
                w_total = (graph_height * w_total)/h_total;
                h_total = graph_height;

            }

            if (graph_width - w_total < 0)
            {
                h_total = (graph_width * h_total)/w_total;
                w_total = graph_width;
            }

            w_offset = (graph_width - w_total)/2; if (w_offset < 0) w_offset = 0;
            h_offset = (graph_height - h_total)/2; if (h_offset < 0) h_offset = 0;
        
            glk_image_draw_scaled(g.ref_number, ResourceIDsOfFigures-->figure, w_offset, h_offset, w_total, h_total);
            ];
        
        -).

        Test me with "examine letter/z/attack letter".