home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Milan_1991 / Devcon91.4 / Compatibility / freewb.c next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  3.8 KB  |  151 lines

  1. /* 
  2.  * freewb.c
  3.  *
  4.  * Example code showing the technique to close the Workbench screen in
  5.  * a manner suitable for use by startup-sequence programs (typically games)
  6.  * that do not wish to load Workbench.
  7.  *
  8.  * This allows these programs to reclaim the space taken by the Workbench
  9.  * bitplanes.
  10.  *
  11.  * What you should do is use the code shown here inside your actual program.
  12.  * Use a method of making your program detach from the CLI.  Most compilers
  13.  * supply such a method (SAS/C's cback.o detach is used in the example).
  14.  * Your startup-sequence should run whatever is necessary, then it should
  15.  * execute your program, and then call 'endcli'.  For this example, a
  16.  * startup-sequence might be:
  17.  *
  18.  *     c:setpatch >NIL:
  19.  *     freewb
  20.  *     endcli >NIL:
  21.  *
  22.  * Stealing the Workbench bitplanes instead of closing the Workbench screen
  23.  * is illegal, and is not guaranteed to work under future versions of the OS.
  24.  *
  25.  * Note that if there is a chance that DOS requesters can appear, you
  26.  * should redirect them to your window's screen (shown in the example).
  27.  * Otherwise, the appearance of a DOS requester will re-open the Workbench
  28.  * screen.
  29.  *
  30.  * This code is valid under Kickstart V33 (1.2 ROM) and higher.
  31.  *
  32.  *     lc freewb
  33.  *     blink lib:cback.o freewb.o to freewb lib lib:lc.lib lib:amiga.lib
  34.  *
  35.  *
  36.  * (C) Copyright 1991, Commodore-Amiga, Inc.
  37.  *
  38.  * Executables based on this information may be used in software
  39.  * for Commodore Amiga computers.  All other rights reserved.
  40.  * This information is provided "as is"; no warranties are made.  All
  41.  * use is at your own risk. No liability or responsibility is assumed.
  42.  *
  43.  */
  44.  
  45. #include <intuition/intuition.h>
  46. #include <libraries/dosextens.h>
  47.  
  48. #include <clib/exec_protos.h>
  49. #include <clib/intuition_protos.h>
  50.  
  51. struct Library *IntuitionBase;
  52.  
  53. /* Number of seconds to wait for the Workbench to close before we give up */
  54. #define MAX_TIMEOUT (5)
  55.  
  56. void _main()
  57. {
  58.     long done = 0;
  59.     long timeout = 0;
  60.  
  61.     if ( IntuitionBase = OpenLibrary( "intuition.library", 33L ) )
  62.     {
  63.     /* Sit here and wait for Workbench to close... */
  64.     while ( !done )
  65.     {
  66.         /* Try to close Workbench */
  67.         if ( CloseWorkBench() )
  68.         {
  69.         /* We succeeded */
  70.         done = 1;
  71.         }
  72.         else
  73.         {
  74.         if ( timeout++ > (MAX_TIMEOUT*5) )
  75.         {
  76.             /* Give up */
  77.             done = 2;
  78.         }
  79.         else
  80.         {
  81.             /* Wait a bit and try again */
  82.             Delay( 10 );
  83.         }
  84.         }
  85.     }
  86.  
  87.     /* We succeeded! */
  88.     if ( done == 1 )
  89.     {
  90.         /* Put the body of your program here
  91.          * (For example purposes, we'll open a screen and window...)
  92.          */
  93.  
  94.         static struct NewScreen newscr =
  95.         {
  96.         0, 0, 640, STDSCREENHEIGHT, 2, 0, 1, HIRES,
  97.         CUSTOMSCREEN, NULL, NULL, NULL, NULL
  98.         };
  99.  
  100.         static struct NewWindow newwin =
  101.         {
  102.         0, 0, 250, 100, -1, -1, CLOSEWINDOW,
  103.         ACTIVATE | WINDOWDEPTH | WINDOWDRAG | WINDOWCLOSE,
  104.         NULL, NULL, "<- Click to close", NULL,
  105.         NULL, 50, 50, -1, -1, CUSTOMSCREEN
  106.         };
  107.  
  108.         struct Screen *screen;
  109.         struct Window *window;
  110.  
  111.         if ( screen = OpenScreen( &newscr ) )
  112.         {
  113.         newwin.Screen = screen;
  114.         if ( window = OpenWindow( &newwin ) )
  115.         {
  116.             /* Force DOS requesters to our screen */
  117.             ( (struct Process *)FindTask( NULL ) )->
  118.             pr_WindowPtr = window;
  119.  
  120.             WaitPort( window->UserPort );
  121.  
  122.             ( (struct Process *)FindTask( NULL ) )->
  123.             pr_WindowPtr = NULL;
  124.  
  125.             CloseWindow( window );
  126.         }
  127.         CloseScreen( screen );
  128.         }
  129.     }
  130.     else
  131.     {
  132.         /* We failed.  Note that we cannot printf(), since we've
  133.          * detached.  For example's sake, we'll use DisplayBeep()
  134.          * to show failure.
  135.          */
  136.         DisplayBeep( NULL );
  137.     }
  138.  
  139.     /* Note:  even if we open the Workbench screen, there are
  140.      * no CLI's available.  Unless the Workbench application is
  141.      * running, the user will be left with a blank screen.
  142.      * This is OK, since this code is intended for startup-sequence
  143.      * games, which generally don't exit.
  144.      */
  145.  
  146.     OpenWorkBench();
  147.  
  148.     CloseLibrary( IntuitionBase );
  149.     }
  150. }
  151.