home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / scrollpf.lzh / SCROLLPF / SCROLLPF.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-01  |  5.8 KB  |  243 lines

  1. /* This program creates and displays a 400 by 300 by 2 bit plane
  2.  * playfield on top of a 320 by 200, 2 plane deep playfield, as
  3.  * a demo of a dual playfield display.  This program is largely
  4.  * the same as the basic display program in the graphics primitives
  5.  * chapter of the ROM Kernel manual, except that it uses a rastport
  6.  * to control the writing instead of writing directly into display 
  7.  * memory.
  8.  */
  9.  
  10. #include "exec/types.h"
  11. #include "intuition/intuition.h"
  12. #include "graphics/gfxbase.h"
  13.  
  14. #define DEPTH   2
  15. #define WIDTH   400
  16. #define HEIGHT  300
  17.  
  18. #define DEPTH2  2     /* for second playfield */
  19. #define WIDTH2  320
  20. #define HEIGHT2 200
  21.  
  22. #define VPWIDTH  320
  23. #define VPHEIGHT 200
  24.  
  25. #define MAXSCROLL_X WIDTH-WIDTH2
  26. #define MAXSCROLL_Y HEIGHT-HEIGHT2
  27.  
  28. #define NOT_ENOUGH_MEMORY -1000
  29.  
  30. struct View     v;
  31. struct ViewPort vp;
  32. struct ColorMap *cm;   /* pointer to colormap structure, dynamic alloc */
  33.  
  34. struct BitMap   b,  b2;
  35. struct RastPort rp, rp2;
  36. struct RasInfo  ri, ri2;
  37.  
  38. LONG  i;
  39. SHORT k,n;
  40.  
  41. struct GfxBase *GfxBase;
  42. struct View *oldview;      /* save pointer to old view so can restore */
  43.  
  44. UWORD colortable[] =  { 
  45.     0x000, 0xf00, 0x0f0, 0x00f,
  46.     0,     0,     0,     0,
  47.     0,     0x4f8, 0xff0, 0xf9c };
  48.  
  49. /*       black,   red,     green,   blue,
  50.                            ignored, ignored, ignored, ignored,
  51.                      (transparent), purple,  yellow,  mauve     */
  52.  
  53. UWORD  *colorpalette;
  54. struct cprlist *LOF[2];
  55. struct cprlist *SHF[2];
  56.  
  57. /* this is a set of pointers which will be used to hold the old values
  58.    of these pointers .... we reuse an old copper list by saying MakeView
  59.    with whatever copper list is currently in LOFCprList and SHFCprList.
  60.    If they are currently = 0, a brand new copper list is created 
  61. */
  62. short w;
  63.  
  64. main()
  65. {
  66.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  67.     if (GfxBase == NULL) exit(1);
  68.     oldview = GfxBase->ActiView; /* save current view to restore later */
  69.     /* example steals screen from Intuition if started from WBench */
  70.  
  71.     /* init bitmap structures */
  72.     InitBitMap(&b,DEPTH,WIDTH,HEIGHT);
  73.     InitBitMap(&b2,DEPTH2,WIDTH2,HEIGHT2);
  74.  
  75.     /* allocate and clear bitplanes */
  76.     for(i=0; i<DEPTH; i++)
  77.     {
  78.         b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  79.         if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  80.         BltClear(b.Planes[i],RASSIZE(WIDTH,HEIGHT),0);
  81.     }
  82.  
  83.     for(i=0; i<DEPTH2; i++)
  84.     {
  85.         b2.Planes[i] = (PLANEPTR)AllocRaster(WIDTH2,HEIGHT2);
  86.         if(b2.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  87.         BltClear(b2.Planes[i],RASSIZE(WIDTH2,HEIGHT2),0);
  88.     }
  89.  
  90.     /* init RasInfo structures */
  91.     ri.BitMap   = &b;
  92.     ri.RxOffset = 0;   
  93.     ri.RyOffset = 0;
  94.     ri.Next = &ri2;
  95.  
  96.     ri2.BitMap = &b2;
  97.     ri2.RxOffset = 0;
  98.     ri2.RyOffset = 0;
  99.     ri2.Next = 0;
  100.  
  101.     InitView(&v);        /* initialize view */
  102.     v.ViewPort = &vp;    /* link view into viewport */
  103.  
  104.     InitVPort(&vp);      /* init view port */
  105.  
  106.     /* First color reg for 2nd playfield is register 8 */
  107.     cm = (struct ColorMap * )GetColorMap(12);
  108.     colorpalette = (UWORD *)cm->ColorTable;
  109.     for(i=0; i<12; i++)
  110.         *colorpalette++ = colortable[i];
  111.     /* copy my colors into this data structure */
  112.     vp.ColorMap = cm;   /* link it with the viewport */
  113.  
  114.     /* now specify critical characteristics */
  115.     vp.DWidth  = VPWIDTH;   /* this is how much you will SEE */
  116.     vp.DHeight = VPHEIGHT;
  117.     vp.RasInfo = &ri;
  118.     vp.Modes   = DUALPF;
  119.  
  120.     MakeVPort( &v, &vp );   /* construct copper instr (prelim) list */
  121.     MrgCop( &v );      /* merge prelim lists together into a real 
  122.                  * copper list in the view structure. */
  123.  
  124.     InitRastPort(&rp);   /* first playfield's rastport */
  125.     rp.BitMap = &b;      /* link to its bitmap */
  126.  
  127.     InitRastPort(&rp2);  /* second playfield's rastport */
  128.     rp2.BitMap = &b2;    /* link to its bitmap */
  129.  
  130.     SetRast(&rp,0);      /* make first one blank */
  131.     SetRast(&rp2,0);     /* make top one transparent */
  132.     LoadView(&v);
  133.  
  134.     SetAPen(&rp2,2);                /* small rectangle in back playfield */
  135.     RectFill(&rp2,140,80,180,100);
  136.  
  137.     SetAPen(&rp,1);                 /* cover except around edges */
  138.     RectFill(&rp,20,20,300,180);
  139.  
  140.     SetAPen(&rp,0);                 /* open hole to see back playfield */
  141.     RectFill(&rp,120,60,200,120);
  142.  
  143.     scrollit();
  144.  
  145.  
  146.     LoadView(oldview);       /* put back the old view  */
  147.     Delay(200);
  148.     FreeMemory();            /* exit gracefully */
  149.     CloseLibrary(GfxBase);   /* since opened library, close it */
  150.  
  151. }   /* end of main() */
  152.  
  153.  
  154. /* return allocated memory */
  155. FreeMemory()
  156. {
  157.     /* free drawing area */
  158.     for(i=0; i<DEPTH; i++) {
  159.         if (b.Planes[i]) FreeRaster(b.Planes[i],WIDTH,HEIGHT); 
  160.     }   
  161.  
  162.     for(i=0; i<DEPTH2; i++) {
  163.         if (b2.Planes[i]) FreeRaster(b2.Planes[i],WIDTH2,HEIGHT2); 
  164.     }   
  165.  
  166.     /* free the color map created by GetColorMap() */
  167.     if (cm) FreeColorMap(cm);
  168.  
  169.     /* free dynamically created structures */
  170.     FreeVPortCopLists(&vp);         
  171.     FreeCprList(LOF[0]);
  172.     FreeCprList(LOF[1]);
  173.  
  174.     return(0);
  175. }   
  176.  
  177.  
  178. scrollit()
  179. {
  180.     short i;
  181.  
  182.     /* graphics library is already opened during main() */
  183.  
  184.     w=0;
  185.  
  186.     /* because these are null, first application creates a new list */
  187.     LOF[1] = NULL;   
  188.     SHF[1] = NULL;
  189.  
  190.     for(i=0; i<MAXSCROLL_X; i++)
  191.     {
  192.         swapPointers();
  193.         ri.RxOffset++;
  194.         /* give it a new offset value, so as to scroll the
  195.                  display */
  196.         remakeView();
  197.         w ^= 1;      /* exclusive-or to swap pointers */
  198.     }   
  199.     for(i=0; i<MAXSCROLL_Y; i++)
  200.     {
  201.         swapPointers(); 
  202.         ri.RyOffset++; 
  203.         remakeView(); 
  204.         w ^= 1;      
  205.     }
  206.     for(i=MAXSCROLL_X; i>(0); i--)
  207.     {
  208.         swapPointers(); 
  209.         ri.RxOffset--; 
  210.         remakeView(); 
  211.         w ^= 1;      
  212.     }
  213.     for(i=MAXSCROLL_Y; i>(0); i--)
  214.     {
  215.         swapPointers(); 
  216.         ri.RyOffset-- ; 
  217.         remakeView(); 
  218.         w ^= 1;      
  219.     }
  220.  
  221. }   /* end of scrollit() */
  222.  
  223.  
  224. swapPointers()      /* provided for double buffering of copper lists */
  225. {
  226.     LOF[w] = v.LOFCprList;
  227.     SHF[w] = v.SHFCprList;
  228.     v.LOFCprList = LOF[(w^1)];
  229.     v.SHFCprList = SHF[(w^1)];
  230.     /* swap the pointers so that they can reuse existing space */
  231. }
  232.  
  233.  
  234. remakeView()
  235. {
  236.     MakeVPort(&v, &vp);
  237.     MrgCop(&v);
  238.     LoadView(&v);      /* and show it */
  239.     WaitTOF();      /* slow things down so we can see it move */
  240. }
  241.  
  242.  
  243.