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; }
AudioSample::~AudioSample() { al_destroy_sample(m_sample); }
void operator ()( SSample *sample ) { m_samples.erase( sample->m_key ); al_destroy_sample( sample->m_pSample ); delete sample; }
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/welcome.wav"; } 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; /* A matrix that puts everything in the left channel. */ float mono_to_stereo[] = {1.0, 0.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) normally.\n", filename, sample_time); al_rest(sample_time); if (al_get_channel_count(al_get_sample_instance_channels(sample)) == 1) { if (!al_set_sample_instance_channel_matrix(sample, mono_to_stereo)) { abort_example("Failed to set channel matrix.\n"); } log_printf("Playing left channel only.\n"); al_rest(sample_time); } if (!al_set_sample_instance_gain(sample, 0.5)) { abort_example("Failed to set gain.\n"); } log_printf("Playing with gain 0.5.\n"); al_rest(sample_time); if (!al_set_sample_instance_gain(sample, 0.25)) { abort_example("Failed to set gain.\n"); } log_printf("Playing with gain 0.25.\n"); al_rest(sample_time); al_stop_sample_instance(sample); log_printf("Done 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; }
/* starting game UI */ void start_game (bool Fullscreen) { /* creating display */ ALLEGRO_DISPLAY * display; if (Fullscreen) { /* creating disp_data struct to store supported resolutions */ ALLEGRO_DISPLAY_MODE disp_data; /* making it fullscreen */ al_set_new_display_flags(ALLEGRO_FULLSCREEN); /* storing info */ al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); CurrentScreenWidth = disp_data.width; CurrentScreenHeight = disp_data.height; /* creating display with different resolutions for different screens */ display = al_create_display(CurrentScreenWidth, CurrentScreenHeight); } else { al_set_new_display_flags(ALLEGRO_WINDOWED); display = al_create_display(CurrentScreenWidth, CurrentScreenHeight); } if (!display) { al_show_native_message_box(display, "Error", "Display Settings", "Couldn't create a display.", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } /* setting new window title */ al_set_window_title(display, "Snake"); // ----------------------- /* creating fonts */ ALLEGRO_FONT * font = al_load_font(MainFont, 36, ALLEGRO_ALIGN_CENTER); ALLEGRO_FONT * credits_font = al_load_font(CreditsFont, 20, NULL); if (!font) { al_show_native_message_box(display, "Error", "Could not load font file.", "Have you included the resources in the same directory of the program?", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } /* loading audio samples */ ALLEGRO_SAMPLE * over_button_sound = al_load_sample(OverButton); ALLEGRO_SAMPLE * pressed_button_sound = al_load_sample(PressedButton); ALLEGRO_SAMPLE * eating_apple_sound = al_load_sample(EatApple); ALLEGRO_SAMPLE * game_over_sound = al_load_sample(GameOverSound); if (!over_button_sound || !pressed_button_sound) { al_show_native_message_box(display, "Error", "Could not load one or more sound files.", "Your resources folder must be corrupt, please download it again.", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } al_reserve_samples(2); /* creating timer */ ALLEGRO_TIMER * timer = al_create_timer(1.0 / FPS); ALLEGRO_TIMER *frametimer = al_create_timer(1.0 / frameFPS); /* creating event queue */ ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue(); 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_timer_event_source(frametimer)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue, al_get_keyboard_event_source()); ALLEGRO_KEYBOARD_STATE keystate; /* loading BITMAPS */ ALLEGRO_BITMAP * SmallWallpaperBitmap = al_load_bitmap(SmallWallpaper); ALLEGRO_BITMAP * BigWallpaperBitmap = al_load_bitmap(BigWallpaper); ALLEGRO_BITMAP * mouse = al_load_bitmap(MouseCursor); ALLEGRO_BITMAP * applepng = al_load_bitmap(Apple_png); if (!mouse) { al_show_native_message_box(display, "Error", "Could not load one or more resource file.", "Your resources folder must be corrupt, please download it again.", NULL, ALLEGRO_MESSAGEBOX_ERROR); exit(-1); } al_hide_mouse_cursor(display); /* ---- VARIABLES ---- */ bool done = false, change_resolution = false, draw = true; int mouse_x = CurrentScreenWidth, mouse_y = CurrentScreenHeight; /* mouse */ bool left_mouse_button_down = false; bool left_mouse_button_up = false; /* MAIN MENU */ int button_displacement; if (!Fullscreen) { button_displacement = 20; } else { button_displacement = 15; } Button play_button(50, 20, Blue, 0, -button_displacement); Button options_button(40, 15, Blue); Button exit_button(40, 15, Blue, 0, button_displacement); /* PLAY */ string score; Direction new_direction; int speed_up, speed_up_anim_frame = 0; bool speed_up_anim = false; Snake snake; Apple apple; /* starting timers */ al_start_timer(timer); al_start_timer(frametimer); while (!done) { /* actually defining our events */ ALLEGRO_EVENT events; al_wait_for_event(event_queue, &events); al_get_keyboard_state(&keystate); switch (gameState) { case MainMenu: { /* WINDOW */ if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } /* MOUSE */ if (events.type == ALLEGRO_EVENT_MOUSE_AXES) { mouse_x = events.mouse.x; mouse_y = events.mouse.y; draw = true; } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = true; draw = true; } } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = false; left_mouse_button_up = true; draw = true; } } /* button conditions */ if (play_button.MouseOverButton(mouse_x, mouse_y) && !play_button.StillOverButton) { play_button.play_over_button_sound = true; } if (options_button.MouseOverButton(mouse_x, mouse_y) && !options_button.StillOverButton) { options_button.play_over_button_sound = true; } if (exit_button.MouseOverButton(mouse_x, mouse_y) && !exit_button.StillOverButton) { exit_button.play_over_button_sound = true; } /* KEYBOARD */ if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: { done = true; break; } case ALLEGRO_KEY_SPACE: case ALLEGRO_KEY_ENTER: { /* STARTING GAME NOW */ new_direction = RIGHT; speed_up = 0; snake.ResetSnakeDetails(); apple.NewApple(snake.GetSnakeCells()); gameState = PlayGame; } } break; } /* ------------ NOW DRAWING ------------ */ if (draw) { /* drawing wallpaper */ if (Fullscreen) { al_draw_scaled_bitmap(BigWallpaperBitmap, 0, 0, al_get_bitmap_width(BigWallpaperBitmap), al_get_bitmap_height(BigWallpaperBitmap), 0, 0, CurrentScreenWidth, CurrentScreenHeight, NULL); } else { al_draw_bitmap(SmallWallpaperBitmap, 0, 0, 0); } /* -- play button -- */ if (play_button.MouseOverButton(mouse_x, mouse_y)) { play_button.SetButtonColor(LightBlue); /* button pressed */ if (left_mouse_button_down) { play_button.pressed_button = true; play_button.SetButtonColor(DarkBlue); } /* button released */ else if (left_mouse_button_up) { play_button.pressed_button = false; play_button.StillPressingButton = false; play_button.SetButtonColor(Blue); /* STARTING GAME NOW */ new_direction = RIGHT; speed_up = 0; snake.ResetSnakeDetails(); apple.NewApple(snake.GetSnakeCells()); gameState = PlayGame; } play_button.DisplayButton(); } else { play_button.pressed_button = false; play_button.StillPressingButton = false; play_button.StillOverButton = false; play_button.SetButtonColor(Blue); play_button.DisplayButton(); } /* -- options button -- */ if (options_button.MouseOverButton(mouse_x, mouse_y)) { options_button.SetButtonColor(LightBlue); /* button pressed */ if (left_mouse_button_down) { options_button.pressed_button = true; options_button.SetButtonColor(DarkBlue); } /* button released */ else if (left_mouse_button_up) { options_button.pressed_button = false; options_button.StillPressingButton = false; options_button.SetButtonColor(Blue); switch (Fullscreen) { case 0: { Fullscreen = 1; done = true; change_resolution = true; break; } case 1: { Fullscreen = 0; done = true; change_resolution = true; break; } } } options_button.DisplayButton(); } else { options_button.pressed_button = false; options_button.StillPressingButton = false; options_button.StillOverButton = false; options_button.SetButtonColor(Blue); options_button.DisplayButton(); } /* -- exit button -- */ if (exit_button.MouseOverButton(mouse_x, mouse_y)) { exit_button.SetButtonColor(LightBlue); /* button pressed */ if (left_mouse_button_down) { exit_button.pressed_button = true; exit_button.SetButtonColor(DarkBlue); } /* button released */ else if (left_mouse_button_up) { exit_button.pressed_button = false; exit_button.StillPressingButton = false; exit_button.SetButtonColor(Blue); done = true; } exit_button.DisplayButton(); } else { exit_button.pressed_button = false; exit_button.StillPressingButton = false; exit_button.StillOverButton = false; exit_button.SetButtonColor(Blue); exit_button.DisplayButton(); } /* -- sound -- */ /* mouse over button */ if (play_button.MouseOverButton(mouse_x, mouse_y) && !play_button.StillOverButton) { play_button.StillOverButton = true; al_play_sample(over_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } if (options_button.MouseOverButton(mouse_x, mouse_y) && !options_button.StillOverButton) { options_button.StillOverButton = true; al_play_sample(over_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } if (exit_button.MouseOverButton(mouse_x, mouse_y) && !exit_button.StillOverButton) { exit_button.StillOverButton = true; al_play_sample(over_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } /* button pressed */ if (play_button.pressed_button && !play_button.StillPressingButton) { play_button.StillPressingButton = true; al_play_sample(pressed_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } else if (options_button.pressed_button && !options_button.StillPressingButton) { options_button.StillPressingButton = true; al_play_sample(pressed_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } else if (exit_button.pressed_button && !exit_button.StillPressingButton) { exit_button.StillPressingButton = true; al_play_sample(pressed_button_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); } /* -- text -- */ string fullscreenmode_string; if (Fullscreen) { fullscreenmode_string = "Fullscreen: On"; } else { fullscreenmode_string = "Fullscreen: Off"; } al_draw_text(font, White, CurrentScreenWidth / 2, play_button.GetButtonHeightCenter() - 23, ALLEGRO_ALIGN_CENTER, "New Game"); al_draw_text(font, White, CurrentScreenWidth / 2, options_button.GetButtonHeightCenter() - 23, ALLEGRO_ALIGN_CENTER, fullscreenmode_string.c_str()); al_draw_text(font, White, CurrentScreenWidth / 2, exit_button.GetButtonHeightCenter() - 23, ALLEGRO_ALIGN_CENTER, "Exit"); al_draw_text(credits_font, White, 3, CurrentScreenHeight - 20, NULL, "FEUP 2013 - Henrique Ferrolho"); /* -- mouse cursor -- */ al_draw_bitmap(mouse, mouse_x, mouse_y, NULL); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); left_mouse_button_up = false; draw = false; } break; } case PlayGame: { if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } /* KEYBOARD */ if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: { /* going back to MAIN MENU */ draw = true; gameState = MainMenu; break; } case ALLEGRO_KEY_ENTER: case ALLEGRO_KEY_SPACE: { /* pausing game */ draw = true; gameState = PauseGame; break; } } break; } if (events.type == ALLEGRO_EVENT_TIMER) { if (events.timer.source == timer) { draw = true; /* navigation keys */ if (al_key_down(&keystate, ALLEGRO_KEY_DOWN) || al_key_down(&keystate, ALLEGRO_KEY_S)) { new_direction = DOWN; } else if (al_key_down(&keystate, ALLEGRO_KEY_UP) || al_key_down(&keystate, ALLEGRO_KEY_W)) { new_direction = UP; } else if (al_key_down(&keystate, ALLEGRO_KEY_RIGHT) || al_key_down(&keystate, ALLEGRO_KEY_D)) { new_direction = RIGHT; } else if (al_key_down(&keystate, ALLEGRO_KEY_LEFT) || al_key_down(&keystate, ALLEGRO_KEY_A)) { new_direction = LEFT; } /* checking boundaries */ if (!snake.IsInScreenBoundaries() || snake.EatedItself()) { draw = true; al_play_sample(game_over_sound, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); gameState = GameOver; break; } } else if (events.timer.source == frametimer) { /* moving snake */ snake.SetSnakeDirection(new_direction); snake.MoveSnake(); if (snake.EatedApple(apple.GetAppleX(), apple.GetAppleY())) { al_play_sample(eating_apple_sound, 0.5, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0); apple.NewApple(snake.GetSnakeCells()); snake.IncreaseSnakeLength(); draw = true; } } } /* ------------ NOW DRAWING ------------ */ if (draw) { /* game frame */ if (Fullscreen) { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); //al_draw_rectangle(0, 0, 1360, 760, DarkRed, 20); } else { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); } apple.DrawApple(applepng); snake.DrawSnake(); /* increasing speed */ if (snake.GetSnakeCells().size() < 5) { al_set_timer_speed(frametimer, 1.0 / frameFPS); } else if (snake.GetSnakeCells().size() < 10) { if (speed_up == 0) { speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 14); } else if (snake.GetSnakeCells().size() < 20) { if (speed_up == 1) { snake.SetColor(LightBlue); speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 16); } else if (snake.GetSnakeCells().size() < 30) { if (speed_up == 2) { speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 18); } else if (snake.GetSnakeCells().size() < 40) { if (speed_up == 3) { snake.SetColor(DarkRed); speed_up++; speed_up_anim = true; } al_set_timer_speed(frametimer, 1.0 / 20); } /* speed up animation */ if (speed_up_anim) { if (speed_up_anim_frame < 10) { al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 20) { al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 30) { al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 40) { al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 50) { al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "SPEED UP!"); speed_up_anim_frame++; } else if (speed_up_anim_frame < 60) { speed_up_anim = false; speed_up_anim_frame = 0; } } /* printing score */ stringstream ss; ss << "Score: " << snake.GetSnakeCells().size(); score = ss.str(); al_draw_text(credits_font, Yellow, 60, 15, ALLEGRO_ALIGN_CENTER, score.c_str()); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); draw = false; } break; } case PauseGame: { if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_SPACE: case ALLEGRO_KEY_ENTER: { /* resume game */ draw = true; gameState = PlayGame; break; } } break; } /* ------------ NOW DRAWING ------------ */ if (draw) { /* game frame */ if (Fullscreen) { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); //al_draw_rectangle(0, 0, 1360, 760, DarkRed, 20); } else { al_draw_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed, 20); } apple.DrawApple(applepng); snake.DrawSnake(); /* printing score */ stringstream ss; ss << "Score: " << snake.GetSnakeCells().size(); score = ss.str(); al_draw_text(credits_font, Yellow, 60, 15, ALLEGRO_ALIGN_CENTER, score.c_str()); al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "GAME PAUSED"); al_flip_display(); draw = false; } break; } case GameOver: { if (events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } /* MOUSE */ if (events.type == ALLEGRO_EVENT_MOUSE_AXES) { mouse_x = events.mouse.x; mouse_y = events.mouse.y; draw = true; } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = true; draw = true; } } if (events.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { /* left button */ if (events.mouse.button & 1) { left_mouse_button_down = false; left_mouse_button_up = true; draw = true; } } /* going to MAIN MENU */ if (events.type == ALLEGRO_EVENT_KEY_UP) { switch (events.keyboard.keycode) { case ALLEGRO_KEY_SPACE: case ALLEGRO_KEY_ENTER: { draw = true; gameState = MainMenu; break; } } break; } if (left_mouse_button_up) { left_mouse_button_up = false; draw = true; gameState = MainMenu; break; } if (draw) { al_draw_filled_rectangle(0, 0, CurrentScreenWidth, CurrentScreenHeight, DarkRed); /* printing score */ stringstream ss; ss << "Score: " << snake.GetSnakeCells().size(); score = ss.str(); al_draw_text(font, Yellow, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 80, ALLEGRO_ALIGN_CENTER, score.c_str()); al_draw_text(font, White, CurrentScreenWidth / 2, (CurrentScreenHeight / 2) - 30, ALLEGRO_ALIGN_CENTER, "Click to continue"); /* -- mouse cursor -- */ al_draw_bitmap(mouse, mouse_x, mouse_y, NULL); al_flip_display(); } break; } } } /* dealocating memory */ al_destroy_display(display); al_destroy_font(font); al_destroy_timer(timer); al_destroy_bitmap(mouse); al_destroy_sample(over_button_sound); al_destroy_sample(pressed_button_sound); al_destroy_event_queue(event_queue); if (change_resolution) { CurrentScreenWidth = DefaultScreenWidth; CurrentScreenHeight = DefaultScreenHeight; start_game(Fullscreen); } }
ColorSelector::~ColorSelector() { al_destroy_sample(sample); };
void explosion_shutdown(void) { 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; }
Pellet::~Pellet() { delete m_rectangle; al_destroy_sample(m_eat_sound); }
void show_sound() { int licznik=0; int x,y; x=500; y=100; ALLEGRO_FONT * font_ttf = al_load_ttf_font("cour.ttf",80, 0); ALLEGRO_FONT * font_ttfbolt = al_load_ttf_font("courbd.ttf",80, 0); ALLEGRO_BITMAP *bitmapa = al_load_bitmap( "media/menu.png" ); bool done=false,draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_flip_display(); ALLEGRO_EVENT_QUEUE *event_queue1 = al_create_event_queue(); ALLEGRO_KEYBOARD_STATE keyState; //al_register_event_source(event_queue, al_get_mouse_event_source()); al_register_event_source(event_queue1, al_get_display_event_source(display)); al_register_event_source(event_queue1, al_get_keyboard_event_source()); while(!done) { ALLEGRO_EVENT events; al_wait_for_event(event_queue1, &events); al_get_keyboard_state(&keyState); if(events.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { done = true; } else if (al_key_down( &keyState, ALLEGRO_KEY_ESCAPE)) { done=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_ENTER)) { if(licznik==2) { done=true; draw=false; } else if(licznik==0) { if(!sample) al_reserve_samples(2); sample= al_load_sample("media/The Wild Hunt.wav"); al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL); FILE *fp; fp=fopen("ustawienia1.txt","w"); fprintf(fp,"%d", 1); fclose(fp); done=true; } else if(licznik==1) { // if(!sample) al_destroy_sample(sample); FILE *fp; fp=fopen("ustawienia1.txt","w"); fprintf(fp,"%d", 0); fclose(fp); done=true; } } else if(al_key_down(&keyState, ALLEGRO_KEY_UP)) { licznik--; if(licznik<0)licznik=2; draw=true; } else if(al_key_down(&keyState, ALLEGRO_KEY_DOWN)) { licznik++; if(licznik>2)licznik=0; draw=true; } if(draw) { draw=false; al_draw_bitmap (bitmapa, 0, 0, 0); if(licznik==0) { al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_rest(0.15); } else if(licznik==1) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_rest(0.15); } else if(licznik==2) { al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y,ALLEGRO_ALIGN_CENTRE,"on"); al_draw_textf(font_ttf,al_map_rgb(255,255,255), x, y+100,ALLEGRO_ALIGN_CENTRE,"off"); al_draw_textf(font_ttfbolt,al_map_rgb(255,255,255), x, y+200,ALLEGRO_ALIGN_CENTRE,"cofnij"); al_rest(0.15); } al_flip_display(); } } al_destroy_bitmap(bitmapa); al_destroy_event_queue(event_queue1); al_destroy_font(font_ttf); al_destroy_font(font_ttfbolt); }
MainLoop::~MainLoop() { al_destroy_sample(win); al_destroy_sample(alert); };
void SampleResource::destroy(void) { al_destroy_sample(sample_data); sample_data = 0; }