Example #1
0
void			move_ball(t_all *all)
{
	int			ret;

	if ((ret = set_ball_pos(&all->ball.pos.y,
		all->ball.pos.y + all->ball.vspeed.y, -1.5, 1)) <= 0)
	{
		if (ret == -1)
			all->ball.vspeed.y = -all->ball.vspeed.y;
		else
		{
			init_ball(&all->ball);
			init_ship(&all->ship);
			if (--all->life == 0)
				ft_print_shell(all, "Vous avez perdu !!");
			all->cur = 1;
		}
	}
	if (set_ball_pos(&all->ball.pos.x,
		all->ball.pos.x + all->ball.vspeed.x, -1, 1) == -1)
		all->ball.vspeed.x = -all->ball.vspeed.x;
	if (check_ship_collision(&all->ball, &all->ship) == TRUE)
		ball_rebound(&all->ball, &all->ship);
	if (check_brick_collision(&all->ball, &all->map))
		all->score += 10;
}
Example #2
0
int main(void)
{
	initXWindows();
    srand(time(NULL));
    clock_gettime(CLOCK_REALTIME, &timePause);
    clock_gettime(CLOCK_REALTIME, &timeStart);
    init_opengl();
    init_ship();
	//Do this to allow fonts
	glEnable(GL_TEXTURE_2D);
	initialize_fonts();
	init_textures();
	while(!done) {
		while(XPending(dpy)) {
			XEvent e;
			XNextEvent(dpy, &e);
			check_resize(&e);
			check_mouse(&e);
			check_keys(&e);
		}
        clock_gettime(CLOCK_REALTIME, &timeCurrent);
        timeSpan = timeDiff(&timeStart, &timeCurrent);
        timeCopy(&timeStart, &timeCurrent);
        physicsCountdown += timeSpan;
        while (physicsCountdown >= physicsRate) {
            physics();
            physicsCountdown -= physicsRate;
            render();
        }
		//physics();
		//render();
		glXSwapBuffers(dpy, win);
	}
	cleanupXWindows();
	cleanup_fonts();
	return 0;
}
int main(int argc, char const *argv[])
{
	const int FPS = 60;
	const int MAX_BULLETS = 10;
	const int MAX_ASTEROIDS = 10;
	const int MAX_EXPLOSIONS = 10;
	srand(time(NULL));
	int done = 0;
	int redraw = 1;

	if(!al_init())
	{
		al_show_native_message_box(NULL, "Error", "Error", 
			"Could not initialize Allegro 5.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	ALLEGRO_DISPLAY *display = al_create_display(screen_width, screen_height);
	if(!display)
	{
		al_show_native_message_box(NULL, "Error", "Error", 
			"Could not create display.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
	if(!event_queue)
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not create event queue.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
	if(!timer)
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not create timer.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	if(!al_install_keyboard())
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not install keyboard.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	if(!al_install_mouse())
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not install mouse.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	if(!al_init_image_addon())
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not initialize image addon.", 0, ALLEGRO_MESSAGEBOX_ERROR);
		return -1;
	}

	if(!al_init_primitives_addon())
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not initialize primitives addon.", 0, ALLEGRO_MESSAGEBOX_ERROR);
	}

	al_init_font_addon(); // for whatever reason this function is void returning

	if(!al_init_ttf_addon())
	{
		al_show_native_message_box(display, "Error", "Error", 
			"Could not initialize ttf addon.", 0, ALLEGRO_MESSAGEBOX_ERROR);
	}
	
	al_hide_mouse_cursor(display);

	al_register_event_source(event_queue, al_get_mouse_event_source());
	al_register_event_source(event_queue, al_get_keyboard_event_source());
	al_register_event_source(event_queue, al_get_display_event_source(display));
	al_register_event_source(event_queue, al_get_timer_event_source(timer));

	ALLEGRO_FONT *font18 = al_load_font("Arial.ttf", 18, 0);

	int prev_x = screen_width, prev_y = screen_height;
	int fps_counter = 0;
	int fps_counter2 = 0;
	int i, j, k;

	struct spaceship ship;
	init_ship(&ship);

	struct bullet bullets[MAX_BULLETS];
	for(i = 0; i < MAX_BULLETS; i++)
	{
		init_bullet(&bullets[i]);
	}

	struct asteroid asteroids[MAX_ASTEROIDS];
	for(i = 0; i < MAX_ASTEROIDS; i++)
	{
		init_asteroid(&asteroids[i]);
	}

	struct explosion explosions[MAX_EXPLOSIONS];
	for(i = 0; i < MAX_EXPLOSIONS; i++)
	{
		init_explosion(&explosions[i]);
	}

	al_start_timer(timer);

	while(!done)
	{
		ALLEGRO_EVENT event;
		al_wait_for_event(event_queue, &event);

		if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
		{
			done = 1;
		}
		if(event.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch(event.keyboard.keycode)
			{
				case ALLEGRO_KEY_Q: 
					done = 1;
					break;
				case ALLEGRO_KEY_ESCAPE:
					done = 1;
					break;
			}
		}
		if(event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
		{
			if(event.mouse.button & 1)
			{
				for(i = 0; i < MAX_BULLETS; i++)
				{
					if(!bullets[i].live)
					{
						fire_bullet(&bullets[i], ship);
						break;
					}
				}
			}
		}
		if(event.type == ALLEGRO_EVENT_MOUSE_AXES ||
              event.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY)
		{
			set_ship_coordinates(&ship, event.mouse.x, event.mouse.y);
		}
		if(event.type == ALLEGRO_EVENT_TIMER)
		{	
			if(ship.x > prev_x)
			{	
				ship.sprite.dir_horizontal = RIGHT;
			}
			else if(ship.x == prev_x)
			{
				ship.sprite.dir_horizontal = CENTER;
			}
			else if(ship.x < prev_x) 
			{
				ship.sprite.dir_horizontal = LEFT;
			}

			if(ship.y > prev_y)
			{
				ship.sprite.dir_vertical = BACK;
			}
			else if(ship.y == prev_y)
			{
				ship.sprite.dir_vertical = NEUTRAL;
			}
			else if(ship.y < prev_y)
			{
				ship.sprite.dir_vertical = FORWARD;
			}

			if(++fps_counter >= FPS / 5)
			{
				fps_counter = 0;
				prev_x = ship.x;
				prev_y = ship.y;	
			}

			if(++fps_counter2 >= 2 * FPS)
			{
				for(i = 0; i < MAX_ASTEROIDS; i++)
				{
					if(!asteroids[i].live)
					{
						start_asteroid(&asteroids[i]);
						break;
					}
				}
				fps_counter2 = 0;
			}

			for(i = 0; i < MAX_BULLETS; i++)
			{
				if(bullets[i].live)
				{
					update_bullet(&bullets[i]);
				}
			}

			for(i = 0; i < MAX_ASTEROIDS; i++)
			{
				if(asteroids[i].live)
				{
					update_asteroid(&asteroids[i], &ship);
				}
			}

			for(i = 0; i < MAX_EXPLOSIONS; i++)
			{
				if(explosions[i].live)
				{
					update_explosion(&explosions[i]);
				}
			}

			update_ship_boundaries(&ship);

			for(i = 0; i < MAX_BULLETS; i++)
			{
				if(bullets[i].live)
				{
					for(j = 0; j < MAX_ASTEROIDS; j++)
					{
						if(asteroids[j].live)
						{
							if(bullet_and_asteroid_collision(bullets[i], asteroids[j]))
							{
								bullets[i].live = 0;
								asteroids[j].live = 0;
								ship.score += 20;
								for(k = 0; k < MAX_EXPLOSIONS; k++)
								{
									if(!explosions[k].live)
									{
										start_explosion(&explosions[k], bullets[i].x, bullets[i].y);
										break;
									}
								}
							}
						}
					}
				}
			}

			for(i = 0; i < MAX_ASTEROIDS; i++)
			{
				if(asteroids[i].live)
				{
					if(ship_and_asteroid_collision(ship, asteroids[i]))
					{
						asteroids[i].live = 0;
						for(k = 0; k < MAX_EXPLOSIONS; k++)
						{
							if(!explosions[k].live)
							{
								start_explosion(&explosions[k], ship.x, ship.y);
								break;
							}
						}
						
						ship.lives--;
					}
				}
			}

			if(!ship.lives)
			{
				done = true;
			}

			redraw = 1;
		}

		if(redraw)
		{
			redraw = 0;
			draw_ship_sprite(ship.sprite, ship.x, ship.y);
			for(i = 0; i < MAX_BULLETS; i++)
			{
				if(bullets[i].live)
				{
					draw_bullet(bullets[i]);
				}
			}
			for(i = 0; i < MAX_ASTEROIDS; i++)
			{
				if(asteroids[i].live)
				{
					draw_asteroid(asteroids[i]);
				}
			}
			for(i = 0; i < MAX_EXPLOSIONS; i++)
			{
				if(explosions[i].live)
				{
					draw_explosion(explosions[i]);
				}
			}
			al_draw_textf(font18, al_map_rgb(255, 255, 255), 50, 5, 0, 
				"Score: %d ", ship.score); // I have no idea why it doesn't print the S...
			al_draw_textf(font18, al_map_rgb(255, 255, 255), 50, 25, 0, 
				"Lives: %d", ship.lives);
			al_flip_display();
			al_clear_to_color(al_map_rgb(0, 0, 0));
		}
	}

	for(i = 0; i < MAX_ASTEROIDS; i++)
	{
		destroy_asteroid(&asteroids[i]);
	}
	for(i = 0; i < MAX_EXPLOSIONS; i++)
	{
		destroy_explosion(&explosions[i]);
	}
	destroy_sprite(&ship.sprite);
	al_destroy_display(display);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	al_destroy_font(font18);

	return 0;
}
Example #4
0
int main(int argc, char *argv[]) {

    int c;

    while((c = getopt(argc, argv, "hv")) != -1) {
        switch(c) {
        case 'h':
            puts(HELP);
            return 0;
        case 'v':
            printf("version %.1f\n", PROGRAM_VERSION);
            return 0;
        }
    }

    if(!al_init()) {
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
    }

    if(argc == 2 && !strcmp(argv[1], "-v")) {
        printf("version %.1f\n", PROGRAM_VERSION);
        return 0;
    }

    bool key[5] = {false, false, false, false, false};
    bool doexit = false;
    bool redraw = true;

    timer = al_create_timer(1.0 / FPS);
    if(!timer) {
        fprintf(stderr, "couldn't initialize timer.\n");
        return -1;
    }

    display = al_create_display(SCREEN_W, SCREEN_H);
    if(!display) {
        fprintf(stderr, "failed to create the display.\n");
        al_destroy_timer(timer);

        return -1;
    }

    al_set_window_title(display, "blasteroids");

    al_init_primitives_addon();

    if(!al_install_keyboard()) {
        fprintf(stderr, "failed to initialize keyboard.\n");
        al_destroy_display(display);
        al_destroy_timer(timer);

        return -1;
    }

    /* Making the sky look like sky */

    colors[0] = al_map_rgba(255, 100, 255, 128);
    colors[1] = al_map_rgba(255, 100, 100, 255);
    colors[2] = al_map_rgba(100, 100, 255, 255);

    for (layer = 0; layer < 3; layer++) {
        for (star = 0; star < NUM_STARS/3; star++) {
            Point *p = &stars[layer][star];
            p->x = rand() % SCREEN_W;
            p->y = rand() % SCREEN_H;
        }
    }


    start = al_get_time() * 1000;
    now = start;
    elapsed = 0;
    frame_count = 0;
    program_start = al_get_time();


    /* done with the sky. Now making the ship. */
    Spaceship *ship = init_ship();
    List *a = (List *)summon_asteroids(NUMBER_ASTEROIDS);
    ListElmt *astElmt = list_head(a);


    if(!ship) {
        fprintf(stderr, "couldn't create bitmap.\n");
        al_destroy_timer(timer);
        al_destroy_display(display);
        return -1;
    }

    Blast *blast = init_blast(ship->sx, ship->sy, ship->heading);

    event_queue = al_create_event_queue();
    if(!event_queue) {
        fprintf(stderr, "failed to create event queue.\n");
        al_destroy_display(display);
        al_destroy_timer(timer);
        return -1;
    }

    al_register_event_source(event_queue, al_get_keyboard_event_source());
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_timer_event_source(timer));

    al_start_timer(timer);

    while(!doexit) {
        /* animate the sky, just sky, starts, but NOT objects. */

        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);

        if(ev.type == ALLEGRO_EVENT_TIMER) {

            if(key[UP])
                ship->speed += 0.04;

            if(key[RIGHT])
                ship->heading += 1.0f;

            if(key[DOWN])
                if(ship->speed > 0.05)
                    ship->speed -= 0.04;

            if(key[LEFT])
                ship->heading -= 1.0f;

            fly_ship(ship);
            // fire ze missile

            if(key[SPACE]) {
                blast->sx = ship->sx;
                blast->sy = ship->sy;
                blast->heading = ship->heading;
                blast->gone = 0;
            }

            float theta = head2theta(blast->heading);
            blast->sx += blast->speed * cos(theta);
            if(blast->sx <= 0 || blast->sx >= SCREEN_W) {
                blast->gone = 1;
            }

            blast->sy += blast->speed * sin(theta);
            if(blast->sy <= 0 || blast->sy >= SCREEN_H) {
                blast->gone = 1;
            }

            /* loop through the list of asteroids */

            astElmt = (astElmt->next)?astElmt->next : list_head(a);
            Asteroid *aster = astElmt->aster;

            /* asteroid eternity */
            if(aster->sx < 0 || aster->sx > SCREEN_W - 33)
                aster->sx = 0;

            if(aster->sy < 0 || aster->sy > SCREEN_H)
                aster->sy = 0;



            aster->twist += aster->rot_velocity;

            /* Fuzzy movement */
            if((int)aster->sx % 3 == 0)
                aster->sx += aster->speed;
            aster->sx += 0.9;
            if((int)aster->sy % 5 == 3)
                aster->sy += aster->speed;
            aster->sy += 0.9;

            aster->twist += 0.4;

            /* detect alteroid collision, but only if 5 seconds have
             passed since the last Death */

            Box s = {{ship->sx, ship->sy}, 16.0f, 20.0f};
            Box a = {{aster->sx, aster->sy}, 45.0f, 40.0f};

            if(ship->heading != -90.0f) {
                if(!aster->gone && !ship->gone && is_collision(&s, &a)) {
                    ship->color = al_map_rgb(0, 0, 255);
                }
            }

            /* detect asteroid being shot */

            Box b = {{blast->sx, blast->sy}, 120.0f, 3.0f};
            if(!(blast->gone) && !(aster->gone) && is_collision(&b, &a)) {
                aster->gone = 1;
            }
            redraw = true;
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {

            switch(ev.keyboard.keycode) {
            case ALLEGRO_KEY_UP:
                key[UP] = true;
                break;
            case ALLEGRO_KEY_RIGHT:
                key[RIGHT] = true;
                break;
            case ALLEGRO_KEY_DOWN:
                key[DOWN] = true;
                break;
            case ALLEGRO_KEY_LEFT:
                key[LEFT] = true;
                break;
            case ALLEGRO_KEY_SPACE:
                key[SPACE] = true;
                break;
            }
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
            switch(ev.keyboard.keycode) {
            case ALLEGRO_KEY_UP:
                key[UP] = false;
                break;
            case ALLEGRO_KEY_RIGHT:
                key[RIGHT] = false;
                break;
            case ALLEGRO_KEY_DOWN:
                key[DOWN] = false;
                break;
            case ALLEGRO_KEY_LEFT:
                key[LEFT] = false;
                break;
            case ALLEGRO_KEY_SPACE:
                key[SPACE] = false;
                break;
            case ALLEGRO_KEY_Q:
            case ALLEGRO_KEY_ESCAPE:
                doexit = true;
                break;
            }
        }

        if(redraw && al_is_event_queue_empty(event_queue)) {
            redraw = false;
            al_clear_to_color(al_map_rgb(0, 0, 0));

            if (frame_count < (1000/TARGET_FPS)) {
                frame_count += elapsed;
            }
            else {
                int X, Y;

                frame_count -= (1000/TARGET_FPS);

                for (star = 0; star < NUM_STARS/3; star++) {
                    Point *p = &stars[0][star];
                    al_draw_pixel(p->x, p->y, colors[0]);
                }
                /*	al_lock_bitmap(al_get_backbuffer(display), ALLEGRO_PIXEL_FORMAT_ANY, 0); */

                for (layer = 1; layer < 3; layer++) {
                    for (star = 0; star < NUM_STARS/3; star++) {
                        Point *p = &stars[layer][star];
                        // put_pixel ignores blending
                        al_put_pixel(p->x, p->y, colors[layer]);
                    }
                }

                /* Check that dots appear at the window extremes. */
                X = SCREEN_W - 1;
                Y = SCREEN_H - 1;
                al_put_pixel(0, 0, al_map_rgb_f(1, 1, 1));
                al_put_pixel(X, 0, al_map_rgb_f(1, 1, 1));
                al_put_pixel(0, Y, al_map_rgb_f(1, 1, 1));
                al_put_pixel(X, Y, al_map_rgb_f(1, 1, 1));

                /* al_unlock_bitmap(al_get_backbuffer(display)); */
                total_frames++;
            }

            now = al_get_time() * 1000;
            elapsed = now - start;
            start = now;

            for (layer = 0; layer < 3; layer++) {
                for (star = 0; star < NUM_STARS/3; star++) {
                    Point *p = &stars[layer][star];
                    p->y -= speeds[layer] * elapsed;
                    if (p->y < 0) {
                        p->x = rand() % SCREEN_W;
                        p->y = SCREEN_H;
                    }
                }
            }
            draw_ship(ship);

            if(!blast->gone) {
                draw_blast(blast);
            }
            draw_asteroids(a);
            al_flip_display();
        }
    }

    length = al_get_time() - program_start;

    /* printf("Length = %f\n", length); */

    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);

    return 0;
}
Example #5
0
int main(void)
{
    bool done =false;
    bool redraw = true;
    ALLEGRO_DISPLAY *display = NULL;
    ALLEGRO_TIMER *timer =NULL;
    ALLEGRO_EVENT_QUEUE *event_queue = NULL;
    SpaceShip ship;
    Bullet bullet[num_bullets];
    Commet commet[num_commets];
    if(!al_init())
    {
        fprintf(stderr,"allegro is not intialized");
        return -1;
    }
    display = al_create_display(SCREEN_W,SCREEN_H);
    if(!display)
    {
        fprintf(stderr,"failed to intialize the display");
        return -1;
    }
    
    timer  = al_create_timer(1.0/FPS);
    if(!timer)
    {
        fprintf(stderr,"failed to intialize timer");
        al_destroy_display(display);
        return -1;
    }
    event_queue = al_create_event_queue();
    if(!event_queue)
    {
        fprintf(stderr,"failed to create event queue");
        al_destroy_display(display);
        al_destroy_timer(timer);
        return -1;
    }
    al_install_keyboard();
    al_register_event_source(event_queue,al_get_timer_event_source(timer));
    al_register_event_source(event_queue,al_get_keyboard_event_source());
    al_register_event_source(event_queue,al_get_display_event_source(display));
    init_ship(&ship);
    init_bullet(&bullet);
    init_commet(&commet);
    al_start_timer(timer);
    while(!done)
    {
        
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue,&ev);
        
        if(ev.type == ALLEGRO_EVENT_TIMER)
        {
            if(keys[KEY_UP])
            {
                move_ship_up(&ship);
            }
            if(keys[KEY_DOWN])
            {
                move_ship_down(&ship);
            }if(keys[KEY_LEFT])
            {
                move_ship_left(&ship);
            }
            if(keys[KEY_RIGHT])
            {
                move_ship_right(&ship);
            }
            if(keys[KEY_SPACEBAR])
            {
                fire_bullet(&bullet,&ship);
            }
            if(!GameOver)
            {
                update_bullet(&bullet);
                update_commet(&commet);
                bullet_collision(&bullet,&commet);
            }
            
            redraw = true;
        }
        else if(ev.type==ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            done = true;
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_UP:
                {
                    keys[KEY_UP]=true;
                    break;
                }
                case ALLEGRO_KEY_DOWN:
                {
                    keys[KEY_DOWN]=true;
                    break;
                }
                case ALLEGRO_KEY_LEFT:
                {
                    keys[KEY_LEFT]=true;
                    break;
                }
                case ALLEGRO_KEY_RIGHT :
                {
                    keys[KEY_RIGHT]=true;
                    break;
                }
                case ALLEGRO_KEY_SPACE:
                {
                    keys[KEY_SPACEBAR] = true;
                    break;
                }
                    
            }
        }
        else if(ev.type == ALLEGRO_EVENT_KEY_UP)
        {
            switch(ev.keyboard.keycode)
            {
                case ALLEGRO_KEY_UP:
                {
                    keys[KEY_UP]=false;
                    break;
                }
                case ALLEGRO_KEY_DOWN:
                {
                    keys[KEY_DOWN]=false;
                    break;
                }
                case ALLEGRO_KEY_LEFT:
                {
                    keys[KEY_LEFT]=false;
                    break;
                }
                case ALLEGRO_KEY_RIGHT :
                {
                    keys[KEY_RIGHT]=false;
                    break;
                }
                case ALLEGRO_KEY_SPACE:
                {
                    keys[KEY_SPACEBAR]=false;
                    break;
                }
                    
            }
        }
        
        if(redraw && al_event_queue_is_empty(event_queue))
        {
            redraw = false ;
            al_clear_to_color(al_map_rgb(255,100,100));
            draw_ship(&ship);
            draw_bullet(bullet);
            draw_commet(commet);
            al_flip_display();
            al_clear_to_color(al_map_rgb(255,100,100));
            
            
        }
    }
    al_destroy_display(display);
    al_destroy_timer(timer);
    al_destroy_event_queue(event_queue);
    return 0;
}