Subversion Repositories shark

Rev

Rev 1550 | 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
 
1606 tullio 13
/*
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 *
28
 */
29
 
1162 tavani 30
#include "asteroid.h"
31
#include <string.h>
32
 
33
int score;             /* current player score */
34
int energy;            /* current player energy */
35
int enemy;             /* current player killed enemy */
36
int lives;             /* current player lives */
37
int crash;             /* astro vs. rock */
38
 
39
TASK stat_write()
40
{
41
        char st[20];
42
        int l;
43
 
44
        while (1) {
45
 
46
                sem_wait(&mx_st_scr);
47
                sprintf(st, "%6d", score);
48
                sem_post(&mx_st_scr);
49
                sem_wait(&mx_grf);
50
                grx_text(st, GB_XMAX+80, GB_YMIN+ 8, RGB_YELLOW, RGB_BLACK);
51
                sem_post(&mx_grf);
52
 
53
                sem_wait(&mx_st_nrg);
54
                sprintf(st, "%6d", energy);
55
                if (energy <= 0) crash = 1;
56
                sem_post(&mx_st_nrg);
57
                sem_wait(&mx_grf);
58
                grx_text(st, GB_XMAX+80, GB_YMIN+24, RGB_YELLOW, RGB_BLACK);
59
                sem_post(&mx_grf);
60
 
61
                sem_wait(&mx_st_kil);
62
                sprintf(st, "%6d", enemy);
63
                sem_post(&mx_st_kil);
64
                sem_wait(&mx_grf);
65
                grx_text(st, GB_XMAX+80, GB_YMIN+40, RGB_YELLOW, RGB_BLACK);
66
                sem_post(&mx_grf);
67
 
68
                strcpy(st,LIVE_X);
69
                sem_wait(&mx_st_liv);
70
                l = lives;
71
                sem_post(&mx_st_liv);
72
                if (l == 0) strcpy(st,LIVE_0);
73
                if (l == 1) strcpy(st,LIVE_1);
74
                if (l == 2) strcpy(st,LIVE_2);
75
                if (l == 3) strcpy(st,LIVE_3);
76
                if (l == 4) strcpy(st,LIVE_4);
77
                if (l == 5) strcpy(st,LIVE_5);
78
                if (l == 6) strcpy(st,LIVE_6);
79
                sem_wait(&mx_grf);
80
                grx_text(st, GB_XMAX+80, GB_YMIN+56, RGB_YELLOW, RGB_BLACK);
81
                sem_post(&mx_grf);
82
 
83
                task_endcycle();
84
        }
85
}
86
 
87
void reset_game()
88
{
89
        sem_wait(&mx_st_liv);
90
        if (lives > 0) {
91
                lives--;
92
                sem_post(&mx_st_liv);
93
        } else {
94
                lives = LIVES_INIT;
95
                sem_post(&mx_st_liv);
96
 
97
                sem_wait(&mx_st_scr);
98
                score = 0;
99
                sem_post(&mx_st_scr);
100
 
101
                sem_wait(&mx_st_kil);
102
                enemy = 0;
103
                sem_post(&mx_st_kil);
104
        }
105
 
106
        sem_wait(&mx_st_nrg);
107
        energy = ENERGY_INIT;
108
        sem_post(&mx_st_nrg);
109
}
110
 
111
void new_game(KEY_EVT *k)
112
{
113
        reset_rock();
114
        reset_astro();
115
        reset_game();
116
}
117
 
118
void start_game(KEY_EVT *k)
119
{
120
        start_astro();
121
        start_rock();
122
}
123
 
124
void frame_stat()
125
{
126
        grx_text("Statistics", GB_XMAX+10, 45, RGB_BLUE, RGB_BLACK);
127
        grx_line(GB_XMAX+8,55,640-8,55,RGB_RED);
128
 
129
        grx_rect(GB_XMAX+7, GB_YMIN-3, 640-6, GB_YMIN+70, RGB_GREEN);
130
        grx_text("Score  : ", GB_XMAX+15, GB_YMIN+ 8, RGB_CYAN, RGB_BLACK);
131
        grx_text("Energy : ", GB_XMAX+15, GB_YMIN+24, RGB_CYAN, RGB_BLACK);
132
        grx_text("Enemy  : ", GB_XMAX+15, GB_YMIN+40, RGB_CYAN, RGB_BLACK);
133
        grx_text("Lives  : ", GB_XMAX+15, GB_YMIN+56, RGB_CYAN, RGB_BLACK);
134
}
135
 
136
void create_stat_task()
137
{
138
        SOFT_TASK_MODEL ms;
139
        PID pid;
140
 
141
        soft_task_default_model(ms);
142
        soft_task_def_ctrl_jet(ms);
143
        soft_task_def_met(ms, STAT_WCET);
144
        soft_task_def_period(ms,STAT_PERIOD);
145
        soft_task_def_usemath(ms);
146
        pid = task_create("StatWrite", stat_write, &ms, NULL);
147
        if (pid == NIL) {
1376 giacomo 148
                sys_shutdown_message("Could not create task <StatWrite>\n");
1550 pj 149
                exit(1);
1162 tavani 150
        } else
151
                task_activate(pid);
152
}
153
 
154
void init_stat()
155
{
156
        KEY_EVT k;
157
 
158
        score  = 0;
159
        enemy  = 0;
160
        energy = ENERGY_INIT;
161
        lives  = LIVES_INIT;
162
 
163
        create_stat_task();
164
 
165
        k.flag = 0;
166
        k.scan = KEY_N;
167
        k.ascii = 'n';
1349 giacomo 168
        k.status = KEY_PRESSED;
169
        keyb_hook(k,new_game,FALSE);
1162 tavani 170
 
171
        k.flag = 0;
172
        k.scan = KEY_B;
173
        k.ascii = 'b';
1349 giacomo 174
        k.status = KEY_PRESSED;
175
        keyb_hook(k,start_game,FALSE);
1162 tavani 176
}
177