Example #1
0
static int allua_sample_instance_set_playmode(lua_State * L)
{
   ALLUA_sample_instance si = allua_check_sample_instance(L, 1);
   int mode = luaL_checkint(L, 2);
   lua_pushboolean(L, al_set_sample_instance_playmode(si, mode));
   return 1;
}
/* ----------------------------------------------------------------------------
 * Play the sample.
 * max_override_pos: Override the currently playing sound
 *   only if it's already in this position, or beyond.
 *   This is in seconds. 0 means always override. -1 means never override.
 * loop: Loop the sound?
 * gain: Volume, 0 - 1.
 * pan: Panning, 0 - 1 (0.5 is centered).
 * speed: Playing speed.
 */
void sample_struct::play(
    const float max_override_pos, const bool loop, const float gain,
    const float pan, const float speed
) {
    if(!sample || !instance) return;
    
    if(max_override_pos != 0 && al_get_sample_instance_playing(instance)) {
        float secs = al_get_sample_instance_position(instance) / (float) 44100;
        if(
            (secs < max_override_pos && max_override_pos > 0) ||
            max_override_pos == -1
        ) {
            return;
        }
    }
    
    al_set_sample_instance_playmode(
        instance, (loop ? ALLEGRO_PLAYMODE_LOOP : ALLEGRO_PLAYMODE_ONCE)
    );
    al_set_sample_instance_gain(instance, gain);
    al_set_sample_instance_pan(instance, pan);
    al_set_sample_instance_speed(instance, speed);
    
    al_set_sample_instance_position(instance, 0);
    al_set_sample_instance_playing( instance, true);
}
Example #3
0
void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
	struct GamestateResources *data = malloc(sizeof(struct GamestateResources));
	data->timeline = TM_Init(game, "main");
	data->bitmap = al_create_bitmap(game->viewport.width, game->viewport.height);
	data->checkerboard = al_create_bitmap(game->viewport.width, game->viewport.height);
	data->pixelator = al_create_bitmap(game->viewport.width, game->viewport.height);

	al_set_target_bitmap(data->checkerboard);
	al_lock_bitmap(data->checkerboard, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_WRITEONLY);
	int x, y;
	for (x = 0; x < al_get_bitmap_width(data->checkerboard); x=x+2) {
		for (y = 0; y < al_get_bitmap_height(data->checkerboard); y=y+2) {
			al_put_pixel(x, y, al_map_rgba(0,0,0,64));
			al_put_pixel(x+1, y, al_map_rgba(0,0,0,0));
			al_put_pixel(x, y+1, al_map_rgba(0,0,0,0));
			al_put_pixel(x+1, y+1, al_map_rgba(0,0,0,0));
		}
	}
	al_unlock_bitmap(data->checkerboard);
	al_set_target_backbuffer(game->display);
	(*progress)(game);

	data->font = al_load_ttf_font(GetDataFilePath(game, "fonts/DejaVuSansMono.ttf"),
	                              (int)(game->viewport.height*0.1666 / 8) * 8 ,0 );
	(*progress)(game);
	data->sample = al_load_sample( GetDataFilePath(game, "dosowisko.flac") );
	data->sound = al_create_sample_instance(data->sample);
	al_attach_sample_instance_to_mixer(data->sound, game->audio.music);
	al_set_sample_instance_playmode(data->sound, ALLEGRO_PLAYMODE_ONCE);
	(*progress)(game);

	data->kbd_sample = al_load_sample( GetDataFilePath(game, "kbd.flac") );
	data->kbd = al_create_sample_instance(data->kbd_sample);
	al_attach_sample_instance_to_mixer(data->kbd, game->audio.fx);
	al_set_sample_instance_playmode(data->kbd, ALLEGRO_PLAYMODE_ONCE);
	(*progress)(game);

	data->key_sample = al_load_sample( GetDataFilePath(game, "key.flac") );
	data->key = al_create_sample_instance(data->key_sample);
	al_attach_sample_instance_to_mixer(data->key, game->audio.fx);
	al_set_sample_instance_playmode(data->key, ALLEGRO_PLAYMODE_ONCE);
	(*progress)(game);

	return data;
}
Example #4
0
void GameMenu::loop(void)
{
    bool refresh = true;
    quit = false;
    ALLEGRO_EVENT event;


    al_set_sample_instance_playmode(GFX::introInstance,ALLEGRO_PLAYMODE_LOOP);
    al_play_sample_instance(GFX::introInstance);

//animação do menu
    barraInferior1.setImagem(GFX::blocksBar);
    barraInferior1.setSpeed(-1,0);
    barraInferior1.setPosition(0,0);

    barraInferior2.setImagem(GFX::blocksBar);
    barraInferior2.setSpeed(-1,0);
    barraInferior2.setPosition(al_get_bitmap_width(GFX::blocksBar),0);

    barraSuperior1.setImagem(GFX::blocksBar);
    barraSuperior1.setSpeed(-1,0);
    barraSuperior1.setPosition(0,GVC::SCREENHEIGHT-al_get_bitmap_height(GFX::blocksBar));

    barraSuperior2.setImagem(GFX::blocksBar);
    barraSuperior2.setSpeed(-1,0);
    barraSuperior2.setPosition(al_get_bitmap_width(GFX::blocksBar),GVC::SCREENHEIGHT-al_get_bitmap_height(GFX::blocksBar));



    while(!quit)
    {
        al_wait_for_event(Allegro::event_queue, &event);

        switch(event.type)
        {
        case ALLEGRO_EVENT_TIMER:
            refresh = true;
            update(); // Update game logic
            break;
        case ALLEGRO_EVENT_DISPLAY_CLOSE:
            quit = true;
            break;
        default:
            processEvent(event); // Get user input
            break;
        }

        if(refresh && al_is_event_queue_empty(Allegro::event_queue))
        {
            refresh = false;
            draw(); // Draw game state

            al_flip_display();
            al_draw_bitmap(GFX::menuInicial,0,0,0);
        }
    }
}
void AllegroSoundSample5::SetLoopMode( ISample::ELoopMode mode )
{
	if ( !m_pInstance )
		return;
	ALLEGRO_PLAYMODE alMode = ALLEGRO_PLAYMODE_ONCE;
	switch( mode )
	{
		case ISample::LOOPMODE_ONCE: alMode = ALLEGRO_PLAYMODE_ONCE; break;
		case ISample::LOOPMODE_LOOP: alMode = ALLEGRO_PLAYMODE_LOOP; break;
	}
	al_set_sample_instance_playmode( m_pInstance, alMode );
}
Example #6
0
void Sound::prepare(){
	if(soundsLoaded){
		al_reserve_samples(2);
		soundsPrepared = true;

		songInstance = al_create_sample_instance(backgroundSong);
		al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
		al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
	}
	else
		throw MyException("Sounds was not attached.");
}
Example #7
0
void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {

	struct MenuResources *data = malloc(sizeof(struct MenuResources));

	data->options.fullscreen = game->config.fullscreen;
	data->options.fps = game->config.fps;
	data->options.width = game->config.width;
	data->options.height = game->config.height;
	data->options.resolution = game->config.width / 320;
	if (game->config.height / 180 < data->options.resolution) data->options.resolution = game->config.height / 180;
	(*progress)(game);

	data->bg = al_load_bitmap( GetDataFilePath(game, "bg.png") );
	data->monster = al_load_bitmap( GetDataFilePath(game, "monster.png") );
	data->title = al_load_bitmap( GetDataFilePath(game, "title.png") );
	(*progress)(game);

	data->sample = al_load_sample( GetDataFilePath(game, "monster.flac") );
	data->click_sample = al_load_sample( GetDataFilePath(game, "click.flac") );
	(*progress)(game);

	data->music = al_create_sample_instance(data->sample);
	al_attach_sample_instance_to_mixer(data->music, game->audio.music);
	al_set_sample_instance_playmode(data->music, ALLEGRO_PLAYMODE_LOOP);

	data->click = al_create_sample_instance(data->click_sample);
	al_attach_sample_instance_to_mixer(data->click, game->audio.fx);
	al_set_sample_instance_playmode(data->click, ALLEGRO_PLAYMODE_ONCE);

	if (!data->click_sample){
		fprintf(stderr, "Audio clip sample not loaded!\n" );
		exit(-1);
	}
	(*progress)(game);

	data->font = al_load_ttf_font(GetDataFilePath(game, "fonts/MonkeyIsland.ttf"),game->viewport.height*0.05,0 );

	al_set_target_backbuffer(game->display);
	return data;
}
Example #8
0
File: audio.c Project: bercik/space
void LoadSamples(Audio* const audio)
{
    al_reserve_samples(10);

    audio->song_sample = al_load_sample("song.ogg");
    audio->song_sample_instance = al_create_sample_instance(audio->song_sample);
    al_set_sample_instance_playmode(audio->song_sample_instance, ALLEGRO_PLAYMODE_LOOP);
    al_attach_sample_instance_to_mixer(audio->song_sample_instance, al_get_default_mixer());

    audio->samples[AT_EXPLOSION] = al_load_sample("explosion.wav");
    audio->samples[AT_RESTART] = al_load_sample("restart.wav");
    audio->samples[AT_TRANSPORT] = al_load_sample("transport.wav");
    audio->samples[AT_WHIZ] = al_load_sample("whiz.wav");
}
Example #9
0
void SoundManager::playMusic(Music *music, float vol, ALLEGRO_PLAYMODE mod)
{
	if (!music) return;

	ALLEGRO_SAMPLE_INSTANCE *inst = streams[music->sound->id]->instance;

	if (!al_get_sample_instance_playing(inst))
	{
		setVolume(music, vol);
		if (v_mute_music) muteMusic();
		al_set_sample_instance_playmode(inst, mod);
		al_play_sample_instance(inst);
	}
}
Example #10
0
void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
	struct dosowiskoResources *data = malloc(sizeof(struct dosowiskoResources));
	  data->bitmap = al_load_bitmap( GetDataFilePath(game, "bg.png"));

		data->font = al_load_font(GetDataFilePath(game, "fonts/MonkeyIsland.ttf"),100, ALLEGRO_TTF_MONOCHROME );
	(*progress)(game);

		    data->sample = al_load_sample( GetDataFilePath(game, "end.flac") );

		data->sound = al_create_sample_instance(data->sample);
		al_attach_sample_instance_to_mixer(data->sound, game->audio.fx);
		al_set_sample_instance_playmode(data->sound, ALLEGRO_PLAYMODE_ONCE);
		al_set_sample_instance_gain(data->sound, 1.5);

	return data;
}
Example #11
0
Music::Music(string name)
{
    sample = NULL;
    instance = NULL;
    sample = al_load_sample(name.c_str());
    if (!sample)
        return;
    instance = al_create_sample_instance(sample);
    if (!instance)
    {
        al_destroy_sample(sample);
    }
    al_attach_sample_instance_to_mixer(instance, al_get_default_mixer());
    al_set_sample_instance_gain(instance, 1.0);
    al_set_sample_instance_playmode(instance, ALLEGRO_PLAYMODE_LOOP);
}
Example #12
0
void Audio::Init(){ //// RESERVE SOUNDS, SET MUSIC, INSTANCE

	al_reserve_samples(3); //// CAN PLAY 3 SOUNDS AT ONCE

	//// LOAD IN SOUNDS

	bMusic = al_load_sample("Sounds/background.ogg");
	endGame = al_load_sample("Sounds/endGame.ogg");

	caught = al_load_sample("Sounds/caught_01.ogg");
	respawn = al_load_sample("Sounds/respawn_01.ogg");
	collect = al_load_sample("Sounds/collect_01.ogg");

	//// SET UP INSTANCE
	MusicInstance = al_create_sample_instance(bMusic);
	al_set_sample_instance_playmode(MusicInstance, ALLEGRO_PLAYMODE_LOOP);
	al_set_sample_instance_gain(MusicInstance, 1.0f);
	al_attach_sample_instance_to_mixer(MusicInstance, al_get_default_mixer());
};
Example #13
0
void install() {
    // setup allegro
    init = al_is_system_installed()
           && al_install_audio()
           && al_init_acodec_addon()
           && al_reserve_samples(0);

    // create bgm channel
    bgm_channel = al_create_sample_instance(NULL);
    al_attach_sample_instance_to_mixer(bgm_channel, al_get_default_mixer());
    al_set_sample_instance_playmode(bgm_channel, ALLEGRO_PLAYMODE_LOOP);
    // create sample channels
    for (int i=0; i<SAMPLE_MAX; i++) {
        effects.push_back( al_create_sample_instance(NULL) );
        al_attach_sample_instance_to_mixer(effects[i], al_get_default_mixer());
    }
    // setup volume
    set_volume(master_vol);
}
Example #14
0
static bool do_play_sample(ALLEGRO_SAMPLE_INSTANCE *splinst,
   ALLEGRO_SAMPLE *spl, float gain, float pan, float speed, ALLEGRO_PLAYMODE loop)
{
   if (!al_set_sample(splinst, spl)) {
      ALLEGRO_ERROR("al_set_sample failed\n");
      return false;
   }

   if (!al_set_sample_instance_gain(splinst, gain) ||
         !al_set_sample_instance_pan(splinst, pan) ||
         !al_set_sample_instance_speed(splinst, speed) ||
         !al_set_sample_instance_playmode(splinst, loop)) {
      return false;
   }

   if (!al_play_sample_instance(splinst)) {
      ALLEGRO_ERROR("al_play_sample_instance failed\n");
      return false;
   }

   return true;
}
Example #15
0
int main(int argc, char **argv)
{
   ALLEGRO_VOICE *voice;
   ALLEGRO_MIXER *mixer;
   ALLEGRO_SAMPLE_INSTANCE *sample;
   int i;
   char const **filenames;
   int n;

   if (argc < 2) {
      n = 1;
      filenames = malloc(sizeof *filenames);
      filenames[0] = "data/testing.ogg";
   }
   else {
      n = argc - 1;
      filenames = malloc(sizeof *filenames * n);
      for (i = 1; i < argc; ++i) {
         filenames[i - 1] = argv[i];
      }
   }

   if (!al_init()) {
      abort_example("Could not init Allegro.\n");
   }

   open_log();

   al_init_acodec_addon();

   if (!al_install_audio()) {
      abort_example("Could not init sound!\n");
   }

   voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16,
      ALLEGRO_CHANNEL_CONF_2);
   if (!voice) {
      abort_example("Could not create ALLEGRO_VOICE.\n");
   }

   mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
      ALLEGRO_CHANNEL_CONF_2);
   if (!mixer) {
      abort_example("al_create_mixer failed.\n");
   }

   if (!al_attach_mixer_to_voice(mixer, voice)) {
      abort_example("al_attach_mixer_to_voice failed.\n");
   }

   sample = al_create_sample_instance(NULL);
   if (!sample) {
      abort_example("al_create_sample failed.\n");
   }

   for (i = 0; i < n; ++i) {
      ALLEGRO_SAMPLE *sample_data = NULL;
      const char *filename = filenames[i];
      float sample_time = 0;

      /* Load the entire sound file from disk. */
      sample_data = al_load_sample(filename);
      if (!sample_data) {
         abort_example("Could not load sample from '%s'!\n",
            filename);
         continue;
      }

      if (!al_set_sample(sample, sample_data)) {
         abort_example("al_set_sample_instance_ptr failed.\n");
         continue;
      }

      if (!al_attach_sample_instance_to_mixer(sample, mixer)) {
         abort_example("al_attach_sample_instance_to_mixer failed.\n");
         goto done;
      }

      /* Play sample in looping mode. */
      al_set_sample_instance_playmode(sample, ALLEGRO_PLAYMODE_LOOP);
      al_play_sample_instance(sample);

      sample_time = al_get_sample_instance_time(sample);
      log_printf("Playing '%s' (%.3f seconds) 3 times", filename,
         sample_time);

      al_rest(sample_time);

      if (!al_set_sample_instance_gain(sample, 0.5)) {
         abort_example("Failed to set gain.\n");
      }
      al_rest(sample_time);

      if (!al_set_sample_instance_gain(sample, 0.25)) {
         abort_example("Failed to set gain.\n");
      }
      al_rest(sample_time);

      al_stop_sample_instance(sample);
      log_printf("\nDone playing '%s'\n", filename);

      /* Free the memory allocated. */
      al_set_sample(sample, NULL);
      al_destroy_sample(sample_data);
   }

   al_destroy_sample_instance(sample);
   al_destroy_mixer(mixer);
   al_destroy_voice(voice);

   al_uninstall_audio();

done:
   close_log(true);

   return 0;
}
Example #16
0
int main(int argc, char **argv)
{
    ALLEGRO_VOICE *voice;
    ALLEGRO_MIXER *mixer;
    ALLEGRO_MIXER *submixer[2];
    ALLEGRO_SAMPLE_INSTANCE *sample[2];
    ALLEGRO_SAMPLE *sample_data[2];
    float sample_time;
    float max_sample_time;
    int i;

    if (!al_init()) {
        abort_example("Could not init Allegro.\n");
    }

    open_log();

    if (argc < 3) {
        log_printf("This example needs to be run from the command line.\nUsage: %s file1 file2\n", argv[0]);
        goto done;
    }

    al_init_acodec_addon();

    if (!al_install_audio()) {
        abort_example("Could not init sound!\n");
    }

    voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16,
                            ALLEGRO_CHANNEL_CONF_2);
    if (!voice) {
        abort_example("Could not create ALLEGRO_VOICE.\n");
    }

    mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
                            ALLEGRO_CHANNEL_CONF_2);
    submixer[0] = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
                                  ALLEGRO_CHANNEL_CONF_2);
    submixer[1] = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32,
                                  ALLEGRO_CHANNEL_CONF_2);
    if (!mixer || !submixer[0] || !submixer[1]) {
        abort_example("al_create_mixer failed.\n");
    }

    if (!al_attach_mixer_to_voice(mixer, voice)) {
        abort_example("al_attach_mixer_to_voice failed.\n");
    }

    for (i = 0; i < 2; i++) {
        const char *filename = argv[i + 1];
        sample_data[i] = al_load_sample(filename);
        if (!sample_data[i]) {
            abort_example("Could not load sample from '%s'!\n", filename);
        }
        sample[i] = al_create_sample_instance(NULL);
        if (!sample[i]) {
            abort_example("al_create_sample failed.\n");
        }
        if (!al_set_sample(sample[i], sample_data[i])) {
            abort_example("al_set_sample_ptr failed.\n");
        }
        if (!al_attach_sample_instance_to_mixer(sample[i], submixer[i])) {
            abort_example("al_attach_sample_instance_to_mixer failed.\n");
        }
        if (!al_attach_mixer_to_mixer(submixer[i], mixer)) {
            abort_example("al_attach_mixer_to_mixer failed.\n");
        }
    }

    /* Play sample in looping mode. */
    for (i = 0; i < 2; i++) {
        al_set_sample_instance_playmode(sample[i], ALLEGRO_PLAYMODE_LOOP);
        al_play_sample_instance(sample[i]);
    }

    max_sample_time = al_get_sample_instance_time(sample[0]);
    sample_time = al_get_sample_instance_time(sample[1]);
    if (sample_time > max_sample_time)
        max_sample_time = sample_time;

    log_printf("Playing...");

    al_rest(max_sample_time);

    al_set_sample_instance_gain(sample[0], 0.5);
    al_rest(max_sample_time);

    al_set_sample_instance_gain(sample[1], 0.25);
    al_rest(max_sample_time);

    al_stop_sample_instance(sample[0]);
    al_stop_sample_instance(sample[1]);
    log_printf("Done\n");

    /* Free the memory allocated. */
    for (i = 0; i < 2; i++) {
        al_set_sample(sample[i], NULL);
        al_destroy_sample(sample_data[i]);
        al_destroy_sample_instance(sample[i]);
        al_destroy_mixer(submixer[i]);
    }
    al_destroy_mixer(mixer);
    al_destroy_voice(voice);

    al_uninstall_audio();

done:
    close_log(true);

    return 0;
}
int main(void)
{
	// don't forget to put allegro-5.0.10-monolith-md-debug.lib
	//primitive variable
	bool done = false;
	bool redraw = true;
	const int FPS = 60;
	bool isGameOver = false;

	//object variables
	spaceShip ship;
	Bullet bullets[NUM_BULLETS];
	Comet comets[NUM_COMETS];
	Explosion explosions[NUM_EXPLOSIONS];

	//Allegro variables
	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;
	ALLEGRO_TIMER *timer = NULL;
	ALLEGRO_FONT *font18 = NULL;
	ALLEGRO_BITMAP *shipImage;
	ALLEGRO_BITMAP *cometImage;
	ALLEGRO_BITMAP *expImage;
	ALLEGRO_SAMPLE * sample = NULL;
	ALLEGRO_SAMPLE * sample2 = NULL;
	ALLEGRO_SAMPLE * sample3 = NULL;
	ALLEGRO_SAMPLE_INSTANCE * instance1 = NULL;
	ALLEGRO_SAMPLE_INSTANCE * instance2 = NULL;
	ALLEGRO_SAMPLE_INSTANCE * instance3 = NULL;

	//Initialization Functions
	if(!al_init())										//initialize Allegro
		return -1;

	display = al_create_display(WIDTH, HEIGHT);			//create our display object

	if(!display)										//test display object
		return -1;

	al_init_primitives_addon();
	al_install_keyboard();
	al_init_font_addon();
	al_init_ttf_addon();
	al_init_image_addon();
	al_install_audio(); // always initialize audio before the codecs
	al_init_acodec_addon();
	
	al_reserve_samples(10); // reserves numbers of samples/ channels or voices

	sample = al_load_sample("chirp.ogg");
	sample2 = al_load_sample("static.ogg");
	sample3 = al_load_sample("JSS - Our Song.ogg");

	instance1 = al_create_sample_instance(sample);
	instance2 = al_create_sample_instance(sample2);
	instance3 = al_create_sample_instance(sample3);	

	al_set_sample_instance_playmode(instance3, ALLEGRO_PLAYMODE_LOOP);

	al_attach_sample_instance_to_mixer(instance1, al_get_default_mixer());
    al_attach_sample_instance_to_mixer(instance2, al_get_default_mixer());
	al_attach_sample_instance_to_mixer(instance3, al_get_default_mixer());

	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / FPS);

	cometImage = al_load_bitmap("asteroid-1-96.png");
	//above does not need convert_mask_to_alpha because it is tranparent background in sprite sheet
	shipImage = al_load_bitmap("Spaceship_by_arboris.png");
	al_convert_mask_to_alpha(shipImage,al_map_rgb(255,0,255));

	expImage = al_load_bitmap("explosion_3_40_128.png");

	srand(time(NULL));
	InitShip(ship, shipImage);
	InitBullet(bullets, NUM_BULLETS);
	InitComet(comets, NUM_COMETS, cometImage);
	InitExplosions(explosions, NUM_EXPLOSIONS, expImage);

	font18 = al_load_font("arial.ttf", 18, 0);

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

	al_play_sample_instance(instance3);

	al_start_timer(timer);
	while(!done)
	{
		ALLEGRO_EVENT ev;
		al_wait_for_event(event_queue, &ev);

		if(ev.type == ALLEGRO_EVENT_TIMER)
		{
			redraw = true;
			int soundX = ship.x;
			int soundY = ship.y;
			
			if(keys[UP])
				MoveShipUp(ship);
			else if(keys[DOWN])
				MoveShipDown(ship);
			else
				ResetShipAnimation(ship,1);
			if(keys[LEFT])
				MoveShipLeft(ship);
			else if(keys[RIGHT])
				MoveShipRight(ship);
			else
				ResetShipAnimation(ship, 2);
			
			if(soundX != ship.x || soundY != ship.y){
				//al_play_sample(sample, 1,0,1, ALLEGRO_PLAYMODE_ONCE, NULL);
				al_play_sample_instance(instance1);
			}
			if (ship.x -10 < 0 || ship.x + 10 > WIDTH || ship.y - 10 < 0 || ship.y + 10 > HEIGHT){
				al_play_sample_instance(instance2);
			}

			if(!isGameOver)
			{
				UpdateExplosions(explosions, NUM_EXPLOSIONS);
				UpdateBullet(bullets, NUM_BULLETS);
				StartComet(comets, NUM_COMETS);
				UpdateComet(comets, NUM_COMETS);
				CollideBullet(bullets, NUM_BULLETS, comets, NUM_COMETS, ship, explosions, NUM_EXPLOSIONS);
				CollideComet(comets, NUM_COMETS, ship, explosions, NUM_BULLETS);

				if(ship.lives <= 0)
					isGameOver = 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_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = true;
				FireBullet(bullets, NUM_BULLETS, ship);
				break;
			}
		}
		else if(ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch(ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPACE] = false;
				break;
			}
		}

		if(redraw && al_is_event_queue_empty(event_queue))
		{
			redraw = false; 

			if(!isGameOver)
			{
				DrawShip(ship);
				if(al_get_sample_instance_playing(instance1)){
					al_draw_text(font18, al_map_rgb(255,255,255),5,30,0, "Instance 1 is playing");
				}
				if(al_get_sample_instance_playing(instance2)){
					al_draw_text(font18, al_map_rgb(255,255,255),WIDTH - 5,30,ALLEGRO_ALIGN_RIGHT, "Instance 2 is playing");
				}
				if(al_get_sample_instance_playing(instance3)){
					al_draw_textf(font18, al_map_rgb(255,255,255),5,HEIGHT - 30,0, "Instance 3 is playing: %.1f %%", al_get_sample_instance_position(instance3) / (float)al_get_sample_instance_length(instance3) * 100);
				}

				DrawBullet(bullets, NUM_BULLETS);
				DrawComet(comets, NUM_COMETS);
				DrawExplosions(explosions, NUM_EXPLOSIONS);

				al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "Player has %i lives left. Player has destroyed %i objects", ship.lives, ship.score);
			}
			else
			{
				al_draw_textf(font18, al_map_rgb(0, 255, 255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Game Over. Final Score: %i", ship.score);
			}
		
			al_flip_display();
			al_clear_to_color(al_map_rgb(0,0,0));
		}
	}

	al_destroy_sample_instance(instance1);
	al_destroy_sample_instance(instance2);
	al_destroy_sample_instance(instance3);
	al_destroy_sample(sample);
	al_destroy_sample(sample2);
	al_destroy_sample(sample3);
	al_destroy_bitmap(expImage);
	al_destroy_bitmap(shipImage);
	al_destroy_bitmap(cometImage);
	al_destroy_event_queue(event_queue);
	al_destroy_timer(timer);
	al_destroy_font(font18);
	al_destroy_display(display);						//destroy our display object

	return 0;
}
Example #18
0
int main(int argc, char **argv)
{
	bool done = false;
	bool render = false;

	float gameTime = 0;
	int frames = 0;
	int gameFPS = 0;

	float evTimer = 0;

	tractor = new Tractor();
	Xml = new xml();

	int state = -1;

	ALLEGRO_BITMAP *icon;
	ALLEGRO_BITMAP *map = NULL;
	ALLEGRO_BITMAP *panel = NULL;
	ALLEGRO_BITMAP *tractorImage = NULL;
	ALLEGRO_BITMAP *titleImage = NULL;
	ALLEGRO_BITMAP *lostImage = NULL;
	ALLEGRO_SAMPLE *titleSong = NULL;
	ALLEGRO_SAMPLE *gameSong = NULL;
	ALLEGRO_SAMPLE *lostSong = NULL;
	ALLEGRO_SAMPLE *cash = NULL;

	ALLEGRO_BITMAP *L1 = NULL;
	ALLEGRO_BITMAP *L2 = NULL;
	ALLEGRO_BITMAP *L3 = NULL;
	ALLEGRO_BITMAP *L4 = NULL;
	ALLEGRO_BITMAP *L5 = NULL;
	ALLEGRO_BITMAP *L6 = NULL;
	ALLEGRO_BITMAP *L7 = NULL;

	ALLEGRO_DISPLAY *display = NULL;
	ALLEGRO_DISPLAY_MODE   disp_data;
	ALLEGRO_EVENT_QUEUE *event_queue = NULL;
	ALLEGRO_TIMER *timer;
	ALLEGRO_FONT *font;
	ALLEGRO_FONT *score;

	if (!al_init())
		return -1;

	al_install_keyboard();
	al_install_mouse();
	al_init_image_addon();
	al_init_font_addon();
	al_init_ttf_addon();
	al_init_primitives_addon();
	al_install_audio();
	al_init_acodec_addon();

	al_get_display_mode(al_get_num_display_modes() - 1, &disp_data);

	//al_set_new_display_flags(ALLEGRO_FULLSCREEN);
	al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE);

	display = al_create_display(disp_data.width, disp_data.height);

	icon = al_load_bitmap("icon.png");
	al_set_display_icon(display, icon);

	float sx = (float)disp_data.width / WIDTH;
	float sy = (float)disp_data.height / HEIGHT;

	ALLEGRO_TRANSFORM trans;
	al_identity_transform(&trans);
	al_scale_transform(&trans, sx, sy);
	al_use_transform(&trans);

	if (!display)
		return -1;

	font = al_load_font("arial.ttf", 20, 0);
	score = al_load_font("score.ttf", 45, 0);
	al_reserve_samples(15);

	map = al_load_bitmap("map2.png");
	panel = al_load_bitmap("panel.png");

	L1 = al_load_bitmap("l1.png");
	L2 = al_load_bitmap("l2.png");
	L3 = al_load_bitmap("l3.png");
	L4 = al_load_bitmap("l4.png");
	L5 = al_load_bitmap("l5.png");
	L6 = al_load_bitmap("l6.png");
	L7 = al_load_bitmap("l7.png");

	Background *Map = new Background(map);
	objects.push_back(Map);

	TextBox *Task = new TextBox;

	Field *field1 = new Field(L1, L2, L3, L4, L5, L6, L7, 50, 50);
	objects.push_back(field1);
	Field *field2 = new Field(L1, L2, L3, L4, L5, L6, L7, 450, 50);
	objects.push_back(field2);
	Field *field3 = new Field(L1, L2, L3, L4, L5, L6, L7, 50, 450);
	objects.push_back(field3);
	Field *field4 = new Field(L1, L2, L3, L4, L5, L6, L7, 450, 450);
	objects.push_back(field4);

	tractorImage = al_load_bitmap("tractor.png");
	cash = al_load_sample("cash.ogg");
	tractor->Init(tractorImage, cash);
	objects.push_back(tractor);

	titleImage = al_load_bitmap("screen_Title.png");
	lostImage = al_load_bitmap("screen_Lost.png");

	titleScreen = new Background(titleImage);
	lostScreen = new Background(lostImage);

	titleSong = al_load_sample("title.ogg");
	gameSong = al_load_sample("game.ogg");
	lostSong = al_load_sample("lost.ogg");

	songInstance = al_create_sample_instance(titleSong);
	al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);

	songInstance2 = al_create_sample_instance(gameSong);
	al_set_sample_instance_playmode(songInstance2, ALLEGRO_PLAYMODE_LOOP);

	songInstance3 = al_create_sample_instance(lostSong);
	al_set_sample_instance_playmode(songInstance3, ALLEGRO_PLAYMODE_LOOP);

	al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
	al_attach_sample_instance_to_mixer(songInstance2, al_get_default_mixer());
	al_attach_sample_instance_to_mixer(songInstance3, al_get_default_mixer());

	ChangeState(state, TITLE);

	event_queue = al_create_event_queue();
	timer = al_create_timer(1.0 / 60);

	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_mouse_event_source());

	al_start_timer(timer);
	gameTime = al_current_time();

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

		if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
		{
			switch (ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = true;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = true;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = true;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = true;
				break;
			case ALLEGRO_KEY_ENTER:
				keys[ENTER] = true;
				if (state == TITLE)
					ChangeState(state, PLAYING);
				else if (state == PLAYING && Task->CheckText())
				{
					TextBox *text = new TextBox();
					text->SetText(Task->Send());
					history.push_back(text);

					for (iter2 = history.begin(); iter2 != history.end(); iter2++)
					{
						if ((*iter2)->GetY() < 400)
						{
							delete (*iter2);
							iter2 = history.erase(iter2);
						}
						(*iter2)->UpdateY();
					}

					Xml->interpreter(Task->GetLast(), tractor);

					TextBox *txtxml = new TextBox();
					txtxml->SetText(Xml->wyslij());
					history.push_back(txtxml);

					for (iter2 = history.begin(); iter2 != history.end(); iter2++)
					{
						if ((*iter2)->GetY() < 300)
						{
							delete (*iter2);
							iter2 = history.erase(iter2);
						}
						(*iter2)->UpdateY();
					}
				}
				else if (state == LOST)
					ChangeState(state, PLAYING);
				break;
			case ALLEGRO_KEY_TAB:
				keys[TAB] = true;
				if (state == PLAYING)
				{

					Task->SetStatus();
					if (Task->GetStatus())
					{
						TextBox *text = new TextBox();
						text->SetText("Konsola zostala wlaczona");
						history.push_back(text);
					}
					else
					{
						TextBox *text = new TextBox();
						text->SetText("Konsola zostala wylaczona");
						history.push_back(text);
					}

					for (iter2 = history.begin(); iter2 != history.end(); iter2++)
					{
						if ((*iter2)->GetY() < 300)
						{
							delete (*iter2);
							iter2 = history.erase(iter2);
						}
						(*iter2)->UpdateY();
					}

					setTimer(evTimer);
				}
				tractor->Sell();
				break;
			case ALLEGRO_KEY_SPACE:
				keys[SPC] = true;
				if (state == PLAYING)
					Task->Add(" ");
				break;
			case ALLEGRO_KEY_BACKSPACE:
				if (state == PLAYING && Task->CheckText())
					Task->Backspace();
				break;
			case ALLEGRO_KEY_COMMA:
				keys[COM] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add(",");
				break;
			case ALLEGRO_KEY_0:
				numb[N0] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("0");
				break;
			case ALLEGRO_KEY_1:
				numb[N1] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("1");
				break;
			case ALLEGRO_KEY_2:
				numb[N2] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("2");
				break;
			case ALLEGRO_KEY_3:
				numb[N3] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("3");
				break;
			case ALLEGRO_KEY_4:
				numb[N4] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("4");
				break;
			case ALLEGRO_KEY_5:
				numb[N5] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("5");
				break;
			case ALLEGRO_KEY_6:
				numb[N6] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("6");
				break;
			case ALLEGRO_KEY_7:
				numb[N7] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("7");
				break;
			case ALLEGRO_KEY_8:
				numb[N8] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("8");
				break;
			case ALLEGRO_KEY_9:
				numb[N9] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("9");
				break;
			case ALLEGRO_KEY_A:
				letters[A] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("a");
				break;
			case ALLEGRO_KEY_B:
				letters[B] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("b");
				break;
			case ALLEGRO_KEY_C:
				letters[C] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("c");
				break;
			case ALLEGRO_KEY_D:
				letters[D] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("d");
				break;
			case ALLEGRO_KEY_E:
				letters[E] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("e");
				break;
			case ALLEGRO_KEY_F:
				letters[F] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("f");
				break;
			case ALLEGRO_KEY_G:
				letters[G] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("g");
				break;
			case ALLEGRO_KEY_H:
				letters[H] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("h");
				break;
			case ALLEGRO_KEY_I:
				letters[I] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("i");
				break;
			case ALLEGRO_KEY_J:
				letters[J] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("j");
				break;
			case ALLEGRO_KEY_K:
				letters[K] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("k");
				break;
			case ALLEGRO_KEY_L:
				letters[L] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("l");
				break;
			case ALLEGRO_KEY_M:
				letters[M] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("m");
				break;
			case ALLEGRO_KEY_N:
				letters[N] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("n");
				break;
			case ALLEGRO_KEY_O:
				letters[O] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("o");
				break;
			case ALLEGRO_KEY_P:
				letters[P] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("p");
				break;
			case ALLEGRO_KEY_Q:
				letters[Q] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("q");
				break;
			case ALLEGRO_KEY_R:
				letters[R] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("r");
				break;
			case ALLEGRO_KEY_S:
				letters[S] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("s");
				break;
			case ALLEGRO_KEY_T:
				letters[T] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("t");
				break;
			case ALLEGRO_KEY_U:
				letters[U] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("u");
				break;
			case ALLEGRO_KEY_V:
				letters[V] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("v");
				break;
			case ALLEGRO_KEY_W:
				letters[W] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("w");
				break;
			case ALLEGRO_KEY_X:
				letters[X] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("x");
				break;
			case ALLEGRO_KEY_Y:
				letters[Y] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("y");
				break;
			case ALLEGRO_KEY_Z:
				letters[Z] = true;
				if (state == PLAYING && Task->GetStatus())
					Task->Add("z");
				break;
			}
		}
		else if (ev.type == ALLEGRO_EVENT_KEY_UP)
		{
			switch (ev.keyboard.keycode)
			{
			case ALLEGRO_KEY_ESCAPE:
				done = true;
				break;
			case ALLEGRO_KEY_LEFT:
				keys[LEFT] = false;
				break;
			case ALLEGRO_KEY_RIGHT:
				keys[RIGHT] = false;
				break;
			case ALLEGRO_KEY_UP:
				keys[UP] = false;
				break;
			case ALLEGRO_KEY_DOWN:
				keys[DOWN] = false;
				break;
			case ALLEGRO_KEY_ENTER:
				keys[ENTER] = false;
				break;
			case ALLEGRO_KEY_TAB:
				keys[TAB] = false;
				break;
			case ALLEGRO_KEY_BACKSPACE:
				keys[BSPC] = false;
				break;
			case ALLEGRO_KEY_COMMA:
				keys[COM] = false;
				break;
			case ALLEGRO_KEY_0:
				numb[N0] = false;
				break;
			case ALLEGRO_KEY_1:
				numb[N1] = false;
				break;
			case ALLEGRO_KEY_2:
				numb[N2] = false;
				break;
			case ALLEGRO_KEY_3:
				numb[N3] = false;
				break;
			case ALLEGRO_KEY_4:
				numb[N4] = false;
				break;
			case ALLEGRO_KEY_5:
				numb[N5] = false;
				break;
			case ALLEGRO_KEY_6:
				numb[N6] = false;
				break;
			case ALLEGRO_KEY_7:
				numb[N7] = false;
				break;
			case ALLEGRO_KEY_8:
				numb[N8] = false;
				break;
			case ALLEGRO_KEY_9:
				numb[N9] = false;
				break;
			case ALLEGRO_KEY_A:
				letters[A] = false;
				break;
			case ALLEGRO_KEY_B:
				letters[B] = false;
				break;
			case ALLEGRO_KEY_C:
				letters[C] = false;
				break;
			case ALLEGRO_KEY_D:
				letters[D] = false;
				break;
			case ALLEGRO_KEY_E:
				letters[E] = false;
				break;
			case ALLEGRO_KEY_F:
				letters[F] = false;
				break;
			case ALLEGRO_KEY_G:
				letters[G] = false;
				break;
			case ALLEGRO_KEY_H:
				letters[H] = false;
				break;
			case ALLEGRO_KEY_I:
				letters[I] = false;
				break;
			case ALLEGRO_KEY_J:
				letters[J] = false;
				break;
			case ALLEGRO_KEY_K:
				letters[K] = false;
				break;
			case ALLEGRO_KEY_L:
				letters[L] = false;
				break;
			case ALLEGRO_KEY_M:
				letters[M] = false;
				break;
			case ALLEGRO_KEY_N:
				letters[N] = false;
				break;
			case ALLEGRO_KEY_O:
				letters[O] = false;
				break;
			case ALLEGRO_KEY_P:
				letters[P] = false;
				break;
			case ALLEGRO_KEY_Q:
				letters[Q] = false;
				break;
			case ALLEGRO_KEY_R:
				letters[R] = false;
				break;
			case ALLEGRO_KEY_S:
				letters[S] = false;
				break;
			case ALLEGRO_KEY_T:
				letters[T] = false;
				break;
			case ALLEGRO_KEY_U:
				letters[U] = false;
				break;
			case ALLEGRO_KEY_V:
				letters[V] = false;
				break;
			case ALLEGRO_KEY_W:
				letters[W] = false;
				break;
			case ALLEGRO_KEY_X:
				letters[X] = false;
				break;
			case ALLEGRO_KEY_Y:
				letters[Y] = false;
				break;
			case ALLEGRO_KEY_Z:
				letters[Z] = false;
				break;
			}
		}

		else if (ev.type == ALLEGRO_EVENT_TIMER)
		{
			render = true;

			frames++;

			if (al_current_time() - gameTime >= 1)
			{
				gameTime = al_current_time();
				gameFPS = frames;
				frames = 0;
			}

			if (state == PLAYING)
			{
				if (keys[UP])
				{
					if (Map->GetY() + Map->frameHeight > disp_data.height)
					{
						for (iter = objects.begin(); iter != objects.end(); ++iter)
						{
							(*iter)->SetY((*iter)->GetY() - 10);
						}
						tractor->SetDistY((tractor->GetDistY() - 10));
					}
				}
				else if (keys[DOWN])
				{
					if (Map->GetY() < 0)
					{
						for (iter = objects.begin(); iter != objects.end(); ++iter)
						{
							(*iter)->SetY((*iter)->GetY() + 10);
						}
						tractor->SetDistY(tractor->GetDistY() + 10);
					}
				}

				if (keys[LEFT])
				{
					if (Map->GetWidth() > (disp_data.width - al_get_bitmap_width(panel)))
					{
						for (iter = objects.begin(); iter != objects.end(); ++iter)
						{
							(*iter)->SetX((*iter)->GetX() - 10);
						}
						tractor->SetDistX(tractor->GetDistX() - 10);
					}
				}
				else if (keys[RIGHT])
				{
					if (Map->GetX() < 0)
					{
						for (iter = objects.begin(); iter != objects.end(); ++iter)
						{
							(*iter)->SetX((*iter)->GetX() + 10);
						}
						tractor->SetDistX(tractor->GetDistX() + 10);
					}
				}

				for (iter = objects.begin(); iter != objects.end(); ++iter)
					(*iter)->Update();

				if (tractor->GetStatus())
					tractor->Move();

				field1->Change_Field();
				field1->Grow_Field();
				field2->Change_Field();
				field2->Grow_Field();
				field3->Change_Field();
				field3->Grow_Field();
				field4->Change_Field();
				field4->Grow_Field();
				field1->Action_On_Field(tractor);
				field2->Action_On_Field(tractor);
				field3->Action_On_Field(tractor);
				field4->Action_On_Field(tractor);

				if (!tractor->Get_Iminwork()){
					Xml->ZKolejki(field1, field2, field3, field4, tractor);
					if (Xml->wyslij() != ""){
						TextBox *txtxml = new TextBox();
						txtxml->SetText(Xml->wyslij());
						history.push_back(txtxml);

						for (iter2 = history.begin(); iter2 != history.end(); iter2++)
						{
							if ((*iter2)->GetY() < 300)
							{
								delete (*iter2);
								iter2 = history.erase(iter2);
							}

							(*iter2)->UpdateY();
						}
					}
				}
				if (evTimer < 60)
				{
					evTimer += 0.1;
				}
				else
				{
					if (tractor->GetPodpowiedz() == 0)
					{
						Xml->podpowiedz(field1, field2, field3, field4, tractor);
						evTimer = 0;

						TextBox *txtxml = new TextBox();
						txtxml->SetText(Xml->wyslij());
						history.push_back(txtxml);

						for (iter2 = history.begin(); iter2 != history.end(); iter2++)
						{
							if ((*iter2)->GetY() < 300)
							{
								delete (*iter2);
								iter2 = history.erase(iter2);
							}

							(*iter2)->UpdateY();
						}
					}
					
				}
			}

			if (tractor->GetMoney() <= 0)
				ChangeState(state, LOST);
		}

		for (iter = objects.begin(); iter != objects.end();)
		{
			if (!(*iter)->GetAlive())
			{
				delete (*iter);
				iter = objects.erase(iter);
			}
			else
				iter++;
		}

		if (render && al_is_event_queue_empty(event_queue))
		{
			render = false;

			if (state == TITLE)
			{
				titleScreen->Render();
			}
			else if (state == PLAYING)
			{
				for (iter = objects.begin(); iter != objects.end(); ++iter)
					(*iter)->Render();

				al_draw_bitmap(panel, WIDTH - al_get_bitmap_width(panel), 0, 0);
				al_draw_textf(font, al_map_rgb(255, 255, 255), Task->GetX(), Task->GetY(), 0, Task->ShowText());

				for (iter2 = history.begin(); iter2 != history.end(); iter2++)
				{
					al_draw_textf(font, al_map_rgb(255, 255, 255), (*iter2)->GetX(), (*iter2)->GetY(), 0, (*iter2)->ShowText());
				}

				if (tractor->GetHealth() < 20)
					al_draw_textf(score, RED, WIDTH - 430, 15, 0, "%i", tractor->GetHealth());
				else
					al_draw_textf(score, BLACK, WIDTH - 430, 15, 0, "%i", tractor->GetHealth());
				
				if (tractor->GetFuel() < 20)
					al_draw_textf(score, RED, WIDTH - 260, 15, 0, "%i", tractor->GetFuel());
				else
					al_draw_textf(score, BLACK, WIDTH - 260, 15, 0, "%i", tractor->GetFuel());
				
				if (tractor->GetMoney() < 200)
					al_draw_textf(score, RED, WIDTH - 400, 100, 0, "%i", tractor->GetMoney());
				else
					al_draw_textf(score, BLACK, WIDTH - 400, 100, 0, "%i", tractor->GetMoney());

				al_draw_textf(score, BLACK, WIDTH - 70, 15, 0, "%i", tractor->GetWater());

				for (int j = 0; j < 5; j++)
				{
					al_draw_textf(font, BLACK, WIDTH - 170, 85 + j * 20, 0, "%i", tractor->GetSupply(0, j));
				}

				for (int j = 0; j < 5; j++)
				{
					al_draw_textf(font, BLACK, WIDTH - 150, 85 + j * 20, 0, "%i", tractor->GetSupply(1, j));
				}

				al_draw_textf(font, al_map_rgb(255, 0, 255), 5, 5, 0, "FPS: %i", WIDTH - al_get_bitmap_width(panel) /*gameFPS*/);
			}
			else if (state == LOST)
				lostScreen->Render();

			al_flip_display();
			al_clear_to_color(al_map_rgb(0, 0, 0));
		}
	}

	for (iter = objects.begin(); iter != objects.end();)
	{
		(*iter)->Destroy();
		delete (*iter);
		iter = objects.erase(iter);
	}

	for (iter2 = history.begin(); iter2 != history.end();)
	{
		(*iter2)->Destroy();
		delete (*iter2);
		iter2 = history.erase(iter2);
	}

	//tractor->Destroy();
	Task->Destroy();
	titleScreen->Destroy();
	lostScreen->Destroy();
	delete titleScreen;
	delete lostScreen;
	al_destroy_sample(cash);
	al_destroy_sample_instance(songInstance);
	al_destroy_sample_instance(songInstance2);
	al_destroy_sample_instance(songInstance3);

	al_destroy_font(score);
	al_destroy_font(font);
	al_destroy_timer(timer);
	al_destroy_event_queue(event_queue);
	al_destroy_display(display);
			
	return 0;
}
Example #19
0
void TocaMusicaPrincipal(ALLEGRO_SAMPLE_INSTANCE *SongInstance)
{
	al_set_sample_instance_playmode(SongInstance, ALLEGRO_PLAYMODE_LOOP);
	al_attach_sample_instance_to_mixer(SongInstance, al_get_default_mixer());
	al_play_sample_instance(SongInstance); //Começa a tocar a Musica
}
int main(int argc, char **argv)
{
   ALLEGRO_VOICE *voice;
   ALLEGRO_SAMPLE_INSTANCE *sample;
   int i;

   if (argc < 2) {
      fprintf(stderr, "Usage: %s {audio_files}\n", argv[0]);
      return 1;
   }

   if (!al_init()) {
      fprintf(stderr, "Could not init Allegro.\n");
      return 1;
   }

   al_init_acodec_addon();

   if (!al_install_audio()) {
      fprintf(stderr, "Could not init sound!\n");
      return 1;
   }

   for (i = 1; i < argc; ++i) {
      ALLEGRO_SAMPLE *sample_data = NULL;
      const char *filename = argv[i];
      ALLEGRO_CHANNEL_CONF chan;
      ALLEGRO_AUDIO_DEPTH depth;
      unsigned long freq;
      float sample_time = 0;

      /* Load the entire sound file from disk. */
      sample_data = al_load_sample(filename);
      if (!sample_data) {
         fprintf(stderr, "Could not load sample from '%s'!\n",
            filename);
         continue;
      }

      sample = al_create_sample_instance(NULL);
      if (!sample) {
         fprintf(stderr, "al_create_sample failed.\n");
        return 1;
      }

      if (!al_set_sample(sample, sample_data)) {
         fprintf(stderr, "al_set_sample failed.\n");
         continue;
      }

      depth = al_get_sample_instance_depth(sample);
      chan = al_get_sample_instance_channels(sample);
      freq = al_get_sample_instance_frequency(sample);
      fprintf(stderr, "Loaded sample: %i-bit depth, %i channels, %li Hz\n",
         (depth < 8) ? (8+depth*8) : 0, (chan>>4)+(chan%0xF), freq);
      fprintf(stderr, "Trying to create a voice with the same specs... ");
      voice = al_create_voice(freq, depth, chan);
      if (!voice) {
         fprintf(stderr, "Could not create ALLEGRO_VOICE.\n");
         return 1;
      }
      fprintf(stderr, "done.\n");

      if (!al_attach_sample_instance_to_voice(sample, voice)) {
         fprintf(stderr, "al_attach_sample_instance_to_voice failed.\n");
         return 1;
      }

      /* Play sample in looping mode. */
      al_set_sample_instance_playmode(sample, ALLEGRO_PLAYMODE_LOOP);
      al_play_sample_instance(sample);

      sample_time = al_get_sample_instance_time(sample);
      fprintf(stderr, "Playing '%s' (%.3f seconds) 3 times", filename,
         sample_time);

      al_rest(sample_time * 3);

      al_stop_sample_instance(sample);
      fprintf(stderr, "\n");

      /* Free the memory allocated. */
      al_set_sample(sample, NULL);
      al_destroy_sample(sample_data);
      al_destroy_sample_instance(sample);
      al_destroy_voice(voice);
   }

   al_uninstall_audio();

   return 0;
}
Example #21
0
void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
	  struct RocketsResources *data = malloc(sizeof(struct RocketsResources));

		data->timeline = TM_Init(game, "riots");

		data->bg = al_load_bitmap( GetDataFilePath(game, "riots/bg.png"));

		data->earth = al_load_bitmap( GetDataFilePath(game, "riots/separator.png"));

		data->clouds = al_load_bitmap( GetDataFilePath(game, "riots/fog.png"));
		(*progress)(game);

		data->rocket_sample = al_load_sample( GetDataFilePath(game, "bump.flac") );
		(*progress)(game);
		data->boom_sample = al_load_sample( GetDataFilePath(game, "boom.flac") );
		(*progress)(game);
		data->jump_sample = al_load_sample( GetDataFilePath(game, "launch.flac") );
		(*progress)(game);
		data->rainbow_sample = al_load_sample( GetDataFilePath(game, "win.flac") );
		(*progress)(game);
		data->wuwu_sample = al_load_sample( GetDataFilePath(game, "riots/vuvu.flac") );
		(*progress)(game);
		data->riot_sample = al_load_sample( GetDataFilePath(game, "riots/riot.flac") );
		(*progress)(game);

		data->rocket_sound = al_create_sample_instance(data->rocket_sample);
		al_attach_sample_instance_to_mixer(data->rocket_sound, game->audio.fx);
		al_set_sample_instance_playmode(data->rocket_sound, ALLEGRO_PLAYMODE_ONCE);

		data->boom_sound = al_create_sample_instance(data->boom_sample);
		al_attach_sample_instance_to_mixer(data->boom_sound, game->audio.fx);
		al_set_sample_instance_playmode(data->boom_sound, ALLEGRO_PLAYMODE_ONCE);

		data->rainbow_sound = al_create_sample_instance(data->rainbow_sample);
		al_attach_sample_instance_to_mixer(data->rainbow_sound, game->audio.fx);
		al_set_sample_instance_playmode(data->rainbow_sound, ALLEGRO_PLAYMODE_ONCE);

		data->jump_sound = al_create_sample_instance(data->jump_sample);
		al_attach_sample_instance_to_mixer(data->jump_sound, game->audio.fx);
		al_set_sample_instance_playmode(data->jump_sound, ALLEGRO_PLAYMODE_ONCE);

		data->riot_sound = al_create_sample_instance(data->riot_sample);
		al_attach_sample_instance_to_mixer(data->riot_sound, game->audio.fx);
		al_set_sample_instance_playmode(data->riot_sound, ALLEGRO_PLAYMODE_ONCE);

		data->wuwu_sound = al_create_sample_instance(data->wuwu_sample);
		al_attach_sample_instance_to_mixer(data->wuwu_sound, game->audio.fx);
		al_set_sample_instance_playmode(data->wuwu_sound, ALLEGRO_PLAYMODE_ONCE);


		data->cursor = CreateCharacter(game, "cursor");
		RegisterSpritesheet(game, data->cursor, "hand");
		LoadSpritesheets(game, data->cursor);
		(*progress)(game);

		data->pixelator = al_create_bitmap(320, 180);
		al_set_target_bitmap(data->pixelator);
		al_clear_to_color(al_map_rgb(0, 0, 0));

		al_set_target_backbuffer(game->display);

		data->rocket_template = CreateCharacter(game, "rocket");
		RegisterSpritesheet(game, data->rocket_template, "rock");
		RegisterSpritesheet(game, data->rocket_template, "bottle");
		RegisterSpritesheet(game, data->rocket_template, "bottle2");
		RegisterSpritesheet(game, data->rocket_template, "atom");
		RegisterSpritesheet(game, data->rocket_template, "boom");
		RegisterSpritesheet(game, data->rocket_template, "blank");
		LoadSpritesheets(game, data->rocket_template);
		(*progress)(game);

		data->usa_flag = CreateCharacter(game, "kibols");
		RegisterSpritesheet(game, data->usa_flag, "legia");
		RegisterSpritesheet(game, data->usa_flag, "poland");
		LoadSpritesheets(game, data->usa_flag);

		data->ru_flag = CreateCharacter(game, "kibols");
		RegisterSpritesheet(game, data->ru_flag, "lech");
		RegisterSpritesheet(game, data->ru_flag, "poland");
		LoadSpritesheets(game, data->ru_flag);
		(*progress)(game);

		data->rainbow = CreateCharacter(game, "rainbow");
		RegisterSpritesheet(game, data->rainbow, "shine");
		RegisterSpritesheet(game, data->rainbow, "be");
		LoadSpritesheets(game, data->rainbow);

		data->riot = CreateCharacter(game, "riot");
		RegisterSpritesheet(game, data->riot, "riot");
		LoadSpritesheets(game, data->riot);
		(*progress)(game);

		data->euro = CreateCharacter(game, "euro");
		RegisterSpritesheet(game, data->euro, "euro");
		LoadSpritesheets(game, data->euro);

		return data;
}
Example #22
0
void Menu_Preload(struct Game *game, void (*progress)(struct Game*, float)) {
	PROGRESS_INIT(16);

	game->menu.options.fullscreen = game->fullscreen;
	game->menu.options.fps = game->fps;
	game->menu.options.width = game->width;
	game->menu.options.height = game->height;
	game->menu.loaded = true;
	game->menu.image = LoadScaledBitmap( "menu/menu.png", game->viewportWidth, game->viewportWidth*(1240.0/3910.0));
	PROGRESS;
	game->menu.mountain = LoadScaledBitmap( "menu/mountain.png", game->viewportHeight*1.6*0.055, game->viewportHeight/9 );
	PROGRESS;
	game->menu.cloud = LoadScaledBitmap( "menu/cloud.png", game->viewportHeight*1.6*0.5, game->viewportHeight*0.25 );
	PROGRESS;
	game->menu.cloud2 = LoadScaledBitmap( "menu/cloud2.png", game->viewportHeight*1.6*0.2, game->viewportHeight*0.1 );
	PROGRESS;
	game->menu.logo = LoadScaledBitmap( "menu/logo.png", game->viewportHeight*1.6*0.3, game->viewportHeight*0.35 );
	game->menu.blurbg = al_create_bitmap(game->viewportHeight*1.6*0.3, game->viewportHeight*0.35);
	game->menu.blurbg2 = al_create_bitmap(game->viewportHeight*1.6*0.3, game->viewportHeight*0.35);
	PROGRESS;
	game->menu.logoblur = al_create_bitmap(game->viewportHeight*1.6*0.3+4, game->viewportHeight*0.35+4);
	al_set_target_bitmap(game->menu.logoblur);
	al_clear_to_color(al_map_rgba(0,0,0,0));
	float alpha = (1.0/40.0);
	ALLEGRO_COLOR color = al_map_rgba_f(alpha, alpha, alpha, alpha);
	int by, bx;
	for (by = -2; by <= 2; by++) {
		for (bx = -2; bx <= 2; bx++) {
			if (sqrt(bx*bx+by*by) <= 2)
				al_draw_tinted_bitmap(game->menu.logo, color, bx, by, 0);
		}
	}
	al_set_target_bitmap(al_get_backbuffer(game->display));
	PROGRESS;
	game->menu.glass = LoadScaledBitmap( "menu/glass.png", game->viewportHeight*1.6*0.3, game->viewportHeight*0.35 );
	PROGRESS;
	//game->menu.pinkcloud = LoadScaledBitmap( "menu/pinkcloud.png", game->viewportWidth*0.33125, game->viewportHeight*0.8122);
	game->menu.pinkcloud = LoadScaledBitmap( "menu/pinkcloud.png", game->viewportHeight*0.8122*(1171.0/2218.0), game->viewportHeight*0.8122);
	PROGRESS;
	al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP);
	game->menu.rain = al_load_bitmap( GetDataFilePath("menu/rain.png") );
	PROGRESS;
	game->menu.pie = al_load_bitmap( GetDataFilePath("menu/pie.png") );
	al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR);
	PROGRESS;

	game->menu.sample = al_load_sample( GetDataFilePath("menu/menu.flac") );
	PROGRESS;
	game->menu.rain_sample = al_load_sample( GetDataFilePath("menu/rain.flac") );
	PROGRESS;
	game->menu.click_sample = al_load_sample( GetDataFilePath("menu/click.flac") );
	PROGRESS;
	game->menu.mountain_position = game->viewportWidth*0.7;

	game->menu.music = al_create_sample_instance(game->menu.sample);
	al_attach_sample_instance_to_mixer(game->menu.music, game->audio.music);
	al_set_sample_instance_playmode(game->menu.music, ALLEGRO_PLAYMODE_LOOP);

	game->menu.rain_sound = al_create_sample_instance(game->menu.rain_sample);
	al_attach_sample_instance_to_mixer(game->menu.rain_sound, game->audio.fx);
	al_set_sample_instance_playmode(game->menu.rain_sound, ALLEGRO_PLAYMODE_LOOP);

	game->menu.click = al_create_sample_instance(game->menu.click_sample);
	al_attach_sample_instance_to_mixer(game->menu.click, game->audio.fx);
	al_set_sample_instance_playmode(game->menu.click, ALLEGRO_PLAYMODE_ONCE);

	game->menu.font_title = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewportHeight*0.16,0 );
	game->menu.font_subtitle = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewportHeight*0.08,0 );
	game->menu.font = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewportHeight*0.05,0 );
	game->menu.font_selected = al_load_ttf_font(GetDataFilePath("fonts/ShadowsIntoLight.ttf"),game->viewportHeight*0.065,0 );
	PROGRESS;

	if (!game->menu.sample){
		fprintf(stderr, "Audio clip sample not loaded!\n" );
		exit(-1);
	}

	if (!game->menu.rain_sample){
		fprintf(stderr, "Audio clip sample#2 not loaded!\n" );
		exit(-1);
	}

	if (!game->menu.click_sample){
		fprintf(stderr, "Audio clip sample#3 not loaded!\n" );
		exit(-1);
	}

	game->menu.pinkcloud_bitmap = al_create_bitmap(game->viewportHeight*0.8122*(1171.0/2218.0), game->viewportHeight);

	game->menu.pie_bitmap = al_create_bitmap(game->viewportHeight*0.8, game->viewportHeight);
	al_set_target_bitmap(game->menu.pie_bitmap);
	al_clear_to_color(al_map_rgba(0,0,0,0));
	al_draw_scaled_bitmap(game->menu.pie, 0, 0, al_get_bitmap_width(game->menu.pie), al_get_bitmap_height(game->menu.pie), al_get_bitmap_width(game->menu.pie_bitmap)*0.5, 0, game->viewportHeight*1.6*0.11875, game->viewportHeight*0.0825, 0);
	al_draw_scaled_bitmap(game->menu.pie, 0, 0, al_get_bitmap_width(game->menu.pie), al_get_bitmap_height(game->menu.pie), al_get_bitmap_width(game->menu.pie_bitmap)*0.1, al_get_bitmap_height(game->menu.pie_bitmap)*0.3, game->viewportHeight*1.6*0.09, game->viewportHeight*0.06, ALLEGRO_FLIP_HORIZONTAL);
	al_draw_scaled_bitmap(game->menu.pie, 0, 0, al_get_bitmap_width(game->menu.pie), al_get_bitmap_height(game->menu.pie), al_get_bitmap_width(game->menu.pie_bitmap)*0.3, al_get_bitmap_height(game->menu.pie_bitmap)*0.6, game->viewportHeight*1.6*0.13, game->viewportHeight*0.1, 0);
	al_destroy_bitmap(game->menu.pie);
	PROGRESS;

	al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP);
	game->menu.rain_bitmap = al_create_bitmap(al_get_bitmap_width(game->menu.pinkcloud_bitmap)*0.5, al_get_bitmap_height(game->menu.pinkcloud_bitmap)*0.1);
	al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);
	al_set_target_bitmap(game->menu.rain_bitmap);
	al_clear_to_color(al_map_rgba(0,0,0,0));
	al_draw_scaled_bitmap(game->menu.rain,0, 0, al_get_bitmap_width(game->menu.rain), al_get_bitmap_height(game->menu.rain), 0, 0, al_get_bitmap_width(game->menu.rain_bitmap), al_get_bitmap_height(game->menu.rain_bitmap),0);
	al_destroy_bitmap(game->menu.rain);
	PROGRESS;
}
Example #23
0
/* this code is sick */
static void init_music()
{
   float vol, val;
   char *p;
   int i;

   if (!al_is_audio_installed())
      return;

   /* sine waves (one straight and one with oscillator sync) for the bass */
   sine = create_sample_u8(22050, 64);
   p = (char *)al_get_sample_data(sine);

   for (i=0; i<64; i++) {
      *p = 128 + (sin((float)i * M_PI / 32.0) + sin((float)i * M_PI / 12.0)) * 8.0;
      p++;
   }

   /* square wave for melody #1 */
   square = create_sample_u8(22050, 64);
   p = (char *)al_get_sample_data(square);

   for (i=0; i<64; i++) {
      *p = (i < 32) ? 120 : 136;
      p++;
   }

   /* saw wave for melody #2 */
   saw = create_sample_u8(22050, 64);
   p = (char *)al_get_sample_data(saw);

   for (i=0; i<64; i++) {
      *p = 120 + (i*4 & 255) / 16;
      p++;
   }

   /* bass drum */
   bd = create_sample_u8(22050, 1024);
   p = (char *)al_get_sample_data(bd);

   for (i=0; i<1024; i++) {
      vol = (float)(1024-i) / 16.0;
      *p = 128 + (sin((float)i / 48.0) + sin((float)i / 32.0)) * vol;
      p++;
   }

   /* snare drum */
   snare = create_sample_u8(22050, 3072);
   p = (char *)al_get_sample_data(snare);

   val = 0;

   for (i=0; i<3072; i++) {
      vol = (float)(3072-i) / 24.0;
      val = (val * 0.9) + (RAND * 0.1);
      *p = 128 + val * vol;
      p++;
   }

   /* hihat */
   hihat = create_sample_u8(22050, 1024);
   p = (char *)al_get_sample_data(hihat);

   for (i=0; i<1024; i++) {
      vol = (float)(1024-i) / 192.0;
      *p = 128 + (sin((float)i / 4.2) + RAND) * vol;
      p++;
   }

   /* start up the player */
   for (i=0; i<256; i++)
      freq_table[i] = (int)(350.0 * pow(2.0, (float)i/12.0));

   for (i=0; i<NUM_PARTS; i++) {
      part_pos[i] = part_ptr[i];
      part_time[i] = 0;
   }

   part_voice[0] = al_create_sample_instance(sine);
   part_voice[1] = al_create_sample_instance(square);
   part_voice[2] = al_create_sample_instance(saw);
   part_voice[3] = al_create_sample_instance(bd);

   al_attach_sample_instance_to_mixer(part_voice[0], al_get_default_mixer());
   al_attach_sample_instance_to_mixer(part_voice[1], al_get_default_mixer());
   al_attach_sample_instance_to_mixer(part_voice[2], al_get_default_mixer());
   al_attach_sample_instance_to_mixer(part_voice[3], al_get_default_mixer());

   al_set_sample_instance_playmode(part_voice[0], ALLEGRO_PLAYMODE_LOOP);
   al_set_sample_instance_playmode(part_voice[1], ALLEGRO_PLAYMODE_LOOP);
   al_set_sample_instance_playmode(part_voice[2], ALLEGRO_PLAYMODE_LOOP);
   al_set_sample_instance_playmode(part_voice[3], ALLEGRO_PLAYMODE_ONCE);

   al_set_sample_instance_gain(part_voice[0], 192/255.0);
   al_set_sample_instance_gain(part_voice[1], 192/255.0);
   al_set_sample_instance_gain(part_voice[2], 192/255.0);
   al_set_sample_instance_gain(part_voice[3], 255/255.0);

   al_set_sample_instance_pan(part_voice[0], PAN(128));
   al_set_sample_instance_pan(part_voice[1], PAN(224));
   al_set_sample_instance_pan(part_voice[2], PAN(32));
   al_set_sample_instance_pan(part_voice[3], PAN(128));

   music_timer = al_install_timer(ALLEGRO_BPS_TO_SECS(22));
}