Subversion Repositories shark

Compare Revisions

Ignore whitespace Rev 1161 → Rev 1162

/demos/trunk/astro/rock.c
0,0 → 1,314
/*
* Project: HARTIK (HA-rd R-eal TI-me K-ernel)
*
* Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
* Gerardo Lamastra <gerardo@sssup.it>
*
* Authors : Paolo Gai <pj@hartik.sssup.it>
* (see authors.txt for full list of hartik's authors)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://hartik.sssup.it
*/
 
/*
* Copyright (C) 2000 Paolo Gai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
#include "asteroid.h"
 
PID pid_RC;
int nrock;
int kill_rock;
rock_pos rocks[ROCK_NMAX];
 
void draw_rock(int x, int y, int r, int c)
{
sem_wait(&mx_grf);
grx_disc(x, y, r, c);
sem_post(&mx_grf);
}
 
TASK rock(rock_ini* ri)
{
int x, y, r; /* rock graphic position */
int ox, oy; /* rock old position */
int x0, y0; /* rock initial position */
float vx, vy; /* rock speed */
float vxo, vyo; /* rock initial speed */
float ty, tx;
float dt;
float rand_ang;
int rand_side;
int ab, ax, ay, i, l;
 
x = y = x0 = y0 = ox = oy = vx = vy = ab = 0;
 
i = ri->i;
r = ri->r;
 
rand_ang = ((rand()%120) - 60) * PI / 180.;
 
sem_wait(&mx_st_scr);
l = score / 50;
sem_post(&mx_st_scr);
 
sem_wait(&mx_mat);
vxo = - (ROCK_VEL + l) * sin(rand_ang);
vyo = - (ROCK_VEL + l) * cos(rand_ang);
sem_post(&mx_mat);
 
#ifdef ASTRO_MOVE
rand_side = rand()%4;
#else
rand_side = 1;
#endif
 
if (rand_side == 0) { // Bottom
vx = vxo;
vy = vyo;
}
if (rand_side == 1) { // Top
vx = -vxo;
vy = -vyo;
}
if (rand_side == 2) { // Right
vx = vyo;
vy = -vxo;
}
if (rand_side == 3) { // Left
vx = -vyo;
vy = vxo;
}
 
if ( (ri->x == 0) && (ri->y == 0) ) {
if (rand_side < 2) { // Bottom or Top
x = ox = x0 = GB_XMIN + (GB_XMAX-GB_XMIN)/4 + (rand()%((GB_XMAX-GB_XMIN)/2));
if (rand_side == 0) { // Bottom
y = oy = y0 = GB_YMAX - (r+1);
}
if (rand_side == 1) { // Top
y = oy = y0 = GB_YMIN + (r+1);
}
} else {
y = oy = y0 = GB_YMIN + (GB_YMAX-GB_YMIN)/4 + (rand()%((GB_YMAX-GB_YMIN)/2));
if (rand_side == 2) { // Right
x = ox = x0 = GB_XMAX - (r+1);
}
if (rand_side == 3) { // Left
x = ox = x0 = GB_XMIN + (r+1);
}
}
} else {
x = ox = x0 = ri->x;
y = oy = y0 = ri->y;
}
 
sem_wait(&mx_rk);
rocks[i].x = x;
rocks[i].y = y;
rocks[i].r = r;
sem_post(&mx_rk);
 
tx = 0;
ty = 0;
dt = ((float)ROCK_PERIOD)/100000;
 
while (1) {
y = y0 + vy * ty;
x = x0 + vx * tx;
 
sem_wait(&mx_rk);
rocks[i].x = x;
rocks[i].y = y;
r = rocks[i].r;
sem_post(&mx_rk);
 
draw_rock(ox, oy, r, RGB_BLACK);
 
if ((kill_rock) || (crash)){
nrock--;
sem_wait(&mx_rk);
rocks[i].pid = NIL;
sem_post(&mx_rk);
return 0;
}
 
ox = x;
oy = y;
 
sem_wait(&mx_xy);
ax = astro_x;
ay = astro_y;
sem_post(&mx_xy);
 
if (dist_xy(x, y, ax, ay) < (ASTRO_RADIUS/2+r)) {
if (!ab) {
sem_wait(&mx_st_nrg);
energy -= ENERGY_GOT;
sem_post(&mx_st_nrg);
ab = 1;
}
} else
ab = 0;
 
sem_wait(&mx_xy);
if (dist_xy(x, y, astro_x, astro_y) < (ASTRO_RADIUS+r)/2) crash = 1;
sem_post(&mx_xy);
 
#ifdef ASTRO_MOVE
if (x <= GB_XMIN + r) {
x0 = x = GB_XMAX - (r+1);
y0 = y;
tx = ty = 0;
}
if (x >= GB_XMAX - r) {
x0 = x = GB_XMIN + (r+1);
y0 = y;
tx = ty = 0;
}
if (y <= GB_YMIN + r) {
x0 = x;
y0 = y = GB_YMAX - (r+1);
tx = ty = 0;
}
if (y >= GB_YMAX - r) {
x0 = x;
y0 = y = GB_YMIN + (r+1);
tx = ty = 0;
}
#else
if ( (x <= GB_XMIN + r) || (x >= GB_XMAX - r) || (y >= GB_YMAX - 2*r) ) {
if (y >= GB_YMAX - 2*r) {
sem_wait(&mx_st_nrg);
energy -= ENERGY_GOT;
sem_post(&mx_st_nrg);
}
nrock--;
sem_wait(&mx_rk);
rocks[i].pid = NIL;
sem_post(&mx_rk);
return 0;
}
#endif
draw_rock(ox, oy, r, RGB_YELLOW);
 
ty += dt;
tx += dt;
 
task_endcycle();
}
}
 
void rock_create(rock_ini* ri)
{
SOFT_TASK_MODEL mp;
PID pid;
int i;
 
soft_task_default_model(mp);
soft_task_def_level(mp,1);
soft_task_def_ctrl_jet(mp);
soft_task_def_group(mp, ROCK_GROUP);
soft_task_def_met(mp, ROCK_WCET);
soft_task_def_period(mp,ROCK_PERIOD);
soft_task_def_usemath(mp);
i = -1;
do {
i++;
sem_wait(&mx_rk);
pid = rocks[i].pid;
sem_post(&mx_rk);
} while (pid != NIL);
ri->i = i;
soft_task_def_arg(mp, (void *)ri);
pid = task_create("Rock", rock, &mp, NULL);
 
if (pid != NIL) {
sem_wait(&mx_rk);
rocks[i].pid = pid;
sem_post(&mx_rk);
task_activate(pid);
nrock++;
}
}
 
TASK rock_creator()
{
while (1) {
sem_wait(&mx_rn);
if (rock_new.i == 0) {
rock_create(&rock_new);
rock_new.i = -1;
}
sem_post(&mx_rn);
 
if ((nrock < ROCK_NMAX/2) && (!kill_rock) && (!crash)) {
if ((rand()%ROCK_NMAX) > nrock) {
rock_new.r = ROCK_RADIUS_I;
rock_new.x = rock_new.y = 0;
rock_create(&rock_new);
}
}
task_endcycle();
}
}
 
void reset_rock()
{
kill_rock = 1;
}
 
void start_rock()
{
kill_rock = 0;
}
 
void create_rock_task()
{
SOFT_TASK_MODEL ms;
 
soft_task_default_model(ms);
soft_task_def_level(ms,1);
soft_task_def_ctrl_jet(ms);
soft_task_def_met(ms, ROCK_WCET);
soft_task_def_period(ms,ROCK_PERIOD*10);
soft_task_def_usemath(ms);
pid_RC = task_create("RockCreator", rock_creator, &ms, NULL);
if (pid_RC == NIL) {
grx_close();
perror("Could not create task <RockCreator>");
sys_end();
} else
task_activate(pid_RC);
}
 
void init_rock()
{
int i;
 
nrock = 0;
kill_rock = 1;
for ( i=0; i<ROCK_NMAX; i++ ) rocks[i].pid = NIL;
 
create_rock_task();
}
 
/demos/trunk/astro/asteroid.h
0,0 → 1,185
/*
* Project: HARTIK (HA-rd R-eal TI-me K-ernel)
*
* Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
* Gerardo Lamastra <gerardo@sssup.it>
*
* Authors : Paolo Gai <pj@hartik.sssup.it>
* (see authors.txt for full list of hartik's authors)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://hartik.sssup.it
*/
 
/*
* Copyright (C) 2000 Paolo Gai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
#include <kernel/func.h>
#include <stdlib.h>
#include <drivers/keyb.h>
#include <drivers/glib.h>
#include <semaphore.h>
#include "modules/sem.h"
#include "modules/hartport.h"
 
#include <math.h>
 
#define ASTRO_MOVE/* If defined then astro else defender */
 
// DA CALCOLARE
#define RGB_BLACK rgb16( 0, 0, 0)
#define RGB_GRAY rgb16(127,127,127)
#define RGB_WHITE rgb16(255,255,255)
#define RGB_RED rgb16(255, 0, 0)
#define RGB_GREEN rgb16( 0,255, 0)
#define RGB_BLUE rgb16( 0, 0,255)
#define RGB_YELLOW rgb16(255,255, 0)
#define RGB_MAGENTA rgb16(255, 0,255)
#define RGB_CYAN rgb16( 0,255,255)
#define RGB_D_RED rgb16(127, 0, 0)
#define RGB_D_GREEN rgb16( 0,127, 0)
#define RGB_D_BLUE rgb16( 0, 0,127)
#define RGB_D_YELLOW rgb16(127,127, 0)
#define RGB_D_MAGENTA rgb16(127, 0,127)
#define RGB_D_CYAN rgb16( 0,127,127)
 
#define LIVE_0 " "
#define LIVE_1 " *"
#define LIVE_2 " **"
#define LIVE_3 " ***"
#define LIVE_4 " ****"
#define LIVE_5 " *****"
#define LIVE_6 "******"
#define LIVE_X "------"
 
/* GameBoard constants */
#define GB_YMIN 65 /* position of the top */
#define GB_YMAX 460 /* position of the bottom */
#define GB_XMIN 5 /* min position X of the asteroid */
#define GB_XMAX 505 /* max position X of the asteroid */
 
/* Asteroid constants */
#define ASTEROID_RADIUS 4 /* radius of the asteroid */
#define ASTEROID_NMAX 60 /* max number of asteroids */
 
#define ASTEROID_GROUP 2 /* task group of asteroids */
 
/* Astro constants */
#ifdef ASTRO_MOVE
#define ASTRO_Y (GB_YMAX+GB_YMIN)/2 /* Y position of the astro */
#else
#define ASTRO_Y GB_YMAX-22 /* Y position of the astro */
#endif
#define ASTRO_X (GB_XMAX+GB_XMIN)/2 /* X position of the astro */
#define ASTRO_MAX_VEL ASTRO_VEL_INC*5 /* Max astro velocity */
#ifdef ASTRO_MOVE
#define ASTRO_MAX_GRAD 180 /* Max astro angle */
#else
#define ASTRO_MAX_GRAD 80 /* Max astro angle */
#endif
#define ASTRO_GRAD_INC 10 /* angular variation */
#define ASTRO_VEL_INC 3 /* velocity variation */
#define ASTRO_RADIUS 16 /* Dimension of the astro */
#define ASTRO_PERIOD 10000
#define ASTRO_MOVE_PERIOD 5000
#define ASTRO_WCET 300
 
/* Shot constants */
#define SHOT_RADIUS 1 /* radius of the shot */
#define SHOT_VEL 35 /* speed of the shot */
#define SHOT_NMAX 30 /* max number of shots */
 
#define SHOT_GROUP 3 /* task group of shots */
 
#define SHOT_PERIOD 10000
#define SHOT_WCET 500
 
/* Rock constants */
#define ROCK_RADIUS_I 8 /* initial radius of the rock */
#define ROCK_RADIUS_S 4 /* radius of the rock after one shot */
#define ROCK_VEL 6 /* speed of the rock */
#define ROCK_NMAX 8 /* max number of rocks */
 
#define ROCK_GROUP 4 /* task group of rocks */
 
#define ROCK_PERIOD 20000
#define ROCK_WCET 500
 
/* Statistic constants */
#define ENERGY_INIT 200
#define ENERGY_SHOT 2
#define ENERGY_GOT 10
#define SCORE_GOT 2
#define LIVES_INIT 3
 
#define STAT_PERIOD 400000
#define STAT_WCET 400
 
/* Statistic constants */
#define LOOK_PERIOD 2000
#define LOOK_WCET 400
 
 
typedef struct {
PID pid;
int x, y, r;
} rock_pos;
 
typedef struct {
int i;
int x, y, r;
} rock_ini;
 
double dist_xy(int x1, int y1, int x2, int y2);
void draw_rock(int x, int y, int r, int c);
void rock_create(rock_ini* ri);
void frame_astro();
void frame_stat();
void init_astro();
void init_stat();
void init_rock();
void reset_astro();
void reset_rock();
void start_astro();
void start_rock();
void reset_game();
 
extern sem_t mx_mat, mx_grf; /* mutex semaphores */
extern sem_t mx_pos, mx_vel; /* mutex semaphores */
extern sem_t mx_xy, mx_rk, mx_rn; /* mutex semaphores */
extern sem_t mx_st_scr, mx_st_nrg; /* mutex semaphores */
extern sem_t mx_st_kil, mx_st_liv; /* mutex semaphores */
extern int nshot; /* number of shot active */
extern int nrock; /* number of rock active */
extern int astro_x, astro_y; /* astro position */
extern int astro_grad; /* astro angolar position */
extern int astro_vel; /* astro velocity */
extern int score; /* current player score */
extern int energy; /* current player energy */
extern int enemy; /* current player strikes */
extern int lives; /* current player lives*/
extern int kill_rock; /* kill active rocks */
extern int kill_shot; /* kill active shots */
extern int freez_astro; /* turn of control pad */
extern int crash; /* astro vs. rock */
extern rock_pos rocks[ROCK_NMAX]; /* rocks position */
extern rock_ini rock_new;
/demos/trunk/astro/initfile.c
0,0 → 1,78
/*
* Project: HARTIK (HA-rd R-eal TI-me K-ernel)
*
* Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
* Gerardo Lamastra <gerardo@sssup.it>
*
* Authors : Paolo Gai <pj@hartik.sssup.it>
* (see authors.txt for full list of hartik's authors)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://hartik.sssup.it
*/
 
/*
* Copyright (C) 2000 Paolo Gai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
#include "asteroid.h"
#include "kernel/kern.h"
#include "modules/edf.h"
#include "modules/cbs.h"
#include "modules/rr.h"
#include "modules/dummy.h"
 
/*+ sysyem tick in us +*/
#define TICK 0 //300
 
/*+ RR tick in us +*/
#define RRTICK 300
 
TIME __kernel_register_levels__(void *arg)
{
struct multiboot_info *mb = (struct multiboot_info *)arg;
 
EDF_register_level(EDF_ENABLE_ALL);
CBS_register_level(CBS_ENABLE_ALL, 0);
RR_register_level(RRTICK, RR_MAIN_YES, mb);
dummy_register_level();
 
SEM_register_module();
 
return TICK;
}
 
TASK __init__(void *arg)
{
struct multiboot_info *mb = (struct multiboot_info *)arg;
 
KEYB_PARMS kparms = BASE_KEYB;
 
HARTPORT_init();
 
keyb_set_map(itaMap);
keyb_def_ctrlC(kparms, NULL);
KEYB_init(&kparms);
 
__call_main__(mb);
 
return (void *)0;
}
/demos/trunk/astro/astro.c
0,0 → 1,591
/*
* Project: S.Ha.R.K.
*
* Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
*
* Authors : Paolo Gai <pj@hartik.sssup.it>
* (see authors.txt for full list of hartik's authors)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://shark.sssup.it
*/
 
/*
* Copyright (C) 2000 Paolo Gai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
#include "asteroid.h"
 
int nshot; /* number of shot active */
int astro_x, astro_y; /* astro position */
int astro_grad; /* astro angolar position */
PID pid_TR, pid_TL; /* pid of turn tasks */
int astro_vel; /* astro speed */
PID pid_SU, pid_SD; /* pid of acc tasks */
PID pid_SZ, pid_FL; /* pid of acc tasks */
int freez_astro; /* turn of pad*/
int kill_shot;
 
rock_ini rock_new;
 
TASK shot(int i)
{
int x, y; /* shot graphic position */
int ox, oy; /* shot old position */
int x0, y0; /* shot initial position */
float vx, vy; /* shot speed */
float ty, tx;
float dt;
float a_rad;
int a_vel;
 
int rx, ry, rr;
PID rp;
 
sem_wait(&mx_xy);
y = oy = y0 = astro_y;
x = ox = x0 = astro_x;
sem_post(&mx_xy);
 
sem_wait(&mx_pos);
a_rad = astro_grad * PI / 180.;
sem_post(&mx_pos);
sem_wait(&mx_vel);
a_vel = astro_vel;
sem_post(&mx_vel);
sem_wait(&mx_mat);
vx = - (SHOT_VEL + a_vel) * sin(a_rad);
vy = - (SHOT_VEL + a_vel) * cos(a_rad);
sem_post(&mx_mat);
tx = 0;
ty = 0;
dt = ((float)SHOT_PERIOD)/100000;
 
while (1) {
y = y0 + vy * ty;
x = x0 + vx * tx;
 
sem_wait(&mx_grf);
grx_disc(ox, oy, SHOT_RADIUS, RGB_BLACK);
sem_post(&mx_grf);
ox = x;
oy = y;
 
if ((kill_shot)||(crash)||(y < GB_YMIN+SHOT_RADIUS)||(x < GB_XMIN+SHOT_RADIUS)||(x > GB_XMAX-SHOT_RADIUS)||(y > GB_YMAX-SHOT_RADIUS)){
nshot--;
return 0;
}
 
for ( i=0; i<ROCK_NMAX; i++ ) {
sem_wait(&mx_rk);
rp = rocks[i].pid;
rx = rocks[i].x;
ry = rocks[i].y;
rr = rocks[i].r;
sem_post(&mx_rk);
 
if ((rp!=NIL) && (dist_xy(x, y, rx, ry) < (SHOT_RADIUS+rr))) {
draw_rock(rx, ry, rr, RGB_BLACK);
if (rr == ROCK_RADIUS_I) {
sem_wait(&mx_rk);
rocks[i].r = ROCK_RADIUS_S;
sem_post(&mx_rk);
sem_wait(&mx_rn);
rock_new.r = ROCK_RADIUS_S;
rock_new.x = rx;
rock_new.y = ry;
rock_new.i = 0;
sem_post(&mx_rn);
sem_wait(&mx_st_scr);
score += SCORE_GOT;
sem_post(&mx_st_scr);
} else {
task_kill(rp);
sem_wait(&mx_rk);
rocks[i].pid = NIL;
sem_post(&mx_rk);
nrock--;
sem_wait(&mx_st_scr);
score += 2*SCORE_GOT;
sem_post(&mx_st_scr);
}
sem_wait(&mx_st_nrg);
energy += ENERGY_GOT;
sem_post(&mx_st_nrg);
sem_wait(&mx_st_kil);
enemy++;
sem_post(&mx_st_kil);
nshot--;
return 0;
}
}
 
sem_wait(&mx_grf);
grx_disc(ox, oy, SHOT_RADIUS, RGB_RED);
sem_post(&mx_grf);
 
ty += dt;
tx += dt;
 
task_endcycle();
}
}
 
TASK astro()
{
float astro_rad;
int ox, oy;
float s, c;
 
//leggo posizione
sem_wait(&mx_pos);
astro_rad = astro_grad * PI / 180.;
sem_post(&mx_pos);
sem_wait(&mx_mat);
s = -sin(astro_rad);
c = -cos(astro_rad);
sem_post(&mx_mat);
sem_wait(&mx_xy);
ox = astro_x;
oy = astro_y;
sem_post(&mx_xy);
 
while (1) {
 
sem_wait(&mx_grf);
//DRAW SHADOW ASTRO
grx_line(ox + 15.*s , oy + 15.*c , ox - 5.*c - 9.*s, oy + 5.*s - 9.*c, RGB_BLACK);
grx_line(ox + 15.*s , oy + 15.*c , ox + 5.*c - 9.*s, oy - 5.*s - 9.*c, RGB_BLACK);
grx_line(ox , oy , ox - 12.*c - 9.*s, oy + 12.*s - 9.*c, RGB_BLACK);
grx_line(ox , oy , ox + 12.*c - 9.*s, oy - 12.*s - 9.*c, RGB_BLACK);
grx_line(ox - 12.*c - 9.*s, oy + 12.*s - 9.*c, ox + 12.*c - 9.*s, oy - 12.*s - 9.*c, RGB_BLACK);
sem_post(&mx_grf);
 
sem_wait(&mx_pos);
astro_rad = astro_grad * PI / 180.;
sem_post(&mx_pos);
sem_wait(&mx_mat);
s = -sin(astro_rad);
c = -cos(astro_rad);
sem_post(&mx_mat);
sem_wait(&mx_xy);
ox = astro_x;
oy = astro_y;
sem_post(&mx_xy);
 
sem_wait(&mx_grf);
//DRAW ASTRO
grx_line(ox + 15.*s , oy + 15.*c , ox - 5.*c - 9.*s, oy + 5.*s - 9.*c, RGB_WHITE);
grx_line(ox + 15.*s , oy + 15.*c , ox + 5.*c - 9.*s, oy - 5.*s - 9.*c, RGB_WHITE);
grx_line(ox , oy , ox - 12.*c - 9.*s, oy + 12.*s - 9.*c, RGB_WHITE);
grx_line(ox , oy , ox + 12.*c - 9.*s, oy - 12.*s - 9.*c, RGB_WHITE);
grx_line(ox - 12.*c - 9.*s, oy + 12.*s - 9.*c, ox + 12.*c - 9.*s, oy - 12.*s - 9.*c, RGB_WHITE);
sem_post(&mx_grf);
 
/* {
int xxx;
for (xxx=0; xxx<10000; xxx++);
}*/
 
task_endcycle();
}
}
 
TASK look()
{
while (1) {
if (crash) {
reset_rock();
reset_astro();
if (nrock==0) {
reset_game();
crash = 0;
}
}
task_endcycle();
}
}
 
TASK turn(int i)
{
 
while (1) {
 
sem_wait(&mx_pos);
if (i==0)
astro_grad += 180;
else {
astro_grad += i;
}
if (astro_grad < -180) astro_grad += 360;
if (astro_grad > 180) astro_grad -= 360;
if (astro_grad < -ASTRO_MAX_GRAD) astro_grad = -ASTRO_MAX_GRAD;
if (astro_grad > ASTRO_MAX_GRAD) astro_grad = ASTRO_MAX_GRAD;
sem_post(&mx_pos);
 
task_endcycle();
}
}
 
TASK speed(int i)
{
 
while (1) {
 
sem_wait(&mx_vel);
if (i==0)
astro_vel = 0;
else {
astro_vel += i;
if (astro_vel < -ASTRO_MAX_VEL) astro_vel = -ASTRO_MAX_VEL;
if (astro_vel > ASTRO_MAX_VEL) astro_vel = ASTRO_MAX_VEL;
}
sem_post(&mx_vel);
 
task_endcycle();
}
}
 
TASK move()
{
int dv, x, y;
float drad;
 
drad = x = y = 0;
 
while (1) {
 
#ifdef ASTRO_MOVE
sem_wait(&mx_pos);
drad = astro_grad * PI / 180.;
sem_post(&mx_pos);
#endif
sem_wait(&mx_vel);
dv = astro_vel;
sem_post(&mx_vel);
sem_wait(&mx_xy);
x = astro_x;
y = astro_y;
sem_post(&mx_xy);
#ifdef ASTRO_MOVE
x -= dv * sin(drad);
y -= dv * cos(drad);
if (x < GB_XMIN + ASTRO_RADIUS) x = GB_XMAX - ASTRO_RADIUS;
if (x > GB_XMAX - ASTRO_RADIUS) x = GB_XMIN + ASTRO_RADIUS;
if (y < GB_YMIN + ASTRO_RADIUS) y = GB_YMAX - ASTRO_RADIUS;
if (y > GB_YMAX - ASTRO_RADIUS) y = GB_YMIN + ASTRO_RADIUS;
#else
x += dv;
if (x < GB_XMIN + 2*ASTRO_RADIUS) {
x = GB_XMIN + 2*ASTRO_RADIUS;
sem_wait(&mx_vel);
astro_vel = - astro_vel - ASTRO_VEL_INC;
sem_post(&mx_vel);
}
if (x > GB_XMAX - 2*ASTRO_RADIUS) {
x = GB_XMAX - 2*ASTRO_RADIUS;
sem_wait(&mx_vel);
astro_vel = - astro_vel + ASTRO_VEL_INC;
sem_post(&mx_vel);
}
#endif
sem_wait(&mx_xy);
astro_x = x;
astro_y = y;
sem_post(&mx_xy);
 
task_endcycle();
}
}
 
void pad(KEY_EVT *k)
{
if (freez_astro) return;
if (k->scan == KEY_O) task_activate(pid_TL);
if (k->scan == KEY_P) task_activate(pid_TR);
if (k->scan == KEY_S) task_activate(pid_SZ);
if (k->scan == KEY_Z) task_activate(pid_SD);
#ifdef ASTRO_MOVE
if (k->scan == KEY_A) task_activate(pid_SU);
if (k->scan == KEY_F) task_activate(pid_FL);
#else
if (k->scan == KEY_X) task_activate(pid_SU);
#endif
}
 
void new_shot(KEY_EVT *k)
{
SOFT_TASK_MODEL mp;
PID pid;
 
if ((nshot >= SHOT_NMAX)||(freez_astro)||(kill_shot)||(crash)) return;
 
soft_task_default_model(mp);
soft_task_def_level(mp,1);
soft_task_def_ctrl_jet(mp);
soft_task_def_arg(mp, (void *)nshot);
soft_task_def_group(mp, SHOT_GROUP);
soft_task_def_met(mp, SHOT_WCET);
soft_task_def_period(mp,SHOT_PERIOD);
soft_task_def_usemath(mp);
pid = task_create("Shot", shot, &mp, NULL);
 
if (pid != NIL) {
task_activate(pid);
nshot++;
sem_wait(&mx_st_nrg);
energy -= ENERGY_SHOT;
sem_post(&mx_st_nrg);
}
}
 
void start_astro()
{
freez_astro = 0;
kill_shot = 0;
}
 
void reset_astro()
{
freez_astro = 1;
sem_wait(&mx_xy);
astro_x = ASTRO_X;
astro_y = ASTRO_Y;
sem_post(&mx_xy);
sem_wait(&mx_pos);
astro_grad = 0;
sem_post(&mx_pos);
sem_wait(&mx_vel);
astro_vel = 0;
sem_post(&mx_vel);
kill_shot = 1;
}
 
void create_astro_task()
{
HARD_TASK_MODEL mp;
SOFT_TASK_MODEL ms;
PID pid;
int incr;
 
soft_task_default_model(ms);
soft_task_def_level(ms,1);
soft_task_def_ctrl_jet(ms);
soft_task_def_arg(ms, (void *)nshot);
soft_task_def_met(ms, ASTRO_WCET);
soft_task_def_period(ms,ASTRO_PERIOD);
soft_task_def_usemath(ms);
pid = task_create("Astro", astro, &ms, NULL);
if (pid == NIL) {
grx_close();
perror("Could not create task <astro>");
sys_end();
} else
task_activate(pid);
 
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_wcet(mp,LOOK_WCET);
hard_task_def_mit(mp, LOOK_PERIOD);
hard_task_def_usemath(mp);
pid = task_create("Taken", look, &mp, NULL);
if (pid == NIL) {
grx_close();
perror("Could not create task <Taken>");
sys_end();
} else
task_activate(pid);
 
incr = ASTRO_GRAD_INC;
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_arg(mp, (void *)incr);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, ASTRO_MOVE_PERIOD);
hard_task_def_aperiodic(mp);
hard_task_def_usemath(mp);
pid_TL = task_create("TurnLeft", turn, &mp, NULL);
if (pid_TL == NIL) {
grx_close();
perror("Could not create task <Turn L>");
sys_end();
}
 
incr = - ASTRO_GRAD_INC;
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_arg(mp, (void *)incr);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, ASTRO_MOVE_PERIOD);
hard_task_def_aperiodic(mp);
hard_task_def_usemath(mp);
pid_TR = task_create("TurnRight", turn, &mp, NULL);
if (pid_TR == NIL) {
grx_close();
perror("Could not create task <Turn R>");
sys_end();
}
 
incr = ASTRO_VEL_INC;
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_arg(mp, (void *)incr);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, ASTRO_PERIOD);
hard_task_def_aperiodic(mp);
hard_task_def_usemath(mp);
pid_SU = task_create("SpeedUP", speed, &mp, NULL);
if (pid_SU == NIL) {
grx_close();
perror("Could not create task <Speed UP>");
sys_end();
}
 
incr = - ASTRO_VEL_INC;
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_arg(mp, (void *)incr);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, ASTRO_PERIOD);
hard_task_def_aperiodic(mp);
hard_task_def_usemath(mp);
pid_SD = task_create("SpeedDOWN", speed, &mp, NULL);
if (pid_SD == NIL) {
grx_close();
perror("Could not create task <Speed DOWN>");
sys_end();
}
 
incr = 0;
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_arg(mp, (void *)incr);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, ASTRO_PERIOD);
hard_task_def_aperiodic(mp);
hard_task_def_usemath(mp);
pid_SZ = task_create("SpeedZERO", speed, &mp, NULL);
if (pid_SZ == NIL) {
grx_close();
perror("Could not create task <Speed ZERO>");
sys_end();
}
 
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, 6*ASTRO_PERIOD);
hard_task_def_usemath(mp);
pid = task_create("MoveAstro", move, &mp, NULL);
if (pid == NIL) {
grx_close();
perror("Could not create task <MoveAstro>");
sys_end();
} else
task_activate(pid);
 
#ifdef ASTRO_MOVE
incr = 0;
hard_task_default_model(mp);
hard_task_def_ctrl_jet(mp);
hard_task_def_arg(mp, (void *)incr);
hard_task_def_wcet(mp,ASTRO_WCET);
hard_task_def_mit(mp, ASTRO_PERIOD);
hard_task_def_aperiodic(mp);
hard_task_def_usemath(mp);
pid_FL = task_create("FlipAstro", turn, &mp, NULL);
if (pid_FL == NIL) {
grx_close();
perror("Could not create task <Flip Astro>");
sys_end();
}
#endif
}
 
void frame_astro()
{
grx_text("Game", 10, 45, RGB_BLUE, RGB_BLACK);
grx_line(GB_XMIN-2,55,GB_XMAX+2,55,RGB_RED);
 
grx_rect(GB_XMIN-3, GB_YMIN-3, GB_XMAX+3, GB_YMAX+3, RGB_GREEN);
 
#ifndef ASTRO_MOVE
grx_rect(GB_XMIN-1, GB_YMAX-1, GB_XMAX+1, GB_YMAX+1, RGB_CYAN);
#endif
}
 
void init_astro()
{
KEY_EVT k;
 
crash = 0;
freez_astro = 1;
kill_shot = 1;
astro_vel = 0;
astro_grad = 0;
astro_x = ASTRO_X;
astro_y = ASTRO_Y;
 
create_astro_task();
 
k.flag = 0;
k.scan = KEY_SPC;
k.ascii = ' ';
keyb_hook(k,new_shot);
 
k.flag = 0;
k.scan = KEY_O;
k.ascii = 'o';
keyb_hook(k,pad);
 
k.flag = 0;
k.scan = KEY_P;
k.ascii = 'p';
keyb_hook(k,pad);
 
k.flag = 0;
k.scan = KEY_S;
k.ascii = 's';
keyb_hook(k,pad);
 
k.flag = 0;
k.scan = KEY_Z;
k.ascii = 'z';
keyb_hook(k,pad);
 
#ifdef ASTRO_MOVE
k.flag = 0;
k.scan = KEY_A;
k.ascii = 'a';
keyb_hook(k,pad);
 
k.flag = 0;
k.scan = KEY_F;
k.ascii = 'f';
keyb_hook(k,pad);
#else
k.flag = 0;
k.scan = KEY_X;
k.ascii = 'x';
keyb_hook(k,pad);
 
#endif
}
/demos/trunk/astro/asteroid.c
0,0 → 1,139
/*
* Project: S.Ha.R.K.
*
* Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
*
* Authors : Paolo Gai <pj@hartik.sssup.it>
* (see authors.txt for full list of hartik's authors)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://shark.sssup.it
*/
 
/*
* Copyright (C) 2000 Paolo Gai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
#include "asteroid.h"
#include <kernel/kern.h>
 
sem_t mx_mat, mx_grf, mx_pos, mx_vel, mx_xy, mx_rk, mx_rn; /* mutex semaphores */
sem_t mx_st_nrg, mx_st_scr, mx_st_kil, mx_st_liv; /* mutex semaphores */
 
double dist_xy(int x1, int y1, int x2, int y2)
{
double dst;
int dx, dy;
 
dx = x2 - x1;
dy = y2 - y1;
sem_wait(&mx_mat);
dst = sqrt(dx*dx + dy*dy);
sem_post(&mx_mat);
 
return dst;
 
}
 
void endfun(KEY_EVT *k)
{
cprintf("Ctrl-Brk pressed! Ending...\n");
sys_end();
}
 
void my_grx_close(void *arg)
{
grx_close();
kern_printf("Graphic mode closed.\n");
}
 
void frame_main()
{
#ifdef ASTRO_MOVE
grx_text("S.Ha.R.K. - Asteroid 2002 RC1", 0, 0, RGB_GREEN, RGB_BLACK );
#else
grx_text("S.Ha.R.K. - Defender 2002 RC1", 0, 0, RGB_GREEN, RGB_BLACK );
#endif
grx_text("by Marinoni Mauro" ,24,16, RGB_GREEN, RGB_BLACK );
grx_text(" Scaricabarozzi Mattia" ,24,24, RGB_GREEN, RGB_BLACK );
 
grx_text("Ctrl-C, Ctrr-C: exit" ,300, 0, RGB_GRAY, RGB_BLACK );
grx_text("O-P : turn left/right" ,300,16, RGB_GRAY, RGB_BLACK );
#ifdef ASTRO_MOVE
grx_text("A-Z : speed up/down" ,300,24, RGB_GRAY, RGB_BLACK );
grx_text("S : stop engine" ,300,32, RGB_GRAY, RGB_BLACK );
grx_text("F : flip astro" ,300,40, RGB_GRAY, RGB_BLACK );
#else
grx_text("Z-X : move left/right" ,300,24, RGB_GRAY, RGB_BLACK );
#endif
grx_text("Space : fire" ,485,16, RGB_GRAY, RGB_BLACK );
grx_text("N : new game" ,485,24, RGB_GRAY, RGB_BLACK );
grx_text("B : begin game" ,485,32, RGB_GRAY, RGB_BLACK );
 
frame_stat();
frame_astro();
}
 
int main(int argc, char **argv)
{
KEY_EVT k;
TIME seme;
int modenum;
 
k.flag = CNTR_BIT;
k.scan = KEY_C;
k.ascii = 'c';
keyb_hook(k,endfun);
 
k.flag = CNTL_BIT;
k.scan = KEY_C;
k.ascii = 'c';
keyb_hook(k,endfun);
 
sem_init(&mx_mat,0,1);
sem_init(&mx_grf,0,1);
sem_init(&mx_pos,0,1);
sem_init(&mx_vel,0,1);
sem_init(&mx_xy ,0,1);
sem_init(&mx_rk ,0,1);
sem_init(&mx_rn ,0,1);
sem_init(&mx_st_scr,0,1);
sem_init(&mx_st_nrg,0,1);
sem_init(&mx_st_kil,0,1);
sem_init(&mx_st_liv,0,1);
 
seme = sys_gettime(NULL);
srand(seme);
 
sys_atrunlevel(my_grx_close, NULL, RUNLEVEL_BEFORE_EXIT);
 
grx_init();
modenum = grx_getmode(640, 480, 16);
grx_setmode(modenum);
 
frame_main();
 
init_stat();
init_astro();
init_rock();
 
return 0;
}
/demos/trunk/astro/stat.c
0,0 → 1,183
/*
* Project: HARTIK (HA-rd R-eal TI-me K-ernel)
*
* Coordinators: Giorgio Buttazzo <giorgio@sssup.it>
* Gerardo Lamastra <gerardo@sssup.it>
*
* Authors : Paolo Gai <pj@hartik.sssup.it>
* (see authors.txt for full list of hartik's authors)
*
* ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
*
* http://www.sssup.it
* http://retis.sssup.it
* http://hartik.sssup.it
*/
 
/*
* Copyright (C) 2000 Paolo Gai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
 
#include "asteroid.h"
#include <string.h>
 
int score; /* current player score */
int energy; /* current player energy */
int enemy; /* current player killed enemy */
int lives; /* current player lives */
int crash; /* astro vs. rock */
 
TASK stat_write()
{
char st[20];
int l;
 
while (1) {
 
sem_wait(&mx_st_scr);
sprintf(st, "%6d", score);
sem_post(&mx_st_scr);
sem_wait(&mx_grf);
grx_text(st, GB_XMAX+80, GB_YMIN+ 8, RGB_YELLOW, RGB_BLACK);
sem_post(&mx_grf);
 
sem_wait(&mx_st_nrg);
sprintf(st, "%6d", energy);
if (energy <= 0) crash = 1;
sem_post(&mx_st_nrg);
sem_wait(&mx_grf);
grx_text(st, GB_XMAX+80, GB_YMIN+24, RGB_YELLOW, RGB_BLACK);
sem_post(&mx_grf);
 
sem_wait(&mx_st_kil);
sprintf(st, "%6d", enemy);
sem_post(&mx_st_kil);
sem_wait(&mx_grf);
grx_text(st, GB_XMAX+80, GB_YMIN+40, RGB_YELLOW, RGB_BLACK);
sem_post(&mx_grf);
 
strcpy(st,LIVE_X);
sem_wait(&mx_st_liv);
l = lives;
sem_post(&mx_st_liv);
if (l == 0) strcpy(st,LIVE_0);
if (l == 1) strcpy(st,LIVE_1);
if (l == 2) strcpy(st,LIVE_2);
if (l == 3) strcpy(st,LIVE_3);
if (l == 4) strcpy(st,LIVE_4);
if (l == 5) strcpy(st,LIVE_5);
if (l == 6) strcpy(st,LIVE_6);
sem_wait(&mx_grf);
grx_text(st, GB_XMAX+80, GB_YMIN+56, RGB_YELLOW, RGB_BLACK);
sem_post(&mx_grf);
 
task_endcycle();
}
}
 
void reset_game()
{
sem_wait(&mx_st_liv);
if (lives > 0) {
lives--;
sem_post(&mx_st_liv);
} else {
lives = LIVES_INIT;
sem_post(&mx_st_liv);
 
sem_wait(&mx_st_scr);
score = 0;
sem_post(&mx_st_scr);
 
sem_wait(&mx_st_kil);
enemy = 0;
sem_post(&mx_st_kil);
}
 
sem_wait(&mx_st_nrg);
energy = ENERGY_INIT;
sem_post(&mx_st_nrg);
}
 
void new_game(KEY_EVT *k)
{
reset_rock();
reset_astro();
reset_game();
}
 
void start_game(KEY_EVT *k)
{
start_astro();
start_rock();
}
 
void frame_stat()
{
grx_text("Statistics", GB_XMAX+10, 45, RGB_BLUE, RGB_BLACK);
grx_line(GB_XMAX+8,55,640-8,55,RGB_RED);
 
grx_rect(GB_XMAX+7, GB_YMIN-3, 640-6, GB_YMIN+70, RGB_GREEN);
grx_text("Score : ", GB_XMAX+15, GB_YMIN+ 8, RGB_CYAN, RGB_BLACK);
grx_text("Energy : ", GB_XMAX+15, GB_YMIN+24, RGB_CYAN, RGB_BLACK);
grx_text("Enemy : ", GB_XMAX+15, GB_YMIN+40, RGB_CYAN, RGB_BLACK);
grx_text("Lives : ", GB_XMAX+15, GB_YMIN+56, RGB_CYAN, RGB_BLACK);
}
 
void create_stat_task()
{
SOFT_TASK_MODEL ms;
PID pid;
 
soft_task_default_model(ms);
soft_task_def_level(ms,1);
soft_task_def_ctrl_jet(ms);
soft_task_def_met(ms, STAT_WCET);
soft_task_def_period(ms,STAT_PERIOD);
soft_task_def_usemath(ms);
pid = task_create("StatWrite", stat_write, &ms, NULL);
if (pid == NIL) {
grx_close();
perror("Could not create task <StatWrite>");
sys_end();
} else
task_activate(pid);
}
 
void init_stat()
{
KEY_EVT k;
 
score = 0;
enemy = 0;
energy = ENERGY_INIT;
lives = LIVES_INIT;
 
create_stat_task();
 
k.flag = 0;
k.scan = KEY_N;
k.ascii = 'n';
keyb_hook(k,new_game);
 
k.flag = 0;
k.scan = KEY_B;
k.ascii = 'b';
keyb_hook(k,start_game);
}
 
/demos/trunk/astro/makefile
0,0 → 1,16
#
#
#
 
ifndef BASE
BASE=../..
endif
include $(BASE)/config/config.mk
 
PROGS= asteroid
 
include $(BASE)/config/example.mk
 
asteroid:
make -f $(SUBMAKE) APP=asteroid INIT= OTHEROBJS="initfile.o astro.o stat.o rock.o" SHARKOPT="__OLDCHAR__ __GRX__"