Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

void InitialiseLeafPositions()
{
  int iNumOfColumns = 3;
  int iNumOfRows = 3;
  int i_;
  for ( i_ = 0;i_ < Max_Leaves;++i_)
    {
      mutex_lock(&leaves_mutex[i_]);
      leaves[i_].x0 = (240 * (i_ % iNumOfColumns ) );
      leaves[i_].y0 = (240 + ((i_ / iNumOfRows)  * 120 ));
      mutex_unlock(&leaves_mutex[i_]);
    }

}

TASK Leaf(void* arg)
{
  int iIndex = arg;
  int iLineNumber = iIndex / 3;

  int iDiff;

    if (iLineNumber % 2 )
    {
      iDiff = -5;
    }
    else
    {
      iDiff = 5;
    }

    int oldx = 0,oldy = 0;
    int tempx,tempy;

    while(1)
    {
      tempx = oldx + leaf_image_width;
      tempy = oldy + leaf_image_height;

      mutex_lock(&grx_mutex);

      if (tempx > 800)
        {
           FAB_image_put_within(image_bg,0,0,0,oldy,tempx - 800,tempy);

           tempx = 800;
        }

      FAB_image_put_within(image_bg,0,0,oldx,oldy,tempx,tempy);

      mutex_lock(&player_mutex);
      mutex_lock(&leaves_mutex[iIndex]);

      FAB_image_put(image_leaf,leaves[iIndex].x0,leaves[iIndex].y0);
      if (player.iLeaf == iIndex)
        {
          int oldPX = player.x;
          int oldPY = player.y;


          FAB_image_put_within(image_bg,0,0,player.x,player.y,player.x +                                player_image_width, player.y +
                               player_image_height);


          player.x = player.x + iDiff;

          FAB_image_put_within(image_bg,0,0,oldPX,oldPY
                               +IMAGE_OFFSET,oldPX +                                                    player_image_width, oldPY+
                               player_image_height + IMAGE_OFFSET);

          FAB_image_put(image_player,player.x,player.y + IMAGE_OFFSET);

            if((player.x + player_image_width > 800) || (player.x <  0))
            {
               mutex_unlock(&leaves_mutex[iIndex]);
               mutex_unlock(&player_mutex);
               mutex_unlock(&grx_mutex);
               sys_end();
            }
        }

      mutex_unlock(&leaves_mutex[iIndex]);
      mutex_unlock(&player_mutex);

      mutex_unlock(&grx_mutex);

      task_endcycle();

      mutex_lock(&leaves_mutex[iIndex]);
      oldx= leaves[iIndex].x0;
      oldy = leaves[iIndex].y0;

      leaves[iIndex].x0 += iDiff;
      leaves[iIndex].x1 += iDiff;

      if (leaves[iIndex].x0 >= PLAY_AREA_WIDTH)
      {
        leaves[iIndex].x0 = 0;
      }

      if ( leaves[iIndex].x0  < 0 )
      {
        leaves[iIndex].x0 = PLAY_AREA_WIDTH;
      }
      mutex_unlock(&leaves_mutex[iIndex]);

    }

}

int CheckIfonAnyLeaf(int iLevel)
{

  int iStartLeaf = iLevel * 3;


  int i_,iLeafIndex;
  iLeafIndex = -1;

  for(i_ = iStartLeaf; i_ <= iStartLeaf +2; ++i_)
    {
      //Allready have player mutex so can saely take leaves mutex
      mutex_lock(&leaves_mutex[i_]);
      if (
            (
                (player.x > leaves[i_].x0 )
                        &&
                (player.x < (leaves[i_].x0 + leaf_image_width) )
            )
                                      ||
            (
                (player.x + player_image_width > leaves[i_].x0)
                        &&
                (player.x + player_image_width < (leaves[i_].x0
                                                 + leaf_image_width))
            )
         )
         {
           mutex_unlock(&leaves_mutex[i_]);
           iLeafIndex = i_;
           break;
         }
         mutex_unlock(&leaves_mutex[i_]);
     }


    return iLeafIndex;

}