Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed


#include <kernel/kern.h>
#include <drivers/glib.h>
#include <drivers/keyb.h>
#include <semaphore.h>
#include <stdlib.h>
#include <math.h>

#define ESC                                     27

#define XMIN                            0
#define YMIN                            0
#define XMAX                            640
#define YMAX                            480

#define T_XMIN                          120
#define T_XMAX                          560
#define T_YMIN                          150
#define T_YMAX                          450

#define RXMIN                           20
#define RYMIN                           10

#define PIX_RATE                        2
#define BACKGROUND                      7
#define FOREGROUND                      14
#define T_BACKGROUND            9
#define LEVEL                           1
#define PLAYER                          2
#define R_MAX                           10

#define FISHGROUP                       1
#define CROCGROUP                       2
#define RHGROUP                         3
#define HOOKGROUP                       4

#define C_DISTANCE                      2

int     DEF_COLOR[]             = {0,3,4,5,6,7,8,10,11,12,13,14,15};
//double  tick                          = 1.0;
PID     p1, p2;
sem_t   mutex, fmutex;

int FISH_PERIOD                         = 80000;
int FISH_WCET                           = 5000;

int CROC_PERIOD                         = 60000;
int CROC_WCET                           = 3000;

int ROD_PERIOD                          = 40000;
int ROD_WCET                            = 2000;

int HOOK_PERIOD                         = 30000;
int HOOK_WCET                           = 2000;

int BUBBLE_PERIOD                       = 90000;
int BUBBLE_WCET                         = 4000;

int TIME_PERIOD                         = 1000000;
int TIME_WCET                           = 7000;

int disp_x, disp_y;
int char_pix;
int player                                      = 1;
int esc_s                                       = -1;
int bubble_c                            = 5;

const int KEY_UP                        = 1;
const int KEY_DOWN                      = 2;
const int KEY_LEFT                      = 3;
const int KEY_RIGHT                     = 4;

enum {
                LEFT                            = 75,         /* Left arrow key */
                UP                                      = 72,         /* Up arrow key */
                RIGHT                           = 77,         /* Right arrow key */
                DOWN                            = 80          /* Down arrow key */
};

struct p_cordinates {
        int x;
        int y;
};

struct score {
        int points;
        int r_time;
        int f_count;
        int c4;
};

struct p_color {
        int c1;
        int c2;
        int c3;
        int c4;
};

struct p_props {
        struct p_cordinates pos;
        int size;
        int velocity;
        int hooked ;
        struct p_color color;
 int direction;
};

struct p_hook {
        struct p_cordinates h_pos;
        struct p_cordinates r1_pos;
        struct p_cordinates r2_pos;
        struct p_props* f_pos;
        struct p_color color;
        struct score score;
        int empty;
};

struct p_game {
        int level;
        int player;
        int fish_c;
        int croc_c;
        struct p_hook hook[2];
};

struct p_game game;

/******************************************************************/

inline void init_fish_rod(int rod) {
        game.hook[rod].h_pos.x  = T_XMIN + RXMIN;
        game.hook[rod].h_pos.y  = T_YMIN + RYMIN;
        game.hook[rod].r1_pos.x = T_XMIN;
        game.hook[rod].r1_pos.y = T_YMIN + RYMIN;
        game.hook[rod].r2_pos.x = T_XMIN + RXMIN;
        game.hook[rod].r2_pos.y = T_YMIN + RYMIN;
        game.hook[rod].empty    = -1;
        game.hook[rod].color.c1 = 0;
        game.hook[rod].color.c2 = 15;
        game.hook[rod].color.c3 = 4;
        game.hook[rod].color.c4 = 14;
        game.hook[rod].score.points   = 0;
        game.hook[rod].score.r_time   = 0;
        game.hook[rod].score.f_count  = 0;
}

/*----------------------------------------------------------------*/

inline void init_game() {
        game.level            = LEVEL;
        game.player           = 1;
        game.fish_c           = 8;
        game.croc_c           = 3;
        init_fish_rod(0);
}

/******************************************************************/

inline void set_fish_pos(struct p_cordinates* f_pos, int x, int y) {
        sem_wait(&fmutex);
        f_pos->x = x;
        f_pos->y = y;
        sem_post(&fmutex);
}

/*----------------------------------------------------------------*/

inline struct p_cordinates* get_hook_pos(int rod) {
        return &game.hook[rod].h_pos;
}

/*----------------------------------------------------------------*/

inline int is_hooked(rod) {
        return game.hook[rod].empty;
}

/*----------------------------------------------------------------*/

inline int calc_dist(struct p_cordinates* d1, int size, int direction) {
        struct p_cordinates* d2;
        int ret_val                             = -1;
        int range                                       = size * direction;
        int i, x, y;
        for (i=0; i<player; i++) {
                if(is_hooked(i) < 0) {
                        d2                                              = get_hook_pos(i);
                        x                                                       = (d1->x + range - d2->x) * (d1->x + range - d2->x);
                        y                                                       = (d1->y - d2->y) * (d1->y - d2->y);

                        if(sqrt(x + y) <= C_DISTANCE) {
                                ret_val                 = i;
                                break;
                        }
                }
        }    
        return ret_val;
}

/*----------------------------------------------------------------*/

inline int calc_area(struct p_cordinates* d1, int size, int direction) {
        struct p_cordinates* d2;
        int ret_val                             = -1;
        int i, x1, x2, y1, y2;
        for (i=0; i<player; i++) {
                if(is_hooked(i) > 0) {
                        d2                                              = get_hook_pos(i);
                        y1                                              = d1->y - size;
                        y2                                              = d1->y + size;
                        if(direction < 0) {
                                x1                                      = d1->x + size /2;
                                x2                                      = d1->x + 4 * size;
                        }
                        else
                        {
                                x1                                      = d1->x - 4 * size;
                                x2                                      = d1->x + size /2;
                        }
/*
    char str[50];
    sprintf(str, "x=%d : x1=%d : x2=%d : y=%d : y1=%d : y2=%d", d2->x, x1, x2, d2->y, y1, y2);
    grx_text(str, 100, 450, FOREGROUND, 0);
*/
                        if((d2->x > x1) & (d2->x < x2) & (d2->y > y1) & (d2->y < y2)) {
/*
        sprintf(str, "x=%d : x1=%d : x2=%d : y=%d : y1=%d : y2=%d", d2->x, x1, x2, d2->y, y1, y2);
        grx_text(str, 100, 400, FOREGROUND, 0);
*/
                                ret_val                 = i;
                                break;
                        }
                }
        }
        return ret_val;
}

/******************************************************************/

void draw_end()
{
        sem_wait(&mutex);
        grx_box(XMAX/3, YMAX/3, (XMAX/4)*3, YMAX/2, 0);
        grx_box(XMAX/3 + 2, YMAX/3 + 2 , (XMAX/4)*3 - 2, YMAX/2 - 2, BACKGROUND);
        grx_box(XMAX/3 + 4, YMAX/3 + 4 , (XMAX/4)*3 - 4, YMAX/2 - 4, 0);  
        grx_text("Thank You For 'Fishing'...", (XMAX/3 + 20), YMAX/3 + 20 , FOREGROUND, 0);
//      grx_text("**************************", (XMAX/3 + 20), YMAX/3 + 40 , FOREGROUND, 0);
        grx_text("Press 'ESC' To Exit...", (XMAX/3 + 20), YMAX/3 + 40 , FOREGROUND, 0);
        sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void g_clear(struct p_cordinates* rp, int size, int color)
{
        sem_wait(&mutex);
        grx_disc(rp->x, rp->y, size, color);
        sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void draw_rh(int key_pressed) {
        int rod = 0;
//      int temp = 0;
//      char str[10];

        if(key_pressed == KEY_UP) {
                if((game.hook[rod].empty < 0) & (game.hook[rod].h_pos.y > (T_YMIN + RYMIN))) {
                        game.hook[rod].h_pos.y -= PIX_RATE;
/*      
                        grx_text("UP...", 490, 0, FOREGROUND, 0);
                        temp = game.hook[rod].h_pos.y;
                        sprintf(str, "%d", temp);
                        grx_text(str, 550, 0, FOREGROUND, 0);
*/
                }
        }
        else if (key_pressed == KEY_DOWN) {
                if((game.hook[rod].empty < 0) & (game.hook[rod].h_pos.y < (T_YMAX - RYMIN))) {
                        game.hook[rod].h_pos.y += PIX_RATE;
/*
                        grx_text("DOWN...", 490, 0, FOREGROUND, 0);
                        temp = game.hook[rod].h_pos.y;
                        sprintf(str, "%d", temp);
                        grx_text(str, 550, 0, FOREGROUND, 0);
*/

// TESTING THE HOOK RETURN WITH FISH.
//      if(game.hook[rod].h_pos.y > 300){
//        game.hook[rod].empty = 1;
//      }
                }
        }
        else if (key_pressed == KEY_LEFT) {
                if(game.hook[rod].r2_pos.x > (T_XMIN + RXMIN)) {
                        game.hook[rod].r2_pos.x -= (PIX_RATE * 2);
/*
                        grx_text("LEFT...", 490, 0, FOREGROUND, 0);
                        temp = game.hook[rod].r2_pos.x;
                        sprintf(str, "%d", temp);
                        grx_text(str, 550, 0, FOREGROUND, 0);
*/
                }
        }
        else if (key_pressed == KEY_RIGHT) {
                if(game.hook[rod].r2_pos.x < (T_XMAX - RXMIN)) {
                        game.hook[rod].r2_pos.x += (PIX_RATE * 2);
/*
                        grx_text("RIGHT...", 490, 0, FOREGROUND, 0);
                        temp = game.hook[rod].r2_pos.x;
                        sprintf(str, "%d", temp);
                        grx_text(str, 550, 0, FOREGROUND, 0);
*/
                }
        }
}

/*----------------------------------------------------------------*/

void draw_bubble(struct p_props* fp)
{
        sem_wait(&mutex);
        grx_circle(fp->pos.x, fp->pos.y, fp->size, fp->color.c1);
        sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void draw_croc(struct p_props* cp, int direction)
{
        sem_wait(&mutex);

        int tlen = cp->size/4;
        if(direction == -1) {
                grx_disc(cp->pos.x, cp->pos.y, cp->size, cp->color.c1);
                grx_disc(cp->pos.x - cp->size, cp->pos.y, cp->size, cp->color.c2);
                grx_box(cp->pos.x, cp->pos.y - tlen, cp->pos.x + (cp->size * 2), cp->pos.y + tlen, cp->color.c1);
                grx_box(cp->pos.x + (cp->size * 2), cp->pos.y - cp->size, cp->pos.x + (cp->size * 4), cp->pos.y + cp->size, cp->color.c1);
                grx_disc(cp->pos.x + (cp->size * 4), cp->pos.y, cp->size, cp->color.c2);
        }
        else {
                grx_disc(cp->pos.x, cp->pos.y, cp->size, cp->color.c1);
                grx_disc(cp->pos.x + cp->size, cp->pos.y, cp->size, cp->color.c2);
                grx_box(cp->pos.x - (cp->size * 2), cp->pos.y - tlen, cp->pos.x, cp->pos.y + tlen, cp->color.c1);
                grx_box(cp->pos.x - (cp->size * 4), cp->pos.y - cp->size, cp->pos.x - (cp->size * 2), cp->pos.y + cp->size, cp->color.c1);
                grx_disc(cp->pos.x - (cp->size * 4), cp->pos.y, cp->size, cp->color.c2);
        }
  sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void draw_f(struct p_cordinates* fp, struct p_color* color, int size, int direction)
{
//  char str[50];
//  int tmp;
        sem_wait(&mutex);
        int tlen = size * 2;
        if(direction == -1) {
                grx_disc(fp->x, fp->y, size, color->c1);
                grx_disc(fp->x + size, fp->y, size, color->c2);
                grx_disc(fp->x + tlen, fp->y, size, color->c3);
        }
        else {
                grx_disc(fp->x, fp->y, size, color->c1);
                grx_disc(fp->x - size, fp->y, size, color->c2);
                grx_disc(fp->x - tlen, fp->y, size, color->c3);
        }
//  tmp = calc_dist(&(fp->pos), fp->size);
//  sprintf(str, "%d", tmp);
//  grx_text(str, fp->pos.x, fp->pos.y, T_BACKGROUND, 14);
        sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void draw_fish(struct p_props* fp)
{
 draw_f(&fp->pos, &fp->color, fp->size, fp->direction);
}

/*----------------------------------------------------------------*/

inline void draw_rod(struct p_hook* rp, int direction)
{
        sem_wait(&mutex);
        grx_line(rp->r1_pos.x, rp->r1_pos.y, rp->r2_pos.x, rp->r2_pos.y, rp->color.c1);
        grx_disc(rp->r2_pos.x, rp->r2_pos.y, 2, rp->color.c2);
        sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void draw_hook(struct p_hook* rp, int direction)
{
        sem_wait(&mutex);

        if(rp->r2_pos.x < rp->h_pos.x) {
                grx_line(rp->r2_pos.x, rp->r2_pos.y, rp->h_pos.x, rp->h_pos.y, rp->color.c1);
                grx_line(rp->h_pos.x - 2, rp->h_pos.y - 2, rp->h_pos.x, rp->h_pos.y, rp->color.c3);
        }
        else {
                grx_line(rp->h_pos.x, rp->h_pos.y, rp->r2_pos.x, rp->r2_pos.y, rp->color.c1);
                grx_line(rp->h_pos.x, rp->h_pos.y, rp->h_pos.x + 2, rp->h_pos.y + 2, rp->color.c3);
        }
        grx_disc(rp->h_pos.x, rp->h_pos.y, 1, rp->color.c2);
        sem_post(&mutex);
}

/******************************************************************/

void end_game(void *arg) {
        grx_close();
        cprintf("Ciao...\n");
}

/*----------------------------------------------------------------*/

inline void disp_score(int rod) {
        char str[4];
        sem_wait(&mutex);
//      grx_text("FISHES : 0", disp_x*3, disp_y + 50, FOREGROUND, 0);
        sprintf(str, "%d", game.hook[rod].score.f_count);
        grx_text(str, disp_x*3 + 65, disp_y + 50, FOREGROUND, 0);
//      grx_text("LEVEL : '1'", disp_x*2, disp_y + 30, FOREGROUND, 0);
        sprintf(str, "%d", game.level);
        grx_text(str, disp_x*2 + 70, disp_y + 30, FOREGROUND, 0);
//      grx_text("TIME LEFT : 01:00", disp_x*3, disp_y + 30, FOREGROUND, 0);
//      sprintf(str, "%d", game.level);
//      grx_text("SCORE : 0", disp_x*2, disp_y + 50, FOREGROUND, 0);
        sprintf(str, "%d", game.hook[rod].score.points);
        grx_text(str, disp_x*2 + 65, disp_y + 50, FOREGROUND, 0);
        sem_post(&mutex);
}

/*----------------------------------------------------------------*/

inline void set_score(int rod) {
        game.hook[rod].score.f_count  += 1;
        game.hook[rod].score.points += 1;
        disp_score(rod);
//      game.hook[rod].score.r_time   = 0;
}

/****************************************************************/

TASK hook_t(void *arg) {
        int rod                                                         = (int)arg;
        struct p_hook* rp                       = &game.hook[rod];
        struct p_hook prp;
        int direction                                   = 1;
        prp.color.c1                                    = T_BACKGROUND;
        prp.color.c2                                    = T_BACKGROUND;
        prp.color.c3                                    = T_BACKGROUND;
        prp.r2_pos.x                                    = rp->r2_pos.x;
        prp.r2_pos.y                                    = rp->r2_pos.y;
        prp.h_pos.x                                             = rp->h_pos.x;
        prp.h_pos.y                                             = rp->h_pos.y;

        while (1) {
                if(game.hook[rod].empty > 0) {
                        if(rp->h_pos.y > (T_YMIN + RYMIN)) {
                                rp->h_pos.y                                     -= game.level;
                                draw_f(&rp->h_pos, &prp.color, rp->f_pos->size, rp->f_pos->direction);
//                              rp->f_pos->pos.x                = rp->h_pos.x;
//                              rp->f_pos->pos.y                = rp->h_pos.y;
                                set_fish_pos(&rp->f_pos->pos, rp->h_pos.x, rp->h_pos.y);
                        }
                        else
                        {
                                set_score(rod);
                                rp->empty                                               = -1;
                                rp->f_pos->hooked       = -1;
                                set_fish_pos(&rp->f_pos->pos, T_XMIN + (rp->f_pos->size * 3), T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN) / game.fish_c) * (rand()%(game.fish_c - 1)));
                                rp->f_pos                                               = 0;
                        }
                }
                draw_hook(&prp, direction);
                prp.h_pos.x                                     = rp->h_pos.x;
                prp.h_pos.y                                     = rp->h_pos.y;
                prp.r2_pos.x                            = rp->r2_pos.x;
                rp->h_pos.x                                     = rp->r2_pos.x;
                draw_hook(rp, direction);

                task_endcycle();
        }
}

/*----------------------------------------------------------------*/

TASK rod_t(void *arg) {
        int rod                                                         = (int)arg;
        struct p_hook* rp                       = &game.hook[rod];
        struct p_hook prp;
        int direction                                   = 1;
        prp.color.c1                                    = T_BACKGROUND;
        prp.color.c2                                    = T_BACKGROUND;
        prp.r1_pos.x                                    = rp->r1_pos.x;
        prp.r1_pos.y                                    = rp->r1_pos.y;
        prp.r2_pos.x                                    = rp->r2_pos.x;
        prp.r2_pos.y                                    = rp->r2_pos.y;

        while (1) {
                draw_rod(&prp, direction);
                prp.r2_pos.x = rp->r2_pos.x;
                draw_rod(rp, direction);
                
                task_endcycle();
        }
}

/*----------------------------------------------------------------*/

TASK fish_t(void *arg) {
        struct p_props fp;
        struct p_props pfp;
//      int fp.direction                                = 1;
//      int p_direction                 = 1;
        int s_player                            = -1;
        fp.color.c1                                     = DEF_COLOR[rand()%12];
        fp.color.c2                                     = DEF_COLOR[rand()%12];
        fp.color.c3                                     = T_BACKGROUND;
        fp.color.c4                                     = DEF_COLOR[rand()%12];
        fp.size                                                 = (rand()%10)+3;
  fp.hooked                                             = -1;
        fp.pos.x                                                = T_XMIN + (fp.size * 3);
        fp.pos.y                                                = T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN) / game.fish_c) * (rand()%(game.fish_c - 1));
//      set_fish_pos(&fp.pos, T_XMIN + (fp.size * 3), T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN) / game.fish_c) * (rand()%(game.fish_c - 1)));
        fp.direction                            = 1;
        pfp.pos.y                                               = fp.pos.y;
        pfp.color.c1                            = T_BACKGROUND;
        pfp.color.c2                            = T_BACKGROUND;
        pfp.color.c3                            = T_BACKGROUND;
        pfp.color.c4                            = T_BACKGROUND;
        pfp.size                                                = fp.size;

        if((rand()%10)>5) {
                fp.direction                    *= -1;
                fp.pos.x                                        = T_XMAX - (fp.size * 3);
        }
        fp.pos.x                                                += fp.direction *  ((rand()%3) + PIX_RATE);
        pfp.pos.x                                       = fp.pos.x;

//  char str[40];

        while (esc_s < 0) {
                if(fp.hooked < 0) {
                        s_player                = calc_dist(&fp.pos, fp.size, fp.direction);
                        if(s_player >= 0) {
                                game.hook[s_player].f_pos               = &fp;
                                game.hook[s_player].empty               = 1;
                                fp.hooked                                                                       = 1;
                        }
                        else
                        {
                                fp.pos.x                        += fp.direction * PIX_RATE;
                                fp.hooked                       = -1;
                                if((fp.pos.x >= (T_XMAX - fp.size)) || (fp.pos.x <= (T_XMIN + fp.size))) {
                                        fp.direction            *= -1;
                                        fp.pos.x                += fp.direction * fp.size * 3;
//                                pfp.pos.x             = fp.pos.x;
                                }
                        }
                }
//    grx_text("AM HERE...", 0, 465, FOREGROUND, 0);
                draw_fish(&pfp);
                draw_fish(&fp);
                pfp.pos.x                       = fp.pos.x;
                pfp.direction   = fp.direction;
                task_endcycle();
        }
}

/*----------------------------------------------------------------*/

TASK croc_t(void *arg) {
        struct p_props cp;
        struct p_props pcp;
//      int cp.direction=1;
        int p_direction=1;
        int s_player                            = -1;
        cp.color.c1 = 2;
        cp.color.c2 = T_BACKGROUND;
        cp.size = 10;
        cp.pos.x = T_XMIN + (cp.size * 6);
        cp.pos.y = T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN - 100) / (game.fish_c - 3)) * (rand()%(game.fish_c - 3));
        cp.direction=1;
        pcp.pos.y = cp.pos.y;
        pcp.color.c1 = T_BACKGROUND;
        pcp.color.c2 = T_BACKGROUND;
        pcp.size = cp.size;

        if((rand()%10)>5) {
                cp.direction *= -1;
                cp.pos.x = T_XMAX - (cp.size * 6);
        }
        cp.pos.x += cp.direction * ((rand()%3) + PIX_RATE);
        pcp.pos.x = cp.pos.x;

        while (esc_s < 0) {
                s_player                = calc_area(&cp.pos, cp.size, cp.direction);
    char str[40];
    sprintf(str, "%d", s_player);
    grx_text(str, 0, 450, FOREGROUND, 0);
                if(s_player >= 0) {
                        grx_text("IN 1...", 0, 525, FOREGROUND, 0);
                        game.hook[s_player].empty                                       = -1;
                        game.hook[s_player].f_pos->hooked = -1;
                        set_fish_pos(&game.hook[s_player].f_pos->pos, T_XMIN + (game.hook[s_player].f_pos->size * 3), T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN) / game.fish_c) * (rand()%(game.fish_c - 1)));
                }
                cp.pos.x += cp.direction * PIX_RATE;
                if((cp.pos.x >= (T_XMAX - (cp.size * 2))) || (cp.pos.x <= (T_XMIN + (cp.size * 2)))) {
                        cp.direction *= -1;
                        cp.pos.x += cp.direction * cp.size * 6;
//                      pcp.pos.x = cp.pos.x;
                }

                draw_croc(&pcp, p_direction);
                draw_croc(&cp, cp.direction);
                p_direction = cp.direction;
                pcp.pos.x = cp.pos.x;

                task_endcycle();
        }
}

/*----------------------------------------------------------------*/

TASK timeinterval_t(void *arg) {
        struct timespec actual_timer;
        int max_time = *((int *)arg);

        int temp_sec = 0;
        long sec,min;
        char str[5];
        int len;
        TIME seme;
        //max_time = 2;

        while(max_time > 0){
                sys_gettime(&actual_timer);
                sec = actual_timer.tv_sec;
                min = sec / 60;
                sec %= 60;
                temp_sec = 60 - sec - 1;
                //if (temp_sec == 60){
                if (sec == 59 ){
                        max_time--;
                }
                sprintf(str,"%02d:%02d",max_time,temp_sec);
                len = strlen(str);
                str[len] = '\0';
                sem_wait(&mutex);
//              grx_text(str ,10, 50, RED, 0);
                grx_text(str, disp_x*3 + 90, disp_y + 30, FOREGROUND, 0);
                sem_post(&mutex);
                task_endcycle();
        }
        grx_text("Time Over" ,10, 70, RED, 0);
        esc_s = 1;
        draw_end();
}

/*----------------------------------------------------------------*/

TASK bubble_t(void *arg) {
        struct p_props bp;
        struct p_props pbp;
        int direction=1;
        int idelay;
        bp.color.c1 = WHITE;
        bp.color.c2 = BACKGROUND;
        bp.size = 4;
        int xval = rand()%9;
        bp.pos.x = pbp.pos.x = T_XMIN + ((int)((T_XMAX-T_XMIN)/8 )) * xval;
        bp.pos.y = T_YMAX - (bp.size * 2);
        pbp.pos.y = bp.pos.y;
        pbp.color.c1 = BACKGROUND;
        pbp.color.c2 = BACKGROUND;
        pbp.size = bp.size;

        while (1) {
                bp.pos.y -= PIX_RATE*(rand()%3) + 1 ;
                if(bp.pos.y <=  (T_YMIN + bp.size) ) {
                        for(idelay=0;idelay<999999;idelay++);
                        draw_bubble(&pbp);
                        xval = rand()%9;
                        bp.pos.x = pbp.pos.x = T_XMIN + ((int)((T_XMAX-T_XMIN)/8 )) * xval;
                        bp.pos.y = T_YMAX - (bp.size * 2);
                        pbp.pos.y = bp.pos.y;
                }

                draw_bubble(&pbp);
                draw_bubble(&bp);
                pbp.pos.y = bp.pos.y;
                pbp.pos.x = bp.pos.x;
                task_endcycle();
        }
}

/****************************************************************/

void create_fish() {
        SOFT_TASK_MODEL m1;
        PID p1;
        int count = 0;
        int i = 0;
        do {
                        soft_task_default_model (m1);
                        soft_task_def_level     (m1, 1);
                        soft_task_def_ctrl_jet  (m1);
                        soft_task_def_arg       (m1, (void *)i);
                        soft_task_def_met       (m1, FISH_WCET);
                        soft_task_def_period    (m1, FISH_PERIOD);
                        soft_task_def_group     (m1, FISHGROUP);
                        soft_task_def_usemath   (m1);
                        p1 = task_create("fish", fish_t, &m1, NULL);
                        if (p1 == NIL) {
                                grx_close();
                                perror("Could not create task <fish>");
                                sys_abort(1);
                        }
                        task_activate(p1);
                        count++;
        }while(count < game.fish_c);
}

/*----------------------------------------------------------------*/

void create_croc() {
        SOFT_TASK_MODEL m1;
        PID p1;
        int count = 0;
        int i = 0;
        do {
                        soft_task_default_model (m1);
                        soft_task_def_level     (m1, 1);
                        soft_task_def_ctrl_jet  (m1);
                        soft_task_def_arg       (m1, (void *)i);
                        soft_task_def_met       (m1, CROC_WCET);
                        soft_task_def_period    (m1, CROC_PERIOD);
                        soft_task_def_group     (m1, CROCGROUP);
                        soft_task_def_usemath   (m1);
                        p1 = task_create("croc", croc_t, &m1, NULL);
                        if (p1 == NIL) {
                                grx_close();
                                perror("Could not create task <croc>");
                                sys_abort(1);
                        }
                        int delay = 0;
                        while(delay <= 500){
                                delay++;
                        }
                        task_activate(p1);
                        count++;
        }while(count <= game.croc_c);
}

/*----------------------------------------------------------------*/

void create_rod() {
        SOFT_TASK_MODEL m1;
        PID p1;
        int count = 0;
        int i = 0;
        do {
                        soft_task_default_model (m1);
                        soft_task_def_level     (m1, 1);
                        soft_task_def_ctrl_jet  (m1);
                        soft_task_def_arg       (m1, (void *)i);
                        soft_task_def_met       (m1, ROD_WCET);
                        soft_task_def_period    (m1, ROD_PERIOD);
                        soft_task_def_group     (m1, RHGROUP);
                        soft_task_def_usemath   (m1);
                        p1 = task_create("rod", rod_t, &m1, NULL);
                        if (p1 == NIL) {
                                grx_close();
                                perror("Could not create task <rod>");
                                sys_abort(1);
                        }
                        int delay = 0;
                        while(delay <= 500){
                                delay++;
                        }
                        task_activate(p1);
                        count++;
        }while(count < player);
}

/*----------------------------------------------------------------*/

void create_hook() {
        SOFT_TASK_MODEL m1;
        PID p1;
        int count = 0;
        int i = 0;
        do {
                        soft_task_default_model (m1);
                        soft_task_def_level     (m1, 1);
                        soft_task_def_ctrl_jet  (m1);
                        soft_task_def_arg       (m1, (void *)i);
                        soft_task_def_met       (m1, HOOK_WCET);
                        soft_task_def_period    (m1, HOOK_PERIOD);
                        soft_task_def_group     (m1, HOOKGROUP);
                        soft_task_def_usemath   (m1);
                        p1 = task_create("hook", hook_t, &m1, NULL);
                        if (p1 == NIL) {
                                grx_close();
                                perror("Could not create task <hook>");
                                sys_abort(1);
                        }
                        int delay = 0;
                        while(delay <= 500){
                                delay++;
                        }
                        task_activate(p1);
                        count++;
        }while(count < player);
}

/*----------------------------------------------------------------*/

void create_bubble() {
        SOFT_TASK_MODEL s1;

        PID p1;
        int count = 0;
        int i = 0;

        do {
                        soft_task_default_model(s1);
                        soft_task_def_level(s1,1);
                        soft_task_def_ctrl_jet (s1);
                        soft_task_def_arg(s1, (void *)i);
                        soft_task_def_met(s1, BUBBLE_WCET);
                        soft_task_def_period(s1,BUBBLE_PERIOD);
                        soft_task_def_usemath(s1);
                        p1 = task_create("bubble", bubble_t, &s1, NULL);
                        if (p1 == NIL) {
                                grx_close();
                                perror("Could not create task <bubble>");
                                sys_abort(1);
                        }
                        task_activate(p1);
                        count++;
        }while(count <= bubble_c);
}

/*----------------------------------------------------------------*/

void create_timer(int max_time) {
        SOFT_TASK_MODEL s1;
        PID p1;
        int i = max_time;
        void *p = &i;
        soft_task_default_model(s1);
        soft_task_def_level(s1,1);
        soft_task_def_ctrl_jet (s1);
        soft_task_def_arg(s1, p);
        soft_task_def_met(s1, TIME_WCET);
        soft_task_def_period(s1,TIME_PERIOD);
        soft_task_def_usemath(s1);
        p1 = task_create("timer", timeinterval_t, &s1, NULL);
        if (p1 == NIL) {
                grx_close();
                perror("Could not create task <timer>");
                sys_abort(1);
        }
        task_activate(p1);
}

/****************************************************************/

void init_display() {
        /* graphic card Initialization */
        if (grx_init() < 1) {
                sys_abort(1);
        }

        if (grx_open(XMAX, YMAX, 8) < 0) {
                cprintf("GRX Err\n");
                sys_abort(1);
        }
        sem_wait(&mutex); 
        disp_x    = (XMAX - XMIN)/6;
        disp_y    = (T_YMIN - YMIN)/8;
        char_pix  = 12;
  
        // Initial Screen.
        grx_box(XMIN, YMIN, XMAX, YMAX, BACKGROUND);
        grx_box(disp_x, disp_y, (disp_x)*5, (disp_y)*6, 0);
        grx_box(disp_x + 2, disp_y + 2, (disp_x)*5 - 2, (disp_y)*6 - 2, BACKGROUND);
        grx_box(disp_x + 4, disp_y + 4, (disp_x)*5 - 4, (disp_y)*6 - 4, 0);
        grx_text("ENJOY FISHING", disp_x*3 - (char_pix * 6), disp_y + 10, FOREGROUND, 0);
        grx_text("LEVEL : '1'", disp_x*2, disp_y + 30, FOREGROUND, 0);
        grx_text("TIME LEFT :", disp_x*3, disp_y + 30, FOREGROUND, 0);
        grx_text("FISHES : ", disp_x*3, disp_y + 50, FOREGROUND, 0);
        grx_text("SCORE : ", disp_x*2, disp_y + 50, FOREGROUND, 0);
        grx_text("Press 'ESC' to Exit...", disp_x*3 - (char_pix * 6), disp_y*6 - char_pix - 3, FOREGROUND, 0);

  // Tank Draw.
        grx_box(T_XMIN - 4, T_YMIN - 4, T_XMAX + 4, T_YMAX + 4, 0);
        grx_rect(T_XMIN - 3, T_YMIN - 3, T_XMAX + 3, T_YMAX + 3, 0);
        grx_box(T_XMIN - 1, T_YMIN - 1, T_XMAX + 1, T_YMAX + 1 , T_BACKGROUND);

//      disp_x    = disp_x * 2;
//      disp_y    = disp_y + 25;
        sem_post(&mutex);
}

/****************************************************************/

int main(int argc, char **argv) {
        char c;             /* character from keyboard      */
        TIME seme;          /* used to init the random seed */

        sys_atrunlevel(end_game, NULL, RUNLEVEL_BEFORE_EXIT);

        init_display();
        seme = sys_gettime(NULL);
        srand(seme);
  
        init_game();
        create_fish();
        create_croc();
        create_rod();
        create_hook();
//      create_bubble();
        create_timer(2); // Pass the upper limit in minutes

        KEY_EVT k;
        k.ascii   = 0;

        do {
                keyb_getcode(&k, BLOCK);

                switch (k.scan) {
                        case LEFT :
                                        draw_rh(KEY_LEFT);
                                        break;
                        case UP :
                                        draw_rh(KEY_UP);
                                        break;
                        case RIGHT :
                                        draw_rh(KEY_RIGHT);
                                        break;
                        case DOWN :
                                        draw_rh(KEY_DOWN);
                                        break;
                }
        }while (k.ascii != ESC);
        esc_s = 1;
        draw_end();

        while((c = keyb_getch(BLOCK)) != ESC){
        }

        sys_end();
        return 0;
}

/******************************************************************/