/*
    Dissolve.c - Do a dissolve effect based on command line arguments

    DESCRIPTION
        This example shows usage of SuperGenSignal().  The dissolve
        effect parameters are taken from the command line.  This program
        waits until either the effect is complete or the user has hit
        Ctrl-C.

    USAGE
        Dissolve BGVal BGTime FGVal FGTime

            BGVal and FGVal are in the range of 0 to 63
            BGTime and FGTime are durations in V-Blanks

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

        Aztec C:    (v5.0)
            cc Dissolve     [works w/ either 16 or 32 bit int's]
            ln Dissolve -lc

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


#include <dos/dos.h>                /* for SIGBREAKF_CTRL_C */
#include <supergen/supergen.h>
#include <stdio.h>
#include <stdlib.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)
{
    short sigbit = -1;

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

    if (argc < 5) {         /* if insufficient arguments */
        printf ("usage: Dissolve BGVal BGTime FGVal FGTime\n");
        goto clean;
    }

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


    /*
        Allocate a signal bit to give to SuperGenSignal().  If -1
        is returned, AllocSignal() failed.
    */

    if ((sigbit = AllocSignal (-1)) == -1) goto clean;


    /*
        Call SuperGenSignal() with the dissolve parameters from the
        command line and the signal bit allocated above.
        SuperGenSignal() begins the dissolve effect and returns
        immediately.  When the effect is finished the signal bit given
        to SuperGenSignal() will be sent back to our task.

        Note that no bounds checking is done on the user supplied
        values.  Care must be taken when actually running this example
        to keep the dissolve effect parameters in range.
    */

    SuperGenSignal (SGM_Software, atoi(argv[1]), atoi(argv[2]),
                                  atoi(argv[3]), atoi(argv[4]), sigbit);

    printf ("Waiting for dissolve to finish.  Press CTRL-C to abort.\n");


    /*
        In the mean time we could do some processing or Wait() for
        additional signals to occur.

        In this case, we are waiting for the effect to finish or for the
        user to press Ctrl-C, whichever happens first.  During this
        time, our task is put to sleep, thus requiring no CPU time.
        Wait() returns a mask of the signals that caused our task to
        wake up.
    */

    if (Wait (1L << sigbit | SIGBREAKF_CTRL_C) & SIGBREAKF_CTRL_C)
        printf ("Dissolve aborted.\n");
    else
        printf ("Dissolve done.\n");

    SGManual();         /* restore SuperGen to manual control */

clean:  /* clean up */
    if (sigbit != -1) FreeSignal (sigbit);
    if (SuperGenBase) CloseLibrary (SuperGenBase);

    return 0;
}
