Glulx Status Window Control

version 1/100630 by Erik Temple

  • Home page
  • Beginning
  • Previous
  • Next



  • Section - Replacement of the library's Initialise routine

    Include (-

    [ VM_PreInitialise res;
    @gestalt 4 2 res; ! Test if this interpreter has Glk...
    if (res == 0) quit; ! ...without which there would be nothing we could do

        unicode_gestalt_ok = false;
        if (glk_gestalt(gestalt_Unicode, 0))
            unicode_gestalt_ok = true;

    ! Set the VM's I/O system to be Glk.
    @setiosys 2 0;
    ];

    [ VM_Initialise res sty i;
    statuswin_pos = 18; ! Sets the status window to open above the main window.

    @gestalt 4 2 res; ! Test if this interpreter has Glk...
    if (res == 0) quit; ! ...without which there would be nothing we could do

    ! First, we must go through all the Glk objects that exist, and see
    ! if we created any of them. One might think this strange, since the
    ! program has just started running, but remember that the player might
    ! have just typed "restart".

    GGRecoverObjects();

        ! Sound channel initialisation, and RNG fixing, must be done now rather
        ! than later in case InitGlkWindow() returns a non-zero value.

        if (glk_gestalt(gestalt_Sound, 0)) {
            if (gg_foregroundchan == 0)
                gg_foregroundchan = glk_schannel_create(GG_FOREGROUNDCHAN_ROCK);
            if (gg_backgroundchan == 0)
                gg_backgroundchan = glk_schannel_create(GG_BACKGROUNDCHAN_ROCK);
        }

        #ifdef FIX_RNG;
        @random 10000 i;
        i = -i-2000;
        print "[Random number generator seed is ", i, "]^";
        @setrandom i;
        #endif; ! FIX_RNG

    res = InitGlkWindow(0);
    if (res ~= 0) return;

    ! Now, gg_mainwin and gg_storywin might already be set. If not, set them.

    if (gg_mainwin == 0) {
    ! Open the story window.
    res = InitGlkWindow(GG_MAINWIN_ROCK);
    if (res == 0) {
    ! Left-justify the header style
                glk_stylehint_set(wintype_TextBuffer, style_Header, stylehint_Justification, 0);
                ! Try to make emphasized type in italics and not boldface
                glk_stylehint_set(wintype_TextBuffer, style_Emphasized, stylehint_Weight, 0);
                glk_stylehint_set(wintype_TextBuffer, style_Emphasized, stylehint_Oblique, 1);
    gg_mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, GG_MAINWIN_ROCK);
            }
    if (gg_mainwin == 0) quit; ! If we can't even open one window, give in
    } else {
    ! There was already a story window. We should erase it.
    glk_window_clear(gg_mainwin);
    }

    if (gg_statuswin == 0) {
    res = InitGlkWindow(GG_STATUSWIN_ROCK);
    if (res == 0) {
    statuswin_cursize = statuswin_size;
                for (sty=0: sty<style_NUMSTYLES: sty++)
                    glk_stylehint_set(wintype_TextGrid, sty, stylehint_ReverseColor, 1);
    gg_statuswin =
    glk_window_open(gg_mainwin, statuswin_pos,
    statuswin_cursize, wintype_TextGrid, GG_STATUSWIN_ROCK);
    }
    }
    ! It's possible that the status window couldn't be opened, in which case
    ! gg_statuswin is now zero. We must allow for that later on.

    glk_set_window(gg_mainwin);

    InitGlkWindow(1);
    ];

    [ GGRecoverObjects id;
    ! If GGRecoverObjects() has been called, all these stored IDs are
    ! invalid, so we start by clearing them all out.
    ! (In fact, after a restoreundo, some of them may still be good.
    ! For simplicity, though, we assume the general case.)
    gg_mainwin = 0;
    gg_statuswin = 0;
    gg_quotewin = 0;
    gg_scriptfref = 0;
    gg_scriptstr = 0;
    gg_savestr = 0;
    statuswin_cursize = 0;
    gg_foregroundchan = 0;
    gg_backgroundchan = 0;
    #Ifdef DEBUG;
    gg_commandstr = 0;
    gg_command_reading = false;
    #Endif; ! DEBUG
    ! Also tell the game to clear its object references.
    IdentifyGlkObject(0);

    id = glk_stream_iterate(0, gg_arguments);
    while (id) {
    switch (gg_arguments-->0) {
    GG_SAVESTR_ROCK: gg_savestr = id;
    GG_SCRIPTSTR_ROCK: gg_scriptstr = id;
    #Ifdef DEBUG;
    GG_COMMANDWSTR_ROCK: gg_commandstr = id;
    gg_command_reading = false;
    GG_COMMANDRSTR_ROCK: gg_commandstr = id;
    gg_command_reading = true;
    #Endif; ! DEBUG
    default: IdentifyGlkObject(1, 1, id, gg_arguments-->0);
    }
    id = glk_stream_iterate(id, gg_arguments);
    }

    id = glk_window_iterate(0, gg_arguments);
    while (id) {
    switch (gg_arguments-->0) {
    GG_MAINWIN_ROCK: gg_mainwin = id;
    GG_STATUSWIN_ROCK: gg_statuswin = id;
    GG_QUOTEWIN_ROCK: gg_quotewin = id;
    default: IdentifyGlkObject(1, 0, id, gg_arguments-->0);
    }
    id = glk_window_iterate(id, gg_arguments);
    }

    id = glk_fileref_iterate(0, gg_arguments);
    while (id) {
    switch (gg_arguments-->0) {
    GG_SCRIPTFREF_ROCK: gg_scriptfref = id;
    default: IdentifyGlkObject(1, 2, id, gg_arguments-->0);
    }
    id = glk_fileref_iterate(id, gg_arguments);
    }

        if (glk_gestalt(gestalt_Sound, 0)) {
            id = glk_schannel_iterate(0, gg_arguments);
            while (id) {
                switch (gg_arguments-->0) {
                    GG_FOREGROUNDCHAN_ROCK: gg_foregroundchan = id;
                    GG_BACKGROUNDCHAN_ROCK: gg_backgroundchan = id;
                }
                id = glk_schannel_iterate(id, gg_arguments);
            }
            if (gg_foregroundchan ~= 0) { glk_schannel_stop(gg_foregroundchan); }
            if (gg_backgroundchan ~= 0) { glk_schannel_stop(gg_backgroundchan); }
        }

    ! Tell the game to tie up any loose ends.
    IdentifyGlkObject(2);
    ];

    -) instead of "Starting Up" in "Glulx.i6t".