home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.1 / JimmDemos / DemoSource / dvdemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  7.3 KB  |  313 lines

  1. /* dvdemo.c -- devcon demo test main loop
  2.  * Copyright (c) 1988, I and I Computing, and Commodore-Amiga, Inc.
  3.  *
  4.  * Executables based on this information may be used in software
  5.  * for Commodore Amiga computers.  All other rights reserved.
  6.  *
  7.  * This information is provided "as is"; no warranties are made.
  8.  * All use is at your own risk, and no liability or responsibility is assumed.
  9.  */
  10.  
  11. #include "sysall.h"
  12. #include "dview.h"
  13. #include "spr.h"
  14. #include "getargs.h"
  15.  
  16. extern struct IntuitionBase *IntuitionBase;
  17. extern struct GfxBase *GfxBase;
  18.  
  19. struct Screen *screen = NULL;
  20. struct DoubleView    DV = {0};
  21.  
  22. /* command line argument: what method of display synchonization is
  23.  * to be demonstrated?
  24.  */
  25. int        wait_type = 0;        /* default waitDV()    */
  26.  
  27. struct Arg myargs[] = {
  28.     {'w', ARG_TINT, &wait_type, "0-waitDV, 1-WaitTOF, 2-no wait, 3-WaitBOVB"},
  29. };
  30.  
  31. /* these globals, plus the single signal SIGF_SINGLE, are the
  32.  * communication between the input handler and the 
  33.  * main task.
  34.  */
  35. int    hdlr_wants_out;
  36. int    hdlr_should_steal;
  37. struct InputEvent *Mintuition();
  38.  
  39. /* for cleanup purposes    */
  40. int    handler_installed = 0;
  41.  
  42. /* alternate mouse sprite and its position */
  43. struct SpritePortRast    myspr;
  44. int        mousex;            /* view-relative mouse coords and limits    */
  45. int        mousey;
  46. int        maxxmouse;
  47. int        maxymouse;
  48.  
  49. main(argc, argv)
  50. char    **argv;
  51. {
  52.     int                cc;
  53.     struct RastPort    *rp;
  54.  
  55.     if (!OpenI( 33 )) cleanup( "no intuition\n");
  56.     if (!OpenGfx( 33 )) cleanup("no graphics\n");
  57.  
  58.     if (getargs( argv, myargs, NUMARGS( myargs ), NULL) < 0) cleanup("-\n");
  59.  
  60.     /* 
  61.      * initialize input theft scheme
  62.      */
  63.     hdlr_should_steal = 0;            /* don't fire him up yet    */
  64.     hdlr_wants_out = 0;
  65.     if ( installInputHandler( Mintuition, 51) ) cleanup("can't install hdlr.\n");
  66.     handler_installed = 1;
  67.     printf("handler installed (not stealing).\n");
  68.  
  69.     /*
  70.      * initialize double view setup
  71.      */
  72.     if (! (screen = IntuitionBase->FirstScreen)) cleanup("can't find screen\n");
  73.     if ( ! initDV( &DV, screen ) ) cleanup("initDV failed!\n");
  74.     dumpDV( &DV );
  75.  
  76.     /*
  77.      * set up to steal Intuition's pointer when the input is
  78.      * being stolen
  79.      */
  80.     if ( ! setupSprite( &myspr) ) cleanup( "no sprite\n" );
  81.  
  82.     /* set up mouse limits and current position */
  83.     maxxmouse = screen->Width - 1;
  84.     if ( screen->ViewPort.Modes & HIRES ) maxxmouse /= 2;
  85.     maxymouse = screen->Height - 1;
  86.     mousey = mousex = 0;        /* you could probably pick up where 
  87.                                  * Intuition's pointer is.  too lazy.
  88.                                  */
  89.  
  90.     printf("mouselimits: %d/%d\n", maxxmouse, maxymouse );
  91.     printf("wait_type: %d\n\n", wait_type );
  92.     printf("press return to start,\n");
  93.     printf(" ... and press MENUBUTTON to pause: ");fflush( stdout ); getchar();
  94.  
  95.     do 
  96.     {
  97.         /* tell the handler to start stealing input    */
  98.         ChangeSprite( NULL, &myspr.spr_SSprite, myspr.spr_Data );
  99.         MoveSprite(  NULL, &myspr.spr_SSprite, (LONG ) mousex, (LONG ) mousey );
  100.  
  101.         hdlr_should_steal = 1;
  102.  
  103.         fflush( stdin );                /* my cheezy user interface        */
  104.  
  105.         rp = selectDV( &DV, 0 );        /* get one of my views loaded    */
  106.  
  107.         while ( ! hdlr_wants_out )
  108.         {
  109.             variousWait();
  110.  
  111.             /* only position differs in code for different buffers    */
  112.             if ( DV_CURRENT( &DV ) == 0 )
  113.             {
  114.                 /* fill with a color that should never be seen    */
  115.                 SetAPen( rp, 1L );
  116.                 RectFill( rp, 120L, 20L, 240L, 180L );
  117.  
  118.                 SetAPen( rp, 2L );
  119.                 RectFill( rp, 120L, 20L, 240L, 180L );
  120.             }
  121.             else
  122.             {
  123.                 /* fill with a color that should never be seen    */
  124.                 SetAPen( rp, 1L );
  125.                 RectFill( rp, 340L, 20L, 460L, 180L );
  126.  
  127.                 SetAPen( rp, 2L );
  128.                 RectFill( rp, 340L, 20L, 460L, 180L );
  129.             }
  130.  
  131.             rp = swapDV( &DV );
  132.         }
  133.  
  134.         /* I am here because input handler has told me (via 'hdlr_wants_out')
  135.          * that he is turning on the input stream again.
  136.          * Although we don't really need to, we demonstrate a
  137.          * method of syncronizing with the handler.
  138.          * He is frozen, waiting for me to signal him.
  139.          */
  140.         hdlr_should_steal = 0;
  141.         hdlr_wants_out = 0;
  142.         Signal( (long) FindTask("input.device"), (long) SIGF_SINGLE );
  143.  
  144.         /* give back view to intuition    */
  145.  
  146.         RethinkDisplay();
  147.         WaitTOF();
  148.  
  149.         /* make sure Intuition re-establishes its pointer.
  150.          * it's possible here to tell Intuition to move its
  151.          * pointer to where mine ended up, but this demo
  152.          * is crowded enough.
  153.          */
  154.         Forbid();    /* not sure that I can pass a NULL window pointer */
  155.         if (IntuitionBase->ActiveWindow)
  156.         {
  157.             ClearPointer( IntuitionBase->ActiveWindow );
  158.         }
  159.         Permit();
  160.  
  161.         /* did I tell you about the bug in MoveSprite()?    */
  162.         printf("mousex/y: %d/%d\n", mousex - 1, mousey );
  163.         printf("pause mode.  Press return to continue ('q' to quit).: ");
  164.         fflush( stdout );
  165.  
  166.     } while ( (cc = getchar()) != 'q' );
  167.  
  168.     cleanup("success.\n");
  169. }
  170.  
  171. /* 
  172.  * there are several things you might try, besides the method
  173.  * of waitDV().  This function supports command line
  174.  * switching between some of the methods
  175.  */
  176. variousWait()
  177. {
  178.     switch ( wait_type )
  179.     {
  180.     case 0:
  181.         waitDV( &DV );
  182.         break;
  183.     case 1:
  184.         WaitTOF();
  185.         break;
  186.     case 2:
  187.         return;
  188.     case 3:
  189.         WaitBOVP( DV.dv_Views[ DV_CURRENT( &DV ) ]->ViewPort );
  190.         break;
  191.     default:
  192.         waitDV( &DV );
  193.         printf("BAD WaitType\n");
  194.     }
  195. }
  196.  
  197. cleanup(str)
  198. char    *str;
  199. {
  200.     if (str)                printf( str );
  201.     if (handler_installed)    extractInputHandler();
  202.     freeSPR( &myspr );
  203.     if ( IntuitionBase )    CloseLibrary( IntuitionBase );
  204.     if ( GfxBase )            CloseLibrary( GfxBase );
  205.     freeDV( &DV );
  206.     exit( 1 );
  207. }
  208.  
  209.  
  210. /* 
  211.  * input handler
  212.  * note that it decides for the application that menus are about
  213.  * to happen.
  214.  */
  215. struct InputEvent *
  216. Mintuition( ie )
  217. struct InputEvent *ie;
  218. {
  219.     extern int    hdlr_should_steal;
  220.     extern int    hdlr_wants_out;
  221.  
  222.     while (ie && hdlr_should_steal )
  223.     {
  224.         /* ... check for terminating events    ... */
  225.         if ( ie->ie_Class == IECLASS_RAWMOUSE ) 
  226.         {
  227.             /* ... track and handle the input you need ... */
  228.             movePointer( ie );        /* as an example    */
  229.  
  230.             if ( ie->ie_Code == IECODE_RBUTTON )
  231.             {
  232.                 /* let the poller know I've got an issue */
  233.                 hdlr_wants_out = 1;
  234.  
  235.                 /* synchronize with him    */
  236.                 Wait( (long) SIGF_SINGLE );
  237.  
  238.                 /* let the event through */
  239.                 if (! hdlr_should_steal ) break;
  240.             }
  241.         }
  242.  
  243.         ie = ie->ie_NextEvent;
  244.     }
  245.     return ( ie );        /* remants, if any , back to intuition     */
  246. }
  247.  
  248. /* only called with class RAWMOUSE    */
  249. movePointer( ie )
  250. struct InputEvent *ie;
  251. {
  252.     mousex += ie->ie_X;
  253.     mousey += ie->ie_Y;
  254.  
  255.     if ( mousex < 0 ) mousex = 0;
  256.     if ( mousey < 0 ) mousey = 0;
  257.  
  258.     if ( mousex > maxxmouse ) mousex = maxxmouse;
  259.     if ( mousey > maxymouse ) mousey = maxymouse;
  260.  
  261.     /* did I tell you about the bug in MoveSprite()?    */
  262.     MoveSprite(  NULL, &myspr.spr_SSprite, (LONG) mousex - 1, (LONG) mousey );
  263.  
  264.     return ;
  265. }
  266.  
  267. #define SPRTEXTTOP        (7)
  268.  
  269. /* returns 0 if failure    */
  270. setupSprite( spr )
  271. struct SpritePortRast     *spr;
  272. {
  273.     int    text_skip;
  274.     int    sprheight;
  275.     register struct RastPort    *rp;
  276.     long    baseline;
  277.  
  278.     sprheight = 2 * SPRTEXTTOP + 3 * GfxBase->DefaultFont->tf_YSize;
  279.  
  280.     if ( initSPR( spr, sprheight, 0, 1 ) == -1 )  return ( 0 );
  281.  
  282.     rp = &spr->spr_RPort;
  283.  
  284.     text_skip = spr->spr_RPort.TxHeight;
  285.  
  286.     /* some rendered sprite visuals     */
  287.     SetAPen( rp, ( LONG ) 1);
  288.     Move( rp, 0L, 0L );
  289.     Draw( rp, 15L, 0L );
  290.     Move( rp, 0L, (LONG) sprheight - 1 );
  291.     Draw( rp, 15L,(LONG) sprheight - 1 );
  292.     SetAPen( rp, ( LONG ) 3);
  293.     RectFill( rp, 0L, 1L, 15L, (LONG) sprheight - 2 );
  294.  
  295.     /* oops: I should set the font    */
  296.     SetAPen( rp, ( LONG ) 0);
  297.     SetBPen( rp, ( LONG ) 1);
  298.  
  299.     baseline = SPRTEXTTOP + text_skip;
  300.     Move( rp, 1L, baseline );
  301.     Text( rp, "I", 1L );
  302.  
  303.     baseline += text_skip;
  304.     Move( rp, 3L,  baseline );
  305.     Text( rp, "&", 1L );
  306.  
  307.     baseline += text_skip;
  308.     Move( rp, 7L,  baseline );
  309.     Text( rp, "I", 1L );
  310.  
  311.     return ( 1 );
  312. }
  313.