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