home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / superdark / blankers / aswarm.dark.c < prev    next >
C/C++ Source or Header  |  1993-03-20  |  12KB  |  462 lines

  1.   /* Fichier Aswarm.dark.c                */
  2. /* Module appele par le programme principal    */
  3. /* pour effectuer un effet defini..appele par    */
  4. /* loadseg                    */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <exec/ports.h>
  9. #include <exec/execbase.h>
  10. #include <graphics/displayinfo.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/gfxmacros.h>
  13. #include <intuition/intuitionbase.h>
  14. #include <intuition/gadgetclass.h>
  15.  
  16. #include <clib/exec_protos.h>
  17. #include <clib/graphics_protos.h>
  18. #include <clib/intuition_protos.h>
  19. #include <clib/macros.h>
  20.  
  21. #include "/includes/struct.h"
  22. #include "/includes/tom_gadget.h"
  23. #include <string.h>
  24. #include <stdlib.h>
  25.  
  26. char    *name_module="Aswarm.dark";
  27. char    *p_text_info="             ASWarm\n\nBased on a programm from\nMarkus 'Ill' Illenseer\nand Matthias 'tron' Scheler\nAdapted by T.Landspurg\n\nBased on Sun's XSwarm";
  28. extern    struct    GfxBase    *GfxBase;
  29.  
  30. #define MAX_SPEED     4L
  31. #define MAX_WASPS     10L
  32. #define MAX_BEES      500L
  33. #define MAX_TIGHTNESS 10L
  34.  
  35. #define DEF_SPEED     4L
  36. #define DEF_WASPS     2L
  37. #define DEF_BEES      25L
  38. #define DEF_TIGHTNESS 5L
  39.  
  40. #define    ID_SPEED    0
  41. #define    ID_WASPS    1
  42. #define    ID_BEES        2
  43. #define    ID_TIGHTNESS    3
  44. #define ID_CYCLE    4
  45. #define ID_AIM        5
  46.  
  47. char *SpeedNames[] = /* List of Speed Modes */
  48.  {"Slow Motion","Very Slow","Slow","Normal","Fast","Very Fast",
  49.   "Incredible",NULL};
  50.  
  51. struct    tom_gadget    my_gadg[]={
  52.     {"_Speed",    CYCLE,  90, 30,   120, 12,DEF_SPEED,1,MAX_SPEED,0,(char *)SpeedNames},
  53.     {"_Wasps",    SLIDER,  90, 50,   120, 10,DEF_WASPS,1,MAX_WASPS,0,0},
  54.     {"_Bees",    SLIDER,  90, 70,   120, 10,DEF_BEES  ,1,MAX_BEES,0,0},
  55.     {"Tig_htness",    SLIDER,  90, 90,   120, 10,DEF_TIGHTNESS,1,MAX_TIGHTNESS,0,0},
  56.     {"Color c_ycling",CHECKBOX,380,50,   10, 10,0,0,0,0},
  57.     {"_Aim mode"    ,CHECKBOX,380,70,    10, 10,0,0,0,0},
  58.     {"_Resolution",    SCREEN,     90,110,120,12,0, 0,0,0,0},
  59.     {0,        END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}};
  60.  
  61. LONG Index,NumWasps,NumBees,Speed,Cycle,AimMode,Count,Tightness;
  62.  
  63. #define VOID_INTERRUPT void __interrupt __saveds
  64. #define REGARGS        __regargs
  65. #define VOID_STDARGS   void __stdargs
  66.  
  67.  
  68. /*
  69.  
  70.    Definitions or Swarm Movement
  71.  
  72. */
  73.  
  74. #define BEEACC  3
  75. #define BEEVEL  9
  76. #define WASPACC 5
  77. #define WASPVEL 13
  78. #define BORDER  5
  79.  
  80. #define FIXED    0
  81.  
  82. #define BEE_COL_NUM 32
  83.  
  84. UWORD BeeColors[BEE_COL_NUM] = /* Color Cycling Table */
  85.  {0x000F,0x004F,0x008F,0x00BF,0x00FF,0x00FB,0x00F7,0x00F3,0x00F0,0x04F0,0x08F0,
  86.   0x09F0,0x0AF0,0x0BF0,0x0CF0,0x0DF0,0x0EF0,0x0FF0,0x0FE0,0x0FD0,0x0FC0,0x0FB0,
  87.   0x0F90,0x0F80,0x0F70,0x0F60,0x0F50,0x0F40,0x0F30,0x0F20,0x0F10,0x0F00};
  88.  
  89. struct NewScreen NewBlankerScreen =
  90.  {0,0,640,256,2,0,1,HIRES,CUSTOMSCREEN,
  91.   NULL,NULL,NULL,NULL};
  92.  
  93. #define RAND(m) (Random(m)-(m)/2)
  94.  
  95. UWORD SwarmColors[4] = {0x0000,0x0FFF,0x0000,0x0FFF};
  96. LONG NumWasps,NumBees,Speed,Tightness;
  97. LONG Cycle,AimMode;
  98. LONG ThisTimeOut,TimeUntilBlank;
  99. UWORD    width,height;
  100. struct SwarmStruct /* structure for a swarm, including the wasp */
  101.  {
  102.   WORD ss_Width;   /* Width and */
  103.   WORD ss_Height;  /* Height of the used Screen, probably useful for
  104.                       future versions of ASwarm :) */
  105.   WORD ss_NumWasps;/* total number of the Wasps */
  106.   WORD *ss_WX[4];  /* The Wasps x-Position*/  /* WX[3] is used for Velocity */
  107.   WORD *ss_WY[4];  /* Y-Position */
  108.   WORD *ss_NB;     /* No. of Bees following this Wasp */
  109.   WORD ss_NumBees; /* Total (!) number of Bees */
  110.   WORD ss_BeeAcc;  /* Acceleration of the Bees */
  111.   WORD *ss_X[4];   /* The Bees X-Position */  /* X[3] used for Velocity */
  112.   WORD *ss_Y[4];   /* Y-Position */
  113.   WORD *ss_MW;     /* The aimed Wasp */
  114.  };
  115.  
  116. /* Ill's strange Macros for easy access to the above structure */
  117.  
  118. #define BXVel(I)   (SP->ss_X[3][I])
  119. #define BYVel(I)   (SP->ss_Y[3][I])
  120. #define BeeX(P,I)  (SP->ss_X[P][I])
  121. #define BeeY(P,I)  (SP->ss_Y[P][I])
  122. #define MyWasp(I)  (SP->ss_MW[I])
  123.  
  124. #define WaXVel(I)  (SP->ss_WX[3][I])
  125. #define WaYVel(I)  (SP->ss_WY[3][I])
  126. #define WaspX(P,I) (SP->ss_WX[P][I])
  127. #define WaspY(P,I) (SP->ss_WY[P][I])
  128.  
  129. /*
  130.  
  131.    Functions for Creating/Drawing/Removing the Swarms
  132.  
  133. */
  134.  
  135. /* Ill's strange and genius Random Function :-) */
  136.  
  137. WORD REGARGS Random(WORD Max)
  138.  
  139. {
  140.  static ULONG Num=0L; /* So the last random-number is still stored ! */
  141.  ULONG Sec,Mic;
  142.  
  143.  CurrentTime((LONG *)&Sec,(LONG *)&Mic);
  144.  
  145.  Num*=Sec;
  146.  Num+=Mic;
  147.  
  148.  while (Num>32767L) Num=Num>>1;
  149.  
  150.  return (WORD)Num%Max;
  151. }
  152.  
  153. ULONG SwarmSize(LONG NumWaps,LONG NumBees)
  154.  
  155. {
  156.  return sizeof(struct SwarmStruct)+sizeof(WORD)*((ULONG)NumWaps*9L+
  157.                                    (ULONG)NumBees*9L);
  158. }
  159.  
  160. struct SwarmStruct *REGARGS CreateSwarms(struct Screen *SwarmScreen,
  161.                           LONG NumWasps,LONG NumBees,LONG Speed,LONG Tightness)
  162.  
  163. /* allocate Memory and initialize the Swarm(s) */
  164.  
  165. {
  166.  LONG Index;
  167.  struct SwarmStruct *SP;
  168.  WORD *Ptr;
  169.  
  170.  if ((SP=AllocMem(SwarmSize(NumWasps,NumBees),0L))==NULL) return NULL;
  171.  
  172.  SP->ss_NumWasps=NumWasps;
  173.  SP->ss_NumBees=NumBees;
  174.  SP->ss_Width=SwarmScreen->Width;    /* Will probably be changed in a */
  175.  SP->ss_Height=SwarmScreen->Height;  /* future version of ASwarm */
  176.  SP->ss_BeeAcc=(Tightness*BEEACC/Speed/MAX_SPEED)*(MAX_SPEED/2)+1;
  177.  
  178.  Ptr=(WORD *)&SP[1];
  179.  for (Index=0L; Index<4L; Index++)
  180.   {
  181.    SP->ss_WX[Index]=Ptr;
  182.    Ptr+=NumWasps;
  183.    SP->ss_WY[Index]=Ptr;
  184.    Ptr+=NumWasps;
  185.    SP->ss_X[Index]=Ptr;
  186.    Ptr+=NumBees;
  187.    SP->ss_Y[Index]=Ptr;
  188.    Ptr+=NumBees;
  189.   }
  190.  SP->ss_NB=Ptr;
  191.  SP->ss_MW=Ptr+NumWasps;
  192.  
  193.  /* Wasps */
  194.  for (Index=0L; Index<NumWasps; Index++)
  195.   {
  196.    WaspX(1,Index)=WaspX(0,Index)=BORDER+Random(SP->ss_Width-2*BORDER);
  197.    WaspY(1,Index)=WaspY(0,Index)=BORDER+Random(SP->ss_Height-2*BORDER);
  198.    WaXVel(Index)=RAND(WASPACC);
  199.    WaYVel(Index)=RAND(WASPACC);
  200.    SP->ss_NB[Index]=0;
  201.   }
  202.  
  203.  /* Bees */
  204.  for (Index=0L; Index<SP->ss_NumBees; Index++)
  205.   {
  206.    BeeX(1,Index)=BeeX(0,Index)=BORDER+Random(SP->ss_Width-2*BORDER);
  207.    BeeY(1,Index)=BeeY(0,Index)=BORDER+Random(SP->ss_Height-2*BORDER);
  208.    BXVel(Index)=RAND(SP->ss_BeeAcc);
  209.    BYVel(Index)=RAND(SP->ss_BeeAcc);
  210.    SP->ss_NB[MyWasp(Index)=Index%SP->ss_NumWasps]++;
  211.   }
  212.  
  213.  return SP;
  214. }
  215.  
  216. /* move Swarms and redraw it */
  217.  
  218. void REGARGS DrawSwarms(struct SwarmStruct *SP,LONG AimMode,
  219.                         struct RastPort *WaspRP,struct RastPort *BeeRP)
  220.  
  221. {
  222.  LONG Index;
  223.  
  224.  /* Wasps */
  225.  
  226.  for (Index=0L; Index<SP->ss_NumWasps; Index++)
  227.   {
  228.    WaspX(2,Index)=WaspX(1,Index);
  229.    WaspX(1,Index)=WaspX(0,Index);
  230.    WaspY(2,Index)=WaspY(1,Index);
  231.    WaspY(1,Index)=WaspY(0,Index);
  232.  
  233.    WaXVel(Index)+=RAND(WASPACC);
  234.    WaYVel(Index)+=RAND(WASPACC);
  235.  
  236.    if (WaXVel(Index)>WASPVEL) WaXVel(Index)=WASPVEL;
  237.    if (WaXVel(Index)<-WASPVEL) WaXVel(Index)=-WASPVEL;
  238.    if (WaYVel(Index)>WASPVEL) WaYVel(Index)=WASPVEL;
  239.    if (WaYVel(Index)<-WASPVEL) WaYVel(Index)=-WASPVEL;
  240.  
  241.    WaspX(0,Index)=WaspX(1,Index)+WaXVel(Index);
  242.    WaspY(0,Index)=WaspY(1,Index)+WaYVel(Index);
  243.  
  244.    /* Bounce check for Wasps */
  245.  
  246.    if ((WaspX(0,Index)<BORDER)||(WaspX(0,Index)>SP->ss_Width-BORDER-1))
  247.     {
  248.      WaXVel(Index)=-WaXVel(Index);
  249.      if (WaspX(0,Index)<BORDER) WaspX(0,Index)=BORDER;
  250.      else WaspX(0,Index)=SP->ss_Width-BORDER-1;
  251.     }
  252.    if ((WaspY(0,Index)<BORDER)||(WaspY(0,Index)>SP->ss_Height-BORDER-1))
  253.     {
  254.      WaYVel(Index)=-WaYVel(Index);
  255.      if (WaspY(0,Index)<BORDER) WaspY(0,Index)=BORDER;
  256.      else WaspY(0,Index)=SP->ss_Height-BORDER-1;
  257.     }
  258.   }
  259.  
  260.  /* Bees */
  261.  
  262.  for (Index=0L; Index<SP->ss_NumBees; Index++)
  263.   {
  264.    WORD DX,DY,ChkIndex;
  265.    LONG Distance,NewDistance;
  266.  
  267.    BeeX(2,Index)=BeeX(1,Index);
  268.    BeeX(1,Index)=BeeX(0,Index);
  269.    BeeY(2,Index)=BeeY(1,Index);
  270.    BeeY(1,Index)=BeeY(0,Index);
  271.  
  272.    DX=WaspX(1,MyWasp(Index))-BeeX(1,Index);
  273.    DY=WaspY(1,MyWasp(Index))-BeeY(1,Index);
  274.    Distance=(ABS(DX)+ABS(DY))/2L;
  275.    if (Distance==0L) Distance=1L;
  276.  
  277.    if (AimMode) /* Look out for the nearest wasp if Aim-Mode is on */
  278.     for (ChkIndex=0; ChkIndex<=SP->ss_NumWasps; ChkIndex++)
  279.      if (ChkIndex!=MyWasp(Index))
  280.       {
  281.        LONG NewDX,NewDY;
  282.  
  283.        NewDX=WaspX(1,ChkIndex)-BeeX(1,Index);
  284.        NewDY=WaspY(1,ChkIndex)-BeeY(1,Index);
  285.        NewDistance=(ABS(NewDX)+ABS(NewDY))/2L;
  286.        if (NewDistance==0L) NewDistance=1L;
  287.        if (Distance>NewDistance)
  288.         {
  289.          DX=NewDX;
  290.          DY=NewDY;
  291.          Distance=NewDistance;
  292.          SP->ss_NB[MyWasp(Index)]--;
  293.          SP->ss_NB[MyWasp(Index)=ChkIndex]++; /* Mark a nearer Wasp */
  294.         }
  295.       }
  296.  
  297.    BXVel(Index)+=(DX*SP->ss_BeeAcc)/Distance+RAND(3);
  298.    BYVel(Index)+=(DY*SP->ss_BeeAcc)/Distance+RAND(3);
  299.  
  300.    if (BXVel(Index)>BEEVEL)  BXVel(Index)=BEEVEL;
  301.    if (BXVel(Index)<-BEEVEL) BXVel(Index)=-BEEVEL;
  302.    if (BYVel(Index)>BEEVEL)  BYVel(Index)=BEEVEL;
  303.    if (BYVel(Index)<-BEEVEL) BYVel(Index)=-BEEVEL;
  304.  
  305.    BeeX(0,Index)=BeeX(1,Index)+BXVel(Index);
  306.    BeeY(0,Index)=BeeY(1,Index)+BYVel(Index);
  307.  
  308.   /* Bounce check for Bees */
  309.  
  310.    if ((BeeX(0,Index)<BORDER)||(BeeX(0,Index)>(SP->ss_Width-BORDER-1)))
  311.     {
  312.      BXVel(Index)=-BXVel(Index);
  313.      BeeX(0,Index)=BeeX(1,Index)+BXVel(Index);
  314.     }
  315.    if ((BeeY(0,Index)<BORDER)||(BeeY(0,Index)>(SP->ss_Height-BORDER-1)))
  316.     {
  317.      BYVel(Index)=-BYVel(Index);
  318.      BeeY(0,Index)=BeeY(1,Index)+BYVel(Index);
  319.     }
  320.   }
  321.  
  322.  
  323.  /* Move our insects */
  324.  
  325.  for(Index=0L; Index<SP->ss_NumWasps; Index++)    /* Wasps */
  326.   {
  327.    SetAPen (WaspRP,0);
  328.    Move (WaspRP,WaspX(2,Index),WaspY(2,Index));
  329.    Draw (WaspRP,WaspX(1,Index),WaspY(1,Index));
  330.    SetAPen (WaspRP,1);
  331.    Draw (WaspRP,WaspX(0,Index),WaspY(0,Index));
  332.   }
  333.  
  334.  for (Index=0L; Index<SP->ss_NumBees; Index++)   /* Bees  */
  335.   {
  336.    SetAPen (BeeRP,0);
  337.    Move (BeeRP,BeeX(2,Index),BeeY(2,Index));
  338.    Draw (BeeRP,BeeX(1,Index),BeeY(1,Index));
  339.    SetAPen (BeeRP,1);
  340.    Draw (BeeRP,BeeX(0,Index),BeeY(0,Index));
  341.   }
  342. }
  343.  
  344.  
  345. struct Screen *CreateScreen()
  346.  
  347. {
  348.  struct Screen *Screen;
  349.  struct    tom_gadget    *pg;
  350.  
  351.   pg=&my_gadg[6];
  352.   /* With WB2.0 or higher, we use facilities of the intuition lib. */
  353.   /* to open screen (overscan...)                       */
  354.   if(pg->d3<2){
  355.     pg->d3=2;
  356.   }
  357.   if((GfxBase->LibNode.lib_Version>=37)&&(pg->d3!=0)){
  358.     Screen=OpenScreenTags(NULL,
  359.                SA_Width,(ULONG)pg->d1,
  360.                SA_Height,(ULONG)pg->d2,
  361.                            SA_Depth,(ULONG)pg->d3,
  362.                SA_Overscan,(ULONG)pg->value,
  363.                            SA_DisplayID,(ULONG)pg->p_data,
  364.                            SA_Quiet,TRUE,TAG_DONE);
  365.      width=pg->d1;
  366.      height=pg->d2;
  367.    }else{
  368.     Screen=OpenScreen(&NewBlankerScreen);
  369.    }
  370.    if(Screen){
  371.     SetRGB4 (&Screen->ViewPort,0,0,0,0);
  372.     SetRast (&Screen->RastPort,0);
  373.    }
  374.    return Screen;
  375. }
  376.  
  377. /* Put here your own code */
  378. /* But don't remember     */
  379.  
  380. void    __saveds    dark()
  381. {
  382.     struct Screen *SwarmScreen;
  383.     struct SwarmStruct *Swarms;
  384.     WORD Color,DColor;
  385.     static struct RastPort RastPorts[2];
  386.     static struct BitMap BitMaps[2];
  387.  
  388.     NumWasps=my_gadg[ID_WASPS].value;
  389.     NumBees=my_gadg[ID_BEES].value;
  390.     Speed=my_gadg[ID_SPEED].value+1;
  391.     Cycle=my_gadg[ID_CYCLE].value;
  392.     AimMode=my_gadg[ID_AIM].value;
  393.     Tightness=my_gadg[ID_TIGHTNESS].value;
  394.  
  395.  SwarmScreen=CreateScreen();
  396.  if(SwarmScreen!=NULL){
  397.     FreeSprite (0);
  398.     GfxBase->SpriteReserved|=1;
  399.  /* initialize requested Number of Swarms */
  400.  
  401.  if ((Swarms=CreateSwarms(SwarmScreen,NumWasps,NumBees,Speed,Tightness))!=NULL)
  402.   {
  403.  /* Here we do a little trick:
  404.    We create two RastPorts - one for the Wasp(s), one for the Bees - with one
  405.    BitPlane. So we have only to draw in one BitPlane and are able to do it
  406.    faster. */
  407.  
  408.  for (Index=0L; Index<2L; Index++)
  409.   {
  410.    InitRastPort (&RastPorts[Index]);
  411.    RastPorts[Index].BitMap=&BitMaps[Index];
  412.    SetDrMd (&RastPorts[Index],JAM1);
  413.    InitBitMap (&BitMaps[Index],1L,SwarmScreen->Width,SwarmScreen->Height);
  414.    BitMaps[Index].Planes[0]=SwarmScreen->BitMap.Planes[Index];
  415.   }
  416.  
  417.  Color=BEE_COL_NUM-1;
  418.  DColor=Cycle?1:0;
  419.  Count=Speed;
  420.  
  421.     while(tst_end()==FALSE){
  422.    /* Color Cycling */
  423.  
  424.    SwarmColors[2]=BeeColors[Color];
  425.    LoadRGB4 (&SwarmScreen->ViewPort,SwarmColors,4);
  426.    Color+=DColor;
  427.    if ((Color==-1)||(Color==BEE_COL_NUM))
  428.     {
  429.      DColor=-DColor;
  430.      Color+=2*DColor;
  431.     }
  432.  
  433.    /* Synchronisation */
  434.  
  435.    WaitTOF();
  436.    if (Count<MAX_SPEED)
  437.     {
  438.      Count++;
  439.      continue;
  440.     }
  441.    Count=Speed;
  442.  
  443.    /* Move the Swarm(s) */
  444.  
  445.    DrawSwarms (Swarms,AimMode,&RastPorts[0],&RastPorts[1]);
  446.   }
  447.  FreeMem (Swarms,SwarmSize(NumWasps,NumBees));
  448.  }
  449.  CloseScreen(SwarmScreen);
  450.  }
  451. }
  452. void    proc_init()
  453. {
  454. }
  455.  
  456. void    proc_save()
  457. {
  458. }
  459. void    proc_end()
  460. {
  461. }
  462.