/*
    ToggleFade.c - Fade in or out depending previous state of faders

    DESCRIPTION
        This example shows usage of StatusGen() function, SGSTAT_FG() macro
        and SuperGenWait() function.

        A 2 second fade in or out is performed based on the current state
        of the Foreground fader (read using StatusGen() and extracted with
        SGSTAT_FG()).  The program waits for the dissolve to complete.

    COMPILATION
        SAS/C:      (v5.1)
            lc -L ToggleFade

        Aztec C:    (v5.0)
            cc ToggleFade
            ln ToggleFade -lc

    Copyright (C) 1989-1992 Digital Creations, Inc.  All rights reserved.
*/


#include <supergen/supergen.h>
#include <stdio.h>

#include <clib/exec_protos.h>
#include <clib/supergen_protos.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/supergen_pragmas.h>

struct Library *SuperGenBase;

#if AZTEC_C
  void _wb_parse (void) {}      /* stub out wb window parser (Aztec C) */
#endif

#if __SASC                      /* suppress normal Ctrl-C processing (SAS/C) */
  void __chkabort (void) {}     /* SAS/C ansi libraries */
  void chkabort (void) {}       /* SAS/C 5.0 pre-ansi libraries */
#endif

int main (int argc, char **argv)
{
    int val;

  #if AZTEC_C                   /* suppress normal Ctrl-C processing (Aztec C) */
    {
        extern int Enable_Abort;
        Enable_Abort = 0;
    }
  #endif

    if (!(SuperGenBase = OpenLibrary ("supergen.library", SUPERGEN_Minimum))) {
        printf ("You need supergen.library v2.0 or higher.\n");
        goto clean;
    }

    /*
        Call StatusGen() to find out the current setting for the Foreground
        (graphics) fader.  SGSTAT_FG() extracts the Foreground setting
        from the StatusGen() return value.

        Fader values are in terms of how much external video is shown:

            value    pos   amiga  video
            -----    ---   -----  -----
            SGF_Min  up     max    min
            SGF_Max  down   min    max

        Select a new value for both faders that is a complement of
        the current setting:

            If current FG > SGF_Min, choose SGF_Min as new value
            Else choose SGF_Max as the new value
    */

    val = SGSTAT_FG(StatusGen()) > SGF_Min ? SGF_Min : SGF_Max;


    /*
        Call SuperGenWait() to do the dissolve and wait around until
        it's done.

        The target value for both faders is val calculated above.  The
        duration of the dissolve for both faders is set to 2 seconds (120
        vertical blanks).

        SGM_Software places the SuperGen in software control (remote) mode.
    */

    SuperGenWait (SGM_Software, val, 120, val, 120);


    /*
        Use the SGManual() macro to restore the SuperGen to manual
        control.

        NOTE:  Using this macro will cause the video output from the
        SuperGen to revert to whatever the dissolve console faders are
        set to.  You will only need to call SGManual() when you are done
        controlling the SuperGen.
    */

    SGManual();

clean:
    if (SuperGenBase) CloseLibrary (SuperGenBase);

    return 0;
}
