Beispiel #1
0
int main (void)
{
// Golobal Integers
    extern int level;
    extern int lifes;
    extern int score;
    extern int health;
// Local Integers
   register int mapxoff, mapyoff;
   extern int oldpy, oldpx;
   extern int facing;
   extern int jump;
   int n, m;
  // DATAFILE *data;
//initilization    
	allegro_init();	
	install_timer();
	install_keyboard();
    set_color_depth(32);
	set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);

//load data
data = load_datafile("test.dat");
    temp = (BITMAP *)data[NEWGUY_BMP].dat;
    for (n=0; n<4; n++)
    player_image[n] = grabframe(temp,43,77,0,0,4,n);
    destroy_bitmap(temp);
    temp2 = (BITMAP *)data[BAT1_BMP].dat;
    for (m=0; m<2; m++)
    bat_img[m] = grabframe(temp2,87,72,0,0,2,m);
    destroy_bitmap(temp2);

    test_img = load_bitmap("test.bmp", NULL);
gameover:
Start();
first:
//initilize Player
    player = malloc(sizeof(SPRITE));
    player->x = 80;
    player->y = 200;
    player->curframe=0;
    player->framecount=0;
    player->framedelay=11;
    player->maxframe=3;
    player->width=player_image[0]->w;
    player->height=player_image[0]->h;
//initilize Bat
    bat = malloc(sizeof(SPRITE));
    bat->x = 170;
    bat->y = 100;
    bat->curframe=0;
    bat->framecount=0;
    bat->framedelay=11;
    bat->maxframe=1;
    bat->width=bat_img[0]->w;
    bat->height=bat_img[0]->h;
    bat->dir = 0;
    bat->xspeed = 0;
//ini test
test = malloc(sizeof(SPRITE));
    test->x = 270;
    test->y = 400;
    test->curframe=0;
    test->framecount=0;
    test->framedelay=11;
    test->maxframe=1;
    test->width=bat_img[0]->w;
    test->height=bat_img[0]->h;
    test->dir = 0;
    test->xspeed = 0;

loadlevel();

    //main loop
	while (!key[KEY_ESC])
	{
      oldpy = player->y; 
      oldpx = player->x;

if (player->x > 3000)
        {level = 2;
		goto first;}

if (lifes == 0)
{lifes = 3;
          goto gameover;}

if (health < 1)
{lifes -=1;
  health =100;
  goto first;}

  updateEnemy(150, 300, test);
  updateEnemy(150, 300, bat);       
  enemycoll(player->x, player->y, bat);       
  keyinput();
//collition control
	if (!facing) 
        { 
            if (collided(player->x, player->y + player->height)) 
                player->x = oldpx; 
                if (collidedobj(player->x, player->y + player->height))
                   {ClearCell(player->x, player->y + player->height); 
                   AddScore();}
                      if (collidedkill(player->x+9, player->y + player->height))
                         {health -= 25;}        
        }
		else 
        { 
            if (collided(player->x + player->width, player->y + player->height)) 
                player->x = oldpx; 
                if (collidedobj(player->x + player->width, player->y + player->height))
                   {ClearCell(player->x + player->width, player->y + player->height);
                   AddScore();}
                      if (collidedkill(player->x + player->width-15, player->y + player->height))
                         {health -= 25;}        
        }
		
        //update the map scroll position
		mapxoff = player->x + player->width/2 - WIDTH/2 + 10;
		mapyoff = player->y + player->height/2 - HEIGHT/2 + 10;
        //avoid moving beyond the map edge
		if (mapxoff < 0) mapxoff = 0;
		if (mapxoff > (mapwidth * mapblockwidth - WIDTH))
            mapxoff = mapwidth * mapblockwidth - WIDTH;
		if (mapyoff < 0) 
            mapyoff = 0;
		if (mapyoff > (mapheight * mapblockheight - HEIGHT)) 
            mapyoff = mapheight * mapblockheight - HEIGHT;

        //draw the background tiles
		MapDrawBG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1);
        //draw foreground tiles
		MapDrawFG(buffer, mapxoff, mapyoff, 0, 0, WIDTH-1, HEIGHT-1, 0);
        //draw the player's sprite
		if (facing) 
            draw_sprite(buffer, player_image[player->curframe], 
                (player->x-mapxoff), (player->y-mapyoff+1));
		else 
            draw_sprite_h_flip(buffer, player_image[player->curframe], 
                (player->x-mapxoff), (player->y-mapyoff));

        //blit the double buffer 
		vsync();
        acquire_screen();
		if (bat->dir == 1)
           draw_sprite(buffer, bat_img[bat->curframe],(bat->x-mapxoff),(bat->y-mapyoff));
        else
           draw_sprite_h_flip(buffer, bat_img[bat->curframe],(bat->x-mapxoff),(bat->y-mapyoff)); 
        
        draw_sprite(buffer, test_img,(test->x-mapxoff),(test->y-mapyoff));
        textprintf(buffer,font,0,0,9999999,"lifes = %d  Score = %d  Health = %d",lifes,score,health);
        blit(buffer, screen, 0, 0, 0, 0, WIDTH-1, HEIGHT-1);
       	release_screen();

	} //End of game loop




    for (n=0; n<4; n++)
        destroy_bitmap(player_image[n]);
    for (m=0; m<2; m++)
        destroy_bitmap(bat_img[m]);

destroy_bitmap(test_img);

    free(player);
	free(bat);
    free(test);
    destroy_bitmap(buffer);
	MapFreeMem ();
	MapFreeMem ();
    unload_datafile(data);
    allegro_exit();
	return 0;
}
Beispiel #2
0
int
qcam_main( int argc, char ** argv )
{
    struct qcam_softc *qs = NULL;
    u_int options = 0;
    int scan_size = QC_S80x60;
    char *infile = "/dev/stdin";
    char *outfile = "/dev/stdout";
    FILE *infilef  = NULL;
    FILE *outfilef = NULL;
    int ch, sizemult=0;
    int pixel_threshold=0, frame_threshold=0;
    int sleeptime = 0;

    progname = argv[0];

    signal( SIGBUS, sigbus_handler );

    if (argc == 1)
        usage();

    while ((ch = getopt(argc, argv, "qm:os:pM:d:twx123z:")) != -1)
        switch (ch) {
        case 'q':
            SET(CAMERA_IN);
            break;
        case 'm':
            SET(MOVIE_IN);
            infile = optarg;
            break;
        case 'o':
            SET(ONE_IN);
            break;
        case 'p':
            SET(PREVIEW_ONE);
            break;
        case 'M':
            SET(MOVIE_OUT);
            outfile = optarg;
            break;
        case 't':
            SET(TTY_OUT);
            break;
        case 'w':
            SET(TTY_OUT);
            SET(TTY_WEB_OUT);
            break;
        case 'x':
            SET(XWIN_OUT);
            break;
        case 's':
            sleeptime = atoi(optarg);
            break;
        case 'd':
        {
            int d = atoi(optarg);
            if (d == 1)
                SET(CAM_DEBUG1);
            else if (d == 2)
                SET(CAM_DEBUG2);
            else if (d != 0)
                usage();
            break;
        }
        case '1':
            if (!sizemult)
                sizemult=1;
            scan_size = QC_S80x60;
            break;
        case '2':
            if (sizemult)
                sizemult=2;
            else {
                sizemult=1;
                scan_size = QC_S160x120;
            }
            break;
        case '3':
            if (sizemult)
                sizemult=3;
            else {
                sizemult=1;
                scan_size = QC_S320x240;
            }
            break;
        case 'z':
            pixel_threshold = atoi(optarg);
            frame_threshold = atoi(argv[optind]);
            optind++;
            SET(MOTION_DET);
            break;
        default:
            usage();
        }
    argc -= optind;
    argv += optind;

    if (ISSET(MOTION_DET))
    {
        motion_detect(options, ISSET(MOVIE_OUT) ? outfile : NULL,
                      pixel_threshold, frame_threshold);
        return 0;
    }

    if (!(ISSET(CAMERA_IN) || ISSET(MOVIE_IN)))
        errx(1, "need input device");

    if (!(ISSET(MOVIE_OUT) || ISSET(TTY_OUT) || ISSET(XWIN_OUT)))
        errx(1, "need output device");

    if (ISSET(MOVIE_IN) && ISSET(MOVIE_OUT))
        errx(1, "file input and file output are mutually exclusive");

    if (ISSET(PREVIEW_ONE) && 
        !(ISSET(TTY_OUT) || ISSET(XWIN_OUT)))
        errx(1, "need tty_out or xwin_out for preview");

    if (ISSET(PREVIEW_ONE) && !(ISSET(MOVIE_OUT)))
        errx(1, "need a file name to output snapshot to");
        
    if (ISSET(CAMERA_IN))
    {
        qs = qcam_open(QCAM);
        if (!qs)
            errx(1, "cannot access quickcam");
        qcam_setsize(qs, scan_size);
        if (ISSET(CAM_DEBUG1))
            qcam_debug = 1;
        if (ISSET(CAM_DEBUG2))
            qcam_debug = 2;
    }

    setuid(getuid());

    if (ISSET(MOVIE_IN))
    {
        int l = strlen(infile);
        qs = qcam_new();
        if (strcmp(infile, "-") == 0)
            infilef = stdin;
        else if ((l > 3 && strcmp(infile+l-3, ".gz")  == 0) ||
                 (l > 4 && strcmp(infile+l-4, ".bz2") == 0))
        {
            int fd, pid, pipefds[2];
            pipe(pipefds);
            fd = open(infile, O_RDONLY);
            if (fd < 0)
                goto error_opening;
            pid = fork();
            if (pid > 0)
            {
                /* in parent */
                close(fd);
                close(pipefds[1]);
                infilef = fdopen(pipefds[0], "r");
            }
            if (pid == 0)
            {
                /* in child */
                close(pipefds[0]);
                if (pipefds[1] != 1)
                {
                    dup2(pipefds[1], 1);
                    close(pipefds[1]);
                }
                if (fd != 0)
                {
                    dup2(fd, 0);
                    close(fd);
                }
                if (strcmp(infile+l-3, ".gz")  == 0)
                    execl("/usr/bin/gzip", "gzip", "-dc", NULL);
                else
                    execl("/usr/bin/bzip2", "bzip2", "-dc", NULL);
                close(0);
                close(1);
                errx(1, "cannot exec");
            }
            if (pid < 0)
            {
                /* busted */
                errx(1, "cannot fork");
            }
        }           
        else
            infilef = fopen(infile, "r");

        if (!infilef)
            error_opening:
        errx(1, "cannot open input file");
        pgm_readhdr(qs, infilef);
    }
 
    if (ISSET(MOVIE_OUT))
    {
        if (strcmp(outfile, "-") == 0)
            outfilef = stdout;
        else
            outfilef = fopen(outfile, "w");

        if (!outfilef)
            errx(1, "cannot open output file");
        pgm_writehdr(qs, outfilef);
    }

    if (ISSET(TTY_OUT))
        ttydisp_init(qs,ISSET(TTY_WEB_OUT));

    if (ISSET(XWIN_OUT) && !ISSET(PREVIEW_ONE))
    {
        if (!sizemult)
            sizemult=1;
        xwindisp_init(qs, sizemult);
    }

    if (ISSET(ONE_IN) && ISSET(PREVIEW_ONE))
    {
        qcam_setsize(qs, QC_S80x60);
        if (ISSET(XWIN_OUT))
            xwindisp_init(qs, 1);
        do {
            qcam_scan(qs);
            qcam_adjustpic(qs);
            if (ISSET(TTY_OUT))
                ttydisp(qs,ISSET(TTY_WEB_OUT));
            if (ISSET(XWIN_OUT))
                xwindisp(qs);
        } while (!keyinput());
        qcam_setsize(qs, scan_size);
        qcam_scan(qs);
        pgm_write(qs, outfilef);
    } else if (ISSET(ONE_IN) && !ISSET(MOVIE_IN))
    {
        qcam_setsize(qs, QC_S80x60);
        do {
            qcam_scan(qs);
        } while (qcam_adjustpic(qs));
        qcam_setsize(qs, scan_size);
        qcam_scan(qs);
        if (ISSET(MOVIE_OUT))
            pgm_write(qs, outfilef);
        if (ISSET(TTY_OUT))
            ttydisp(qs,ISSET(TTY_WEB_OUT));
    } else {
        int done = 0;
        if (ISSET(CAMERA_IN) && ISSET(MOVIE_OUT))
        {
            qcam_setsize(qs, QC_S80x60);
            do {
                qcam_scan(qs);
            } while (qcam_adjustpic(qs));
            qcam_setsize(qs, scan_size);
        }
        while (!done)
        {
            if ( sleeptime != 0 )
                usleep( sleeptime );
            if (ISSET(MOVIE_IN))
            {
                pgm_read(qs, infilef);
                if (feof(infilef))
                    done++;
            }
            if (ISSET(CAMERA_IN))
            {
                qcam_scan(qs);
                qcam_adjustpic(qs);
            }
            if (ISSET(MOVIE_OUT))
                pgm_write(qs, outfilef);
            if (ISSET(TTY_OUT))
            {
                ttydisp(qs,ISSET(TTY_WEB_OUT));
                if (ISSET(MOVIE_IN))
                    usleep(200000);
            }
            if (ISSET(XWIN_OUT))
            {
                xwindisp(qs);
                if (ISSET(MOVIE_IN))
                    usleep(200000);
            }
            if (ISSET(ONE_IN))
            {
                char dummy;
                done++;
                if (ISSET(MOVIE_IN))
                    read(0, &dummy, 1);
            }
            if (!ISSET(MOVIE_IN) && keyinput())
                done++;
        }
    }
    
    if (ISSET(MOVIE_IN))
        fclose(infilef);
    if (ISSET(MOVIE_OUT))
        fclose(outfilef);
    if (ISSET(XWIN_OUT))
        xwindisp_close();
    if (ISSET(CAMERA_IN))
        qcam_close(qs);

    return 0;
}