Subversion Repositories shark

Rev

Rev 1373 | Rev 1378 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1162 tavani 1
/*
1349 giacomo 2
 * Project: S.Ha.R.K.
1162 tavani 3
 *
4
 * Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
5
 *
6
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
7
 *
8
 * http://www.sssup.it
9
 * http://retis.sssup.it
10
 * http://hartik.sssup.it
11
 */
12
 
13
#include "asteroid.h"
14
#include <string.h>
15
 
16
int score;             /* current player score */
17
int energy;            /* current player energy */
18
int enemy;             /* current player killed enemy */
19
int lives;             /* current player lives */
20
int crash;             /* astro vs. rock */
21
 
1373 giacomo 22
extern volatile int shark_running;
23
 
1162 tavani 24
TASK stat_write()
25
{
26
        char st[20];
27
        int l;
28
 
29
        while (1) {
30
 
31
                sem_wait(&mx_st_scr);
32
                sprintf(st, "%6d", score);
33
                sem_post(&mx_st_scr);
34
                sem_wait(&mx_grf);
35
                grx_text(st, GB_XMAX+80, GB_YMIN+ 8, RGB_YELLOW, RGB_BLACK);
36
                sem_post(&mx_grf);
37
 
38
                sem_wait(&mx_st_nrg);
39
                sprintf(st, "%6d", energy);
40
                if (energy <= 0) crash = 1;
41
                sem_post(&mx_st_nrg);
42
                sem_wait(&mx_grf);
43
                grx_text(st, GB_XMAX+80, GB_YMIN+24, RGB_YELLOW, RGB_BLACK);
44
                sem_post(&mx_grf);
45
 
46
                sem_wait(&mx_st_kil);
47
                sprintf(st, "%6d", enemy);
48
                sem_post(&mx_st_kil);
49
                sem_wait(&mx_grf);
50
                grx_text(st, GB_XMAX+80, GB_YMIN+40, RGB_YELLOW, RGB_BLACK);
51
                sem_post(&mx_grf);
52
 
53
                strcpy(st,LIVE_X);
54
                sem_wait(&mx_st_liv);
55
                l = lives;
56
                sem_post(&mx_st_liv);
57
                if (l == 0) strcpy(st,LIVE_0);
58
                if (l == 1) strcpy(st,LIVE_1);
59
                if (l == 2) strcpy(st,LIVE_2);
60
                if (l == 3) strcpy(st,LIVE_3);
61
                if (l == 4) strcpy(st,LIVE_4);
62
                if (l == 5) strcpy(st,LIVE_5);
63
                if (l == 6) strcpy(st,LIVE_6);
64
                sem_wait(&mx_grf);
65
                grx_text(st, GB_XMAX+80, GB_YMIN+56, RGB_YELLOW, RGB_BLACK);
66
                sem_post(&mx_grf);
67
 
68
                task_endcycle();
69
        }
70
}
71
 
72
void reset_game()
73
{
74
        sem_wait(&mx_st_liv);
75
        if (lives > 0) {
76
                lives--;
77
                sem_post(&mx_st_liv);
78
        } else {
79
                lives = LIVES_INIT;
80
                sem_post(&mx_st_liv);
81
 
82
                sem_wait(&mx_st_scr);
83
                score = 0;
84
                sem_post(&mx_st_scr);
85
 
86
                sem_wait(&mx_st_kil);
87
                enemy = 0;
88
                sem_post(&mx_st_kil);
89
        }
90
 
91
        sem_wait(&mx_st_nrg);
92
        energy = ENERGY_INIT;
93
        sem_post(&mx_st_nrg);
94
}
95
 
96
void new_game(KEY_EVT *k)
97
{
98
        reset_rock();
99
        reset_astro();
100
        reset_game();
101
}
102
 
103
void start_game(KEY_EVT *k)
104
{
105
        start_astro();
106
        start_rock();
107
}
108
 
109
void frame_stat()
110
{
111
        grx_text("Statistics", GB_XMAX+10, 45, RGB_BLUE, RGB_BLACK);
112
        grx_line(GB_XMAX+8,55,640-8,55,RGB_RED);
113
 
114
        grx_rect(GB_XMAX+7, GB_YMIN-3, 640-6, GB_YMIN+70, RGB_GREEN);
115
        grx_text("Score  : ", GB_XMAX+15, GB_YMIN+ 8, RGB_CYAN, RGB_BLACK);
116
        grx_text("Energy : ", GB_XMAX+15, GB_YMIN+24, RGB_CYAN, RGB_BLACK);
117
        grx_text("Enemy  : ", GB_XMAX+15, GB_YMIN+40, RGB_CYAN, RGB_BLACK);
118
        grx_text("Lives  : ", GB_XMAX+15, GB_YMIN+56, RGB_CYAN, RGB_BLACK);
119
}
120
 
121
void create_stat_task()
122
{
123
        SOFT_TASK_MODEL ms;
124
        PID pid;
125
 
126
        soft_task_default_model(ms);
127
        soft_task_def_ctrl_jet(ms);
128
        soft_task_def_met(ms, STAT_WCET);
129
        soft_task_def_period(ms,STAT_PERIOD);
130
        soft_task_def_usemath(ms);
131
        pid = task_create("StatWrite", stat_write, &ms, NULL);
132
        if (pid == NIL) {
1376 giacomo 133
                sys_shutdown_message("Could not create task <StatWrite>\n");
1373 giacomo 134
                shark_running = 0;
1376 giacomo 135
                return;
1162 tavani 136
        } else
137
                task_activate(pid);
138
}
139
 
140
void init_stat()
141
{
142
        KEY_EVT k;
143
 
144
        score  = 0;
145
        enemy  = 0;
146
        energy = ENERGY_INIT;
147
        lives  = LIVES_INIT;
148
 
149
        create_stat_task();
150
 
151
        k.flag = 0;
152
        k.scan = KEY_N;
153
        k.ascii = 'n';
1349 giacomo 154
        k.status = KEY_PRESSED;
155
        keyb_hook(k,new_game,FALSE);
1162 tavani 156
 
157
        k.flag = 0;
158
        k.scan = KEY_B;
159
        k.ascii = 'b';
1349 giacomo 160
        k.status = KEY_PRESSED;
161
        keyb_hook(k,start_game,FALSE);
1162 tavani 162
}
163