Blame |
Last modification |
View Log
| RSS feed
TASK MovePlayerDown(void* arg)
{
int bLevelInitialised = 0;
int iNextLevel,iNextLevelY;
while(1)
{
mutex_lock(&player_mutex);
if( !bLevelInitialised)
{
iNextLevel = player.iLevel + 1;
iNextLevelY = (iNextLevel * 120) + 120 - player_image_height;
bDown = 1;
bLevelInitialised = 1;
}
int oldX,oldY;
oldX = player.x;
oldY = player.y;
player.iLeaf = -1;
player.y += 5;
if (player.y == iNextLevelY)
{
mutex_unlock(&player_mutex);
mutex_lock(&grx_mutex);
mutex_lock(&player_mutex);
FAB_image_put_within(image_bg,0,0,oldX,oldY,
oldX+player_image_width,
oldY+player_image_height);
FAB_image_put(image_player,player.x,player.y +
IMAGE_OFFSET);
mutex_unlock(&player_mutex);
mutex_unlock(&grx_mutex);
mutex_lock(&player_mutex);
int iLeaf = CheckIfonAnyLeaf(player.iLevel);
if(-1 != iLeaf)
{
++player.iLevel;
if((iScoreDown) && player.iLevel > iScoreLevel)
{
++iScoreLevel;
iScore = iScore + 10;
if (iScoreLevel == MAX_DOWN_LEVELS)
{
iScoreDown = 0;
}
}
player.iLeaf = iLeaf;
}
else
{
mutex_unlock(&player_mutex);
sys_end();
}
bDown = 0;
bLevelInitialised = 0;
mutex_unlock(&player_mutex);
task_disable(g_DownPlayer);
task_endcycle();
continue;
}
mutex_unlock(&player_mutex);
mutex_lock(&grx_mutex);
mutex_lock(&player_mutex);
FAB_image_put_within(image_bg,0,0,oldX,oldY,oldX+player_image_width,
oldY+player_image_height);
FAB_image_put(image_player,player.x,player.y);
mutex_unlock(&player_mutex);
mutex_unlock(&grx_mutex);
task_endcycle();
}
return;
}
TASK MovePlayerUp(void* arg)
{
int bLevelInitialised = 0;
int iNextLevel,iNextLevelY;
while(1)
{
mutex_lock(&player_mutex);
if (! bLevelInitialised)
{
iNextLevel = player.iLevel - 1;
iNextLevelY = (iNextLevel * 120) + 120 - player_image_height;
bUp = 1;
bLevelInitialised = 1;
}
int oldX,oldY;
oldX = player.x;
oldY = player.y;
player.iLeaf = -1;
player.y -= 5;
if (player.y == iNextLevelY)
{
int iTempOffset;
if (iNextLevel != 0)
{
iTempOffset = IMAGE_OFFSET;
}
else
{
iTempOffset = 0;
}
mutex_unlock(&player_mutex);
mutex_lock(&grx_mutex);
mutex_lock(&player_mutex);
FAB_image_put_within(image_bg,0,0,oldX,oldY,
oldX+player_image_width,
oldY+player_image_height);
FAB_image_put(image_player,player.x,player.y +
iTempOffset );
mutex_unlock(&player_mutex);
mutex_unlock(&grx_mutex);
mutex_lock(&player_mutex);
if (iNextLevel != 0)
{
int iLeaf = CheckIfonAnyLeaf(iNextLevel -1);
if(-1 != iLeaf)
{
--player.iLevel;
if((!iScoreDown) && player.iLevel < iScoreLevel)
{
--iScoreLevel;
iScore = iScore + 10;
}
player.iLeaf = iLeaf;
}
else
{
mutex_unlock(&player_mutex);
sys_end();
}
}
else
{
--player.iLevel;
if((!iScoreDown) && player.iLevel < iScoreLevel)
{
--iScoreLevel;
iScore = iScore + 10;
iScoreDown = 1;
}
}
bUp = 0;
bLevelInitialised = 0;
mutex_unlock(&player_mutex);
task_disable(g_UpPlayer);
task_endcycle();
continue;
}
mutex_unlock(&player_mutex);
mutex_lock(&grx_mutex);
mutex_lock(&player_mutex);
FAB_image_put_within(image_bg,0,0,oldX,oldY, oldX + player_image_width,
oldY+player_image_height);
FAB_image_put(image_player,player.x,player.y);
mutex_unlock(&player_mutex);
mutex_unlock(&grx_mutex);
task_endcycle();
}
return;
}
void moveright(KEY_EVT* k )
{
mutex_lock(&player_mutex);
if (player.x < 700 )
{
int oldX,oldY;
oldX = player.x;
oldY = player.y;
player.x += 10;
if ( player.iLeaf != -1)
{
mutex_lock(&leaves_mutex[player.iLeaf]);
int iFallDown = player.x > leaves[player.iLeaf].x0 + leaf_image_width ;
mutex_unlock(&leaves_mutex[player.iLeaf]);
if (iFallDown)
{
mutex_unlock(&player_mutex);
KEY_EVT k;
movedown(k);
return;
}
}
//Release the player mutex before taking graphics mutex
mutex_unlock(&player_mutex);
mutex_lock(&grx_mutex);
//Now take the player mutex
mutex_lock(&player_mutex);
FAB_image_put_within(image_bg,0,0,oldX,oldY,oldX + player_image_width,oldY + player_image_height);
FAB_image_put(image_player,player.x,player.y);
//Release player mutex and graphics mutex
mutex_unlock(&player_mutex);
mutex_unlock(&grx_mutex);
}
else
{
mutex_unlock(&player_mutex);
}
}
void moveleft(KEY_EVT* k)
{
mutex_lock(&player_mutex);
if(player.x > 0)
{
int oldX,oldY;
oldX = player.x;
oldY = player.y;
player.x -= 10;
if ( player.iLeaf != -1)
{
mutex_lock(&leaves_mutex[player.iLeaf]);
int iFallDown = player.x + player_image_width < leaves[player.iLeaf].x0;
mutex_unlock(&leaves_mutex[player.iLeaf]);
if (iFallDown )
{
mutex_unlock(&player_mutex);
KEY_EVT k;
movedown(k);
return;
}
}
mutex_unlock(&player_mutex);
mutex_lock(&grx_mutex);
mutex_lock(&player_mutex);
FAB_image_put_within(image_bg,0,0,
oldX,oldY,oldX+player_image_width,
oldY+player_image_height);
FAB_image_put(image_player,player.x,player.y);
mutex_unlock(&player_mutex);
mutex_unlock(&grx_mutex);
}
else
{
mutex_unlock(&player_mutex);
}
}
void CreatePlayerMoveTask()
{
SOFT_TASK_MODEL m;
soft_task_default_model(m);
soft_task_def_level(m,0);
soft_task_def_periodic(m);
soft_task_def_save_arrivals(m);
soft_task_def_period(m,25000);
soft_task_def_wcet(m,500);
soft_task_def_met(m,100);
g_UpPlayer = task_create("UpPlayer", MovePlayerUp, &m, NULL);
g_DownPlayer = task_create("DownPlayer", MovePlayerDown,&m, NULL);
}