home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d599 / dbuff.lha / DBuff / src.lzh / DBuff.c
C/C++ Source or Header  |  1992-01-30  |  13KB  |  467 lines

  1. /***************************************************************************
  2.  
  3.    Program:    
  4.    File:       DBuff.c
  5.    
  6.    Version:    V1.0
  7.    Date:       05.02.91
  8.    Function:   Setup routines for double buffering based on Intuition screens
  9.    
  10.    Copyright:  SciTech Software 1991
  11.    Author:     Andrew C. R. Martin
  12.    Address:    SciTech Software
  13.                23, Stag Leys,
  14.                Ashtead,
  15.                Surrey,
  16.                KT21 2TD.
  17.    Phone:      +44 (0372) 275775
  18.    EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
  19.                JANET: andrew@uk.ac.ox.biop
  20.                
  21. ****************************************************************************
  22.  
  23.    This program is not in the public domain, but it may be freely copied
  24.    and distributed for no charge providing this header is included.
  25.    The code may be modified as required, but any modifications must be
  26.    documented so that the person responsible can be identified. If someone
  27.    else breaks this code, I don't want to be blamed for code that does not
  28.    work! The code may not be sold commercially without prior permission from
  29.    the author, although it may be given away free with commercial products,
  30.    providing it is made clear that this program is free and that the source
  31.    code is provided with the program.
  32.  
  33. ****************************************************************************
  34.  
  35.    Description:
  36.    ============
  37.    These routines set up, manipulate and tidy up for double buffered
  38.    animation using an Intuition Screen and Window
  39.  
  40.    Usage:
  41.    ======
  42.    5 routines are supplied:
  43.  
  44. >  RastPort = (struct RastPort *)InitDBuff(GfxBase,Screen,depth,Window)
  45.    --------------------------------------------------------------------
  46.    Given the GfxBase, Screen and Window, this routine returns the
  47.    second RastPort
  48.  
  49. >  SetView1()
  50.    ----------
  51. >  SetView2()
  52.    ----------
  53.    Sets the required view ready for drawing. SetView1() sets the Intuition
  54.    view, SetView2() sets the alternate view.
  55.    
  56. >  SwapView()
  57.    ----------
  58.    Displays the new view (after drawing)
  59.    
  60. >  FreeDBuff(screen,depth,RastPort)
  61.    --------------------------------
  62.    Frees up the memory, etc. for the second view.
  63.  
  64.    Notes:
  65.    ======
  66.    1. Assumes both graphics and intuition libraries are open
  67.    2. The window should be NOBORDER and have a null title
  68.    3. For maximum speed in swapping views, the SwapView() call could
  69.       be replaced by the following code.
  70.       Put this up the top somewhere:
  71.          #include <graphics/gfxbase.h>
  72.          struct View *DB_view;
  73.          DB_view = GfxBase->ActiView;
  74.       Replace exch SwapView() call with:
  75.          MrgCpr(DB_view);
  76.    4. #define DEMO to see the demonstration.
  77.  
  78. ****************************************************************************
  79.  
  80.    Revision History:
  81.  
  82. ***************************************************************************/
  83.  
  84. #include <exec/types.h>
  85. #include <exec/memory.h>
  86. #include <intuition/intuition.h>
  87. #include <graphics/display.h>
  88. #include <graphics/gfxbase.h>
  89.  
  90.  
  91. /**************************************************************************/
  92. /* Global Amiga system variables */
  93.  
  94. struct cprlist  *DB_LOF,               /* Pointers within View structure */
  95.                 *DB_SHF,
  96.                 *DB_MyLOF,             /* My second copper lists */
  97.                 *DB_MySHF,
  98.                 *DB_IntuiLOF,          /* Intuition supplied copper lists */
  99.                 *DB_IntuiSHF;
  100. struct RasInfo  *DB_rinfo     = NULL;  /* Intuition supplied rasinfo */
  101. struct BitMap   *DB_bmap1     = NULL,  /* Intuition supplied bitmap */
  102.                 *DB_bmap2     = NULL;  /* Second bitmap */
  103. struct View     *DB_view      = NULL;  /* Intuition View */
  104.  
  105.  
  106.  
  107. /**************************************************************************/
  108. InitDBuff(GfxBase,screen,depth,window)
  109. struct GfxBase *GfxBase;
  110. struct Screen  *screen;
  111. int    depth;
  112. struct Window  *window;
  113. {
  114.    /* Amiga system variables */
  115.    struct RastPort *rport1    = NULL;  /* Intuition supplied rastport */
  116.    struct ViewPort *vport     = NULL;  /* Intuition Viewport */
  117.    struct RastPort *rport2    = NULL;  /* new rastport */
  118.    
  119.    int    j,
  120.           problem = 0;
  121.  
  122.  
  123.    rport2 = NULL;
  124.  
  125.  
  126.    /* Get the bitmap, rastport, viewport, view and rasinfo supplied by Intuition 
  127.       from the window, screen and graphics base from the call. */
  128.    rport1 = window->RPort;
  129.    DB_bmap1  = rport1->BitMap;
  130.    vport  = &(screen->ViewPort);
  131.    DB_view   = GfxBase->ActiView;
  132.    DB_rinfo  = (*vport).RasInfo;
  133.  
  134.  
  135.  
  136.    /************ Allocate second bitmap & memory for bitplanes ***********/
  137.    if(!(DB_bmap2=(struct BitMap *)
  138.               AllocMem(sizeof(struct BitMap), MEMF_PUBLIC|MEMF_CLEAR)))
  139.    {
  140.        printf("alloc bitmap failed\n");
  141.       rport2 = NULL;
  142.       problem = 1;
  143.        goto EXITING;
  144.    }
  145.    InitBitMap(DB_bmap2, depth, screen->Width, screen->Height);
  146.    /* we'll use depth planes. */
  147.    for(j=0; j<depth; j++)
  148.    {
  149.       if (!(DB_bmap2->Planes[j] =
  150.        (PLANEPTR) AllocRaster(screen->Width, screen->Height)))
  151.       {
  152.           printf("alloc raster failed\n");
  153.          rport2 = NULL;
  154.          problem = 1;
  155.           goto EXITING;
  156.       }
  157.    }
  158.  
  159.    /******** Create a rastport for this bitmap to simplify drawing **********/
  160.    if(!(rport2 = (struct RastPort *)AllocMem(sizeof(struct RastPort), MEMF_PUBLIC)))
  161.    {
  162.        printf("alloc rastport failed\n");
  163.       rport2 = NULL;
  164.       problem = 1;
  165.        goto EXITING;
  166.    }
  167.    InitRastPort(rport2);
  168.    rport2->BitMap = DB_bmap2;
  169.  
  170.    SetRast(rport2, 0);
  171.  
  172.  
  173.  
  174.    /********** Create pointers for View
  175.                long-frame Copper list (LOFCprList) and
  176.                short-frame Copper list (SHFCprList)    *************/
  177.    DB_IntuiLOF = DB_LOF = DB_view->LOFCprList;
  178.    DB_IntuiSHF = DB_SHF = DB_view->SHFCprList;
  179.  
  180.    DB_MyLOF = NULL;
  181.    DB_MySHF = NULL;
  182.  
  183.    /********** Swap views back and forth to get the MakeScreen()
  184.                out of the way    **************/
  185.                
  186.    SetView2();
  187.    MakeScreen(screen);
  188.    MrgCop(DB_view);
  189.  
  190.    SetView1();
  191.    MrgCop(DB_view);
  192.    
  193. EXITING:
  194.    if(problem) FreeDBuff(screen,depth,rport2);
  195.    
  196.    return((int)rport2);
  197. }
  198.  
  199. /**************************************************************************/
  200. SetView1()
  201. {
  202.    DB_LOF = DB_IntuiLOF;
  203.    DB_SHF = DB_IntuiSHF;
  204.    DB_rinfo->BitMap = DB_bmap1;
  205.    
  206.    return(0);
  207. }
  208.  
  209. /**************************************************************************/
  210. SetView2()
  211. {
  212.    DB_LOF = DB_MyLOF;
  213.    DB_SHF = DB_MySHF;
  214.    DB_rinfo->BitMap = DB_bmap2;
  215.    
  216.    return(0);
  217. }
  218.  
  219. /**************************************************************************/
  220. SwapView()
  221. {
  222.    MrgCop(DB_view);
  223.    
  224.    return(0);
  225. }
  226.  
  227. /**************************************************************************/
  228. FreeDBuff(screen,depth,rport2)
  229. struct Screen   *screen;
  230. int depth;
  231. struct RastPort *rport2;
  232. {
  233.    int j;
  234.    
  235.    /* Ensure we're on the Intuition-supplied bit map before exiting */
  236.    SetView1();
  237.    MrgCop(DB_view);
  238.  
  239.    /* Free up the second rastport */
  240.    if(rport2) FreeMem(rport2, sizeof(struct RastPort));
  241.  
  242.    /* Free up the second bit map */
  243.    if(DB_bmap2) 
  244.    {
  245.       for(j=0;j<depth;j++)
  246.       {
  247.          if (DB_bmap2->Planes[j])
  248.          {
  249.             FreeRaster(DB_bmap2->Planes[j], screen->Width, screen->Height);
  250.          }
  251.       }
  252.       FreeMem(DB_bmap2, sizeof (struct BitMap));
  253.    }
  254.    
  255.    return(0);
  256. }
  257.  
  258. /***************************************************************************
  259. *                                                                          *
  260. *              DEMO CODE --- Compiled if DEMO defined                      *
  261. *                                                                          *
  262. ***************************************************************************/
  263. #ifdef DEMO
  264.  
  265. #include <exec/types.h>
  266. #include <intuition/intuition.h>
  267. #include <graphics/display.h>
  268.  
  269. #define DEPTH 1   /* Screen depth (number of bitplanes) */
  270.  
  271. /* Global Amiga system variables */
  272. struct  IntuitionBase   *IntuitionBase = NULL;
  273. struct  GfxBase         *GfxBase       = NULL;
  274.  
  275. main()
  276. {
  277.    /* Amiga system variables */
  278.    struct Screen   *screen    = NULL;  /* Intuition screen */
  279.    struct Window   *window    = NULL;  /* Intuition window */
  280.    struct RastPort *rport1    = NULL,  /* Intuition supplied rastport */
  281.                    *rport2    = NULL;
  282.  
  283.    int    j,
  284.           exitval = 0;
  285.  
  286.  
  287.    /********************** Open Libraries ************************/
  288.    if (!(IntuitionBase = (struct IntuitionBase *)
  289.    OpenLibrary("intuition.library", 0)))
  290.    {
  291.        printf("NO INTUITION LIBRARY\n");
  292.        exitval = 1;
  293.        goto CRAPOUT;
  294.    }
  295.  
  296.    if (!(GfxBase = (struct GfxBase *)
  297.     OpenLibrary("graphics.library", 0)))
  298.    {
  299.        printf("NO GRAPHICS LIBRARY\n");
  300.        exitval = 2;
  301.        goto CRAPOUT;
  302.    }
  303.  
  304.    /************* Open Intuition screen and window ********************/
  305.    if((screen = (struct Screen *)make_screen(0,640,400,DEPTH,-1,-1,
  306.                 HIRES|INTERLACE,"***Test Screen***"))==NULL)
  307.    {
  308.       printf("Can't open screen\n");
  309.       exitval = 1;
  310.       goto CRAPOUT;
  311.    }
  312.  
  313.    /* get a window on the workbench    */
  314.    if((window = (struct Window *)make_window(0,0,640,400,NULL,
  315.                 BORDERLESS|SIMPLE_REFRESH,NULL,-1,-1,screen,NULL))==NULL)
  316.    {
  317.       printf("Can't open window\n");
  318.       exitval = 1;
  319.       goto CRAPOUT;
  320.    }
  321.  
  322.    rport1 = window->RPort;
  323.    rport2 = (struct RastPort *)InitDBuff(GfxBase,screen,DEPTH,window);
  324.  
  325.    /********** Loop which draws into alternate bitmaps and swaps view *********/
  326.    for(j=0;j<500;j++)
  327.    {
  328.       if(j%2)  /* Odd cases --- Draw to rport1 */
  329.       {
  330.          SetView2();
  331.  
  332.          if(j-2) DrawFigure(rport1,j-2,0);      /* Clear previous version */
  333.          DrawFigure(rport1,j,1);                /* Draw this version      */
  334.       }
  335.       else     /* Even cases --- Draw to rport2 */
  336.       {
  337.          SetView1();
  338.  
  339.          if(j-2 >= 0) DrawFigure(rport2,j-2,0); /* Clear previous version */
  340.          DrawFigure(rport2,j,1);                /* Draw this version      */
  341.       }
  342.       
  343.       SwapView();
  344.       Delay(1);
  345.    }
  346.    
  347.    
  348.  
  349. CRAPOUT:
  350.    FreeDBuff(screen,DEPTH,rport2);
  351.    
  352.    /* Close stuff */
  353.    if(window)        CloseWindow(window);
  354.    if(screen)        CloseScreen(screen);
  355.    if(GfxBase)       CloseLibrary(GfxBase);
  356.    if(IntuitionBase) CloseLibrary(IntuitionBase);
  357.  
  358.    exit(exitval);
  359. }
  360.       
  361. /***********************************************************************/
  362.  
  363. make_screen(y,w,h,d,colour0,colour1,mode,name)
  364. SHORT y,w,h,d;
  365. UBYTE colour0,colour1,*name;
  366. USHORT mode;
  367. {
  368.    struct NewScreen NewScreen;
  369.  
  370.  
  371.    NewScreen.LeftEdge=0;
  372.    NewScreen.TopEdge=y;
  373.    NewScreen.Width=w;
  374.    NewScreen.Height=h;
  375.    NewScreen.Depth=d;
  376.    NewScreen.DetailPen=colour0;
  377.    NewScreen.BlockPen=colour1;
  378.    NewScreen.ViewModes=mode;
  379.    NewScreen.Type=CUSTOMSCREEN;
  380.    NewScreen.Font=NULL;
  381.    NewScreen.DefaultTitle=name;
  382.    NewScreen.Gadgets=NULL;
  383.    NewScreen.CustomBitMap=NULL;
  384.  
  385.    return(OpenScreen(&NewScreen));
  386. }
  387.  
  388. /***********************************************************************/
  389.  
  390. make_window(x,y,w,h,name,flags,iflags,colour0,colour1,screen,gadg)
  391. SHORT x,y,w,h;
  392. UBYTE *name,colour0,colour1;
  393. ULONG flags,iflags;
  394. struct Screen *screen;
  395. struct Gadget *gadg;
  396.  
  397. /* Initialize a NewWindow and Open it. */
  398.  
  399. {
  400.    struct NewWindow NewWindow;
  401.  
  402.    NewWindow.LeftEdge=x;
  403.    NewWindow.TopEdge=y;
  404.    NewWindow.Width=w;
  405.    NewWindow.Height=h;
  406.    NewWindow.DetailPen=colour0;
  407.    NewWindow.BlockPen=colour1;
  408.    NewWindow.Title=name;
  409.    NewWindow.Flags=flags;
  410.    NewWindow.IDCMPFlags=iflags;
  411.    NewWindow.Type=CUSTOMSCREEN;
  412.    NewWindow.FirstGadget=gadg;
  413.    NewWindow.CheckMark=NULL;
  414.    NewWindow.Screen=screen;
  415.    NewWindow.BitMap=NULL;
  416.    NewWindow.MinWidth=0;
  417.    NewWindow.MinHeight=0;
  418.    NewWindow.MaxWidth=0;
  419.    NewWindow.MaxHeight=0;
  420.  
  421.    return(OpenWindow(&NewWindow));
  422.  
  423. }
  424.  
  425. /**************************************************************************/
  426.  
  427. DrawFigure(rp,n,p)
  428. struct RastPort *rp;
  429. int n,p;
  430. {
  431.    SetAPen(rp,p);
  432.    Move(rp,n+20,270);
  433.    Draw(rp,n+0,250);
  434.    Draw(rp,n+0,220);
  435.    Draw(rp,n+20,200);
  436.    Draw(rp,n+40,220);
  437.    Draw(rp,n+40,250);
  438.    Draw(rp,n+20,270);
  439.    Draw(rp,n+20,300);
  440.    Draw(rp,n+40,320);
  441.    Draw(rp,n+20,340);
  442.    Move(rp,n+40,320);
  443.    Draw(rp,n+70,320);
  444.    Draw(rp,n+70,340);
  445.    Move(rp,n+70,320);
  446.    Draw(rp,n+90,300);
  447.  
  448.    Move(rp,n+20, 70);
  449.    Draw(rp,n+0,  50);
  450.    Draw(rp,n+0,  20);
  451.    Draw(rp,n+20, 00);
  452.    Draw(rp,n+40, 20);
  453.    Draw(rp,n+40, 50);
  454.    Draw(rp,n+20, 70);
  455.    Draw(rp,n+20,100);
  456.    Draw(rp,n+40,120);
  457.    Draw(rp,n+20,140);
  458.    Move(rp,n+40,120);
  459.    Draw(rp,n+70,120);
  460.    Draw(rp,n+70,140);
  461.    Move(rp,n+70,120);
  462.    Draw(rp,n+90,100);
  463.  
  464.    return(0);
  465. }
  466.                
  467. #endif /* DEMO */