static void event_handler(const ALLEGRO_EVENT * event) { int i; switch (event->type) { /* Was the X button on the window pressed? */ case ALLEGRO_EVENT_DISPLAY_CLOSE: exiting = true; break; /* Was a key pressed? */ case ALLEGRO_EVENT_KEY_CHAR: if (event->keyboard.keycode == ALLEGRO_KEY_LEFT) { double pos = al_get_audio_stream_position_secs(music_stream); pos -= 5.0; if (pos < 0.0) pos = 0.0; al_seek_audio_stream_secs(music_stream, pos); } else if (event->keyboard.keycode == ALLEGRO_KEY_RIGHT) { double pos = al_get_audio_stream_position_secs(music_stream); pos += 5.0; if (!al_seek_audio_stream_secs(music_stream, pos)) printf("seek error!\n"); } else if (event->keyboard.keycode == ALLEGRO_KEY_SPACE) { bool playing; playing = al_get_mixer_playing(al_get_default_mixer()); playing = !playing; al_set_mixer_playing(al_get_default_mixer(), playing); } else if (event->keyboard.keycode == ALLEGRO_KEY_ESCAPE) { exiting = true; } break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: mouse_button[event->mouse.button] = 1; maybe_fiddle_sliders(event->mouse.x, event->mouse.y); break; case ALLEGRO_EVENT_MOUSE_AXES: maybe_fiddle_sliders(event->mouse.x, event->mouse.y); break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: mouse_button[event->mouse.button] = 0; break; case ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY: for (i = 0; i < 16; i++) mouse_button[i] = 0; break; /* Is it time for the next timer tick? */ case ALLEGRO_EVENT_TIMER: logic(); render(); break; } }
bool GameLogic::LoadAssets() { Bitmap.Logo = al_load_bitmap("assets/images/daklutz.png"); Bitmap.Title = al_load_bitmap("assets/images/Bitmap.Title.png"); if(!Bitmap.Title || !Bitmap.Logo) { fprintf(stderr, "Failed to load image files!\n"); return false; } if(Debug) { fprintf(stderr, "Bitmaps loaded.\n"); } // actually load the font, I loaded the same font at different sizes. Font.Small = al_load_font("assets/fonts/Geo-Regular.ttf", 24, 0); Font.Medium = al_load_font("assets/fonts/Geo-Regular.ttf", 38, 0); Font.Big = al_load_font("assets/fonts/Geo-Regular.ttf", 70, 0); if(!Font.Small || !Font.Medium || !Font.Big) { fprintf(stderr, "Failed to load fonts!\n"); return false; } if (!al_reserve_samples(2)) { fprintf(stderr, "Could not reserve samples!\n"); return false; } if(Debug) { fprintf(stderr, "Samples reserved.\n"); } Sound.HitWall = al_load_sample("assets/sounds/wall_collision.ogg"); Sound.HitPaddle = al_load_sample("assets/sounds/paddle_collision.ogg"); Sound.BackgroundMusic = al_load_audio_stream("assets/sounds/Sound.BackgroundMusic-tech.ogg", 4, 2048); if (!Sound.HitWall || !Sound.HitPaddle || !Sound.BackgroundMusic) { fprintf(stderr, "Failed to load sound files!\n"); return false; } if(Debug) { fprintf(stderr, "Sound files loaded.\n"); } if(!al_attach_audio_stream_to_mixer(Sound.BackgroundMusic, Mixer) ) { fprintf(stderr, "Failed to attach stream to default Mixer!\n"); return 1; } if(Debug) { fprintf(stderr, "Stream attached to Mixer.\n"); } if(!al_set_mixer_playing(Mixer, true)) { fprintf(stderr, "Failed to play Mixer!\n"); } if(Debug) { fprintf(stderr, "Mixer playing.\n"); } if(!al_set_audio_stream_playmode(Sound.BackgroundMusic, ALLEGRO_PLAYMODE_LOOP)) { fprintf(stderr, "Failed to loop stream!\n"); } if(Debug) { fprintf(stderr, "Stream is set to loop.\n"); } if(!al_set_audio_stream_playing(Sound.BackgroundMusic, true)) { fprintf(stderr, "Failed to play stream!\n"); } if(Debug) { fprintf(stderr, "Stream playing.\n"); } return true; }