Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/*
 * Project: S.Ha.R.K.
 *
 * Coordinators: 
 *   Giorgio Buttazzo    <giorgio@sssup.it>
 *   Giuseppe Lipari       <lipari@sssup.it> 
 *   Paolo Gai               <pj@gandalf.sssup.it>
 * 
 * Authors     : 
 *   Kabilan Sukumar      <kabbys2@yahoo.com>
 *   Rajenish Kumar jain  <rajenish_jain@yahoo.com>
 *   Deepaknath T K       <deepaknathtk@yahoo.com>
 *  
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
 *
 * http://www.sssup.it
 * http://retis.sssup.it
 * http://shark.sssup.it
 */

/*
 * Copyright (C) 2002  Kabilan Sukumar,  Rajenish Kumar jain, Deepaknath T K 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "demo.h"
#include <kernel/func.h>
#include <string.h>
#include <stdlib.h>
#include <drivers/keyb.h>
#include <drivers/glib.h>

/* graphic mutex... */
mutex_t mutex;

/* keys to browse the menu */
KEY_EVT keypress;
int keyflag=0;
int escapeflag=1;
/* to check for the finish of the game */
extern int gamefinish;

/* useful colors... */
int green;
int white;
int black;
int red;
int gray;
int yellow;
int blue;

void app_mutex_init(mutex_t *m);

/* to get random numbers */
int myrand(int x)
{
  return rand()%x;
}

void reverse(char s[])
{
  int c, i, j;

  for (i = 0, j = strlen(s)-1; i<j; i++, j--)
  {
    c = s[i];
    s[i] = s[j];
    s[j] = c;
  }
}

char * itoa(int n, char *s)
{
  int i, sign;

  if ((sign = n) < 0)
    n = -n;

  i = 0;

  do
  {
    s[i++] = n % 10 + '0';
  } while ((n /= 10) > 0);

  if (sign < 0)
    s[i++] = '-';

  s[i] = 0;

  reverse(s);

  return s;
}

/* builds the scenario for the game */
void scenario()
{

  grx_clear(black);

  grx_rect(10,15,380,460,red);
  grx_rect(5,10,385,465,red);

  grx_rect(400,10,638,465,red);
  grx_rect(405,15,633,460,red);
  grx_text("PING PONG",435,20,green,black);
  grx_line(405,30,630,30,white);

  grx_text("PLAYER 1",425,90,green,black);
  grx_text("PLAYER 2",510,150,green,black);

  grx_rect(435,120,500,140,red);
  grx_rect(520,180,585,200,red);

  grx_text("0",465,117,green,black);
  grx_text("0",550,177,green,black);


  grx_line(405,225,633,225,white);
  grx_text("PROGRAMMERS",445,235,green,black);
  grx_line(440,245,580,245,white);

  grx_text("Kabilan Sukumar",413,255,green,black);
  grx_text("Rajenish Kumar Jain",413,265,green,black);
  grx_text("Deepaknath T K",413,275,green,black);

  grx_line(440,300,580,300,white);
  grx_text("ESC : To Exit to menu",413,325,green,black);
  grx_text("CTRL C : To exit to sys",413,335,green,black);
  grx_text("SHIFT/CTRL : To move ",413,345,green,black);
  grx_text("the bats",500,355,green,black);



  grx_line(405,410,630,410,white);
  grx_text("CONTACT",490,400,green,black);
  grx_text("http://www.sssup.it",430,425,green,black);
  grx_text("http://shark.sssup.it",440,440,green,black);

  /* calls the ball and bat initializations */
  scenario_game();

}


void demo_exc_handler(int signo, siginfo_t *info, void *extra)
{
  struct timespec t;

  grx_close();

  /* Default action for an kern exception is  */
  kern_cli();
  ll_gettime(TIME_EXACT, &t),
  kern_printf("\nS.Ha.R.K. Exception raised!!!"
              "\nTime (s:ns)     :%d:%d"
              "\nException number:%d"
              "\nPID             :%d\n",
              t.tv_sec, t.tv_nsec, info->si_value.sival_int,
              info->si_task);
  sys_end();
}

void my_close(void *arg)
{
  grx_close();
  kern_printf("bye! bye! Thanks for playing ping pong. Hope you had a good time.\n");
}


void endfun(KEY_EVT *k)
{
    cprintf("Ctrl-Brk pressed! Ending...\n");
    sys_end();
}

/* used to find out which key is pressed for the menu */
void menukey(KEY_EVT *k){
     keyflag=1;
     keypress=*k;
}

/* the main menu*/
int menu()
{
     int  pos;

     pos = 1;
     grx_clear(black);


     grx_rect(200,50,400,350,red);
     grx_rect(201,51,399,349,red);
     grx_box(202,52,398,348,black);

     grx_text("PING PONG",270,100,yellow,black);

     /*choices*/


     grx_text("Start One Player",250,150,yellow,black);
     grx_text("Start Two Player",250,200,yellow,black);
     grx_text("Instructions",250,250,yellow,black);
     grx_text("Exit",250,300,yellow,black);

     grx_text("Press TAB to NAVIGATE and ENTER to SELECT",150,400,green,black);


     grx_disc(240,150,4,red);


     while (1)
     {    if(keyflag){   /* indicates that a key has been pressed */
           if(keypress.scan==KEY_ENT){
                   keyflag=0;
                   break;
           }
           if(keypress.scan==KEY_TAB) /*down*/
           {
             if(pos == 1)
             {
                grx_disc(240,150,4,black);
                grx_disc(240,200,4,red);
                pos=2;
              }
              else if(pos == 2)
              {
                grx_disc(240,200,4,black);
                grx_disc(240,250,4,red);
                pos=3;
              }
              else if(pos == 3)
              {
                grx_disc(240,250,4,black);
                grx_disc(240,300,4,red);
                pos=4;
              }
              else if(pos == 4)
              {
                grx_disc(240,300,4,black);
                grx_disc(240,150,4,red);
                pos=1;
              }
           }


          keyflag=0;
         }
     }
        return pos;
}

/* instructions to play the game */
void instructions()
{
     grx_clear(black);

     grx_text("INSTRUCTIONS",200,15,red,black);

     grx_text("Player One(Blue bat) : Use shift(Left) and ctrl(Left) to move the bat",10,100,rgb16(0,200,0),black);

     grx_text("Player Two(Red bat) : Use the keys CTRL(Right) and ALT(Right)",10,150,rgb16(200,200,0),black);

     grx_text("Hitting the centre of the bat causes ball to reflect more vertically",10,200,rgb16(0,0,200),black);

     grx_text("Ends of the bat cause ball to reverse x-direction or to go more horizontally",10,250,rgb16(100,100,100),black);

}

int main(int argc, char **argv)
{
    int modenum;
    int pos;

    KEY_EVT k;

    srand(4);


    keyb_set_map(itaMap);
    k.flag = CNTR_BIT;
    k.scan = KEY_C;
    k.ascii = 'c';
    keyb_hook(k,endfun);
    k.flag = CNTL_BIT;
    k.scan = KEY_C;
    k.ascii = 'c';
    keyb_hook(k,endfun);
    k.flag = 0;
    k.scan = KEY_ENT;
    k.ascii = 13;
    keyb_hook(k,menukey);
    k.flag = 0;
    k.scan = KEY_TAB;
    k.ascii = 15;
    keyb_hook(k,menukey);
    k.flag = 0;
    k.scan = KEY_ESC;
    k.ascii = 1;
    keyb_hook(k,EscapeToMenu);



    set_exchandler_grx();
    sys_atrunlevel(my_close, NULL, RUNLEVEL_BEFORE_EXIT);


    grx_init();
    modenum = grx_getmode(640, 480, 16);

    grx_setmode(modenum);

    /* init the graphic mutex */
    app_mutex_init(&mutex);

    /* useful colors ... */
    white = rgb16(255,255,255);
    black = rgb16(0,0,0);
    red   = rgb16(255,0,0);
    gray  = rgb16(128,128,128);
    green = rgb16(0,200,0);
    yellow=rgb16(200,200,0);
    blue=rgb16(0,0,200);

    while(1){
    if(escapeflag){
         pos=menu();
         gamefinish=0;
         escapeflag=0;
    }
    if(pos==2){
        gamefinish=0;
        scenario(pos);
        init_ball(pos);
        pos=0;
        group_activate(1);
    }
    else if(pos==1){
        gamefinish=0;
        scenario(pos);
        init_ball(pos);
        pos=0;
        group_activate(1);
    }
    else if(pos==3){
        gamefinish=0;
        instructions();
        pos=0;
    }
    else if(pos==4){
        return 0;
    }
    }
}