home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505b.lha / Ham_e_library / TwoDemo.c < prev   
C/C++ Source or Header  |  1991-05-05  |  8KB  |  442 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. #include "HameStruct.h"
  12. #include "HameProtos.h"
  13. #include "HameRev.h"
  14.  
  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. struct HamePort *HamePort2;
  30.  
  31. extern struct Screen *OpenScreen();
  32. extern struct Window *OpenWindow();
  33.  
  34. extern struct ExecBase *SysBase;  /* These are all pointers to system */
  35. struct IntuitionBase *IntuitionBase;  /* These are all pointers to system */
  36. struct GfxBase       *GfxBase;        /* data structures I need access to */
  37.  
  38. struct Screen        *screen;
  39. struct Window        *window;
  40.  
  41. #define WIDTH 640
  42. #define HEIGHT 400
  43. #define YSIZ 190
  44.  
  45. struct NewScreen my_screen =
  46.   {
  47.     0, 0, WIDTH, HEIGHT,
  48.     4,
  49.     0, 1,
  50.     HIRES | LACE,
  51.     CUSTOMSCREEN,
  52.     (struct TextAttr *) NULL,
  53.     NULL,
  54.     NULL, NULL
  55.   };
  56.  
  57. struct NewWindow my_window =
  58.   {
  59.     0, 0, WIDTH, HEIGHT,
  60.     (UBYTE) 0, (UBYTE) 1,
  61.     MOUSEBUTTONS
  62.        | INTUITICKS,
  63.     SMART_REFRESH
  64.        | BORDERLESS
  65.        | BACKDROP
  66.        | ACTIVATE
  67.        | NOCAREREFRESH
  68.        | REPORTMOUSE,
  69.     NULL, NULL,
  70.     NULL,
  71.     NULL, NULL,
  72.     0, 0, 0, 0,
  73.     CUSTOMSCREEN
  74.   };
  75.  
  76.  
  77. /*
  78.   Clean up and exit
  79. */
  80. void CloseStuff()
  81.   {
  82.     struct Library *result;
  83.       if (window)             CloseWindow(window);
  84.       if (screen)             CloseScreen(screen);
  85.       if (HamePort)           HAME_Dispose(HamePort);
  86.       if (HamePort2)          HAME_Dispose(HamePort2);
  87.       if (HameBase)           CloseLibrary(HameBase);
  88.       if (GfxBase)            CloseLibrary(GfxBase);
  89.       if (IntuitionBase)      CloseLibrary(IntuitionBase);
  90.  
  91.  
  92. /* This sequence expunges the library if it is no longer in use */
  93.       Forbid();
  94.       if ( (result = (struct Library *) 
  95.            FindName(&SysBase->LibList,"hame.library")) != NULL ) 
  96.         {
  97.           RemLibrary(result);
  98.         }
  99.       Permit();
  100.  
  101.       exit(0);
  102.   }
  103.  
  104. /*
  105.   Draw a random colored, random sized ellipse at a random location
  106. */
  107. void DrawOval()
  108.   {
  109.   int cc, x, y, xr, yr, c, f;
  110.  
  111.   x = rand() % 320;
  112.   y = rand() % YSIZ;
  113.   xr = rand() % 140;
  114.   yr = rand() % 50;
  115.   f  = rand() % 2;
  116.  
  117.   while (!(c =  rand() % 256));
  118.  
  119.   HAME_SetAPen( HamePort, c );
  120.   HAME_Ellipse(HamePort, x, y, xr, yr, f );
  121.  
  122.   HAME_SetAPen( HamePort2, c );
  123.   HAME_Ellipse(HamePort2, x, YSIZ-y, xr, yr, f );
  124.  
  125.   if (f)
  126.     {
  127.       if ( (cc = HAME_ReadPixel(HamePort, x, y)) != c )
  128.         {
  129.           printf("EL wrote %ld read %ld at %ld,%ld\n", c, cc, x, y);
  130.         }
  131.     }
  132.   }
  133.  
  134. /*
  135.   Draw a random colored line in a random spot
  136. */
  137. void DrawLine()
  138.   {
  139.   int cc, x1, y1, x2, y2, c;
  140.  
  141.   if (rand() & 1)  /* Vertical line */
  142.     {
  143.       x1 = rand() % 320;
  144.       x2 = rand() % 320;
  145.       y1 = rand() % YSIZ;
  146.       y2 = y1;
  147.     }
  148.   else             /* Horizontal line */
  149.     {
  150.       x1 = rand() % 320;
  151.       y1 = rand() % YSIZ;
  152.       x2 = x1;
  153.       y2 = rand() % YSIZ;
  154.     }
  155.  
  156.   /* any color but color 0 */
  157.   while (!(c =  rand() % 256));
  158.  
  159.   HAME_SetAPen( HamePort, c );
  160.   HAME_Line(HamePort, x1, y1, x2, y2 );
  161.  
  162.   HAME_SetAPen( HamePort2, c );
  163.   HAME_Line(HamePort2, x1, YSIZ-y1, x2, YSIZ-y2 );
  164.  
  165.   if ( (cc = HAME_ReadPixel(HamePort, x1, y1)) != c )
  166.     {
  167.       printf("LN wrote %ld read %ld at %ld,%ld\n", c, cc, x1, y1);
  168.     }
  169.   }
  170.  
  171. /*
  172.   Change a random color register to random colors
  173. */
  174. void DoCREG()
  175.   {
  176.   int reg, r, g, b;
  177.  
  178.   while (!(reg =  rand() % 256));
  179.   r = rand() % 256;
  180.   g = rand() % 256;
  181.   b = rand() % 256;
  182.  
  183.   HAME_SetRGB8(HamePort, reg, r, g, b );
  184.   HAME_GetRGB8(HamePort, reg );
  185.   if ( HamePort->r != r ) printf(".R1.");
  186.   if ( HamePort->g != g ) printf(".G1.");
  187.   if ( HamePort->b != b ) printf(".B1.");
  188.  
  189.   HAME_SetRGB8(HamePort2, reg, r, g, b );
  190.   HAME_GetRGB8(HamePort2, reg );
  191.   if ( HamePort2->r != r ) printf(".R2.");
  192.   if ( HamePort2->g != g ) printf(".G2.");
  193.   if ( HamePort2->b != b ) printf(".B2.");
  194.   }
  195.  
  196. /*
  197.   Change a random color register to random colors
  198. */
  199. void DoCycle()
  200.   {
  201.   int r1, r2, t;
  202.  
  203.   while(!(r1 = rand() % 256));
  204.   while(!(r2 = rand() % 240));
  205.   if ( r2 == r1 ) r2 += 8;
  206.  
  207.   if (r2 < r1)
  208.     {
  209.       t = r1; r1 = r2; r2 = t;
  210.     }
  211.  
  212.   if ( rand() & 1)
  213.     {
  214.       HAME_CycleLeft(HamePort, r1, r2);
  215.       HAME_CycleLeft(HamePort2, r1, r2);
  216.     }
  217.   else
  218.     {
  219.       HAME_CycleRight(HamePort, r1, r2);
  220.       HAME_CycleRight(HamePort2, r1, r2);
  221.     }
  222.   }
  223.  
  224. /*
  225.   Draw a random colored box in a random location
  226. */
  227. void DrawBox()
  228.   {
  229.   int x1, y1, x2, y2, c, f;
  230.  
  231.   x1 = rand() % 320;
  232.   x2 = rand() % 320;
  233.  
  234.   y1 = rand() % YSIZ;
  235.   y2 = rand() % YSIZ;
  236.  
  237.   f  = rand() % 2;
  238.  
  239.   while (!(c =  rand() % 256));
  240.  
  241.   HAME_SetAPen( HamePort, c );
  242.   HAME_Box(HamePort, x1, y1, x2, y2, f );
  243.   HAME_SetAPen( HamePort2, c );
  244.   HAME_Box(HamePort2, x1, YSIZ-y1, x2, YSIZ-y2, f );
  245.   }
  246.  
  247.  
  248. /*
  249.   Decide which random object to draw
  250. */
  251. void DrawThing()
  252.  {
  253.  int thing;
  254.  
  255.  thing = rand() % 5;
  256.  switch(thing)
  257.   {
  258.    case 0:
  259.     DoCREG();
  260.    break;
  261.  
  262.    case 1:
  263.     DrawLine();
  264.    break;
  265.  
  266.    case 2:
  267.     DrawBox();
  268.    break;
  269.  
  270.    case 3:
  271.     DrawOval();
  272.    break;
  273.  
  274.    case 4:
  275.     DoCycle();
  276.    break;
  277.  
  278.    default:
  279.    break;
  280.   }
  281.  }
  282.  
  283. /*
  284.   Draw a vertical column of pixels at the left margin to prevent hame from
  285.   turning off.
  286. */
  287. void DrawLeftMargin()
  288.   {
  289.   int i;
  290.  
  291.   HAME_SetAPen( HamePort, 1 );
  292.   HAME_SetAPen( HamePort2, 2 );
  293.   for ( i = 0; i < YSIZ; i++ )
  294.     {
  295.       HAME_WritePixel( HamePort, 0, i);
  296.       HAME_WritePixel( HamePort2, 0, i);
  297.     }
  298.   }
  299.  
  300. /*
  301.   Initialize the pallette to random colors
  302. */
  303. void palette1()
  304.   {
  305.   int i, r, g, b;
  306.  
  307.   for ( i = 1; i < 256; i++ )
  308.     {
  309.       r = rand() & 0xff;
  310.       g = rand() & 0xff;
  311.       b = rand() & 0xff;
  312.       HAME_SetRGB8(HamePort, i, r, g, b);
  313.       HAME_SetRGB8(HamePort2, i, r, g, b);
  314.     }
  315.   }
  316.  
  317. extern struct View *ViewAddress();
  318.  
  319. /*
  320.   Main routine
  321. */
  322. int main(argc, argv)
  323.   int argc;
  324.   char *argv[];
  325.   {
  326.   ULONG  msg_class;
  327.   USHORT msg_code;
  328.   int    finished=0;
  329.   struct IntuiMessage *message;
  330.   struct Gadget *gadget;
  331.  
  332.  
  333.   if ((IntuitionBase = (struct IntuitionBase *)
  334.        OpenLibrary("intuition.library",33L))==NULL)
  335.     {
  336.       CloseStuff();
  337.     }
  338.  
  339.   if ((GfxBase = (struct GfxBase *)
  340.        OpenLibrary("graphics.library" , 0L))==NULL)
  341.     {
  342.       CloseStuff();
  343.     }
  344.  
  345.   /* Open the hame library */
  346.   if ((HameBase = (struct Library *) 
  347.        OpenLibrary("hame.library",HAMELIB_VERSION)) == NULL)
  348.     {
  349.       printf("No hame.library\n");
  350.       CloseStuff();
  351.     }
  352.  
  353.  
  354.   /* Get a screen to play with */
  355.   if ((screen = OpenScreen(&my_screen))     ==NULL)
  356.     {
  357.       CloseStuff();
  358.     }
  359.  
  360.   my_window.Screen = screen;
  361.  
  362.   /* Get a window to play with */
  363.   if ((window = OpenWindow(&my_window)) == NULL)
  364.     {
  365.       CloseStuff();
  366.     }
  367.  
  368.   /* turn off the title bar */
  369.   ShowTitle(screen, 0L);
  370.  
  371.   /* Tell the library where to put pixels */
  372.   if ((HamePort = HAME_Init(screen, 640, 400, 400, 0, 0, 4, YSIZ)) == NULL)
  373.     {
  374.       CloseStuff();
  375.     }
  376.  
  377.   /* Set up a second screen region */
  378.   if ((HamePort2 = HAME_Init(screen, 640, 400, 400, 0, 200, 4, YSIZ)) == NULL)
  379.     {
  380.       CloseStuff();
  381.     }
  382.  
  383.   /* Set the pallette */
  384.   palette1();
  385.  
  386.   /* Keep hame from turning off because of screen wide color 0 */
  387.   DrawLeftMargin();
  388.  
  389.   while( !finished )
  390.     {
  391. /*
  392.       message = (struct IntuiMessage *) WaitPort(window->UserPort);
  393. */
  394.       message = (struct IntuiMessage *) GetMsg(window->UserPort);
  395.       if (message)
  396.         {
  397.           msg_class = message->Class;
  398.           msg_code = message->Code;
  399.           gadget = (struct Gadget *) message->IAddress;
  400.           ReplyMsg((struct Message *)message);
  401.  
  402.           switch( msg_class )
  403.             {
  404.               case MOUSEBUTTONS:
  405.                 finished = 1;
  406.               break;
  407.  
  408.               case MENUPICK:
  409.               break;
  410.  
  411.               case GADGETUP:
  412.               break;
  413.  
  414.               case INTUITICKS:
  415.                 /* Draw something in HAME mode */
  416.                 DrawThing();
  417.               break;
  418.  
  419.               case CLOSEWINDOW:
  420.                 finished = 1;
  421.               break;
  422.  
  423.               default:
  424.                 printf("?");
  425.             }
  426.         }
  427.       else
  428.         {
  429.         DrawThing();
  430.         }
  431.      }
  432.  
  433.   while ((message = (struct IntuiMessage *)
  434.           GetMsg( window->UserPort )) != NULL)
  435.     {
  436.       ReplyMsg((struct Message *)message);
  437.     }
  438.  
  439.   CloseStuff();
  440. }
  441.  
  442.