Subversion Repositories shark

Rev

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