コード例 #1
0
ファイル: soundManager.cpp プロジェクト: Taurous/asteroids
Music *SoundManager::loadMusic(const char *path)
{
	Music *music = new Music;
	music->sound = new Sound;

	music->vol = 0.0;
	music->instance = NULL;
	music->sound->sample = NULL;

	music->sound->sample = al_load_sample(path);

	if (music->sound->sample)
	{
		music->instance = NULL;
		music->instance = al_create_sample_instance(music->sound->sample);
		music->sound->path = path;
		
		if (music->instance)
		{
			music->sound->id = cur_id;
			cur_id++;
			al_attach_sample_instance_to_mixer(music->instance, al_get_default_mixer());
			streams[music->sound->id] = music;
			return music;
		}
		else printf("Unable to create instance of path: %s\n", path);
	}
	else printf("Unable to load sample at path: %s\n", path);

	delete music->sound;
	delete music;
	return NULL;
}
コード例 #2
0
/* ----------------------------------------------------------------------------
 * Creates a structure with sample info.
 */
sample_struct::sample_struct(ALLEGRO_SAMPLE* s, ALLEGRO_MIXER* mixer) :
    sample(s),
    instance(NULL) {
    
    if(!s) return;
    instance = al_create_sample_instance(s);
    al_attach_sample_instance_to_mixer(instance, mixer);
}
コード例 #3
0
ファイル: dosowisko.c プロジェクト: dos1/RadioEdit
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;
}
コード例 #4
0
ファイル: Audio.cpp プロジェクト: thuskey/UltimateTreta
void playBGM(ALLEGRO_SAMPLE *sample)
{
    if (!sample)
        return;
    bgmInstance = al_create_sample_instance(sample);
    if (!bgmInstance)
        return;
    al_attach_sample_instance_to_mixer(bgmInstance, al_get_default_mixer());
    al_play_sample_instance(bgmInstance);
}
コード例 #5
0
ファイル: audio.cpp プロジェクト: douglett/hexa
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);
}
コード例 #6
0
ファイル: Sound.cpp プロジェクト: PUT-PTM/StarHunter
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.");
}
コード例 #7
0
ファイル: menu.c プロジェクト: dos1/TickleMonster
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;
}
コード例 #8
0
ファイル: audio.c プロジェクト: 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");
}
コード例 #9
0
ファイル: kcm_sample.c プロジェクト: tsteinholz/SR-Gaming
/* Function: al_reserve_samples
 */
bool al_reserve_samples(int reserve_samples)
{
   int i;
   int current_samples_count = (int) _al_vector_size(&auto_samples);

   ASSERT(reserve_samples >= 0);

   /* If no default mixer has been set by the user, then create a voice
    * and a mixer, and set them to be the default one for use with
    * al_play_sample().
    */
   if (default_mixer == NULL) {
      if (!al_restore_default_mixer())
         goto Error;
   }

   if (current_samples_count < reserve_samples) {
      /* We need to reserve more samples than currently are reserved. */
      for (i = 0; i < reserve_samples - current_samples_count; i++) {
         ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_alloc_back(&auto_samples);
         int *id = _al_vector_alloc_back(&auto_sample_ids);
         *id = 0;
         *slot = al_create_sample_instance(NULL);
         if (!*slot) {
            ALLEGRO_ERROR("al_create_sample failed\n");
            goto Error;
         }
         if (!al_attach_sample_instance_to_mixer(*slot, default_mixer)) {
            ALLEGRO_ERROR("al_attach_mixer_to_sample failed\n");
            goto Error;
         }
      }
   }
   else if (current_samples_count > reserve_samples) {
      /* We need to reserve fewer samples than currently are reserved. */
      while (current_samples_count-- > reserve_samples) {
         _al_vector_delete_at(&auto_samples, current_samples_count);
         _al_vector_delete_at(&auto_sample_ids, current_samples_count);
      }
   }

   return true;

 Error:
   free_sample_vector();
   
   return false;
}
コード例 #10
0
ファイル: theend.c プロジェクト: dos1/mediator
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;
}
コード例 #11
0
ファイル: Audio.cpp プロジェクト: thuskey/UltimateTreta
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);
}
コード例 #12
0
bool AllegroSoundSample5::Load( const std::string & path )
{
	if ( m_pInstance )
	{
		al_detach_sample_instance( m_pInstance );
		al_destroy_sample_instance( m_pInstance );
	}

	SamplePtr pSampleData = GetSamples().Load( path );
	if ( !pSampleData || !pSampleData->m_pSample )
		return false;

	m_pInstance = al_create_sample_instance( pSampleData->m_pSample );
	al_attach_sample_instance_to_mixer( m_pInstance,  ((AllegroSound5&)GetSound()).GetSoundMixer() );

	return ( m_pInstance != 0 );
}
コード例 #13
0
ファイル: SampleResource.cpp プロジェクト: sesc4mt/mvcdecoder
bool SampleResource::load(void)
{
   sample_data = al_load_sample(filename.c_str());
   if (!sample_data) {
      debug_message("Error loading sample %s\n", filename.c_str());
      return false;
   }

   sample = al_create_sample_instance(sample_data);
   if (!sample) {
       debug_message("Error creating sample\n");
       al_destroy_sample(sample_data);
       sample_data = 0;
       return false;
   }

   return true;
}
コード例 #14
0
static void t3f_play_queued_sample(void)
{
	int i;
	
	if(t3f_sample_queue[0])
	{
		t3f_queue_sample_instance = al_create_sample_instance(t3f_sample_queue[0]);
		al_set_sample_instance_gain(t3f_queue_sample_instance, t3f_sound_volume);
		al_set_sample_instance_speed(t3f_queue_sample_instance, 1.0);
		al_set_sample_instance_pan(t3f_queue_sample_instance, 0.0);
		al_play_sample_instance(t3f_queue_sample_instance);
		for(i = 0; i < t3f_queued_samples - 1; i++)
		{
			t3f_sample_queue[i] = t3f_sample_queue[i + 1];
		}
		t3f_sample_queue[i] = NULL;
		t3f_queued_samples--;
	}
}
コード例 #15
0
ファイル: Audio.cpp プロジェクト: mThorhauge/StudyRush
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());
};
コード例 #16
0
ファイル: kcm_sample.c プロジェクト: tsteinholz/SR-Gaming
/* Function: al_set_default_mixer
 */
bool al_set_default_mixer(ALLEGRO_MIXER *mixer)
{
   ASSERT(mixer != NULL);

   if (mixer != default_mixer) {
      int i;

      default_mixer = mixer;

      /* Destroy all current sample instances, recreate them, and
       * attach them to the new mixer */
      for (i = 0; i < (int) _al_vector_size(&auto_samples); i++) {
         ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&auto_samples, i);
         int *id = _al_vector_ref(&auto_sample_ids, i);

         *id = 0;
         al_destroy_sample_instance(*slot);

         *slot = al_create_sample_instance(NULL);
         if (!*slot) {
            ALLEGRO_ERROR("al_create_sample failed\n");
            goto Error;
         }
         if (!al_attach_sample_instance_to_mixer(*slot, default_mixer)) {
            ALLEGRO_ERROR("al_attach_mixer_to_sample failed\n");
            goto Error;
         }
      }      
   }

   return true;

Error:
   free_sample_vector();
   default_mixer = NULL;   
   return false;
}
コード例 #17
0
ファイル: sound.c プロジェクト: sesc4mt/mvcdecoder
/* 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));
}
コード例 #18
0
ファイル: menu.c プロジェクト: tknowak/SuperDerpy
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;
}
コード例 #19
0
ファイル: ex_mixer_chain.c プロジェクト: ufaith/d2imdev
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;
}
コード例 #20
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;
}
コード例 #21
0
ファイル: ex_acodec.c プロジェクト: gitustc/d2imdev
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;
}
コード例 #22
0
ファイル: main.cpp プロジェクト: kruci/Juicy-Plebs
int main(int argc, char *argv[])
{
    bool windowed = false;
    float rescale = 1.0f;
    std::string arg;
    if(argc > 1)
    {
        arg = argv[1];
        if(arg == "--help")
        {
            std::cout << "-s {scale} for running in windowed mode and scaled (standart size is 1440*810)" << std::endl;
            return 0;
        }
        else if(arg == "-s")
        {
            if( argc > 2)
            {
                arg = argv[2];
                rescale = atof(argv[2]);
            }
            windowed = true;
        }
    }


    /**initialize allegro*/
    if(!al_init()){error_message("al_init()");return 33;}
    if(!al_init_primitives_addon()){error_message("al_init_primitives_addon()");return 33;}
    //if(!al_install_keyboard()){error_message("al_install_keyboard()");return 33;} //no use for keyboard in this game
    if(!al_install_mouse()){error_message("al_install_mouse()");return 33;}
    if(!al_init_image_addon()){error_message("al_init_image_addon()");return 33;}
    al_init_font_addon(); // returns void
    if(!al_init_ttf_addon()){error_message("al_init_ttf_addon()");return 33;}
    //audio
    if(al_install_audio() == true)
    {
        if(al_init_acodec_addon() == true){}
        else
        {
            error_message("al_init_acodec_addon() - cant initialize audio codec");
            global::audio = false;
            global::sound_card = false;
        }
    }
    else
    {
        error_message("al_install_audio() - cant found sound device");
        global::audio = false;
        global::sound_card = false;
    }

    /**Some allegro variables*/
    ALLEGRO_DISPLAY *display = nullptr;
    ALLEGRO_EVENT_QUEUE *event_queue = nullptr;
    ALLEGRO_TIMER *timer = nullptr;
    ALLEGRO_BITMAP *logo = nullptr;

    /**Display preparation*/
    bool supported_ratio = true;
    ALLEGRO_MONITOR_INFO mon_info;
    al_get_monitor_info(0, &mon_info);
    global::sHeight = mon_info.y2 - mon_info.y1; //gets monitor size in pixels
    global::sWidth = mon_info.x2 - mon_info.x1;
    global::aspectratio = round( ((float)global::sWidth / (float)global::sHeight) * 100.0f) / 100.0f; //gets aspectratio
    if(global::aspectratio == 1.78f){global::xratio = 16; global::yratio = 9;}      // 16:9 screen ration
    else if(global::aspectratio == 1.6f){global::xratio = 16; global::yratio = 10;} // 16:10
    else if(global::aspectratio == 1.33f){global::xratio = 4; global::yratio = 3;}  // 4:3
    else{supported_ratio = false;}
    global::dHeight = global::dWidth  / global::xratio * global::yratio;
    global::xscale = (float)global::sWidth / (float)global::dWidth;
    global::yscale = (float)global::sHeight / (float)global::dHeight;

    /**display creation*/
    al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR); // Thanks to this, magnified fonts dont look retarted, and game is fast (hopefully) :D
    if(windowed == true || supported_ratio == false)
    {
        supported_ratio = true;
        al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL);
        global::xscale = rescale;
        global::yscale = rescale;
        global::dWidth = 1440;
        global::dHeight = 810;
        display = al_create_display(global::dWidth*rescale, global::dHeight*rescale);
    }
    else
    {
        al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_OPENGL);
        display = al_create_display(global::dWidth, global::dHeight);
    }
    if(display == nullptr){error_message("al_create_display()"); return 1;}
    al_set_window_title(display, "Este neviem meno, ale asi neco so zemiakom");

    /**logo*/
    logo = al_load_bitmap("resources/graphics/logo.png");
    if(logo == nullptr){error_message("resources/graphics/logo.png not found");}
    else{ al_set_display_icon(display, logo);}


    /**Transformation*/
    al_identity_transform(&global::trans);
    if(supported_ratio == true)
    {
        al_scale_transform(&global::trans, global::xscale, global::yscale);
    }
    else
    {
        error_message("Unsupported monitor type - upgrade you monitor pls");
        float scale_backup_plan = (global::xscale > global::yscale ? global::yscale : global::xscale);
        global::xscale = scale_backup_plan;
        global::yscale = scale_backup_plan;
        al_scale_transform(&global::trans, global::xscale, global::yscale);
    }
    al_use_transform(&global::trans);

    /**timer*/
    timer = al_create_timer(1.0f/global::FPS);
    if(timer == nullptr){error_message("al_create_timer()"); return 44;}
    bool redraw = true;

    /**even que*/
    event_queue = al_create_event_queue();
    if(event_queue == nullptr){error_message("al_create_event_queue()"); return 44;}

    /**registering event sources*/
    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_register_event_source(event_queue, al_get_mouse_event_source());

    al_start_timer(timer);

    rguil::mouse_state = &global::mouse_state;

    global::audio_player = new AudioHandler(10);

    #ifdef _SOUND_TEST
    ALLEGRO_SAMPLE *s = al_load_sample("resources/music/Fuck_This_Shit_Im_Out.wav");
    ALLEGRO_SAMPLE_INSTANCE *si = al_create_sample_instance(s);
    global::audio_player->global_sounds.push_back(si);
    global::audio_player->Play_sample_instance(&si, ALLEGRO_PLAYMODE_LOOP);
    #endif // _SOUND_TEST

    #ifdef _FPS
    ALLEGRO_FONT *fps_font = nullptr;
    fps_font = al_load_font("resources/fonts/Asimov.otf", 12,0);
    int counter = 0;
    time_t tsttme2 = time(nullptr), tsttme = time(nullptr);
    int fps = 0;
    #endif // FPS

    global::save = new GameSave();
    ScreenMain *SCMain = new ScreenMain();
    global::audio_b = new Button("resources/fonts/Calibri.ttf", 1240, global::dHeight -65, 1240 + 40, global::dHeight - 25,
                                 "", al_map_rgba(0,0,0,0),
                                 ( global::audio == true ? MusicON : MusicOFF));

    /**Main loop*/ //forced 30 FPS, drawing and computing in same thread
    while(global::loop == true)
    {
        ALLEGRO_EVENT ev;
        al_wait_for_event(event_queue, &ev);

        al_get_mouse_state(&global::mouse_state);

        if(ev.type == ALLEGRO_EVENT_TIMER)
        {
            redraw = true;
        }
        else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
        {
            break;
        }

        /**Take event input here*/
        if(global::audio_b->Input(ev, global::xscale, global::yscale) == 2)
        {
            global::audio_b->unclick();
            global::audio = (global::audio == true ? false : true);

            if(global::audio == true)
            {
                al_destroy_bitmap(global::audio_b->bmp);
                global::audio_b->bmp = al_load_bitmap(MusicON);
                global::audio_player->Mute_sample_instances(false);
            }
            else
            {
                al_destroy_bitmap(global::audio_b->bmp);
                global::audio_b->bmp = al_load_bitmap(MusicOFF);
                global::audio_player->Mute_sample_instances(true);
            }
        }

        SCMain->Input(ev, global::xscale, global::yscale);
        /**---------------------*/

        #ifdef _FPS
        tsttme2 = time(&tsttme2);
        if(difftime(tsttme2,tsttme) >= 1.0f)
        {
            tsttme = time(&tsttme);
            fps = counter;
            counter = 0;
        }
        #endif // FPS


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


            /**Draw and compute here*/
            SCMain->Print();
            global::audio_b->Print();
            /**---------------------*/

            #ifdef _FPS
            counter++;
            al_draw_text(fps_font, al_map_rgb(255,0,0), 0.0f,0.0f, 0, std::to_string(fps).c_str());
            #endif // FPS
            al_flip_display();
        }
    }
    #ifdef _SOUND_TEST
    global::audio_player->Stop_sample_instances();
    global::audio_player->global_sounds.erase(global::audio_player->global_sounds.begin());
    al_destroy_sample_instance(si);
    al_destroy_sample(s);
    #endif // _SOUND_TEST

    delete global::audio_b;
    delete SCMain;
    delete global::save;
    delete global::audio_player;

    al_destroy_timer(timer);
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);
    if(logo != nullptr)
        al_destroy_bitmap(logo);
    #ifdef _FPS
    al_destroy_font(fps_font);
    #endif // FPS

    return 0;
}
コード例 #23
0
ファイル: Main.cpp プロジェクト: kckrepo/kck-repo
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;
}
コード例 #24
0
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;
}
コード例 #25
0
ファイル: ScreenMain.cpp プロジェクト: kruci/Juicy-Plebs
ScreenMain::ScreenMain()
{
    //background = al_load_bitmap("resources/graphics/zemiak.png");
    zemak_bitmap = al_load_bitmap("resources/graphics/zemiacik.png");
    if(zemak_bitmap == nullptr)
    {
        error_message("Could not load image : resources/graphics/zemiacik.png");
    }
    zemak_button = new Button(100, (global::dHeight - zemiak_size)/2,
                              100 + zemiak_size,
                              (global::dHeight - zemiak_size)/2 + zemiak_size);

    std::string bnames[3] = {"Exit", "Play", "About"};

                        //x1, y1, width, height
    /*int bpoz[4][4] = {  {1300, global::dHeight -70, 120, 50},
                        {((float)global::dWidth-250.0f)/2.0f, ((float)global::dHeight-80.0f)/2.0f, 250, 80},
                        {global::dWidth - 1300 -120,global::dHeight -70,120,50},
                        {((float)global::dWidth-120.0f)/2.0f, global::dHeight-70, 120, 50}  };*/

    int b_y = (float)global::dHeight / 9.0f;

    int bpoz[3][4] = {  {1300, global::dHeight -70, 120, 50},
                        {((float)global::dWidth-220.0f), b_y*1, 200, 65},
                        {global::dWidth -200, b_y*2, 180, 58}   };

    for(int a = 0;a < 3;a++)
    {
        buttons.push_back(new Button("resources/fonts/Calibri.ttf", bpoz[a][0], bpoz[a][1], bpoz[a][0] + bpoz[a][2],bpoz[a][1] + bpoz[a][3], bnames[a], al_map_rgb(0,0,128)));
    }

    hlasky_font = al_load_ttf_font("resources/fonts/Andada-Italic.otf", 30, 0);
    if(hlasky_font == nullptr) error_message("Could not load font : resources/fonts/Andada-Italic.otf");

    intro_music = al_load_sample(INTRO_SUND_FILE);
    if(intro_music == nullptr)
    {
        std::string dum = INTRO_SUND_FILE;
        error_message("Could not load file: " + dum);
    }
    intro_music_instance = al_create_sample_instance(intro_music);
    if(intro_music_instance == nullptr)
    {
        std::string dum = INTRO_SUND_FILE;
        error_message("Could not create sample instance: " + dum);
    }
    global::audio_player->Play_sample_instance(&intro_music_instance, 0.8,ALLEGRO_PLAYMODE_LOOP);

    /*std::string dm = "resources/music/mms/";
    std::string soundfiles = "";

    for(int a = 1;a <= NUMBER_OF_HLASKY;a++)
    {
        hlasky.push_back(new AudioHandler::sound_effect);

        soundfiles = dm + std::to_string(a) + ".ogg";

        hlasky[hlasky.size()-1]->sample = al_load_sample(soundfiles.c_str());
        if(hlasky[hlasky.size()-1]->sample == nullptr)
        {
            error_message("Could not load file: " + soundfiles);
        }
        hlasky[hlasky.size()-1]->instance = al_create_sample_instance(hlasky[hlasky.size()-1]->sample);
        if(hlasky[hlasky.size()-1]->instance == nullptr)
        {
            error_message("Could not create sample instance: " + soundfiles);
        }
    }*/

    generator.seed(std::chrono::system_clock::now().time_since_epoch().count());
}
コード例 #26
0
ファイル: audio.c プロジェクト: SaiSrini/Shooter
ALLEGRO_SAMPLE* shot1=NULL;
ALLEGRO_SAMPLE* shot2=NULL;
ALLEGRO_SAMPLE* boom=NULL;
ALLEGRO_SAMPLE* song=NULL;
ALLEGRO_SAMPLE_INSTANCE* songInstance=NULL;

al_install_audio();
al_init_acodec_addon();

al_reserve_samples(10);
shot1=al_load_sample("shot1.ogg");
shot2=al_load_sample("shot2.ogg");
boom=al_load_sample("boom.ogg");
song=al_load_sample("song.ogg");

songInstance=al_create_sample_instance(song);
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);
al_stop_sample_instance(songInstance);

al_play_sample(shot1, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, 0);


al_destroy_sample(shot1);
al_destroy_sample(shot2); 
al_destroy_sample(boom);
al_destroy_sample(song);
al_destroy_sample_instance(songInstance);
コード例 #27
0
ファイル: main.c プロジェクト: fspacheco/ProjetoC
int main()
{
    bool quit = false;
    int gamestate = 0;
    srand(time(NULL));

    int change_bkg = 0;
    int stopwatch = 120;
    bool iddle = false;

    int bulletID=0;
    int bulletCount=0;

    ALLEGRO_DISPLAY*            display;
    ALLEGRO_TIMER*              timer_0p2;
    ALLEGRO_TIMER*              timer_1;
    ALLEGRO_TIMER*              timer_60;
    ALLEGRO_EVENT_QUEUE*        event_queue;
    ALLEGRO_EVENT               ev;

    ALLEGRO_BITMAP*             img_home_screen;
    ALLEGRO_BITMAP*             img_dica_h1n1;
    ALLEGRO_BITMAP*             img_background0;
    ALLEGRO_BITMAP*             img_game_over;
    ALLEGRO_BITMAP*             img_you_win;

    ALLEGRO_BITMAP*             img_heart;
    ALLEGRO_BITMAP*             img_medal;
    ALLEGRO_BITMAP*             img_clock;
    ALLEGRO_BITMAP*             img_block1;
    ALLEGRO_BITMAP*             img_block2;

    ALLEGRO_BITMAP*             img_player_walking;
    ALLEGRO_BITMAP*             img_player_walking_shoot;
    ALLEGRO_BITMAP*             img_player_immobile;
    ALLEGRO_BITMAP*             img_player_immobile_shoot;
    ALLEGRO_BITMAP*             img_player_jump;
    ALLEGRO_BITMAP*             img_player_jump_shoot;
    ALLEGRO_BITMAP*             img_player_bullet;

    ALLEGRO_BITMAP*             img_enemy1;
    ALLEGRO_BITMAP*             img_boss1;
    ALLEGRO_BITMAP*             img_enemy_bullet;

    ALLEGRO_SAMPLE*             spl_theme;
    ALLEGRO_SAMPLE*             spl_playerShoot;
    ALLEGRO_SAMPLE*             spl_mlk;

    ALLEGRO_SAMPLE_INSTANCE*    instance_theme;
    ALLEGRO_SAMPLE_INSTANCE*    instance_playerShoot;
    ALLEGRO_SAMPLE_INSTANCE*    instance_mlk;

    ALLEGRO_FONT*           fonte16;

    /* Estruturas */
    s_object player;
    s_object block[LINHA_MAX][COLUNA_MAX];
    s_object enemy1[LINHA_MAX][COLUNA_MAX];

    for (i=0; i<LINHA_MAX; i++)
    {
        for(j=0; j<COLUNA_MAX; j++)
        {
            /* Cria o player */
            if(mapa[i][j] == 1)
            {
                player.y = i*16 - 24;
                player.x = j*64 + 24;
                player.speed = 3;
                player.direction = 1;

                player.live = true;
                player.life = 100;

                block[i][j].live = false;
            }
            /* Cria os Blocos */
            if(mapa[i][j] == 2)
            {
                block[i][j].y = i*16;
                block[i][j].x = j*64;
                block[i][j].live = true;
            }
            if(mapa[i][j] == 3)
            {
                block[i][j].y = i*16;
                block[i][j].x = j*64;
                block[i][j].live = true;
            }
            if(mapa[i][j] == 4)
            {
                block[i][j].y = i*16;
                block[i][j].x = j*64;
                block[i][j].live = true;
            }
            if(mapa[i][j] == 5)
            {
                block[i][j].y = i*16;
                block[i][j].x = j*64;
                block[i][j].live = false;
            }
            /* Cria os Inimigos */
            if(mapa[i][j] == 6)
            {
                enemy1[i][j].y = i*16 - 24;
                enemy1[i][j].x = j*64;
                enemy1[i][j].speed = 2;
                enemy1[i][j].direction = -1;

                enemy1[i][j].life = 3;
                enemy1[i][j].live = true;

                block[i][j].live = false;
            }
            if(mapa[i][j] == 7 || mapa[i][j] == 8)
            {
                enemy1[i][j].y = i*16 - 24;
                enemy1[i][j].x = j*64;
                enemy1[i][j].speed = 2;
                enemy1[i][j].direction = 1;

                enemy1[i][j].life = 3;
                enemy1[i][j].live = false;

                block[i][j].live = false;
            }
            if(mapa[i][j] == 9)
            {
                enemy1[i][j].y = i*16 - 24;
                enemy1[i][j].x = j*64;
                enemy1[i][j].speed = 2;
                enemy1[i][j].direction = -1;

                enemy1[i][j].life = 25;
                enemy1[i][j].live = false;

                block[i][j].live = false;
            }
        }
    }

    s_bullet playerBullet[NUM_BULLET];
    s_bullet enemyBullet[NUM_BULLET];

    for(i=0; i<NUM_BULLET; i++)
    {
        playerBullet[i].x = 0;
        playerBullet[i].y = 0;
        playerBullet[i].speed = 5;
        playerBullet[i].direction = 1;
        playerBullet[i].live = false;

        enemyBullet[i].x = 0;
        enemyBullet[i].y = 0;
        enemyBullet[i].speed = 5;
        enemyBullet[i].direction = 0;
        enemyBullet[i].live = false;
    }
    s_animation walking;
    walking.maxFrame = 8;
    walking.frameDelay = 5;
    walking.frameCount = 0;
    walking.curFrame = 0;
    walking.frameHeight = 40;
    walking.frameWidth = 40;

    s_animation jumping;
    jumping.maxFrame = 7;
    jumping.frameDelay = 5;
    jumping.frameCount = 0;
    jumping.curFrame = 0;
    jumping.frameHeight = 52;
    jumping.frameWidth = 40;

    s_animation immobile;
    immobile.maxFrame = 7;
    immobile.frameDelay = 15;
    immobile.frameCount = 0;
    immobile.curFrame = 0;
    immobile.frameHeight = 40;
    immobile.frameWidth = 40;

    s_animation anim_enemy1;
    anim_enemy1.maxFrame = 3;
    anim_enemy1.frameDelay = 15;
    anim_enemy1.frameCount = 0;
    anim_enemy1.curFrame = 0;
    anim_enemy1.frameHeight = 40;
    anim_enemy1.frameWidth = 40;

    /* Faz com que as teclas comecem em false */
    for(i=0; i<KEY_MAX; i++)
    {
        keys[i] = false;
    }

    /* Carrega as configuracoes (teclado, audio, etc) */
    al_init();
    al_install_keyboard();
    al_init_image_addon();
    al_install_audio();
    al_init_acodec_addon();
    al_init_font_addon();
    al_init_ttf_addon();


    /* Erros ao criar algo */
    display = al_create_display(SCREEN_W, SCREEN_H);
    if(!display)
    {
        printf("Erro ao criar o display");
        exit(-1);
    }

    timer_0p2 = al_create_timer(5.0);
    timer_1 = al_create_timer(1.0);
    timer_60 = al_create_timer(1/60.0);

    if(!timer_0p2)
    {
        printf("Erro ao criar o timer de 0.2 FPS");
        exit(-1);
    }
    if(!timer_1)
    {
        printf("Erro ao criar o timer de 1 FPS");
        exit(-1);
    }
    if(!timer_60)
    {
        printf("Erro ao criar o timer de 60 FPS");
        exit(-1);
    }

    event_queue = al_create_event_queue();
    if(!event_queue)
    {
        printf("Erro ao criar o event_queue");
        exit(-1);
    }

    /* Carregando as Imagens */
    img_home_screen = al_load_bitmap("Sprites/Background/home_screen.png");
    img_dica_h1n1 = al_load_bitmap("Sprites/Background/dica_h1n1.png");
    img_background0 = al_load_bitmap("Sprites/Background/background_h1n1.png");
    img_you_win = al_load_bitmap("Sprites/Background/you_win.png");
    img_game_over = al_load_bitmap("Sprites/Background/game_over.png");

    img_heart = al_load_bitmap("Sprites/heart.png");
    img_medal = al_load_bitmap("Sprites/medal.png");
    img_clock = al_load_bitmap("Sprites/clock.png");
    img_block1 = al_load_bitmap("Sprites/block1.png");
    img_block2 = al_load_bitmap("Sprites/block2.png");

    img_player_walking = al_load_bitmap("Sprites/Player/player_walking.png");
    img_player_walking_shoot = al_load_bitmap("Sprites/Player/player_walking_shoot.png");
    img_player_immobile = al_load_bitmap("Sprites/Player/player_immobile.png");
    img_player_immobile_shoot = al_load_bitmap("Sprites/Player/player_immobile_shoot.png");
    img_player_jump = al_load_bitmap("Sprites/Player/player_jump.png");
    img_player_jump_shoot = al_load_bitmap("Sprites/Player/player_jump_shoot.png");
    img_player_bullet = al_load_bitmap("Sprites/Player/player_bullet.png");

    img_enemy1 = al_load_bitmap("Sprites/Enemies/enemy_h1n1.png");
    img_boss1 = al_load_bitmap("Sprites/Enemies/boss_h1n1.png");
    img_enemy_bullet = al_load_bitmap("Sprites/Enemies/enemy_bullet.png");

    /* Carregando os Samples */
    al_reserve_samples(10);
    spl_theme = al_load_sample("Sounds/theme.wav");
    spl_playerShoot = al_load_sample("Sounds/shoot.wav");
    spl_mlk = al_load_sample("Sounds/mlk.wav");

    instance_theme = al_create_sample_instance(spl_theme);
    instance_playerShoot = al_create_sample_instance(spl_playerShoot);
    instance_mlk = al_create_sample_instance(spl_mlk);

    al_set_sample_instance_gain(instance_playerShoot, 0.5);

    al_attach_sample_instance_to_mixer(instance_theme, al_get_default_mixer());
    al_attach_sample_instance_to_mixer(instance_playerShoot, al_get_default_mixer());
    al_attach_sample_instance_to_mixer(instance_mlk, al_get_default_mixer());

    /* Registra os Eventos */
    al_register_event_source(event_queue, al_get_display_event_source(display));
    al_register_event_source(event_queue, al_get_timer_event_source(timer_0p2));
    al_register_event_source(event_queue, al_get_timer_event_source(timer_1));
    al_register_event_source(event_queue, al_get_timer_event_source(timer_60));
    al_register_event_source(event_queue, al_get_keyboard_event_source());

    /* Carregando os timers */
    al_start_timer(timer_0p2);
    al_start_timer(timer_1);
    al_start_timer(timer_60);

    /* Carregando a fonte */
    fonte16 = al_load_ttf_font("Joystix.TTF", 16, 0);
    if (!fonte16) {
        printf("Erro ao carregar Joystix.TTF\n");
        exit(1);
    }

    while(!quit)
    {
        switch(gamestate)
        {
        case 0:
            al_wait_for_event(event_queue, &ev);

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) /* Faz com que o jogo feche ao clicar no botao "X" do display */
            {
                quit = true;
            }

            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) /* Faz com que o jogo feche ao pressionar esc */
                {
                    quit = true;
                }
                if(ev.keyboard.keycode == ALLEGRO_KEY_SPACE) /* Faz com que o jogo inicie ao pressionar space */
                {
                    change_bkg++;
                    if(change_bkg >= 2)
                    {
                        gamestate = 1;
                    }
                }
            }
            if(ev.type == ALLEGRO_EVENT_TIMER) /* Mostrar mensagens na tela */
            {
                if(ev.timer.source == timer_60)
                {
                    al_clear_to_color(al_map_rgb(0,0,0));
                    if(change_bkg == 0)
                    {
                        al_draw_bitmap(img_home_screen, 0, 0, 0);
                    }
                    if(change_bkg == 1)
                    {
                        al_draw_bitmap(img_dica_h1n1, 0, 0, 0);

                        if(++anim_enemy1.frameCount >= anim_enemy1.frameDelay)
                        {
                            if(++anim_enemy1.curFrame >= anim_enemy1.maxFrame)
                            {
                                anim_enemy1.curFrame = 0;
                            }
                            anim_enemy1.frameCount = 0;
                        }

                        al_draw_bitmap_region(img_enemy1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, 330, 320, 0);
                        al_draw_bitmap_region(img_boss1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, 450, 320, 0);
                    }
                    al_flip_display();
                }
            }
            break;
        case 1:

            if(force>= -7.5)
            {
                force-=0.5; /* Queda */
            }

            /* Toca a música de fundo */
            if(!al_get_sample_instance_playing(instance_theme) && !al_get_sample_instance_playing(instance_mlk))
            {
                al_play_sample_instance(instance_theme);
            }

            /* Fechar o display */
            al_wait_for_event(event_queue, &ev);

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
            {
                quit = true;
            }

            /* Evento de quando a tecla eh pressionada */
            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                switch(ev.keyboard.keycode)
                {
                case ALLEGRO_KEY_ESCAPE:
                    quit = true;
                    break;
                case ALLEGRO_KEY_SPACE:
                    keys[KEY_SPACE]=true;
                    break;
                case ALLEGRO_KEY_UP:
                    keys[KEY_UP] = true;
                    if(jump == false)
                    {
                        jump = true;
                        force = gravity;
                    }
                    break;
                case ALLEGRO_KEY_LEFT:
                    keys[KEY_RIGHT]=false;
                    keys[KEY_LEFT]=true;
                    break;
                case ALLEGRO_KEY_RIGHT:
                    keys[KEY_LEFT]=false;
                    keys[KEY_RIGHT]=true;
                    break;
                case ALLEGRO_KEY_F1:
                    if(!al_get_sample_instance_playing(instance_mlk))
                    {
                        al_play_sample_instance(instance_mlk);
                        al_stop_sample_instance(instance_theme);
                    }
                    break;
                case ALLEGRO_KEY_M:
                    if(al_get_sample_instance_playing(instance_mlk))
                    {
                        al_stop_sample_instance(instance_mlk);
                    }
                    break;
                }
            }

            /* Evento de quando a tecla eh solta */
            if(ev.type == ALLEGRO_EVENT_KEY_UP)
            {
                switch(ev.keyboard.keycode)
                {
                case ALLEGRO_KEY_SPACE:
                    keys[KEY_SPACE]=false;
                    break;
                case ALLEGRO_KEY_UP:
                    keys[KEY_UP] = false;
                    break;
                case ALLEGRO_KEY_LEFT:
                    keys[KEY_LEFT]=false;
                    break;
                case ALLEGRO_KEY_RIGHT:
                    keys[KEY_RIGHT]=false;
                    break;
                }
            }

            if(ev.type == ALLEGRO_EVENT_TIMER)
            {
                if(ev.timer.source == timer_0p2)
                {
                    if((iddle == false) && (keys[KEY_RIGHT] == false) && (keys[KEY_LEFT] == false) && (jump == false))
                    {
                        iddle = true;
                    }
                }
                if(ev.timer.source == timer_1)
                {
                    stopwatch--;
                }
                if(ev.timer.source == timer_60)
                {
                    /* Posicionamento do player*/
                    player.y-=force;

                    if(keys[KEY_RIGHT])
                    {
                        player.direction = 1;
                        player.x+=player.speed;
                    }
                    if(keys[KEY_LEFT])
                    {
                        player.direction = -1;
                        player.x-=player.speed;
                    }

                    if(keys[KEY_SPACE])
                    {
                        for(i=0; i<NUM_BULLET; i++)
                        {
                            if(!al_get_sample_instance_playing(instance_playerShoot))
                            {
                                playerShoot(&player, &playerBullet[i], instance_playerShoot);
                            }
                        }
                    }


                    /*Posicionamento do Inimigo */

                    for (i=0; i<LINHA_MAX; i++)
                    {
                        for(j=0; j<COLUNA_MAX; j++)
                        {
                            if(player.x > enemy1[i][j].x + 40)
                            {
                                enemy1[i][j].direction = 1;
                            }
                            else if(player.x + 40 <= enemy1[i][j].x)
                            {
                                enemy1[i][j].direction = -1;
                            }
                        }
                    }

                    /* ~~Posicionamento do projetil~~ */

                    /* Chance do Inimigo Atirar */
                    chance_enemy_shoot = rand() % 40;
                    for (i=0; i<LINHA_MAX; i++)
                    {
                        for(j=0; j<COLUNA_MAX; j++)
                        {
                            enemyShoot(&player, &enemy1[i][j], enemyBullet, &bulletID, &bulletCount);
                        }
                    }
                    for(i=0; i<NUM_BULLET; i++)
                    {
                        if(!playerBullet[i].live)
                        {
                            playerBullet[i].direction = player.direction;
                        }

                        if(playerBullet[i].live)
                        {
                            if(playerBullet[i].direction == -1)
                            {
                                playerBullet[i].x-=playerBullet[i].speed;
                            }
                            else if(playerBullet[i].direction == 1)
                            {
                                playerBullet[i].x+=playerBullet[i].speed;
                            }
                        }
                        if(enemyBullet[i].live)
                        {
                            if(enemyBullet[i].direction == -1)
                            {
                                enemyBullet[i].x-=enemyBullet[i].speed;
                            }
                            if(enemyBullet[i].direction == 1)
                            {
                                enemyBullet[i].x+=enemyBullet[i].speed;
                            }

                        }
                    }

                    /* Prende a Camera no Personagem */
                    cameraX = player.x-(SCREEN_W/2);
                    cameraY = player.y-(SCREEN_H/2);

                    /* Fazer com que a camera nao passe dos limites do mapa */
                    if (cameraX < 0) cameraX = 0;
                    if (cameraY < 0) cameraY = 0;
                    if (cameraX > WORLD_W - SCREEN_W) cameraX = WORLD_W - SCREEN_W;
                    if (cameraY > WORLD_H - SCREEN_H) cameraY = WORLD_H - SCREEN_H;

                    /* Colisoes + check trap */
                    for (i = 0; i<LINHA_MAX; i++)
                    {
                        for(j = 0; j<COLUNA_MAX; j++)
                        {
                            for(k=0; k<NUM_BULLET; k++)
                            {
                                collision_bullet_player(&player, &enemyBullet[k], img_enemy_bullet, &bulletCount, 40, 40);
                            }
                            if(block[i][j].live == true)
                            {
                                if(mapa[i][j] == 2)
                                {
                                    collision_player_tiles(&player, &block[i][j], &jumping, img_block2);
                                }
                                if(mapa[i][j] == 3)
                                {
                                    collision_player_tiles(&player, &block[i][j], &jumping, img_block2);
                                }
                                if(mapa[i][j] == 4)
                                {
                                    check_trap(&player, &block[i][j], &enemy1[i][j], 4);
                                    collision_player_tiles(&player, &block[i][j], &jumping, img_block2);
                                }
                                if(mapa[i][j] == 5)
                                {
                                    collision_player_tiles(&player, &block[i][j], &jumping, img_block2);
                                }
                                for(k=0; k<NUM_BULLET; k++)
                                {
                                    collision_bullet_tiles(&playerBullet[k], &block[i][j], img_player_bullet, img_block2, 0, &bulletCount);
                                    collision_bullet_tiles(&enemyBullet[k], &block[i][j], img_player_bullet, img_block2, 1, &bulletCount);
                                }
                            }
                            if(block[i][j].live == false)
                            {
                                if(mapa[i][j] == 5)
                                {
                                    check_trap(&player, &block[i][j], &enemy1[i][j], 5);
                                }
                                if(mapa[i][j] == 6)
                                {
                                    collision_player_enemy(&player, &enemy1[i][j], 40, 40);

                                    for(k=0; k<NUM_BULLET; k++)
                                    {
                                        collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40);
                                    }
                                }
                                if(mapa[i][j] == 7)
                                {
                                    check_trap(&player, &block[i][j], &enemy1[i][j], 7);
                                    collision_player_enemy(&player, &enemy1[i][j], 40, 40);

                                    for(k=0; k<NUM_BULLET; k++)
                                    {
                                        collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40);
                                    }
                                }
                                if(mapa[i][j] == 8)
                                {
                                    check_trap(&player, &block[i][j], &enemy1[i][j], 8);
                                    collision_player_enemy(&player, &enemy1[i][j], 40, 40);

                                    for(k=0; k<NUM_BULLET; k++)
                                    {
                                        collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40);
                                    }
                                }
                                if(mapa[i][j] == 9)
                                {
                                    check_trap(&player, &block[i][j], &enemy1[i][j], 9);
                                    collision_player_enemy(&player, &enemy1[i][j], 40, 40);

                                    for(k=0; k<NUM_BULLET; k++)
                                    {
                                        collision_bullet_enemy(&playerBullet[k], &enemy1[i][j], img_player_bullet, 40, 40);
                                    }
                                }
                            }
                        }
                    }

                    collision_player_wall(&player, &jumping, img_block1);

                    /* ~~Desenha o Background~~ */
                    al_draw_bitmap(img_background0, 0 - cameraX, 0 - cameraY, 0);

                    /* ~~Animação dos inimigos~~ */
                    if(++anim_enemy1.frameCount >= anim_enemy1.frameDelay)
                    {
                        if(++anim_enemy1.curFrame >= anim_enemy1.maxFrame)
                        {
                            anim_enemy1.curFrame = 0;
                        }
                        anim_enemy1.frameCount = 0;
                    }

                    /* ~~Desenha os Blocos/Inimigos~~ */
                    for (i = 0; i<LINHA_MAX; i++)
                    {
                        for(j = 0; j<COLUNA_MAX; j++)
                        {
                            if(mapa[i][j] == 2 && block[i][j].live == true)
                            {
                                al_draw_bitmap(img_block1, block[i][j].x - cameraX, block[i][j].y - cameraY, 0);
                            }
                            if(mapa[i][j] == 3 && block[i][j].live == true)
                            {
                                al_draw_bitmap(img_block2, block[i][j].x - cameraX, block[i][j].y - cameraY, 0);
                            }
                            if(mapa[i][j] == 4 && block[i][j].live == true)
                            {
                                al_draw_bitmap(img_block2, block[i][j].x - cameraX, block[i][j].y - cameraY, 0);
                            }
                            if(mapa[i][j] == 5 && block[i][j].live == true)
                            {
                                al_draw_bitmap(img_block2, block[i][j].x - cameraX, block[i][j].y - cameraY, 0);
                            }
                            if((mapa[i][j] == 6 || mapa[i][j] == 7 || mapa[i][j] == 8) && (enemy1[i][j].direction == -1) && (enemy1[i][j].live == true))
                            {
                                al_draw_bitmap_region(img_enemy1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, enemy1[i][j].x - cameraX, enemy1[i][j].y - cameraY, 0);
                            }
                            if((mapa[i][j] == 6 || mapa[i][j] == 7 || mapa[i][j] == 8) && (enemy1[i][j].direction == 1) && (enemy1[i][j].live == true))
                            {
                                al_draw_bitmap_region(img_enemy1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, enemy1[i][j].x - cameraX, enemy1[i][j].y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                            }
                            if(mapa[i][j] == 9 && enemy1[i][j].live == true)
                            {
                                al_draw_bitmap_region(img_boss1, anim_enemy1.curFrame * anim_enemy1.frameWidth, 0, anim_enemy1.frameWidth, anim_enemy1.frameHeight, enemy1[i][j].x - cameraX, enemy1[i][j].y - cameraY, 0);
                            }
                        }
                    }

                    /* ~~Desenho do player~~ */

                    /* Player parado */
                    if(iddle == true)
                    {
                        if(++immobile.frameCount >= immobile.frameDelay)
                        {
                            if(++immobile.curFrame >= immobile.maxFrame)
                            {
                                immobile.curFrame = 0;
                                iddle = false;
                            }
                            immobile.frameCount = 0;
                        }
                    }
                    if((keys[KEY_SPACE] == false) && (jump == false) && ((!keys[KEY_LEFT] && player.direction == -1) || player.x == 64))
                    {
                        al_draw_bitmap_region(img_player_immobile, immobile.curFrame * immobile.frameWidth, 0, immobile.frameWidth, immobile.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                    }
                    if((keys[KEY_SPACE] == false) && (jump == false) && ((!keys[KEY_RIGHT] && player.direction == 1) || player.x == WORLD_W - (64-immobile.frameWidth)))
                    {
                        al_draw_bitmap_region(img_player_immobile, immobile.curFrame * immobile.frameWidth, 0, immobile.frameWidth, immobile.frameHeight, player.x - cameraX, player.y - cameraY, 0);
                    }
                    if((keys[KEY_SPACE] == true) && (jump == false) && ((!keys[KEY_LEFT] && player.direction == -1)))
                    {
                        al_draw_bitmap(img_player_immobile_shoot, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                    }
                    if((keys[KEY_SPACE] == true) && (jump == false) && ((!keys[KEY_RIGHT] && player.direction == 1)))
                    {
                        al_draw_bitmap(img_player_immobile_shoot, player.x - cameraX, player.y - cameraY, 0);
                    }

                    /* Player andando */
                    if(++walking.frameCount >= walking.frameDelay)
                    {
                        if(++walking.curFrame >= walking.maxFrame)
                        {
                            walking.curFrame = 0;
                        }
                        walking.frameCount = 0;
                    }

                    if(keys[KEY_SPACE] == false)
                    {
                        if(jump == false && keys[KEY_LEFT] && player.direction == -1)
                        {
                            al_draw_bitmap_region(img_player_walking, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                        }
                        if(jump == false && keys[KEY_RIGHT] && player.direction == 1)
                        {
                            al_draw_bitmap_region(img_player_walking, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, 0);
                        }
                    }
                    if(keys[KEY_SPACE] == true)
                    {
                        if(jump == false && keys[KEY_LEFT] && player.direction == -1)
                        {
                            al_draw_bitmap_region(img_player_walking_shoot, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                        }
                        if(jump == false && keys[KEY_RIGHT] && player.direction == 1)
                        {
                            al_draw_bitmap_region(img_player_walking_shoot, walking.curFrame * walking.frameWidth, 0, walking.frameWidth, walking.frameHeight, player.x - cameraX, player.y - cameraY, 0);
                        }
                    }

                    /* Player pulando */
                    if(jump == true)
                    {
                        if(++jumping.frameCount >= jumping.frameDelay)
                        {
                            if(++jumping.curFrame >= jumping.maxFrame)
                            {
                                jumping.curFrame = 0;
                            }
                            jumping.frameCount = 0;
                        }

                        if(keys[KEY_SPACE] == false)
                        {
                            if(player.direction == -1)
                            {
                                al_draw_bitmap_region(img_player_jump, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                            }
                            if(player.direction == 1)
                            {
                                al_draw_bitmap_region(img_player_jump, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, 0);
                            }
                        }
                        else if(keys[KEY_SPACE] == true)
                        {
                            if(player.direction == -1)
                            {
                                al_draw_bitmap_region(img_player_jump_shoot, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, ALLEGRO_FLIP_HORIZONTAL);
                            }
                            if(player.direction == 1)
                            {
                                al_draw_bitmap_region(img_player_jump_shoot, jumping.curFrame * jumping.frameWidth, 0, jumping.frameWidth, jumping.frameHeight, player.x - cameraX, player.y - cameraY, 0);
                            }
                        }

                    }

                    /* ~~Desenho dos projeteis~~ */
                    for(i=0; i<NUM_BULLET; i++)
                    {
                        if(playerBullet[i].live)
                        {
                            al_draw_bitmap(img_player_bullet, playerBullet[i].x - cameraX, playerBullet[i].y - cameraY, 0);
                        }
                        if(enemyBullet[i].live)
                        {
                            al_draw_bitmap(img_enemy_bullet, enemyBullet[i].x - cameraX, enemyBullet[i].y - cameraY, 0);
                        }
                    }

                    /* Pontuacao e Porcentagem de Vida */
                    al_draw_bitmap(img_heart, 0, 0, 0);
                    al_draw_textf(fonte16, al_map_rgb(255, 255, 255), 20, 0, ALLEGRO_ALIGN_LEFT, ("%03d"), player.life);

                    al_draw_bitmap(img_medal, 64, 0, 0);
                    al_draw_textf(fonte16, al_map_rgb(255, 255, 255), 80, 0, ALLEGRO_ALIGN_LEFT, ("%04d"), scores);

                    al_draw_bitmap(img_clock, 144, 0, 0);
                    al_draw_textf(fonte16, al_map_rgb(255, 255, 255), 160, 0, ALLEGRO_ALIGN_LEFT, ("%03d"), stopwatch);
                }

                /* Termino da Fase */
                if(enemyKilled == ENEMY_MAX) /* You Win! */
                {
                    scores = scores + (25 * stopwatch) + (10*player.life);
                    gamestate = 2;
                }
                if(player.life <= 0 || stopwatch == 0) /* Game Over! */
                {
                    gamestate = 3;
                }

                /* Troca o display */
                al_flip_display();
            }
            break;
        case 2:
            al_wait_for_event(event_queue, &ev);

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) /* Faz com que o jogo feche ao clicar no botao "X" do display */
            {
                quit = true;
            }

            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) /* Faz com que o jogo feche ao pressionar esc */
                {
                    quit = true;
                }
            }
            if(ev.type == ALLEGRO_EVENT_TIMER) /* Mostrar mensagens na tela */
            {
                if(ev.timer.source == timer_60)
                {
                    al_clear_to_color(al_map_rgb(0,0,0));
                    al_draw_bitmap(img_you_win, 0, 0, 0);
                    al_draw_textf(fonte16, al_map_rgb(0, 0, 0), SCREEN_W/2, SCREEN_H - 48, ALLEGRO_ALIGN_CENTRE, "Seus pontos: %d", scores);
                    al_draw_textf(fonte16, al_map_rgb(0, 0, 0), SCREEN_W/2, SCREEN_H - 32, ALLEGRO_ALIGN_CENTRE, "Pressione Esc para sair");
                    al_flip_display();
                }
            }
            break;

        case 3:
            al_wait_for_event(event_queue, &ev);

            if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) /* Faz com que o jogo feche ao clicar no botao "X" do display */
            {
                quit = true;
            }

            if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
            {
                if(ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE) /* Faz com que o jogo feche ao pressionar esc */
                {
                    quit = true;
                }
            }
            if(ev.type == ALLEGRO_EVENT_TIMER) /* Mostrar mensagens na tela */
            {
                if(ev.timer.source == timer_60)
                {
                    al_clear_to_color(al_map_rgb(0,0,0));
                    al_draw_bitmap(img_game_over, 0, 0, 0);
                    al_draw_textf(fonte16, al_map_rgb(255, 0, 0), SCREEN_W/2, SCREEN_H - 48, ALLEGRO_ALIGN_CENTRE, "Seus pontos: %d", scores);
                    al_draw_textf(fonte16, al_map_rgb(255, 0, 0), SCREEN_W/2, SCREEN_H - 32, ALLEGRO_ALIGN_CENTRE, "Pressione Esc para sair");
                    al_flip_display();
                }
            }
            break;
        }
    }
    /* Destruindo as variaveis */
    al_destroy_display(display);
    al_destroy_event_queue(event_queue);
    al_destroy_timer(timer_0p2);
    al_destroy_timer(timer_1);
    al_destroy_timer(timer_60);

    al_destroy_bitmap(img_home_screen);
    al_destroy_bitmap(img_dica_h1n1);
    al_destroy_bitmap(img_background0);

    al_destroy_bitmap(img_heart);
    al_destroy_bitmap(img_medal);
    al_destroy_bitmap(img_clock);
    al_destroy_bitmap(img_block1);
    al_destroy_bitmap(img_block2);

    al_destroy_bitmap(img_player_walking);
    al_destroy_bitmap(img_player_walking_shoot);
    al_destroy_bitmap(img_player_immobile);
    al_destroy_bitmap(img_player_immobile_shoot);
    al_destroy_bitmap(img_player_jump);
    al_destroy_bitmap(img_player_jump_shoot);
    al_destroy_bitmap(img_player_bullet);

    al_destroy_bitmap(img_enemy1);
    al_destroy_bitmap(img_boss1);
    al_destroy_bitmap(img_enemy_bullet);

    al_destroy_sample(spl_theme);
    al_destroy_sample(spl_playerShoot);
    al_destroy_sample(spl_mlk);

    return 0;
}
コード例 #28
0
ファイル: riots.c プロジェクト: dos1/mediator
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;
}