Example #1
0
/** \brief Get the current time
 *
 * \b os_timeGet gets the current time by calling
 * \b clock_gettime with clock ID \b CLOCK_REALTIME
 * and converting the result in \b struct
 * \b timespec format into \b os_time format.
 */
os_time
os_timeGet (
    void)
{
    struct timeval t;
    os_time rt;

    if (clockGet) {
        rt = clockGet ();
    } else {
        gettimeofday (&t, NULL);
        rt.tv_sec = t.tv_sec;
        rt.tv_nsec = 1000 * t.tv_usec;
    }
    return rt;
}
Example #2
0
/*
    Purpose: animate and run the game with the clock
    Input  : a char length pointer to the frame buffer and a long length pointer to the frame buffer
    Returns: nothing, but constantly runs the game and its functions
    Assume : nothing
*/
void animate_main(char *base,long *iBase)
{
    long *back,*temp;

    int musicCounter = 0;
    int enMisCount = 0;
    int plMisCount = 0;
    int x,y = 0;
    long clock;
    long newClock;
    int mouseClicked = 0;
    unsigned short mouseX,mouseY;
    int i= 0,j = 0,k = 0,l = 0;
    int randX,randY,randX2;
    int fire = 0;
    int s = 0;
    int done = 0;
	int render = 0;

    struct missile friendlyMis[NUM_MISSILES];
    struct missile enemyMis[NUM_MISSILES];
    struct explosion explosions[NUM_EXPLOSIONS];
    struct missile_Silo silo;
    struct city city1;
    struct city city2;
    struct city city3;
    struct city city4;
    struct city city5;
    struct city city6;
    int *music = get_music();
    
	
	Vector original_Vector;
	/*start the music*/
	stop_sound();
    start_music();
    
	/*init ther model*/
	initialize_model(&city1,&city2,&city3,&city4,&city5,&city6,&silo,friendlyMis,enemyMis,explosions);

	/*create the backbuffer*/
    back = (long *)(buffer + (256 -((long)&buffer)%256));
    
	init_VBL_ISR(original_Vector);
	
	
	
	while(done == 0)
    {
        /*random values for enemy missiles*/

        randX = rand() % BORDER_RIGHT + 16;
        randX2 = rand() % BORDER_RIGHT + 16;
        randY = rand() % GROUND_LEVEL - 200;

        /* needs to fire a player missile at the mouse click coordinates */
        if (mouseClicked == TRUE)
        {                     /* process on input */
            if (k > NUM_MISSILES-1)
            {
                k = 0;
            }
            if (friendlyMis[k].destroyed == TRUE)
            {
                friendlyMis[k].destroyed = FALSE;
                friendlyMis[k].currentY = GROUND_LEVEL-20;
                friendly_Missile_Fired(mouseX,mouseY, &friendlyMis[k],MISSILE_SILO_FIRING_POINT);
                missile_fired_sound();
                k++;
            }
        }

        clock = clockGet();

        if (clock != newClock)
        {                      /* process on clock change */
            fire++;
            if(fire == MISSILE_FIRED_TIME)
            {
                enemyMis[j].destroyed = FALSE;
                enemy_Missile_Fired(randX, GROUND_LEVEL, &enemyMis[j],randX2);
                j++;
                if(fire == MISSILE_FIRED_TIME)
                {
                    fire = 0;
                }
                if (j == NUM_MISSILES-1)
                {
                    j = 0;
                }
            }

            mouseClicked = get_mouse_pos(&mouseX,&mouseY);

			
			if(render == 1)
			{
				render_main(back, city1, city2, city3, city4, city5, city6, silo, enemyMis,friendlyMis, explosions,mouseX,mouseY,render);
			}
			
			
			
			
			
			/* T */
            temp = iBase;
            iBase = back;
            back = temp;

            /* wait for sync and then set the screen to the current buffer */
            set_video_base_wrap((char *)iBase);

            /* process missiles
               might need to change to be able to handle more */
            for (i = 0; i < NUM_MISSILES; i++)
            {
                if (enemyMis[i].destroyed == FALSE && explosions[i].exists == FALSE)
                {
                    processEnemyMissiles(&enemyMis[i],&explosions[i],clock,ENEMY_SPEED);
                }
                if (friendlyMis[i].destroyed == FALSE && explosions[i+NUM_MISSILES].exists == FALSE)
                {
                    processEnemyMissiles(&friendlyMis[i],&explosions[i+NUM_MISSILES],clock,FRIENDLY_SPEED);
                }
            }
            /* process explosions
               might need to change to be able to handle more */
            for (i = 0; i < NUM_EXPLOSIONS; i++)
            {
                if (explosions[i].exists == TRUE)
                {
                    processExplosions(&explosions[i]);
                }
            }
            /* process missile -> explosion collisions */
            for (i = NUM_MISSILES; i < NUM_EXPLOSIONS; i++)
            {
                if (explosions[i].exists == TRUE)
                {
                    processCollisions(friendlyMis,enemyMis,explosions[i]);
                }
            }
            /* process explosion -> city explosions */
            for (i = 0; i < NUM_MISSILES; i++)
            {
                if (explosions[i].hit_city != FALSE)
                {
                    city_collision(explosions[i].hit_city,&city1,&city2,&city3,&city4,&city5,&city6);
                }
            }
            if (musicCounter < SONG_LENGTH){
                if (fire % 4 == 0)
                {
                    play_music(music[musicCounter]);
                    musicCounter++;
                }
            }
            else
            {
                musicCounter = 0;
            }
        }
        newClock = clockGet();
        done = game_over(city1,city2,city3,city4,city5,city6);
    }
    stop_sound();
}