home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / gfxcard / rainbow_system / developer / demo.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  11KB  |  319 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <intuition/intuition.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <math.h>
  9.  
  10. #include <proto/dos.h>
  11. #include <proto/exec.h>
  12. #include <proto/graphics.h>
  13. #include <proto/intuition.h>
  14.  
  15. #include <proto/RainbowSystem.h>
  16.  
  17. struct RainbowSystemBase *RainbowSystemBase;
  18.  
  19. void rs_DrawHSBSpace(APTR, UWORD, UWORD, UWORD, UWORD );
  20.  
  21. static void ErrorMessage(STRPTR text)
  22. {
  23.     struct EasyStruct easy;
  24.  
  25.     easy.es_StructSize=sizeof(struct EasyStruct);
  26.     easy.es_Flags=0;
  27.     easy.es_Title="Rainbow Demo";
  28.     easy.es_TextFormat=text;
  29.     easy.es_GadgetFormat="Ok";
  30.  
  31.     EasyRequestArgs(NULL,&easy,NULL,NULL);
  32. }
  33.  
  34. int main(int argc, char **argv )
  35. {
  36.     LONG error_code;
  37.     char scr[256];
  38.     APTR rainbow;
  39.  
  40.     if (argc == 2)  strcpy(scr,argv[1]);
  41.     else            strcpy(scr,"Workbench");
  42.  
  43.     if (RainbowSystemBase=(struct RainbowSystemBase *)OpenLibrary("rainbow.library",1L)) {
  44.  
  45.         if (rainbow=rs_ObtainScreen(scr,&error_code)) {
  46.  
  47.             ULONG width=256,height=256;
  48.             struct Window *wnd;
  49.  
  50.             if (wnd=OpenWindowTags(NULL,WA_Left,    16,
  51.                                         WA_Top,     rs_GetScreen(rainbow)->WBorTop+rs_GetScreen(rainbow)->RastPort.TxHeight+1+12,
  52.                                         WA_InnerWidth,   width,
  53.                                         WA_InnerHeight,  height,
  54.                                         WA_Title,   "Demo",
  55.                                         WA_IDCMP,   IDCMP_CLOSEWINDOW,
  56.                                         WA_PubScreen,rs_GetScreen(rainbow),
  57.                                         WA_Flags,   WFLG_CLOSEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_SMART_REFRESH,TAG_DONE))
  58.             {
  59.                 const UWORD offx=wnd->BorderLeft,
  60.                       offy=wnd->BorderTop;
  61.                 UWORD x,y;
  62.  
  63.                 if (rs_BeginDraw(rainbow,wnd->RPort))
  64.                 {
  65.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  66.  
  67.                     SetWindowTitles(wnd,"rs_DrawLine()",NULL);
  68.  
  69.                     for (y=0;y<height;y+=16)
  70.                     {
  71.                         rs_DrawLine(rainbow,offx,offy+255-y,offx+y,offy,255,0,0);
  72.                         rs_DrawLine(rainbow,offx+255,offy+y,offx+255-y,offy+255,0,255,0);
  73.                     }
  74.  
  75.                     Delay(100);
  76.  
  77.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  78.  
  79.                     SetWindowTitles(wnd,"rs_DrawCircle()",NULL);
  80.  
  81.                     for (y=0;y<4;y++)
  82.                         for (x=0;x<4;x++)
  83.                         {
  84.                             rs_DrawCircle(rainbow,offx+31+64*x,offy+31+64*y,31,63+y*64,63+x*64,255);
  85.                         }
  86.  
  87.                     Delay(100);
  88.  
  89.                     SetWindowTitles(wnd,"rs_FillCircle()",NULL);
  90.  
  91.                     for (y=0;y<4;y++)
  92.                         for (x=0;x<4;x++)   rs_FillCircle(rainbow,offx+31+64*x,offy+31+64*y,31,RGB(63+y*64,63+x*64,255),RGB_RED);
  93.  
  94.                     Delay(100);
  95.  
  96.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  97.  
  98.                     SetWindowTitles(wnd,"rs_DrawPolygon()",NULL);
  99.  
  100.                     for (y=0;y<4;y++)
  101.                         for (x=0;x<4;x++)
  102.                         {
  103.                             WORD array[8];
  104.  
  105.                             array[0]=offx+x*64+8; array[1]=offy+y*64+10;
  106.                             array[2]=offx+x*64+54; array[3]=offy+y*64+40;
  107.                             array[4]=offx+x*64+15; array[5]=offy+y*64+60;
  108.                             array[6]=array[0];     array[7]=array[1];
  109.  
  110.                             rs_DrawPolygon(rainbow,4,array,255,63+x*64,63+y*64);
  111.                         }
  112.  
  113.                     Delay(100);
  114.  
  115.                     SetWindowTitles(wnd,"rs_FillPolygon()",NULL);
  116.  
  117.                     for (y=0;y<4;y++)
  118.                         for (x=0;x<4;x++)
  119.                         {
  120.                             WORD array[6];
  121.  
  122.                             array[0]=offx+x*64+8;   array[1]=offy+y*64+10;
  123.                             array[2]=offx+x*64+54;  array[3]=offy+y*64+40;
  124.                             array[4]=offx+x*64+15;  array[5]=offy+y*64+60;
  125.  
  126.                             rs_FillPolygon(rainbow,3,array,RGB(255,63+x*64,63+y*64),RGB_NONE);
  127.                         }
  128.  
  129.                     Delay(100);
  130.  
  131.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  132.  
  133.                     SetWindowTitles(wnd,"rs_FillRectangle()",NULL);
  134.  
  135.                     for (y=0;y<256;y+=16)   rs_FillRectangle(rainbow,offx+y,offy+y,255-y,255-y,RGB(255,y,y),RGB_NONE);
  136.  
  137.                     Delay(100);
  138.  
  139.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  140.  
  141.                     SetWindowTitles(wnd,"rs_FadeHorizontal()",NULL);
  142.  
  143.                     rs_FadeHorizontal(rainbow,offx,offy,     width,36,RGB_RED,     RGB_WHITE);
  144.                     rs_FadeHorizontal(rainbow,offx,offy+36,  width,36,RGB_GREEN,   RGB_WHITE);
  145.                     rs_FadeHorizontal(rainbow,offx,offy+2*36,width,36,RGB_BLUE,    RGB_WHITE);
  146.                     rs_FadeHorizontal(rainbow,offx,offy+3*36,width,36,RGB_CYAN,    RGB_WHITE);
  147.                     rs_FadeHorizontal(rainbow,offx,offy+4*36,width,36,RGB_MAGENTA, RGB_WHITE);
  148.                     rs_FadeHorizontal(rainbow,offx,offy+5*36,width,36,RGB_YELLOW,  RGB_WHITE);
  149.                     rs_FadeHorizontal(rainbow,offx,offy+6*36,width,40,RGB_BLACK,   RGB_WHITE);
  150.  
  151.                     Delay(100);
  152.  
  153.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  154.  
  155.                     SetWindowTitles(wnd,"rs_FadeVertical()",NULL);
  156.  
  157.                     rs_FadeVertical(rainbow,offx+0*36,offy,36,height,RGB_RED,    RGB_WHITE);
  158.                     rs_FadeVertical(rainbow,offx+1*36,offy,36,height,RGB_GREEN,  RGB_WHITE);
  159.                     rs_FadeVertical(rainbow,offx+2*36,offy,36,height,RGB_BLUE,   RGB_WHITE);
  160.                     rs_FadeVertical(rainbow,offx+3*36,offy,36,height,RGB_CYAN,   RGB_WHITE);
  161.                     rs_FadeVertical(rainbow,offx+4*36,offy,36,height,RGB_MAGENTA,RGB_WHITE);
  162.                     rs_FadeVertical(rainbow,offx+5*36,offy,36,height,RGB_YELLOW, RGB_WHITE);
  163.                     rs_FadeVertical(rainbow,offx+6*36,offy,40,height,RGB_BLACK,  RGB_WHITE);
  164.  
  165.                     Delay(100);
  166.  
  167.                     SetWindowTitles(wnd,"rs_DrawHSBSpace()",NULL);
  168.  
  169.                     rs_DrawHSBSpace(rainbow,offx,offy,width,height);
  170.  
  171.                     Delay(100);
  172.  
  173.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  174.  
  175.                     SetWindowTitles(wnd,"rs_Move() - rs_Draw()",NULL);
  176.  
  177.                     rs_Move(rainbow,offx+width/2-1+(width*5/12),offy+height/2-1);
  178.  
  179.                     for (y=70;y<=2520;y+=70)
  180.                     {
  181.                         float angle=y*PI/180.0;
  182.                         UWORD x0,y0;
  183.  
  184.                         x0=offx+width/2-1+(width*5/12)*cos(angle);
  185.                         y0=offy+height/2-1-(height*5/12)*sin(angle);
  186.  
  187.                         rs_Draw(rainbow,x0,y0,255,255*y/2520,0);
  188.                     }
  189.  
  190.                     Delay(100);
  191.  
  192.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  193.  
  194.                     SetWindowTitles(wnd,"Various",NULL);
  195.  
  196.                     for (y=0;y<40;y++)
  197.                     {
  198.                          UWORD x0=rand()%width,
  199.                                y0=rand()%height,
  200.                                w=rand()%(width-x0),
  201.                                h=rand()%(height-y0);
  202.  
  203.                         rs_FillRectangle(rainbow,offx+x0,offy+y0,w,h,RGB(rand()&0xFF,rand()&0xFF,rand()&0xFF),RGB_NONE);
  204.                     }
  205.  
  206.                     Delay(100);
  207.  
  208.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  209.  
  210.                     for (y=0;y<40;y++)
  211.                     {
  212.                         UWORD x0=rand()%(width>>1),
  213.                               y0=rand()%(height>>1),
  214.                               r0=rand()%(width>>2);
  215.  
  216.                         rs_FillCircle(rainbow,offx+(width>>2)+x0,offy+(height>>2)+y0,r0,RGB(rand()&0xFF,rand()&0xFF,rand()&0xFF),RGB_NONE);
  217.                     }
  218.  
  219.                     Delay(100);
  220.  
  221.                     rs_FillRectangle(rainbow,offx,offy,width,height,RGB_BLACK,RGB_NONE);
  222.  
  223.                     for (y=0;y<40;y++)
  224.                     {
  225.                         WORD array[6];
  226.  
  227.                         array[0]=offx+rand()%width;  array[1]=offy+rand()%height;
  228.                         array[2]=offx+rand()%width;  array[3]=offy+rand()%height;
  229.                         array[4]=offx+rand()%width;  array[5]=offy+rand()%height;
  230.  
  231.                         rs_FillPolygon(rainbow,3,array,RGB(rand()&0xFF,rand()&0xFF,rand()&0xFF),RGB_NONE);
  232.                     }
  233.  
  234.                     rs_EndDraw(rainbow);
  235.                 }
  236.  
  237.                 WaitPort(wnd->UserPort);
  238.  
  239.                 CloseWindow(wnd);
  240.             }
  241.  
  242.             rs_ReleaseScreen(rainbow);
  243.  
  244.         } else switch(error_code) {
  245.  
  246.             case RAINBOWMANAGER_ISNT_ACTIVE:    ErrorMessage("First, you must activate the RainbowManager"); break;
  247.             case NOT_ENOUGH_MEMORY:             ErrorMessage("Not enough memeory"); break;
  248.  
  249.         }
  250.  
  251.         CloseLibrary((struct Library*)RainbowSystemBase);
  252.  
  253.     } else ErrorMessage("First, you must acivate the RainbowManager");
  254.  
  255.     return 0;
  256. }
  257.  
  258. void rs_DrawHSBSpace(APTR rainbow, UWORD xstart, UWORD ystart, UWORD w, UWORD h )
  259. {
  260.     UBYTE *buffer;
  261.  
  262.     if (buffer=AllocVec(3*max(w,16)*h,MEMF_ANY)) {
  263.  
  264.         UWORD www=3*w,x,y;
  265.  
  266.         for (x=0;x<w;x++)
  267.         {
  268.             UWORD hue=(6*(x<<13))/w,f=hue&0x1FFF;
  269.             ULONG xx;
  270.  
  271.             for (xx=3*x,y=0;y<h;y++,xx+=www)
  272.             {
  273.                 UBYTE v=(y<<8)/h,s=255-((v*v)>>8);
  274.                 UBYTE r,g,b;
  275.  
  276.                 #define p1 (v*(255-s))>>8
  277.                 #define p2 (v*(255*8192-s*f))>>21
  278.                 #define p3 (v*(((255-s)<<13)+s*f))>>21
  279.  
  280.                 switch (hue>>13)
  281.                 {
  282.                     case 0:     r=v;
  283.                                 g=p3;
  284.                                 b=p1;
  285.                                 break;
  286.                     case 1:     r=p2;
  287.                                 g=v;
  288.                                 b=p1;
  289.                                 break;
  290.                     case 2:     r=p1;
  291.                                 g=v;
  292.                                 b=p3;
  293.                                 break;
  294.                     case 3:     r=p1;
  295.                                 g=p2;
  296.                                 b=v;
  297.                                 break;
  298.                     case 4:     r=p3;
  299.                                 g=p1;
  300.                                 b=v;
  301.                                 break;
  302.                     case 5:     r=v;
  303.                                 g=p1;
  304.                                 b=p2;
  305.                                 break;
  306.                 }
  307.  
  308.                 buffer[xx]  =r;
  309.                 buffer[xx+1]=g;
  310.                 buffer[xx+2]=b;
  311.             }
  312.         }
  313.  
  314.         rs_WritePixelArray(rainbow,xstart,ystart,w,h,buffer);
  315.  
  316.         FreeVec(buffer);
  317.     }
  318. }
  319.