home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505b.lha / Ham_e_library / BuffDemo.c < prev    next >
C/C++ Source or Header  |  1991-05-05  |  6KB  |  275 lines

  1.  
  2. #include <intuition/intuition.h>
  3. #include <exec/types.h>
  4. #include <exec/execbase.h>
  5. #include <graphics/text.h>
  6.  
  7. #include <proto/exec.h>
  8. #include <proto/graphics.h>
  9.  
  10. #include "hame_pragmas.h"
  11.  
  12. #include "HameStruct.h"
  13. #include "HameProtos.h"
  14. #include "HameRev.h"
  15.  
  16. #include <string.h>
  17. #include <exec/memory.h>
  18. #include <exec/tasks.h>
  19.  
  20. #include <graphics/view.h>
  21. #include <graphics/gfx.h>
  22. #include <graphics/rastport.h>
  23.  
  24. #include <proto/intuition.h>
  25.  
  26.  
  27. struct Library *HameBase;
  28. struct HamePort *HamePort;
  29.  
  30. extern struct Screen *OpenScreen();
  31. extern struct Window *OpenWindow();
  32.  
  33. extern struct ExecBase *SysBase;  /* These are all pointers to system */
  34. struct IntuitionBase *IntuitionBase;  /* These are all pointers to system */
  35. struct GfxBase       *GfxBase;        /* data structures I need access to */
  36.  
  37. struct Screen        *screen;
  38. struct Window        *window;
  39.  
  40. struct Clip *clip;
  41. UBYTE  *ptr1, *ptr2;
  42. long ptsize;
  43.  
  44. #define WIDTH 640
  45. #define HEIGHT 200
  46.  
  47. struct NewScreen my_screen =
  48.   {
  49.     0, 0, WIDTH, HEIGHT,
  50.     4,
  51.     0, 1,
  52.     HIRES,
  53.     CUSTOMSCREEN,
  54.     (struct TextAttr *) NULL,
  55.     NULL,
  56.     NULL, NULL
  57.   };
  58.  
  59. struct NewWindow my_window =
  60.   {
  61.     0, 0, WIDTH, HEIGHT,
  62.     (UBYTE) 0, (UBYTE) 1,
  63.     MOUSEBUTTONS
  64.        | INTUITICKS,
  65.     SMART_REFRESH
  66.        | BORDERLESS
  67.        | BACKDROP
  68.        | ACTIVATE
  69.        | NOCAREREFRESH
  70.        | REPORTMOUSE,
  71.     NULL, NULL,
  72.     NULL,
  73.     NULL, NULL,
  74.     0, 0, 0, 0,
  75.     CUSTOMSCREEN
  76.   };
  77.  
  78.  
  79. /*
  80.   Clean up and exit
  81. */
  82. void CloseStuff()
  83.   {
  84.       if (clip)               HAME_DisposeClip(clip);
  85.       if (ptr1)               FreeMem(ptr1, ptsize);
  86.       if (window)             CloseWindow(window);
  87.       if (screen)             CloseScreen(screen);
  88.       if (HamePort)           HAME_Dispose(HamePort);
  89.       if (HameBase)           CloseLibrary(HameBase);
  90.       if (GfxBase)            CloseLibrary(GfxBase);
  91.       if (IntuitionBase)      CloseLibrary(IntuitionBase);
  92.  
  93.       exit(0);
  94.   }
  95.  
  96.  
  97. /*
  98.   Draw a vertical column of pixels at the left margin to prevent hame from
  99.   turning off.
  100. */
  101. void DrawLeftMargin()
  102.   {
  103.   int i;
  104.  
  105.   HAME_SetAPen( HamePort, 1 );
  106.   for ( i = 0; i < 196; i++ )
  107.     {
  108.       HAME_WritePixel( HamePort, 0, i);
  109.     }
  110.   }
  111.  
  112. /*
  113.   Initialize the palette to a grey, a red, a green and a blue scale
  114.   Note that the four groups are intermingled, and not separate groups
  115. */
  116. void palette1()
  117.   {
  118.   int i, j, r;
  119.  
  120.   j = 1;
  121.   for ( i = 0; i < 64; i++ )
  122.     {
  123.       r = i << 2;
  124.       HAME_SetRGB8(HamePort, j++, r, r, r);
  125.       HAME_SetRGB8(HamePort, j++, r, 0, 0);
  126.       HAME_SetRGB8(HamePort, j++, 0, r, 0);
  127.       HAME_SetRGB8(HamePort, j++, 0, 0, r);
  128.     }
  129.   }
  130.  
  131. /*
  132.   Main routine
  133. */
  134. int main(argc, argv)
  135.   int argc;
  136.   char *argv[];
  137.   {
  138.   ULONG  msg_class;
  139.   USHORT msg_code;
  140.   int    reg, i, j, finished=0;
  141.   struct IntuiMessage *message;
  142.   struct Gadget *gadget;
  143.  
  144.   if ((IntuitionBase = (struct IntuitionBase *)
  145.        OpenLibrary("intuition.library",33L))==NULL)
  146.     {
  147.       CloseStuff();
  148.     }
  149.  
  150.   if ((GfxBase = (struct GfxBase *)
  151.        OpenLibrary("graphics.library" , 0L))==NULL)
  152.     {
  153.       CloseStuff();
  154.     }
  155.  
  156.   /* Open the hame library */
  157.   if ((HameBase = (struct Library *) 
  158.        OpenLibrary("hame.library",HAMELIB_VERSION)) == NULL)
  159.     {
  160.       printf("No hame.library\n");
  161.       CloseStuff();
  162.     }
  163.  
  164.  
  165.   /* Get a screen to play with */
  166.   if ((screen = OpenScreen(&my_screen))     ==NULL)
  167.     {
  168.       CloseStuff();
  169.     }
  170.  
  171.   my_window.Screen = screen;
  172.  
  173.   /* Get a window to play with */
  174.   if ((window = OpenWindow(&my_window)) == NULL)
  175.     {
  176.       CloseStuff();
  177.     }
  178.  
  179.   /* turn off the title bar */
  180.   ShowTitle(screen, 0L);
  181.  
  182.   /* Tell the library where to put pixels */
  183.   if ((HamePort = HAME_Init(screen, 640, 400, 400, 0, 0, 4, 196)) == NULL)
  184.     {
  185.       CloseStuff();
  186.     }
  187.  
  188.   /* Set the pallette */
  189.   palette1();
  190.  
  191.   /* Keep hame from turning off because of screen wide color 0 */
  192.   DrawLeftMargin();
  193.  
  194.   /* 8 bit buffer is 128 x 128 */
  195.   ptsize = 128*128;
  196.  
  197.   /* Get fast memory */
  198.   ptr1 = AllocMem(ptsize, MEMF_PUBLIC|MEMF_CLEAR);
  199.   ptr2 = ptr1;
  200.  
  201.   /* Create a strange pattern in the buffer */
  202.   for ( i = 0; i < 128; i++ )
  203.     {
  204.       for ( j = 0; j < 128 ; j++ )
  205.         {
  206.           *ptr2++ = ((i & ~7) << 1) + (j >> 3);
  207.         }
  208.     }
  209.  
  210.   /* Render using increasingly wider and taller parameters */
  211.   for ( i = 1; i <= 128; i++ )
  212.     {
  213.       HAME_8BitRender(HamePort, ptr1, 0, 0, i, i);
  214.     }
  215.  
  216.   /* Pick up the final result */
  217.   HAME_SetAPen(HamePort, 0);
  218.   clip = HAME_GetClip(HamePort, 0, 0, 128, 128);
  219.   if (!clip)
  220.     {
  221.       printf("No clip!\n");
  222.       CloseStuff();
  223.     }
  224.  
  225.   /* Copy it accross and down the sreen */
  226.   for ( i = 0; i <= 3; i++ )
  227.     {
  228.       for ( j = 0; j < 2 ; j++ )
  229.         {
  230.           HAME_PutClip(HamePort, clip, (i << 7), (j << 7));
  231.         }
  232.     }
  233.  
  234.   /* Wait for mouse press */
  235.   while( !finished )
  236.     {
  237.       message = (struct IntuiMessage *) GetMsg(window->UserPort);
  238.       if (message)
  239.         {
  240.           msg_class = message->Class;
  241.           msg_code = message->Code;
  242.           gadget = (struct Gadget *) message->IAddress;
  243.           ReplyMsg((struct Message *)message);
  244.  
  245.           switch( msg_class )
  246.             {
  247.               case MOUSEBUTTONS:
  248.                 finished = 1;
  249.               break;
  250.  
  251.               case INTUITICKS:
  252.                 for ( reg = 0; reg == 0; reg = rand() % 255 );
  253.                 HAME_SetRGB8(HamePort, reg, rand() % 255,
  254.                                        rand() % 255, rand() % 255);
  255.               break;
  256.  
  257.               case CLOSEWINDOW:
  258.                 finished = 1;
  259.               break;
  260.  
  261.               default:
  262.                 printf("?");
  263.             }
  264.         }
  265.      }
  266.  
  267.   while ((message = (struct IntuiMessage *)
  268.           GetMsg( window->UserPort )) != NULL)
  269.     {
  270.       ReplyMsg((struct Message *)message);
  271.     }
  272.  
  273.   CloseStuff();
  274. }
  275.