void Menu_Unload(struct Game *game) { if (!game->menu.loaded) return; if (game->gamestate==GAMESTATE_MENU) Menu_Stop(game); al_destroy_bitmap(game->menu.pinkcloud); al_destroy_bitmap(game->menu.image); al_destroy_bitmap(game->menu.cloud); al_destroy_bitmap(game->menu.cloud2); al_destroy_bitmap(game->menu.pinkcloud_bitmap); al_destroy_bitmap(game->menu.rain_bitmap); al_destroy_bitmap(game->menu.mountain); al_destroy_bitmap(game->menu.pie_bitmap); al_destroy_bitmap(game->menu.logo); al_destroy_bitmap(game->menu.logoblur); al_destroy_bitmap(game->menu.glass); al_destroy_bitmap(game->menu.blurbg); al_destroy_bitmap(game->menu.blurbg2); al_destroy_font(game->menu.font_title); al_destroy_font(game->menu.font_subtitle); al_destroy_font(game->menu.font); al_destroy_font(game->menu.font_selected); al_destroy_sample_instance(game->menu.music); al_destroy_sample_instance(game->menu.rain_sound); al_destroy_sample_instance(game->menu.click); al_destroy_sample(game->menu.sample); al_destroy_sample(game->menu.rain_sample); al_destroy_sample(game->menu.click_sample); game->menu.loaded = false; }
/* closes down the sound system */ void shutdown_sound() { if (!al_is_audio_installed()) return; al_destroy_thread(sound_update_thread); al_uninstall_timer(ping_timer); al_stop_samples(); al_destroy_sample(zap); al_destroy_sample(bang); al_destroy_sample(bigbang); al_destroy_sample(ping); if (!no_music) { al_uninstall_timer(music_timer); al_destroy_sample_instance(part_voice[0]); al_destroy_sample_instance(part_voice[1]); al_destroy_sample_instance(part_voice[2]); al_destroy_sample_instance(part_voice[3]); al_destroy_sample(sine); al_destroy_sample(square); al_destroy_sample(saw); al_destroy_sample(bd); al_destroy_sample(snare); al_destroy_sample(hihat); } }
void Gamestate_Unload(struct Game *game, struct RocketsResources* data) { al_destroy_bitmap(data->bg); al_destroy_bitmap(data->earth); al_destroy_bitmap(data->clouds); al_destroy_bitmap(data->pixelator); al_destroy_sample_instance(data->rocket_sound); al_destroy_sample_instance(data->boom_sound); al_destroy_sample(data->rocket_sample); al_destroy_sample(data->boom_sample); // TODO: DestroyCharacters free(data); }
void Gamestate_Unload(struct Game *game, struct GamestateResources* data) { al_destroy_font(data->font); al_destroy_sample_instance(data->sound); al_destroy_sample(data->sample); al_destroy_sample_instance(data->kbd); al_destroy_sample(data->kbd_sample); al_destroy_sample_instance(data->key); al_destroy_sample(data->key_sample); al_destroy_bitmap(data->bitmap); al_destroy_bitmap(data->checkerboard); al_destroy_bitmap(data->pixelator); TM_Destroy(data->timeline); free(data); }
void SoundManager::unloadAll() { std::map<int, Sound*>::iterator it; for (it = samples.begin(); it != samples.end(); ++it) { if ((*it).second->sample) { al_destroy_sample((*it).second->sample); } delete (*it).second; } samples.clear(); std::map<int, Music*>::iterator it2; for (it2 = streams.begin(); it2 != streams.end(); ++it2) { if ((*it2).second->instance) { al_destroy_sample_instance((*it2).second->instance); al_destroy_sample((*it2).second->sound->sample); } delete (*it2).second->sound; delete (*it2).second; } streams.clear(); }
AllegroSoundSample5::~AllegroSoundSample5() { if ( m_pInstance ) { al_detach_sample_instance( m_pInstance ); al_destroy_sample_instance( m_pInstance ); } }
void SampleResource::destroy(void) { if (!sample) return; al_destroy_sample_instance(sample); al_destroy_sample(sample_data); sample = 0; sample_data = 0; }
Audio::~Audio(){ //// DESTROY/ DEALLOCATE MEMORY al_destroy_sample(bMusic); al_destroy_sample(endGame); al_destroy_sample(caught); al_destroy_sample(respawn); al_destroy_sample(collect); al_destroy_sample_instance(MusicInstance); };
/* Destroy all sample instances, and frees the associated vectors. */ static void free_sample_vector(void) { int j; for (j = 0; j < (int) _al_vector_size(&auto_samples); j++) { ALLEGRO_SAMPLE_INSTANCE **slot = _al_vector_ref(&auto_samples, j); al_destroy_sample_instance(*slot); } _al_vector_free(&auto_samples); _al_vector_free(&auto_sample_ids); }
void Gamestate_Unload(struct Game *game, struct MenuResources* data) { al_stop_sample_instance(data->music); al_destroy_bitmap(data->bg); al_destroy_bitmap(data->title); al_destroy_bitmap(data->monster); al_destroy_font(data->font); //al_destroy_sample_instance(data->music); al_destroy_sample_instance(data->click); //al_destroy_sample(data->sample); al_destroy_sample(data->click_sample); }
void uninstall() { // clear bgm bgm_stop(); // clear samples for (SampleIter it = samples.begin(); it != samples.end(); it++) al_destroy_sample(it->second); samples.clear(); // clear channels if (bgm_channel != NULL) { al_destroy_sample_instance(bgm_channel); bgm_channel = NULL; } for (unsigned int i=0; i<effects.size(); i++) al_destroy_sample_instance(effects[i]); effects.clear(); // uninstall al_uninstall_audio(); }
Music::~Music() { stopMusic(this); if (sample) al_destroy_sample(sample); if (instance) { al_detach_sample_instance(instance); al_destroy_sample_instance(instance); } }
/* GC and meta * */ static int allua_sample_instance_gc(lua_State * L) { struct ALLUA_sample_instance_s *pi = (struct ALLUA_sample_instance_s *)lua_touserdata(L, 1); if (pi->gc_allowed) { ALLUA_sample_instance im = pi->sample_instance; printf("goodbye sample_instance (%p)\n", (void *)pi); if (im) al_destroy_sample_instance(im); luaL_unref(L, LUA_REGISTRYINDEX, pi->sample_ref); } return 0; }
void DestroySamples(Audio* const audio) { al_destroy_sample(audio->song_sample); audio->song_sample = NULL; al_destroy_sample_instance(audio->song_sample_instance); audio->song_sample_instance = NULL; int i; for (i = 0; i < AT_NUMBER_OF_TYPES; ++i) { al_destroy_sample(audio->samples[i]); audio->samples[i] = NULL; } }
void t3f_poll_sound_queue(void) { /* a queued sample is playing */ if(t3f_queue_sample_instance) { if(!al_get_sample_instance_playing(t3f_queue_sample_instance)) { al_destroy_sample_instance(t3f_queue_sample_instance); t3f_queue_sample_instance = NULL; } } if(!t3f_queue_sample_instance) { t3f_play_queued_sample(); } }
void t3f_clear_sample_queue(void) { int i; for(i = 0; i < T3F_MAX_QUEUED_SAMPLES; i++) { t3f_sample_queue[i] = NULL; } t3f_queued_samples = 0; if(al_get_sample_instance_playing(t3f_queue_sample_instance)) { al_stop_sample_instance(t3f_queue_sample_instance); al_destroy_sample_instance(t3f_queue_sample_instance); t3f_queue_sample_instance = NULL; } }
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 ); }
void SoundManager::unload(Music *music) { if (music == NULL) return; int id = music->sound->id; if (streams[id]->instance) { al_destroy_sample_instance(streams[id]->instance); al_destroy_sample(streams[id]->sound->sample); } delete streams[id]->sound; delete streams[id]; streams.erase(id); }
ScreenMain::~ScreenMain() { if(background != nullptr) al_destroy_bitmap(background); for(int a = 0;a < (int)buttons.size();a++) { delete buttons[a]; } buttons.clear(); if(SCAbout != nullptr) delete SCAbout; if(SCPlay != nullptr) delete SCPlay; if(SCGame != nullptr) delete SCGame; if(raycallback != nullptr) delete raycallback; if(colider != nullptr) delete colider; if(zemak_bitmap != nullptr) al_destroy_bitmap(zemak_bitmap); if(zemak_button != nullptr) delete zemak_button; if(hlasky_font != nullptr) al_destroy_font(hlasky_font); al_stop_sample_instance(intro_music_instance); al_detach_sample_instance(intro_music_instance); if(intro_music_instance != nullptr) al_destroy_sample_instance(intro_music_instance); if(intro_music != nullptr) al_destroy_sample(intro_music); /*for(int a = 0;a < (int)hlasky.size();a++) { global::audio_player->Stop_sample_instance(&hlasky[a]->instance); al_destroy_sample(hlasky[a]->sample); al_destroy_sample_instance(hlasky[a]->instance); delete hlasky[a]; } hlasky.clear();*/ }
/* 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; }
/* ---------------------------------------------------------------------------- * Destroys a structure with sample info. */ void sample_struct::destroy() { //TODO uncommenting this is causing a crash. //al_detach_sample_instance(instance); al_destroy_sample_instance(instance); al_destroy_sample(sample); }
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; }
Sound::~Sound(){ al_destroy_sample_instance(songInstance); };
int main(int argc, char **argv) { ALLEGRO_VOICE *voice; ALLEGRO_MIXER *mixer; ALLEGRO_MIXER *submixer[2]; ALLEGRO_SAMPLE_INSTANCE *sample[2]; ALLEGRO_SAMPLE *sample_data[2]; float sample_time; float max_sample_time; int i; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); if (argc < 3) { log_printf("This example needs to be run from the command line.\nUsage: %s file1 file2\n", argv[0]); goto done; } al_init_acodec_addon(); if (!al_install_audio()) { abort_example("Could not init sound!\n"); } voice = al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); if (!voice) { abort_example("Could not create ALLEGRO_VOICE.\n"); } mixer = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); submixer[0] = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); submixer[1] = al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); if (!mixer || !submixer[0] || !submixer[1]) { abort_example("al_create_mixer failed.\n"); } if (!al_attach_mixer_to_voice(mixer, voice)) { abort_example("al_attach_mixer_to_voice failed.\n"); } for (i = 0; i < 2; i++) { const char *filename = argv[i + 1]; sample_data[i] = al_load_sample(filename); if (!sample_data[i]) { abort_example("Could not load sample from '%s'!\n", filename); } sample[i] = al_create_sample_instance(NULL); if (!sample[i]) { abort_example("al_create_sample failed.\n"); } if (!al_set_sample(sample[i], sample_data[i])) { abort_example("al_set_sample_ptr failed.\n"); } if (!al_attach_sample_instance_to_mixer(sample[i], submixer[i])) { abort_example("al_attach_sample_instance_to_mixer failed.\n"); } if (!al_attach_mixer_to_mixer(submixer[i], mixer)) { abort_example("al_attach_mixer_to_mixer failed.\n"); } } /* Play sample in looping mode. */ for (i = 0; i < 2; i++) { al_set_sample_instance_playmode(sample[i], ALLEGRO_PLAYMODE_LOOP); al_play_sample_instance(sample[i]); } max_sample_time = al_get_sample_instance_time(sample[0]); sample_time = al_get_sample_instance_time(sample[1]); if (sample_time > max_sample_time) max_sample_time = sample_time; log_printf("Playing..."); al_rest(max_sample_time); al_set_sample_instance_gain(sample[0], 0.5); al_rest(max_sample_time); al_set_sample_instance_gain(sample[1], 0.25); al_rest(max_sample_time); al_stop_sample_instance(sample[0]); al_stop_sample_instance(sample[1]); log_printf("Done\n"); /* Free the memory allocated. */ for (i = 0; i < 2; i++) { al_set_sample(sample[i], NULL); al_destroy_sample(sample_data[i]); al_destroy_sample_instance(sample[i]); al_destroy_mixer(submixer[i]); } al_destroy_mixer(mixer); al_destroy_voice(voice); al_uninstall_audio(); done: close_log(true); return 0; }
int main(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; }
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; }
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; }
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; }