home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / dev / c2p.lha / C2P / CPU+Blitter / c2p8_demo.c < prev    next >
C/C++ Source or Header  |  1994-07-01  |  6KB  |  325 lines

  1. // set tabs to 4
  2.  
  3. #define WIDTH    320            // MUST be a multiple of 32
  4. #define HEIGHT    200
  5.  
  6. struct TagItem TagArray[] = {
  7.     SA_Interleaved, FALSE,        // c2p8 does NOT work on interleaved screens
  8.     // can add other tags here
  9.     TAG_DONE,0
  10. };
  11.  
  12. struct ExtNewScreen NewScreenStructure = {
  13.     0,0,
  14.     WIDTH,HEIGHT,
  15.     8,                // depth
  16.     0,1,
  17.     NULL,
  18.     CUSTOMSCREEN+SCREENQUIET+NS_EXTENDED,
  19.     NULL,
  20.     NULL,
  21.     NULL,
  22.     NULL,
  23.     (struct TagItem *)&TagArray
  24. };
  25.  
  26. struct NewWindow NewWindowStructure1 = {
  27.     0,0,
  28.     WIDTH,HEIGHT,
  29.     0,1,
  30.     NULL,
  31.     SIMPLE_REFRESH+BORDERLESS+NOCAREREFRESH,
  32.     NULL,
  33.     NULL,
  34.     NULL,
  35.     NULL,
  36.     NULL,
  37.     5,5,
  38.     WIDTH,HEIGHT,
  39.     CUSTOMSCREEN
  40. };
  41.  
  42.  
  43. // external function prototypes -----------------
  44.  
  45. void __asm c2p8_init (    register __a0 UBYTE *chunky,        // pointer to chunky data
  46.                         register __a1 UBYTE *chunky_cmp,    // pointer to chunky comparison buffer
  47.                         register __a2 PLANEPTR *planes,        // pointer to planes
  48.                         register __d0 ULONG signals1,        // 1 << sigbit1
  49.                         register __d1 ULONG signals2,        // 1 << sigbit2
  50.                         register __d2 ULONG pixels,            // WIDTH * HEIGHT
  51.                         register __d3 ULONG offset,            // byte offset into plane
  52.                         register __d4 UBYTE *buff2,            // Chip buffer (size = width*height)
  53.                         register __d5 UBYTE *buff3,            // Chip buffer (size = width*height)
  54.                         register __a3 struct GfxBase *GfxBase);
  55.  
  56. void __asm c2p8_go(void);
  57.  
  58.  
  59. // internal function prototypes -----------------
  60.  
  61. long get_signals(void);
  62. void free_signals(void);
  63. long get_chunky_mem(void);
  64. void free_chunky_mem(void);
  65. void init_chunky(void);
  66. long get_window(void);
  67. void free_window(void);
  68.  
  69.  
  70. // library bases --------------------------------
  71.  
  72. struct DosLibrary        *DOSBase;
  73. struct IntuitionBase    *IntuitionBase;
  74. struct ExecBase            *SysBase;
  75. struct GfxBase            *GfxBase;
  76.  
  77.  
  78. // window related variables ---------------------
  79.  
  80. struct RastPort *RP;
  81. struct Screen *s;
  82. struct Window *w;
  83.  
  84.  
  85. // chunky data and c2p8() related variables -----
  86.  
  87. UBYTE *chunky;        // chunky data (preferably in fast ram)
  88. UBYTE *chunky_cmp;    // chunky data comparison buffer (preferably in fast ram)
  89. UBYTE *buff2;        // blitter buffer (chip ram)
  90. UBYTE *buff3;        // blitter buffer (chip ram)
  91.  
  92. long sigbit1 = -1;        // used by c2p8()
  93. long sigbit2 = -1;        // used by c2p8()
  94.  
  95.  
  96. #define nokick    "This needs Kickstart 3.0!\n"
  97. #define REPEAT_COUNT 10
  98.  
  99. long __saveds main(void)
  100. {
  101.     int count;
  102.  
  103.     SysBase = *(struct ExecBase **)4;
  104.  
  105.     if(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library",33))
  106.     {
  107.         // check what kickstart version we are using
  108.         // inform the user and exit if lower than 39
  109.  
  110.         if( DOSBase->dl_lib.lib_Version < 39)
  111.         {
  112.             Write(Output(), nokick, sizeof(nokick) );
  113.             CloseLibrary( (struct Library *) DOSBase);
  114.             return(0);
  115.         }
  116.  
  117.  
  118.         // if compiling with 68020+ code, exit before we crash
  119.         // a 68000 machine
  120.  
  121.  
  122.         #ifdef _M68020
  123.         if(! ( SysBase->AttnFlags & AFF_68020) )
  124.         {
  125.             Printf("This version needs at least a 68020!\n");
  126.             return(0);
  127.         }
  128.         #endif
  129.  
  130.  
  131.         if(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",39))
  132.         if(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",39))
  133.         {
  134.  
  135.             if( get_window() )
  136.             if( get_chunky_mem() )
  137.             if( get_signals() )
  138.             {
  139.  
  140.                 // initialize c2p converter
  141.  
  142.                 c2p8_init (    chunky,
  143.                             chunky_cmp,
  144.                             &RP->BitMap->Planes[0],
  145.                             1 << sigbit1,
  146.                             1 << sigbit2,
  147.                             WIDTH * HEIGHT,
  148.                             0,
  149.                             buff2,
  150.                             buff3,
  151.                             GfxBase);
  152.  
  153.                 // fill the chunky buffer with a distinct pattern and a triangle
  154.  
  155.                 init_chunky();
  156.             
  157.                 for (count = 0; count < REPEAT_COUNT; count++)
  158.                 {
  159.             
  160.                     // render next frame here
  161.  
  162.                     c2p8_go();    // Convert chunky buffer to planar
  163.                                 // only writes to the screen if the chunky
  164.                                 // buffer has changed since last time.
  165.                                 // Only converts the changed data
  166.  
  167.                 }
  168.  
  169.                 Delay(120);
  170.  
  171.             }
  172.  
  173.             free_signals();            // wait for c2p8 to finish before
  174.                                     // freeing memory or closing screens
  175.  
  176.             free_chunky_mem();
  177.             free_window();
  178.  
  179.             CloseLibrary((struct Library *)GfxBase);
  180.  
  181.         }
  182.  
  183.         if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  184.  
  185.         CloseLibrary((struct Library *)DOSBase);
  186.     }
  187.  
  188.     return(0);
  189.  
  190. }
  191.  
  192.  
  193. // get signals necessary for c2p8() -------------
  194.  
  195. long get_signals(void)
  196. {
  197.     long ok = 0;
  198.  
  199.     if(-1 != (sigbit1 = AllocSignal(-1)))
  200.     {
  201.         SetSignal (1 << sigbit1, 1 << sigbit1); // Initial state is "finished"
  202.  
  203.         if(-1 != (sigbit2 = AllocSignal(-1)))
  204.         {
  205.             SetSignal (1 << sigbit2, 1 << sigbit2); // Initial state is "finished"
  206.  
  207.             ok = 1;
  208.         }
  209.     }
  210.  
  211.     return(ok);
  212.  
  213. }
  214.  
  215. void free_signals(void)
  216. {
  217.     if(sigbit1 != -1)
  218.     {
  219.         Wait (1 << sigbit1);    // wait for last c2p8 to finish pass 3
  220.         FreeSignal(sigbit1);
  221.         sigbit1 = -1;
  222.     }
  223.  
  224.     if(sigbit2 != -1)
  225.     {
  226.         Wait (1 << sigbit2);    // wait for last c2p8 to finish totally
  227.         FreeSignal(sigbit2);
  228.         sigbit2 = -1;
  229.     }
  230. }
  231.  
  232.  
  233. // get memory for chunky buffer, chunky comparsion buffer
  234. // and two blitter buffers needed by c2p8() -----
  235.  
  236. long get_chunky_mem(void)
  237. {
  238.     long ok = 0, size = WIDTH * HEIGHT;
  239.  
  240.     if( chunky = AllocVec(size, MEMF_CLEAR+MEMF_ANY))
  241.     if( chunky_cmp = AllocVec(size, MEMF_CLEAR+MEMF_ANY))
  242.     if( buff2 = AllocVec(size, MEMF_CLEAR+MEMF_CHIP))
  243.     if( buff3 = AllocVec(size, MEMF_CLEAR+MEMF_CHIP))
  244.     {
  245.         ok = 1;
  246.     }
  247.  
  248.     return(ok);
  249.  
  250. }
  251.  
  252. void free_chunky_mem(void)
  253. {
  254.     if(buff3)
  255.         FreeVec(buff3);
  256.  
  257.     if(buff2)
  258.         FreeVec(buff2);
  259.  
  260.     if(chunky_cmp)
  261.         FreeVec(chunky_cmp);
  262.  
  263.     if(chunky)
  264.         FreeVec(chunky);
  265.  
  266. }
  267.  
  268.  
  269. // Write a distinctive pattern to chunky buffer and a triangle
  270.  
  271. #define write_pixel(x,y,p) (chunky[y * WIDTH + x] = p )
  272.  
  273. void init_chunky(void)
  274. {
  275.     int i, j;
  276.     UBYTE *p;
  277.  
  278.     p = chunky;
  279.     for (j = 0; j < HEIGHT; j++)
  280.     for (i = 0; i < WIDTH; i++)
  281.     *p++ = (i + j) & 255;
  282.  
  283.     // Draw a triangle to check orientation
  284.  
  285.     for (i = 50; i < 150; i++)
  286.     {
  287.         write_pixel (i, 150, i);
  288.         write_pixel (i+120, 150, i);
  289.  
  290.         write_pixel (50, i, i);
  291.         write_pixel (170, i, i);
  292.  
  293.         write_pixel (i, i, i);
  294.         write_pixel (i+120, i, i);
  295.     }
  296. }
  297.  
  298.  
  299. // open a screen and a window -------------------
  300.  
  301. long get_window(void)
  302. {
  303.     long ok = 0;
  304.  
  305.     if(s = OpenScreen( (struct NewScreen *) &NewScreenStructure))
  306.     {
  307.         NewWindowStructure1.Screen = s;
  308.  
  309.         if(w = OpenWindow(&NewWindowStructure1))
  310.         {
  311.             RP = w->RPort;
  312.             ok = 1;
  313.         }
  314.     }
  315.  
  316.     return(ok);
  317.  
  318. }
  319.  
  320. void free_window(void)
  321. {
  322.     if(w) CloseWindow(w);
  323.     if(s) CloseScreen(s);
  324. }
  325.