Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Advice for music in different locations...  (Read 2147 times)

0 Members and 1 Guest are viewing this topic.

JM1082

  • Newbie
  • *
  • Posts: 4
    • View Profile
Advice for music in different locations...
« on: March 29, 2012, 06:54:44 pm »
Hi all!

I want different music for my game locations but I can't come up with a clever way of stopping the music of the previous location before starting the next location's music...

So, my code so far plays the first song in location1 then plays both songs together when I move to location2!

Please see my code below:
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

using namespace std;

int main()
{
    sf::RenderWindow gameWindow( sf::VideoMode( 800, 600, 32 ), "Hello, World!" );

    sf::Image Image, Image2;
    if ( !Image.LoadFromFile( "DN1.png" ) )
        cout << "Picture load error..." << endl;

    sf::Sprite sprite1, sprite2;
    sprite1.SetImage( Image );

    if ( !Image2.LoadFromFile( "DN2.png" ) )
        cout << "Picture load error..." << endl;

    sprite2.SetImage( Image2 );

    sprite1.SetPosition( 200.f, 250.f );
    sprite2.SetPosition( 600.f, 250.f );

    sf::Music music1, music2;
    if ( !music1.OpenFromFile( "T1.ogg" ) )
        cout << "No music 1..." << endl;

    if ( !music2.OpenFromFile( "T2.ogg" ) )
        cout << "No music 2..." << endl;

    while ( gameWindow.IsOpened() )
    {
        sf::Event Event;
        while ( gameWindow.GetEvent( Event ) )
        {
            // Window closed
            if ( Event.Type == sf::Event::Closed )
                gameWindow.Close();

            // Escape key pressed
            if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Escape ) )
                gameWindow.Close();

            // Left key pressed
            if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Right ) )
            {
                gameWindow.Clear();
                music1.Play();
                gameWindow.Draw( sprite1 );
                gameWindow.Display();
            } // end if


            // Right key pressed
            if ( ( Event.Type == sf::Event::KeyPressed ) && ( Event.Key.Code == sf::Key::Left ) )
            {
                gameWindow.Clear();
                music2.Play();
                gameWindow.Draw( sprite2 );
                gameWindow.Display();
            } // end if
        } // end while
    } // end while
} // end main
 
« Last Edit: March 29, 2012, 07:01:08 pm by Laurent »

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
Re: Advice for music in different locations...
« Reply #1 on: March 29, 2012, 07:24:55 pm »
I think you want to stop the music playing before start playing the next music, don't you?
...
music1.Stop();
music2.Play();
...
...
music2.Stop();
music1.Play();
...

JM1082

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Advice for music in different locations...
« Reply #2 on: March 30, 2012, 02:33:07 pm »
That's correct, but the game isn't linear, so I could be coming at a location from any number of other locations!
I was hoping there might be a way to stop all music instances with one function, but maybe there is not...  :-(