Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <SFML/Audio.hpp>
- #include <iostream>
- #define FPS 60
- using namespace std;
- // Usage: this.exe musicfilename soundfilename
- // Sample files to use: my.mixtape.moe/kpcaxi.wav
- // my.mixtape.moe/obopyb.wav
- void help()
- {
- cout << "Music Controls: \n";
- cout << " > M - play/pause \n";
- cout << " > R - restart \n";
- cout << " > W - increase volume \n";
- cout << " > S - decrease volume \n";
- cout << " > D - increase pitch \n";
- cout << " > A - decrease pitch \n";
- cout << "Sound Controls: \n";
- cout << " > Space - play \n";
- cout << " > Up - increase volume \n";
- cout << " > Down - decrease volume \n";
- cout << " > Right - increase pitch \n";
- cout << " > Left - decrease pitch \n";
- cout << "Press H for help." << endl;
- }
- int main(int argc, char **argv)
- {
- string musicFilename(argv[1]);
- string soundFilename(argv[2]);
- sf::Music music;
- if (!music.openFromFile(musicFilename))
- {
- cout << "Error loading music." << endl;
- return 0;
- }
- music.setLoop(true);
- float musicPitch = 1.f;
- float musicVol = 100.f;
- sf::SoundBuffer buffer;
- if (!buffer.loadFromFile(soundFilename))
- {
- cout << "Error loading sound." << endl;
- return 0;
- }
- sf::Sound sounds[32];
- for (int i = 0; i < 32; i++) { sounds[i].setBuffer(buffer); }
- int currentsound = 0;
- float soundPitch = 1.f;
- float soundVol = 100.f;
- //initialize window.
- sf::RenderWindow window( sf::VideoMode( 800, 600 ), "h" );
- window.setFramerateLimit( FPS );
- window.setKeyRepeatEnabled(false);
- help();
- while( window.isOpen() )
- {
- sf::Event event;
- while( window.pollEvent( event ) )
- {
- switch( event.type ) {
- case sf::Event::Closed:
- window.close();
- break;
- case sf::Event::KeyPressed:
- if( event.key.code == sf::Keyboard::Escape ) { window.close(); }
- // Main Controls
- // Help
- if( event.key.code == sf::Keyboard::H )
- {
- help();
- }
- // Music: Play/Pause
- if( event.key.code == sf::Keyboard::M )
- {
- if( music.getStatus() == sf::SoundSource::Status::Playing )
- {
- music.pause();
- cout << "Music paused." << endl;
- }
- else
- {
- music.play();
- cout << "Music playing." << endl;
- }
- }
- // Music: Reset
- if( event.key.code == sf::Keyboard::R )
- {
- music.stop();
- music.play();
- cout << "Music restarted." << endl;
- }
- // Music: Decrease Pitch
- if( event.key.code == sf::Keyboard::A )
- {
- if( musicPitch > 0.1f )
- {
- musicPitch -= 0.1f;
- music.setPitch(musicPitch);
- cout << "Music -Pitch. Now at " << musicPitch << endl;
- }
- else
- {
- musicPitch = 0.1f;
- cout << "Music at minimum pitch." << endl;
- }
- }
- // Music: Increase Pitch
- if( event.key.code == sf::Keyboard::D )
- {
- musicPitch += 0.1f;
- music.setPitch(musicPitch);
- cout << "Music +Pitch. Now at " << musicPitch << endl;
- }
- // Music: Decrease Volume
- if( event.key.code == sf::Keyboard::S )
- {
- if( musicVol > 0.f )
- {
- musicVol -= 2.5f;
- music.setVolume(musicVol);
- cout << "Music -Volume. Now at " << musicVol << endl;
- }
- else
- {
- musicVol = 0.f;
- music.setVolume(musicVol);
- cout << "Music at minimum volume." << endl;
- }
- }
- // Music: Increase Volume
- if( event.key.code == sf::Keyboard::W )
- {
- musicVol += 2.5f;
- if( musicVol > 100.f )
- {
- musicVol = 100.f;
- music.setVolume(musicVol);
- cout << "Music at maximum volume." << endl;
- }
- else
- {
- music.setVolume(musicVol);
- cout << "Music +Volume. Now at " << musicVol << endl;
- }
- }
- // Sound: Play
- if( event.key.code == sf::Keyboard::Space )
- {
- sounds[currentsound].setPitch(soundPitch);
- sounds[currentsound].setVolume(soundVol);
- cout << "Playing sound on track " << currentsound << endl;
- sounds[currentsound].play();
- if( currentsound >= 31 ) { currentsound = 0; }
- else { currentsound++; }
- }
- // Sound: Decrease Pitch
- if( event.key.code == sf::Keyboard::Left )
- {
- if( soundPitch > 0.1f )
- {
- soundPitch -= 0.1f;
- cout << "Sound -Pitch. Now at " << soundPitch << endl;
- }
- else
- {
- soundPitch = 0.1f;
- cout << "Sound at minimum pitch." << endl;
- }
- }
- // Sound: Increase Pitch
- if( event.key.code == sf::Keyboard::Right )
- {
- soundPitch += 0.1f;
- cout << "Sound +Pitch. Now at " << soundPitch << endl;
- }
- // Sound: Decrease Volume
- if( event.key.code == sf::Keyboard::Down )
- {
- if( soundVol > 0.f )
- {
- soundVol -= 2.5f;
- cout << "Sound -Volume. Now at " << soundVol << endl;
- }
- else
- {
- soundVol = 0.f;
- cout << "Sound at minimum volume." << endl;
- }
- }
- // Sound: Increase Volume
- if( event.key.code == sf::Keyboard::Up )
- {
- soundVol += 2.5f;
- if( soundVol > 100.f )
- {
- soundVol = 100.f;
- cout << "Sound at maximum volume." << endl;
- }
- else { cout << "Sound +Volume. Now at " << soundVol << endl; }
- }
- break;
- }
- }
- window.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement