Subversion Repositories shark

Rev

Blame | Last modification | View Log | RSS feed

/************************** Simcity ************************/
// Main,initialization functions and global variables allocation
#include <kernel/func.h>
#include <string.h>
#include <stdlib.h>
#include <drivers/keyb.h>
#include <drivers/glib.h>
#include <kernel/kern.h>
#include <semaphor.h>
#include <math.h>
#include "include/constant.h"
#include "include/misc.h"
#include "include/draw.h"
#include "include/proc.h"
#define rgb rgb16

/* graphic mutex... */
sem_t mutex;
//kill flag mutexes
sem_t kill_mutex[MAX_CAR];

// various sprites to use with grx_putimage()....
BYTE vbuf[MAX_CAR][ROW*COL*2];
BYTE clrscr[800*600*2];
BYTE clrcam[(ROW+2)*(COL+2)*2];
BYTE gauge_img[ROW*COL*2];
BYTE brk_gauge[ROW*COL*2];
BYTE arrow[3][ROW*COL*2];
BYTE street[H*W*2];

//task chain pointers
car_data *free_n,*free_o,*busy_n,*busy_o;

// various sprites to plot
extern DWORD macchine[ROW][COL][NCAR];
extern DWORD sprites[ROW][COL][NO_SPRITE];
extern DWORD strada[H][W];

//resolution to use
WORD r=600;
WORD c=800;
BYTE bpp=16;

// useful colors...
DWORD white;
DWORD black;
DWORD red;
DWORD gray;
DWORD blue;
DWORD green;
DWORD border;
DWORD tl_bg;

//PID vectors

PID p_table[MAX_CAR];
PID c_table[MAX_CAR];
PID g_table[MAX_CAR];
PID a_table[MAX_CAR];

char kill_flag[MAX_CAR];
float cosine[360],sine[360];

// data structures
car_data car_data_array[MAX_CAR];
tl_data tl_data_array[MAX_TL];
starting_set starting_set_array[S_POINT];

void keyb_h(void);

static void version( void )
{
  cprintf("\n\nDemo presented by\n");
  cprintf("Aguzzi Marco\n");
  cprintf("   &\n");
  cprintf("Ferrari Fabio\n");
}

void demo_exc_handler(int signo, siginfo_t *info, void *extra)
{
  struct timespec t;

  grx_close();
  /* Default action for an kern exception is  */
  kern_cli();
  ll_gettime(TIME_EXACT, &t),
  kern_printf("\nS.Ha.R.K. Exception raised!!!"
              "\nTime (s:ns)     :%d:%d"
              "\nException number:%d"
              "\nPID             :%d\n",
              t.tv_sec, t.tv_nsec, info->si_value.sival_int,
              info->si_task);
  sys_end();
}

void my_close(void *arg)
{
  grx_close();
  kern_printf("Shutting down SIMCITY\n");
}

int main(int argc, char **argv)
{
  int i;
  char tl_name[4];
  struct sigaction action;

  version();

/* Init the standard Hartik exception handler */
/* Set the signal action */
  action.sa_flags = SA_SIGINFO;
  action.sa_sigaction = demo_exc_handler;
  action.sa_handler = 0;
  sigfillset(&action.sa_mask); /* we block all the other signals... */
  if (sigaction(SIGHEXC, &action, NULL) == -1) {
    perror("Error initializing signals...");
    sys_end();
  }
  sys_atrunlevel(my_close, NULL, RUNLEVEL_BEFORE_EXIT);

//resetting kill flags
  for(i=0;i<MAX_CAR;i++) {
    p_table[i]=0;
    kill_flag[i]=0;
  }

// graphic mode initialization
#ifdef GRAPH
  if (grx_init() < 1) {
    kern_printf("Error initializing graphics\n");
    sys_abort(1);
  }
  if (grx_open(c,r,bpp) < 0) {
    kern_printf("GRX Open Err\n");
    sys_abort(1);
  }
  get_images();
#endif
  srand(sys_gettime(NULL));

//init the graphic mutex
  sem_init(&mutex,1,1);
//init kill flag mutexes
  for(i=0;i<MAX_CAR;i++) sem_init(&(kill_mutex[i]),1,1);
//fill sine & cosine lookup tables
  fill_table();
/*init keys*/
  keyb_h();
  set_start_point();
  tl_init();
  init_struct();

/* useful colors ... */
  white = rgb(255,255,255);
  black = rgb(0,0,0);
  red   = rgb(255,0,0);
  gray  = rgb(210,210,210);
  blue  = rgb(0,0,220);
  green = rgb(0,255,0);
  border= rgb(128,128,128);
  tl_bg= rgb(0,128,0);
#ifdef GRAPH
/* paint scenario*/
  draw_scenario();
#endif
#ifndef GRAPH
  cprintf("Main: max_tl:%d\n",MAX_TL);
#endif

//creating refresher,killer and traffic light processes...
  ref_create();
  killer_create();
  for(i=0;i<MAX_TL;i++) {
    sprintf(tl_name,"tl%d",i+1);
#ifndef GRAPH
    cprintf("main:tname=%s",tl_name);
#endif
    stl_create(tl_name,i);
  }
  return 0;
}