Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1664 pj 1
 
2
#include <kernel/kern.h>
3
#include <drivers/glib.h>
4
#include <drivers/keyb.h>
5
#include <semaphore.h>
6
#include <stdlib.h>
7
#include <math.h>
8
 
9
#define ESC					27
10
 
11
#define XMIN				0
12
#define YMIN				0
13
#define XMAX				640
14
#define YMAX				480
15
 
16
#define T_XMIN				120
17
#define T_XMAX				560
18
#define T_YMIN				150
19
#define T_YMAX				450
20
 
21
#define RXMIN				20
22
#define RYMIN				10
23
 
24
#define PIX_RATE			2
25
#define BACKGROUND			7
26
#define FOREGROUND			14
27
#define T_BACKGROUND		9
28
#define LEVEL				1
29
#define PLAYER				2
30
#define R_MAX				10
31
 
32
#define FISHGROUP			1
33
#define CROCGROUP			2
34
#define RHGROUP				3
35
#define HOOKGROUP			4
36
 
37
#define C_DISTANCE			2
38
 
39
int     DEF_COLOR[] 		= {0,3,4,5,6,7,8,10,11,12,13,14,15};
40
//double  tick				= 1.0;
41
PID     p1, p2;
42
sem_t   mutex, fmutex;
43
 
44
int FISH_PERIOD				= 80000;
45
int FISH_WCET				= 5000;
46
 
47
int CROC_PERIOD				= 60000;
48
int CROC_WCET				= 3000;
49
 
50
int ROD_PERIOD				= 40000;
51
int ROD_WCET				= 2000;
52
 
53
int HOOK_PERIOD				= 30000;
54
int HOOK_WCET				= 2000;
55
 
56
int BUBBLE_PERIOD			= 90000;
57
int BUBBLE_WCET				= 4000;
58
 
59
int TIME_PERIOD				= 1000000;
60
int TIME_WCET				= 7000;
61
 
62
int disp_x, disp_y;
63
int char_pix;
64
int player					= 1;
65
int esc_s					= -1;
66
int bubble_c				= 5;
67
 
68
const int KEY_UP			= 1;
69
const int KEY_DOWN			= 2;
70
const int KEY_LEFT			= 3;
71
const int KEY_RIGHT			= 4;
72
 
73
enum {
74
		LEFT				= 75,         /* Left arrow key */
75
		UP					= 72,         /* Up arrow key */
76
		RIGHT				= 77,         /* Right arrow key */
77
		DOWN				= 80          /* Down arrow key */
78
};
79
 
80
struct p_cordinates {
81
	int x;
82
	int y;
83
};
84
 
85
struct score {
86
	int points;
87
	int r_time;
88
	int f_count;
89
	int c4;
90
};
91
 
92
struct p_color {
93
	int c1;
94
	int c2;
95
	int c3;
96
	int c4;
97
};
98
 
99
struct p_props {
100
	struct p_cordinates pos;
101
	int size;
102
	int velocity;
103
	int hooked ;
104
	struct p_color color;
105
 int direction;
106
};
107
 
108
struct p_hook {
109
	struct p_cordinates h_pos;
110
	struct p_cordinates r1_pos;
111
	struct p_cordinates r2_pos;
112
	struct p_props* f_pos;
113
	struct p_color color;
114
	struct score score;
115
	int empty;
116
};
117
 
118
struct p_game {
119
	int level;
120
	int player;
121
	int fish_c;
122
	int croc_c;
123
	struct p_hook hook[2];
124
};
125
 
126
struct p_game game;
127
 
128
/******************************************************************/
129
 
130
inline void init_fish_rod(int rod) {
131
	game.hook[rod].h_pos.x  = T_XMIN + RXMIN;
132
	game.hook[rod].h_pos.y  = T_YMIN + RYMIN;
133
	game.hook[rod].r1_pos.x = T_XMIN;
134
	game.hook[rod].r1_pos.y = T_YMIN + RYMIN;
135
	game.hook[rod].r2_pos.x = T_XMIN + RXMIN;
136
	game.hook[rod].r2_pos.y = T_YMIN + RYMIN;
137
	game.hook[rod].empty    = -1;
138
	game.hook[rod].color.c1 = 0;
139
	game.hook[rod].color.c2 = 15;
140
	game.hook[rod].color.c3 = 4;
141
	game.hook[rod].color.c4 = 14;
142
	game.hook[rod].score.points   = 0;
143
	game.hook[rod].score.r_time   = 0;
144
	game.hook[rod].score.f_count  = 0;
145
}
146
 
147
/*----------------------------------------------------------------*/
148
 
149
inline void init_game() {
150
	game.level            = LEVEL;
151
	game.player           = 1;
152
	game.fish_c           = 8;
153
	game.croc_c           = 3;
154
	init_fish_rod(0);
155
}
156
 
157
/******************************************************************/
158
 
159
inline void set_fish_pos(struct p_cordinates* f_pos, int x, int y) {
160
	sem_wait(&fmutex);
161
	f_pos->x = x;
162
	f_pos->y = y;
163
	sem_post(&fmutex);
164
}
165
 
166
/*----------------------------------------------------------------*/
167
 
168
inline struct p_cordinates* get_hook_pos(int rod) {
169
	return &game.hook[rod].h_pos;
170
}
171
 
172
/*----------------------------------------------------------------*/
173
 
174
inline int is_hooked(rod) {
175
	return game.hook[rod].empty;
176
}
177
 
178
/*----------------------------------------------------------------*/
179
 
180
inline int calc_dist(struct p_cordinates* d1, int size, int direction) {
181
	struct p_cordinates* d2;
182
	int ret_val				= -1;
183
	int range					= size * direction;
184
	int i, x, y;
185
	for (i=0; i<player; i++) {
186
		if(is_hooked(i) < 0) {
187
			d2						= get_hook_pos(i);
188
			x							= (d1->x + range - d2->x) * (d1->x + range - d2->x);
189
			y							= (d1->y - d2->y) * (d1->y - d2->y);
190
 
191
			if(sqrt(x + y) <= C_DISTANCE) {
192
				ret_val			= i;
193
				break;
194
			}
195
		}
196
	}
197
	return ret_val;
198
}
199
 
200
/*----------------------------------------------------------------*/
201
 
202
inline int calc_area(struct p_cordinates* d1, int size, int direction) {
203
	struct p_cordinates* d2;
204
	int ret_val				= -1;
205
	int i, x1, x2, y1, y2;
206
	for (i=0; i<player; i++) {
207
		if(is_hooked(i) > 0) {
208
			d2						= get_hook_pos(i);
209
			y1						= d1->y - size;
210
			y2						= d1->y + size;
211
			if(direction < 0) {
212
				x1					= d1->x + size /2;
213
				x2					= d1->x + 4 * size;
214
			}
215
			else
216
			{
217
				x1					= d1->x - 4 * size;
218
				x2					= d1->x + size /2;
219
			}
220
/*
221
    char str[50];
222
    sprintf(str, "x=%d : x1=%d : x2=%d : y=%d : y1=%d : y2=%d", d2->x, x1, x2, d2->y, y1, y2);
223
    grx_text(str, 100, 450, FOREGROUND, 0);
224
*/
225
			if((d2->x > x1) & (d2->x < x2) & (d2->y > y1) & (d2->y < y2)) {
226
/*
227
        sprintf(str, "x=%d : x1=%d : x2=%d : y=%d : y1=%d : y2=%d", d2->x, x1, x2, d2->y, y1, y2);
228
        grx_text(str, 100, 400, FOREGROUND, 0);
229
*/
230
				ret_val			= i;
231
				break;
232
			}
233
		}
234
	}
235
	return ret_val;
236
}
237
 
238
/******************************************************************/
239
 
240
void draw_end()
241
{
242
	sem_wait(&mutex);
243
	grx_box(XMAX/3, YMAX/3, (XMAX/4)*3, YMAX/2, 0);
244
	grx_box(XMAX/3 + 2, YMAX/3 + 2 , (XMAX/4)*3 - 2, YMAX/2 - 2, BACKGROUND);
245
	grx_box(XMAX/3 + 4, YMAX/3 + 4 , (XMAX/4)*3 - 4, YMAX/2 - 4, 0);
246
	grx_text("Thank You For 'Fishing'...", (XMAX/3 + 20), YMAX/3 + 20 , FOREGROUND, 0);
247
//	grx_text("**************************", (XMAX/3 + 20), YMAX/3 + 40 , FOREGROUND, 0);
248
	grx_text("Press 'ESC' To Exit...", (XMAX/3 + 20), YMAX/3 + 40 , FOREGROUND, 0);
249
	sem_post(&mutex);
250
}
251
 
252
/*----------------------------------------------------------------*/
253
 
254
inline void g_clear(struct p_cordinates* rp, int size, int color)
255
{
256
	sem_wait(&mutex);
257
	grx_disc(rp->x, rp->y, size, color);
258
	sem_post(&mutex);
259
}
260
 
261
/*----------------------------------------------------------------*/
262
 
263
inline void draw_rh(int key_pressed) {
264
	int rod = 0;
265
//	int temp = 0;
266
//	char str[10];
267
 
268
	if(key_pressed == KEY_UP) {
269
		if((game.hook[rod].empty < 0) & (game.hook[rod].h_pos.y > (T_YMIN + RYMIN))) {
270
			game.hook[rod].h_pos.y -= PIX_RATE;
271
/*
272
			grx_text("UP...", 490, 0, FOREGROUND, 0);
273
			temp = game.hook[rod].h_pos.y;
274
			sprintf(str, "%d", temp);
275
			grx_text(str, 550, 0, FOREGROUND, 0);
276
*/
277
		}
278
	}
279
	else if (key_pressed == KEY_DOWN) {
280
		if((game.hook[rod].empty < 0) & (game.hook[rod].h_pos.y < (T_YMAX - RYMIN))) {
281
			game.hook[rod].h_pos.y += PIX_RATE;
282
/*
283
			grx_text("DOWN...", 490, 0, FOREGROUND, 0);
284
			temp = game.hook[rod].h_pos.y;
285
			sprintf(str, "%d", temp);
286
			grx_text(str, 550, 0, FOREGROUND, 0);
287
*/
288
 
289
// TESTING THE HOOK RETURN WITH FISH.
290
//      if(game.hook[rod].h_pos.y > 300){
291
//        game.hook[rod].empty = 1;
292
//      }
293
		}
294
	}
295
	else if (key_pressed == KEY_LEFT) {
296
		if(game.hook[rod].r2_pos.x > (T_XMIN + RXMIN)) {
297
			game.hook[rod].r2_pos.x -= (PIX_RATE * 2);
298
/*
299
			grx_text("LEFT...", 490, 0, FOREGROUND, 0);
300
			temp = game.hook[rod].r2_pos.x;
301
			sprintf(str, "%d", temp);
302
			grx_text(str, 550, 0, FOREGROUND, 0);
303
*/
304
		}
305
	}
306
	else if (key_pressed == KEY_RIGHT) {
307
		if(game.hook[rod].r2_pos.x < (T_XMAX - RXMIN)) {
308
			game.hook[rod].r2_pos.x += (PIX_RATE * 2);
309
/*
310
			grx_text("RIGHT...", 490, 0, FOREGROUND, 0);
311
			temp = game.hook[rod].r2_pos.x;
312
			sprintf(str, "%d", temp);
313
			grx_text(str, 550, 0, FOREGROUND, 0);
314
*/
315
		}
316
	}
317
}
318
 
319
/*----------------------------------------------------------------*/
320
 
321
void draw_bubble(struct p_props* fp)
322
{
323
	sem_wait(&mutex);
324
	grx_circle(fp->pos.x, fp->pos.y, fp->size, fp->color.c1);
325
	sem_post(&mutex);
326
}
327
 
328
/*----------------------------------------------------------------*/
329
 
330
inline void draw_croc(struct p_props* cp, int direction)
331
{
332
	sem_wait(&mutex);
333
 
334
	int tlen = cp->size/4;
335
	if(direction == -1) {
336
		grx_disc(cp->pos.x, cp->pos.y, cp->size, cp->color.c1);
337
		grx_disc(cp->pos.x - cp->size, cp->pos.y, cp->size, cp->color.c2);
338
		grx_box(cp->pos.x, cp->pos.y - tlen, cp->pos.x + (cp->size * 2), cp->pos.y + tlen, cp->color.c1);
339
		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);
340
		grx_disc(cp->pos.x + (cp->size * 4), cp->pos.y, cp->size, cp->color.c2);
341
	}
342
	else {
343
		grx_disc(cp->pos.x, cp->pos.y, cp->size, cp->color.c1);
344
		grx_disc(cp->pos.x + cp->size, cp->pos.y, cp->size, cp->color.c2);
345
		grx_box(cp->pos.x - (cp->size * 2), cp->pos.y - tlen, cp->pos.x, cp->pos.y + tlen, cp->color.c1);
346
		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);
347
		grx_disc(cp->pos.x - (cp->size * 4), cp->pos.y, cp->size, cp->color.c2);
348
	}
349
  sem_post(&mutex);
350
}
351
 
352
/*----------------------------------------------------------------*/
353
 
354
inline void draw_f(struct p_cordinates* fp, struct p_color* color, int size, int direction)
355
{
356
//  char str[50];
357
//  int tmp;
358
	sem_wait(&mutex);
359
	int tlen = size * 2;
360
	if(direction == -1) {
361
		grx_disc(fp->x, fp->y, size, color->c1);
362
		grx_disc(fp->x + size, fp->y, size, color->c2);
363
		grx_disc(fp->x + tlen, fp->y, size, color->c3);
364
	}
365
	else {
366
		grx_disc(fp->x, fp->y, size, color->c1);
367
		grx_disc(fp->x - size, fp->y, size, color->c2);
368
		grx_disc(fp->x - tlen, fp->y, size, color->c3);
369
	}
370
//  tmp = calc_dist(&(fp->pos), fp->size);
371
//  sprintf(str, "%d", tmp);
372
//  grx_text(str, fp->pos.x, fp->pos.y, T_BACKGROUND, 14);
373
	sem_post(&mutex);
374
}
375
 
376
/*----------------------------------------------------------------*/
377
 
378
inline void draw_fish(struct p_props* fp)
379
{
380
 draw_f(&fp->pos, &fp->color, fp->size, fp->direction);
381
}
382
 
383
/*----------------------------------------------------------------*/
384
 
385
inline void draw_rod(struct p_hook* rp, int direction)
386
{
387
	sem_wait(&mutex);
388
	grx_line(rp->r1_pos.x, rp->r1_pos.y, rp->r2_pos.x, rp->r2_pos.y, rp->color.c1);
389
	grx_disc(rp->r2_pos.x, rp->r2_pos.y, 2, rp->color.c2);
390
	sem_post(&mutex);
391
}
392
 
393
/*----------------------------------------------------------------*/
394
 
395
inline void draw_hook(struct p_hook* rp, int direction)
396
{
397
	sem_wait(&mutex);
398
 
399
	if(rp->r2_pos.x < rp->h_pos.x) {
400
		grx_line(rp->r2_pos.x, rp->r2_pos.y, rp->h_pos.x, rp->h_pos.y, rp->color.c1);
401
		grx_line(rp->h_pos.x - 2, rp->h_pos.y - 2, rp->h_pos.x, rp->h_pos.y, rp->color.c3);
402
	}
403
	else {
404
		grx_line(rp->h_pos.x, rp->h_pos.y, rp->r2_pos.x, rp->r2_pos.y, rp->color.c1);
405
		grx_line(rp->h_pos.x, rp->h_pos.y, rp->h_pos.x + 2, rp->h_pos.y + 2, rp->color.c3);
406
	}
407
	grx_disc(rp->h_pos.x, rp->h_pos.y, 1, rp->color.c2);
408
	sem_post(&mutex);
409
}
410
 
411
/******************************************************************/
412
 
413
void end_game(void *arg) {
414
	grx_close();
415
	cprintf("Ciao...\n");
416
}
417
 
418
/*----------------------------------------------------------------*/
419
 
420
inline void disp_score(int rod) {
421
	char str[4];
422
	sem_wait(&mutex);
423
//	grx_text("FISHES : 0", disp_x*3, disp_y + 50, FOREGROUND, 0);
424
	sprintf(str, "%d", game.hook[rod].score.f_count);
425
	grx_text(str, disp_x*3 + 65, disp_y + 50, FOREGROUND, 0);
426
//	grx_text("LEVEL : '1'", disp_x*2, disp_y + 30, FOREGROUND, 0);
427
	sprintf(str, "%d", game.level);
428
	grx_text(str, disp_x*2 + 70, disp_y + 30, FOREGROUND, 0);
429
//	grx_text("TIME LEFT : 01:00", disp_x*3, disp_y + 30, FOREGROUND, 0);
430
//	sprintf(str, "%d", game.level);
431
//	grx_text("SCORE : 0", disp_x*2, disp_y + 50, FOREGROUND, 0);
432
	sprintf(str, "%d", game.hook[rod].score.points);
433
	grx_text(str, disp_x*2 + 65, disp_y + 50, FOREGROUND, 0);
434
	sem_post(&mutex);
435
}
436
 
437
/*----------------------------------------------------------------*/
438
 
439
inline void set_score(int rod) {
440
	game.hook[rod].score.f_count  += 1;
441
	game.hook[rod].score.points += 1;
442
	disp_score(rod);
443
//	game.hook[rod].score.r_time   = 0;
444
}
445
 
446
/****************************************************************/
447
 
448
TASK hook_t(void *arg) {
449
	int rod								= (int)arg;
450
	struct p_hook* rp			= &game.hook[rod];
451
	struct p_hook prp;
452
	int direction					= 1;
453
	prp.color.c1					= T_BACKGROUND;
454
	prp.color.c2					= T_BACKGROUND;
455
	prp.color.c3					= T_BACKGROUND;
456
	prp.r2_pos.x					= rp->r2_pos.x;
457
	prp.r2_pos.y					= rp->r2_pos.y;
458
	prp.h_pos.x						= rp->h_pos.x;
459
	prp.h_pos.y						= rp->h_pos.y;
460
 
461
	while (1) {
462
		if(game.hook[rod].empty > 0) {
463
			if(rp->h_pos.y > (T_YMIN + RYMIN)) {
464
				rp->h_pos.y					-= game.level;
465
				draw_f(&rp->h_pos, &prp.color, rp->f_pos->size, rp->f_pos->direction);
466
//				rp->f_pos->pos.x		= rp->h_pos.x;
467
//				rp->f_pos->pos.y		= rp->h_pos.y;
468
				set_fish_pos(&rp->f_pos->pos, rp->h_pos.x, rp->h_pos.y);
469
			}
470
			else
471
			{
472
				set_score(rod);
473
				rp->empty						= -1;
474
				rp->f_pos->hooked 	= -1;
475
				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)));
476
				rp->f_pos						= 0;
477
			}
478
		}
479
		draw_hook(&prp, direction);
480
		prp.h_pos.x					= rp->h_pos.x;
481
		prp.h_pos.y					= rp->h_pos.y;
482
		prp.r2_pos.x				= rp->r2_pos.x;
483
		rp->h_pos.x					= rp->r2_pos.x;
484
		draw_hook(rp, direction);
485
 
486
		task_endcycle();
487
	}
488
}
489
 
490
/*----------------------------------------------------------------*/
491
 
492
TASK rod_t(void *arg) {
493
	int rod								= (int)arg;
494
	struct p_hook* rp			= &game.hook[rod];
495
	struct p_hook prp;
496
	int direction					= 1;
497
	prp.color.c1					= T_BACKGROUND;
498
	prp.color.c2					= T_BACKGROUND;
499
	prp.r1_pos.x					= rp->r1_pos.x;
500
	prp.r1_pos.y					= rp->r1_pos.y;
501
	prp.r2_pos.x					= rp->r2_pos.x;
502
	prp.r2_pos.y					= rp->r2_pos.y;
503
 
504
	while (1) {
505
		draw_rod(&prp, direction);
506
		prp.r2_pos.x = rp->r2_pos.x;
507
		draw_rod(rp, direction);
508
 
509
		task_endcycle();
510
	}
511
}
512
 
513
/*----------------------------------------------------------------*/
514
 
515
TASK fish_t(void *arg) {
516
	struct p_props fp;
517
	struct p_props pfp;
518
//	int fp.direction				= 1;
519
//	int p_direction			= 1;
520
	int s_player				= -1;
521
	fp.color.c1					= DEF_COLOR[rand()%12];
522
	fp.color.c2					= DEF_COLOR[rand()%12];
523
	fp.color.c3					= T_BACKGROUND;
524
	fp.color.c4					= DEF_COLOR[rand()%12];
525
	fp.size							= (rand()%10)+3;
526
  fp.hooked						= -1;
527
	fp.pos.x						= T_XMIN + (fp.size * 3);
528
	fp.pos.y						= T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN) / game.fish_c) * (rand()%(game.fish_c - 1));
529
//	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)));
530
	fp.direction				= 1;
531
	pfp.pos.y						= fp.pos.y;
532
	pfp.color.c1				= T_BACKGROUND;
533
	pfp.color.c2				= T_BACKGROUND;
534
	pfp.color.c3				= T_BACKGROUND;
535
	pfp.color.c4				= T_BACKGROUND;
536
	pfp.size						= fp.size;
537
 
538
	if((rand()%10)>5) {
539
		fp.direction 			*= -1;
540
		fp.pos.x 					= T_XMAX - (fp.size * 3);
541
	}
542
	fp.pos.x 						+= fp.direction *  ((rand()%3) + PIX_RATE);
543
	pfp.pos.x 					= fp.pos.x;
544
 
545
//  char str[40];
546
 
547
	while (esc_s < 0) {
548
		if(fp.hooked < 0) {
549
			s_player 		= calc_dist(&fp.pos, fp.size, fp.direction);
550
			if(s_player >= 0) {
551
				game.hook[s_player].f_pos		= &fp;
552
				game.hook[s_player].empty		= 1;
553
				fp.hooked 									= 1;
554
			}
555
			else
556
			{
557
				fp.pos.x			+= fp.direction * PIX_RATE;
558
				fp.hooked			= -1;
559
				if((fp.pos.x >= (T_XMAX - fp.size)) || (fp.pos.x <= (T_XMIN + fp.size))) {
560
					fp.direction		*= -1;
561
					fp.pos.x		+= fp.direction * fp.size * 3;
562
//				  pfp.pos.x		= fp.pos.x;
563
				}
564
			}
565
		}
566
//    grx_text("AM HERE...", 0, 465, FOREGROUND, 0);
567
		draw_fish(&pfp);
568
		draw_fish(&fp);
569
		pfp.pos.x			= fp.pos.x;
570
		pfp.direction	= fp.direction;
571
		task_endcycle();
572
	}
573
}
574
 
575
/*----------------------------------------------------------------*/
576
 
577
TASK croc_t(void *arg) {
578
	struct p_props cp;
579
	struct p_props pcp;
580
//	int cp.direction=1;
581
	int p_direction=1;
582
	int s_player				= -1;
583
	cp.color.c1 = 2;
584
	cp.color.c2 = T_BACKGROUND;
585
	cp.size = 10;
586
	cp.pos.x = T_XMIN + (cp.size * 6);
587
	cp.pos.y = T_YMIN + (RYMIN * 2) + ((T_YMAX - T_YMIN - 100) / (game.fish_c - 3)) * (rand()%(game.fish_c - 3));
588
	cp.direction=1;
589
	pcp.pos.y = cp.pos.y;
590
	pcp.color.c1 = T_BACKGROUND;
591
	pcp.color.c2 = T_BACKGROUND;
592
	pcp.size = cp.size;
593
 
594
	if((rand()%10)>5) {
595
		cp.direction *= -1;
596
		cp.pos.x = T_XMAX - (cp.size * 6);
597
	}
598
	cp.pos.x += cp.direction * ((rand()%3) + PIX_RATE);
599
	pcp.pos.x = cp.pos.x;
600
 
601
	while (esc_s < 0) {
602
		s_player 		= calc_area(&cp.pos, cp.size, cp.direction);
603
    char str[40];
604
    sprintf(str, "%d", s_player);
605
    grx_text(str, 0, 450, FOREGROUND, 0);
606
		if(s_player >= 0) {
607
			grx_text("IN 1...", 0, 525, FOREGROUND, 0);
608
			game.hook[s_player].empty					= -1;
609
			game.hook[s_player].f_pos->hooked = -1;
610
			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)));
611
		}
612
		cp.pos.x += cp.direction * PIX_RATE;
613
		if((cp.pos.x >= (T_XMAX - (cp.size * 2))) || (cp.pos.x <= (T_XMIN + (cp.size * 2)))) {
614
			cp.direction *= -1;
615
			cp.pos.x += cp.direction * cp.size * 6;
616
//			pcp.pos.x = cp.pos.x;
617
		}
618
 
619
		draw_croc(&pcp, p_direction);
620
		draw_croc(&cp, cp.direction);
621
		p_direction = cp.direction;
622
		pcp.pos.x = cp.pos.x;
623
 
624
		task_endcycle();
625
	}
626
}
627
 
628
/*----------------------------------------------------------------*/
629
 
630
TASK timeinterval_t(void *arg) {
631
	struct timespec actual_timer;
632
	int max_time = *((int *)arg);
633
 
634
	int temp_sec = 0;
635
	long sec,min;
636
	char str[5];
637
	int len;
638
	TIME seme;
639
	//max_time = 2;
640
 
641
	while(max_time > 0){
642
		sys_gettime(&actual_timer);
643
		sec = actual_timer.tv_sec;
644
		min = sec / 60;
645
		sec %= 60;
646
		temp_sec = 60 - sec - 1;
647
		//if (temp_sec == 60){
648
		if (sec == 59 ){
649
			max_time--;
650
		}
651
		sprintf(str,"%02d:%02d",max_time,temp_sec);
652
		len = strlen(str);
653
		str[len] = '\0';
654
		sem_wait(&mutex);
655
//		grx_text(str ,10, 50, RED, 0);
656
		grx_text(str, disp_x*3 + 90, disp_y + 30, FOREGROUND, 0);
657
		sem_post(&mutex);
658
		task_endcycle();
659
	}
660
	grx_text("Time Over" ,10, 70, RED, 0);
661
	esc_s = 1;
662
	draw_end();
663
}
664
 
665
/*----------------------------------------------------------------*/
666
 
667
TASK bubble_t(void *arg) {
668
	struct p_props bp;
669
	struct p_props pbp;
670
	int direction=1;
671
	int idelay;
672
	bp.color.c1 = WHITE;
673
	bp.color.c2 = BACKGROUND;
674
	bp.size = 4;
675
	int xval = rand()%9;
676
	bp.pos.x = pbp.pos.x = T_XMIN + ((int)((T_XMAX-T_XMIN)/8 )) * xval;
677
	bp.pos.y = T_YMAX - (bp.size * 2);
678
	pbp.pos.y = bp.pos.y;
679
	pbp.color.c1 = BACKGROUND;
680
	pbp.color.c2 = BACKGROUND;
681
	pbp.size = bp.size;
682
 
683
	while (1) {
684
		bp.pos.y -= PIX_RATE*(rand()%3) + 1 ;
685
		if(bp.pos.y <=  (T_YMIN + bp.size) ) {
686
			for(idelay=0;idelay<999999;idelay++);
687
			draw_bubble(&pbp);
688
			xval = rand()%9;
689
			bp.pos.x = pbp.pos.x = T_XMIN + ((int)((T_XMAX-T_XMIN)/8 )) * xval;
690
			bp.pos.y = T_YMAX - (bp.size * 2);
691
			pbp.pos.y = bp.pos.y;
692
		}
693
 
694
		draw_bubble(&pbp);
695
		draw_bubble(&bp);
696
		pbp.pos.y = bp.pos.y;
697
		pbp.pos.x = bp.pos.x;
698
		task_endcycle();
699
	}
700
}
701
 
702
/****************************************************************/
703
 
704
void create_fish() {
705
	SOFT_TASK_MODEL m1;
706
	PID p1;
707
	int count = 0;
708
	int i = 0;
709
	do {
710
			soft_task_default_model (m1);
711
			soft_task_def_level     (m1, 1);
712
			soft_task_def_ctrl_jet  (m1);
713
			soft_task_def_arg       (m1, (void *)i);
714
			soft_task_def_met       (m1, FISH_WCET);
715
			soft_task_def_period    (m1, FISH_PERIOD);
716
			soft_task_def_group     (m1, FISHGROUP);
717
			soft_task_def_usemath   (m1);
718
			p1 = task_create("fish", fish_t, &m1, NULL);
719
			if (p1 == NIL) {
720
				grx_close();
721
				perror("Could not create task <fish>");
722
				sys_abort(1);
723
			}
724
			task_activate(p1);
725
			count++;
726
	}while(count < game.fish_c);
727
}
728
 
729
/*----------------------------------------------------------------*/
730
 
731
void create_croc() {
732
	SOFT_TASK_MODEL m1;
733
	PID p1;
734
	int count = 0;
735
	int i = 0;
736
	do {
737
			soft_task_default_model (m1);
738
			soft_task_def_level     (m1, 1);
739
			soft_task_def_ctrl_jet  (m1);
740
			soft_task_def_arg       (m1, (void *)i);
741
			soft_task_def_met       (m1, CROC_WCET);
742
			soft_task_def_period    (m1, CROC_PERIOD);
743
			soft_task_def_group     (m1, CROCGROUP);
744
			soft_task_def_usemath   (m1);
745
			p1 = task_create("croc", croc_t, &m1, NULL);
746
			if (p1 == NIL) {
747
				grx_close();
748
				perror("Could not create task <croc>");
749
				sys_abort(1);
750
			}
751
			int delay = 0;
752
			while(delay <= 500){
753
				delay++;
754
			}
755
			task_activate(p1);
756
			count++;
757
	}while(count <= game.croc_c);
758
}
759
 
760
/*----------------------------------------------------------------*/
761
 
762
void create_rod() {
763
	SOFT_TASK_MODEL m1;
764
	PID p1;
765
	int count = 0;
766
	int i = 0;
767
	do {
768
			soft_task_default_model (m1);
769
			soft_task_def_level     (m1, 1);
770
			soft_task_def_ctrl_jet  (m1);
771
			soft_task_def_arg       (m1, (void *)i);
772
			soft_task_def_met       (m1, ROD_WCET);
773
			soft_task_def_period    (m1, ROD_PERIOD);
774
			soft_task_def_group     (m1, RHGROUP);
775
			soft_task_def_usemath   (m1);
776
			p1 = task_create("rod", rod_t, &m1, NULL);
777
			if (p1 == NIL) {
778
				grx_close();
779
				perror("Could not create task <rod>");
780
				sys_abort(1);
781
			}
782
			int delay = 0;
783
			while(delay <= 500){
784
				delay++;
785
			}
786
			task_activate(p1);
787
			count++;
788
	}while(count < player);
789
}
790
 
791
/*----------------------------------------------------------------*/
792
 
793
void create_hook() {
794
	SOFT_TASK_MODEL m1;
795
	PID p1;
796
	int count = 0;
797
	int i = 0;
798
	do {
799
			soft_task_default_model (m1);
800
			soft_task_def_level     (m1, 1);
801
			soft_task_def_ctrl_jet  (m1);
802
			soft_task_def_arg       (m1, (void *)i);
803
			soft_task_def_met       (m1, HOOK_WCET);
804
			soft_task_def_period    (m1, HOOK_PERIOD);
805
			soft_task_def_group     (m1, HOOKGROUP);
806
			soft_task_def_usemath   (m1);
807
			p1 = task_create("hook", hook_t, &m1, NULL);
808
			if (p1 == NIL) {
809
				grx_close();
810
				perror("Could not create task <hook>");
811
				sys_abort(1);
812
			}
813
			int delay = 0;
814
			while(delay <= 500){
815
				delay++;
816
			}
817
			task_activate(p1);
818
			count++;
819
	}while(count < player);
820
}
821
 
822
/*----------------------------------------------------------------*/
823
 
824
void create_bubble() {
825
	SOFT_TASK_MODEL s1;
826
 
827
	PID p1;
828
	int count = 0;
829
	int i = 0;
830
 
831
	do {
832
			soft_task_default_model(s1);
833
			soft_task_def_level(s1,1);
834
			soft_task_def_ctrl_jet (s1);
835
			soft_task_def_arg(s1, (void *)i);
836
			soft_task_def_met(s1, BUBBLE_WCET);
837
			soft_task_def_period(s1,BUBBLE_PERIOD);
838
			soft_task_def_usemath(s1);
839
			p1 = task_create("bubble", bubble_t, &s1, NULL);
840
			if (p1 == NIL) {
841
				grx_close();
842
				perror("Could not create task <bubble>");
843
				sys_abort(1);
844
			}
845
			task_activate(p1);
846
			count++;
847
	}while(count <= bubble_c);
848
}
849
 
850
/*----------------------------------------------------------------*/
851
 
852
void create_timer(int max_time) {
853
	SOFT_TASK_MODEL s1;
854
	PID p1;
855
	int i = max_time;
856
	void *p = &i;
857
	soft_task_default_model(s1);
858
	soft_task_def_level(s1,1);
859
	soft_task_def_ctrl_jet (s1);
860
	soft_task_def_arg(s1, p);
861
	soft_task_def_met(s1, TIME_WCET);
862
	soft_task_def_period(s1,TIME_PERIOD);
863
	soft_task_def_usemath(s1);
864
	p1 = task_create("timer", timeinterval_t, &s1, NULL);
865
	if (p1 == NIL) {
866
		grx_close();
867
		perror("Could not create task <timer>");
868
		sys_abort(1);
869
	}
870
	task_activate(p1);
871
}
872
 
873
/****************************************************************/
874
 
875
void init_display() {
876
	/* graphic card Initialization */
877
	if (grx_init() < 1) {
878
		sys_abort(1);
879
	}
880
 
881
	if (grx_open(XMAX, YMAX, 8) < 0) {
882
		cprintf("GRX Err\n");
883
		sys_abort(1);
884
	}
885
	sem_wait(&mutex);
886
	disp_x    = (XMAX - XMIN)/6;
887
	disp_y    = (T_YMIN - YMIN)/8;
888
	char_pix  = 12;
889
 
890
	// Initial Screen.
891
	grx_box(XMIN, YMIN, XMAX, YMAX, BACKGROUND);
892
	grx_box(disp_x, disp_y, (disp_x)*5, (disp_y)*6, 0);
893
	grx_box(disp_x + 2, disp_y + 2, (disp_x)*5 - 2, (disp_y)*6 - 2, BACKGROUND);
894
	grx_box(disp_x + 4, disp_y + 4, (disp_x)*5 - 4, (disp_y)*6 - 4, 0);
895
	grx_text("ENJOY FISHING", disp_x*3 - (char_pix * 6), disp_y + 10, FOREGROUND, 0);
896
	grx_text("LEVEL : '1'", disp_x*2, disp_y + 30, FOREGROUND, 0);
897
	grx_text("TIME LEFT :", disp_x*3, disp_y + 30, FOREGROUND, 0);
898
	grx_text("FISHES : ", disp_x*3, disp_y + 50, FOREGROUND, 0);
899
	grx_text("SCORE : ", disp_x*2, disp_y + 50, FOREGROUND, 0);
900
	grx_text("Press 'ESC' to Exit...", disp_x*3 - (char_pix * 6), disp_y*6 - char_pix - 3, FOREGROUND, 0);
901
 
902
  // Tank Draw.
903
	grx_box(T_XMIN - 4, T_YMIN - 4, T_XMAX + 4, T_YMAX + 4, 0);
904
	grx_rect(T_XMIN - 3, T_YMIN - 3, T_XMAX + 3, T_YMAX + 3, 0);
905
	grx_box(T_XMIN - 1, T_YMIN - 1, T_XMAX + 1, T_YMAX + 1 , T_BACKGROUND);
906
 
907
//	disp_x    = disp_x * 2;
908
//	disp_y    = disp_y + 25;
909
	sem_post(&mutex);
910
}
911
 
912
/****************************************************************/
913
 
914
int main(int argc, char **argv) {
915
	char c;             /* character from keyboard      */
916
	TIME seme;          /* used to init the random seed */
917
 
918
	sys_atrunlevel(end_game, NULL, RUNLEVEL_BEFORE_EXIT);
919
 
920
	init_display();
921
	seme = sys_gettime(NULL);
922
	srand(seme);
923
 
924
	init_game();
925
	create_fish();
926
	create_croc();
927
	create_rod();
928
	create_hook();
929
//	create_bubble();
930
	create_timer(2); // Pass the upper limit in minutes
931
 
932
	KEY_EVT k;
933
	k.ascii   = 0;
934
 
935
	do {
936
		keyb_getcode(&k, BLOCK);
937
 
938
		switch (k.scan) {
939
			case LEFT :
940
					draw_rh(KEY_LEFT);
941
					break;
942
			case UP :
943
					draw_rh(KEY_UP);
944
					break;
945
			case RIGHT :
946
					draw_rh(KEY_RIGHT);
947
					break;
948
			case DOWN :
949
					draw_rh(KEY_DOWN);
950
					break;
951
		}
952
	}while (k.ascii != ESC);
953
	esc_s = 1;
954
	draw_end();
955
 
956
	while((c = keyb_getch(BLOCK)) != ESC){
957
	}
958
 
959
	sys_end();
960
	return 0;
961
}
962
 
963
/******************************************************************/