Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1664 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Giuseppe Lipari       <lipari@sssup.it>
7
 *   Paolo Gai               <pj@gandalf.sssup.it>
8
 *
9
 * Authors     :
10
 *   Kabilan Sukumar      <kabbys2@yahoo.com>
11
 *   Rajenish Kumar jain  <rajenish_jain@yahoo.com>
12
 *   Deepaknath T K       <deepaknathtk@yahoo.com>
13
 *
14
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
15
 *
16
 * http://www.sssup.it
17
 * http://retis.sssup.it
18
 * http://shark.sssup.it
19
 */
20
 
21
/*
22
 * Copyright (C) 2002  Kabilan Sukumar,  Rajenish Kumar jain, Deepaknath T K
23
 *
24
 * This program is free software; you can redistribute it and/or modify
25
 * it under the terms of the GNU General Public License as published by
26
 * the Free Software Foundation; either version 2 of the License, or
27
 * (at your option) any later version.
28
 *
29
 * This program is distributed in the hope that it will be useful,
30
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32
 * GNU General Public License for more details.
33
 *
34
 * You should have received a copy of the GNU General Public License
35
 * along with this program; if not, write to the Free Software
36
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37
 *
38
 */
39
 
40
#include "demo.h"
41
#include <kernel/func.h>
42
#include <string.h>
43
#include <stdlib.h>
44
#include <drivers/keyb.h>
45
#include <drivers/glib.h>
46
 
47
/* graphic mutex... */
48
mutex_t mutex;
49
 
50
/* keys to browse the menu */
51
KEY_EVT keypress;
52
int keyflag=0;
53
int escapeflag=1;
54
/* to check for the finish of the game */
55
extern int gamefinish;
56
 
57
/* useful colors... */
58
int green;
59
int white;
60
int black;
61
int red;
62
int gray;
63
int yellow;
64
int blue;
65
 
66
void app_mutex_init(mutex_t *m);
67
 
68
/* to get random numbers */
69
int myrand(int x)
70
{
71
  return rand()%x;
72
}
73
 
74
void reverse(char s[])
75
{
76
  int c, i, j;
77
 
78
  for (i = 0, j = strlen(s)-1; i<j; i++, j--)
79
  {
80
    c = s[i];
81
    s[i] = s[j];
82
    s[j] = c;
83
  }
84
}
85
 
86
char * itoa(int n, char *s)
87
{
88
  int i, sign;
89
 
90
  if ((sign = n) < 0)
91
    n = -n;
92
 
93
  i = 0;
94
 
95
  do
96
  {
97
    s[i++] = n % 10 + '0';
98
  } while ((n /= 10) > 0);
99
 
100
  if (sign < 0)
101
    s[i++] = '-';
102
 
103
  s[i] = 0;
104
 
105
  reverse(s);
106
 
107
  return s;
108
}
109
 
110
/* builds the scenario for the game */
111
void scenario()
112
{
113
 
114
  grx_clear(black);
115
 
116
  grx_rect(10,15,380,460,red);
117
  grx_rect(5,10,385,465,red);
118
 
119
  grx_rect(400,10,638,465,red);
120
  grx_rect(405,15,633,460,red);
121
  grx_text("PING PONG",435,20,green,black);
122
  grx_line(405,30,630,30,white);
123
 
124
  grx_text("PLAYER 1",425,90,green,black);
125
  grx_text("PLAYER 2",510,150,green,black);
126
 
127
  grx_rect(435,120,500,140,red);
128
  grx_rect(520,180,585,200,red);
129
 
130
  grx_text("0",465,117,green,black);
131
  grx_text("0",550,177,green,black);
132
 
133
 
134
  grx_line(405,225,633,225,white);
135
  grx_text("PROGRAMMERS",445,235,green,black);
136
  grx_line(440,245,580,245,white);
137
 
138
  grx_text("Kabilan Sukumar",413,255,green,black);
139
  grx_text("Rajenish Kumar Jain",413,265,green,black);
140
  grx_text("Deepaknath T K",413,275,green,black);
141
 
142
  grx_line(440,300,580,300,white);
143
  grx_text("ESC : To Exit to menu",413,325,green,black);
144
  grx_text("CTRL C : To exit to sys",413,335,green,black);
145
  grx_text("SHIFT/CTRL : To move ",413,345,green,black);
146
  grx_text("the bats",500,355,green,black);
147
 
148
 
149
 
150
  grx_line(405,410,630,410,white);
151
  grx_text("CONTACT",490,400,green,black);
152
  grx_text("http://www.sssup.it",430,425,green,black);
153
  grx_text("http://shark.sssup.it",440,440,green,black);
154
 
155
  /* calls the ball and bat initializations */
156
  scenario_game();
157
 
158
}
159
 
160
 
161
void demo_exc_handler(int signo, siginfo_t *info, void *extra)
162
{
163
  struct timespec t;
164
 
165
  grx_close();
166
 
167
  /* Default action for an kern exception is  */
168
  kern_cli();
169
  ll_gettime(TIME_EXACT, &t),
170
  kern_printf("\nS.Ha.R.K. Exception raised!!!"
171
              "\nTime (s:ns)     :%d:%d"
172
              "\nException number:%d"
173
              "\nPID             :%d\n",
174
              t.tv_sec, t.tv_nsec, info->si_value.sival_int,
175
              info->si_task);
176
  sys_end();
177
}
178
 
179
void my_close(void *arg)
180
{
181
  grx_close();
182
  kern_printf("bye! bye! Thanks for playing ping pong. Hope you had a good time.\n");
183
}
184
 
185
 
186
void endfun(KEY_EVT *k)
187
{
188
    cprintf("Ctrl-Brk pressed! Ending...\n");
189
    sys_end();
190
}
191
 
192
/* used to find out which key is pressed for the menu */
193
void menukey(KEY_EVT *k){
194
     keyflag=1;
195
     keypress=*k;
196
}
197
 
198
/* the main menu*/
199
int menu()
200
{
201
     int  pos;
202
 
203
     pos = 1;
204
     grx_clear(black);
205
 
206
 
207
     grx_rect(200,50,400,350,red);
208
     grx_rect(201,51,399,349,red);
209
     grx_box(202,52,398,348,black);
210
 
211
     grx_text("PING PONG",270,100,yellow,black);
212
 
213
     /*choices*/
214
 
215
 
216
     grx_text("Start One Player",250,150,yellow,black);
217
     grx_text("Start Two Player",250,200,yellow,black);
218
     grx_text("Instructions",250,250,yellow,black);
219
     grx_text("Exit",250,300,yellow,black);
220
 
221
     grx_text("Press TAB to NAVIGATE and ENTER to SELECT",150,400,green,black);
222
 
223
 
224
     grx_disc(240,150,4,red);
225
 
226
 
227
     while (1)
228
     {    if(keyflag){   /* indicates that a key has been pressed */
229
           if(keypress.scan==KEY_ENT){
230
                   keyflag=0;
231
                   break;
232
           }
233
	   if(keypress.scan==KEY_TAB) /*down*/
234
	   {
235
	     if(pos == 1)
236
	     {
237
                grx_disc(240,150,4,black);
238
	        grx_disc(240,200,4,red);
239
	 	pos=2;
240
	      }
241
	      else if(pos == 2)
242
	      {
243
                grx_disc(240,200,4,black);
244
	        grx_disc(240,250,4,red);
245
	        pos=3;
246
	      }
247
	      else if(pos == 3)
248
	      {
249
                grx_disc(240,250,4,black);
250
	        grx_disc(240,300,4,red);
251
	        pos=4;
252
	      }
253
	      else if(pos == 4)
254
	      {
255
	        grx_disc(240,300,4,black);
256
	        grx_disc(240,150,4,red);
257
	        pos=1;
258
	      }
259
	   }
260
 
261
 
262
          keyflag=0;
263
         }
264
     }
265
	return pos;
266
}
267
 
268
/* instructions to play the game */
269
void instructions()
270
{
271
     grx_clear(black);
272
 
273
     grx_text("INSTRUCTIONS",200,15,red,black);
274
 
275
     grx_text("Player One(Blue bat) : Use shift(Left) and ctrl(Left) to move the bat",10,100,rgb16(0,200,0),black);
276
 
277
     grx_text("Player Two(Red bat) : Use the keys CTRL(Right) and ALT(Right)",10,150,rgb16(200,200,0),black);
278
 
279
     grx_text("Hitting the centre of the bat causes ball to reflect more vertically",10,200,rgb16(0,0,200),black);
280
 
281
     grx_text("Ends of the bat cause ball to reverse x-direction or to go more horizontally",10,250,rgb16(100,100,100),black);
282
 
283
}
284
 
285
int main(int argc, char **argv)
286
{
287
    int modenum;
288
    int pos;
289
 
290
    KEY_EVT k;
291
 
292
    srand(4);
293
 
294
 
295
    keyb_set_map(itaMap);
296
    k.flag = CNTR_BIT;
297
    k.scan = KEY_C;
298
    k.ascii = 'c';
299
    keyb_hook(k,endfun);
300
    k.flag = CNTL_BIT;
301
    k.scan = KEY_C;
302
    k.ascii = 'c';
303
    keyb_hook(k,endfun);
304
    k.flag = 0;
305
    k.scan = KEY_ENT;
306
    k.ascii = 13;
307
    keyb_hook(k,menukey);
308
    k.flag = 0;
309
    k.scan = KEY_TAB;
310
    k.ascii = 15;
311
    keyb_hook(k,menukey);
312
    k.flag = 0;
313
    k.scan = KEY_ESC;
314
    k.ascii = 1;
315
    keyb_hook(k,EscapeToMenu);
316
 
317
 
318
 
319
    set_exchandler_grx();
320
    sys_atrunlevel(my_close, NULL, RUNLEVEL_BEFORE_EXIT);
321
 
322
 
323
    grx_init();
324
    modenum = grx_getmode(640, 480, 16);
325
 
326
    grx_setmode(modenum);
327
 
328
    /* init the graphic mutex */
329
    app_mutex_init(&mutex);
330
 
331
    /* useful colors ... */
332
    white = rgb16(255,255,255);
333
    black = rgb16(0,0,0);
334
    red   = rgb16(255,0,0);
335
    gray  = rgb16(128,128,128);
336
    green = rgb16(0,200,0);
337
    yellow=rgb16(200,200,0);
338
    blue=rgb16(0,0,200);
339
 
340
    while(1){
341
    if(escapeflag){
342
         pos=menu();
343
         gamefinish=0;
344
         escapeflag=0;
345
    }
346
    if(pos==2){
347
        gamefinish=0;
348
        scenario(pos);
349
        init_ball(pos);
350
        pos=0;
351
        group_activate(1);
352
    }
353
    else if(pos==1){
354
        gamefinish=0;
355
        scenario(pos);
356
        init_ball(pos);
357
        pos=0;
358
        group_activate(1);
359
    }
360
    else if(pos==3){
361
        gamefinish=0;
362
        instructions();
363
        pos=0;
364
    }
365
    else if(pos==4){
366
        return 0;
367
    }
368
    }
369
}
370
 
371