Commit 693b18bc authored by Geequette's avatar Geequette

Complete Part_008, music and sound examples

parent 0155273c
/*!
* \file main.cpp
* \brief First example of SFML (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme (from official SFML tutorial)
* \brief Music and sound examples (http://www.lucidarme.me/?p=6127)
* \author Philippe Lucidarme
* \version 1.0
* \date 12/18/2016
* \date 12/20/2016
*/
// SFML libraries
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
// Wait for ms milliseconds
void delay_ms(int ms)
{
sf::Clock Timer;
while (Timer.getElapsedTime().asMilliseconds()<ms);
}
int main()
{
// _____________________________
// ::: load background music :::
sf::Music music;
if (!music.openFromFile("../music.ogg"))
{
std::cerr << "Error while loading background music file" << std::endl;
return -1;
}
// __________________
// ::: Load sound :::
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("../birds.wav"))
{
std::cerr << "Error while loading sound file" << std::endl;
return -1;
}
// ____________________
// ::: Play sounds :::
// Play music
std::cout << "Start background music" << std::endl;
music.play();
delay_ms(1500);
// Play wav sound over the music
std::cout << "Play wav sound" << std::endl;
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
delay_ms(1500);
// Create a new 200x200 pixels window with a title
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML tutorial : part 8");
// Increase pitch
std::cout << "Increase pitch" << std::endl;
music.setPitch(1.4);
delay_ms(1500);
// Create a green circle with a radius of 100. pixels
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
// Reset pitch
std::cout << "Reset pitch" << std::endl;
music.setPitch(1.);
delay_ms(1500);
// Main loop, while the window is open
while (window.isOpen())
// Modify volume
std::cout << "Fade out" << std::endl;
for (int vol=100;vol>5;vol--)
{
// Event loop
sf::Event event;
while (window.pollEvent(event))
{
// If close is requested by user, close the window
if (event.type == sf::Event::Closed) window.close();
}
// Display sequence : clear screen, draw circle and update display
window.clear();
window.draw(shape);
window.display();
music.setVolume(vol);
delay_ms(50);
}
// End of application
// Stop music
std::cout << "Stop music" << std::endl;
music.stop();
return 0;
}
......@@ -30,7 +30,9 @@ LIBS += -lsfml-system
SOURCES += main.cpp
# Fragment files
DISTFILES += example_001.frag
DISTFILES += example_001.frag \
birds.wav \
music.ogg
DISTFILES += example_002.frag
DISTFILES += example_003.frag
DISTFILES += example_004.frag
......
......@@ -4,4 +4,7 @@ SUBDIRS += part_001
SUBDIRS += part_002
SUBDIRS += part_003
SUBDIRS += part_004
SUBDIRS += part_005
SUBDIRS += part_006
SUBDIRS += part_007
SUBDIRS += part_008
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment