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

Author Topic: sf::soundbuffer plays no sound  (Read 3913 times)

0 Members and 1 Guest are viewing this topic.

Captain Lightning

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::soundbuffer plays no sound
« on: September 14, 2011, 10:34:18 pm »
sf::music works just fine for me, but for some reason playing a sound using sf::soundbuffer creates no response.

My code:
Code: [Select]

    // Init sounds
    sf::SoundBuffer hit_sb, step_sb;
    if (!hit_sb.LoadFromFile("audio/hit.wav") && step_sb.LoadFromFile("audio/step.wav"))
    {
        return EXIT_FAILURE;
    }
    sf::Sound step(step_sb);
    std::cout << "Duration: " << step_sb.GetDuration() << " Status: " << step.GetStatus();


It returns:
Code: [Select]
Duration: 0 Status: 0

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::soundbuffer plays no sound
« Reply #1 on: September 14, 2011, 10:41:35 pm »
Is this the real code? There should be another '!' in your condition, but then it means that step_sb fails to load, which would explain the zeros.
Laurent Gomila - SFML developer

Captain Lightning

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::soundbuffer plays no sound
« Reply #2 on: September 15, 2011, 12:02:04 am »
Oho! You're right, actually. I fixed the '!', but I still have the same issue. No sound plays :(

Grimshaw

  • Hero Member
  • *****
  • Posts: 631
  • Nephilim SDK
    • View Profile
sf::soundbuffer plays no sound
« Reply #3 on: September 15, 2011, 01:12:39 am »
Maybe you're creating those objects inside a function, so they are destroyed automaticly at the end of it?

Captain Lightning

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::soundbuffer plays no sound
« Reply #4 on: September 15, 2011, 01:29:38 am »
The entire source:
Code: [Select]


#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>

struct player
{
    sf::Image image_player;
    sf::Font font_visitor;
    sf::Sprite sprite;
    sf::String score_string;
    int score_int;
    std::stringstream ss;
    player();
    void update_score(int);
};

player::player()
{
    // Init player sprite and score string.
    image_player.Create(15, 100, sf::Color(255,255,255));
    font_visitor.LoadFromFile("visitor1.ttf", 72);
    sprite.SetImage(image_player);
    score_string.SetText("0");
    score_string.SetFont(font_visitor);
    score_string.SetSize(72);
    score_int = 0;
}

void player::update_score(int x)
{
    score_int += x;
    ss << score_int;
    score_string.SetText(ss.str());
    ss.str("");
}

bool fps_enabled = true, in_menu = true;

int main()
{
    // Create the main window
    sf::RenderWindow App(sf::VideoMode(1280, 800), "Monotone", sf::Style::Fullscreen);
    App.UseVerticalSync(true);

    // Load font
    sf::Font font_visitor;
    if (!font_visitor.LoadFromFile("visitor1.ttf", 16))
    {
        return EXIT_FAILURE;
    }

    // Create the players, set positions
    player p1, p2;
    p1.sprite.SetPosition(0, floor(App.GetHeight()/2));
    p2.sprite.SetPosition(floor(App.GetWidth()-15), floor(App.GetHeight())/2);
    p1.score_string.SetPosition(floor((App.GetWidth()/2)-100), 10);
    p2.score_string.SetPosition(floor((App.GetWidth()/2)+100), 10);

    // Create fps counter
    std::string framerate_string;
    sf::String fps(framerate_string, font_visitor, 16);
    fps.SetPosition(floor(App.GetWidth())-150,floor(App.GetHeight())-25);

    // Initiate clock
    sf::Clock Clock;
    Clock.Reset();

    // Init music
    sf::Music title_music;
    if (!title_music.OpenFromFile("audio/Feryl - Beakortolaris - Cerror.ogg"))
    {
        return EXIT_FAILURE;
    }

    // Init sounds
    sf::SoundBuffer hit_sb, step_sb;
    if (!hit_sb.LoadFromFile("audio/hit.wav") && !step_sb.LoadFromFile("audio/step.wav"))
    {
        return EXIT_FAILURE;
    }
    sf::Sound step(step_sb);

    // Create menu
    sf::Sprite logo_sprite;
    sf::Image logo_image;
    if (!logo_image.LoadFromFile("monotone.png"))
    {
        return EXIT_FAILURE;
    }
    logo_image.SetSmooth(false);
    logo_sprite.SetImage(logo_image);
    logo_sprite.SetPosition(App.GetWidth()/2-(logo_sprite.GetSize().x/2), (App.GetHeight()/10)*4);

    // Start the game loop
    while (App.IsOpened())
    {
        // Have fps counter update only every second, rather than several times a second
        float Time = Clock.GetElapsedTime();
        if (Time > 1 && fps_enabled)
        {
            // Framerate value
            float framerate_float = floor(1.f / App.GetFrameTime());
            std::stringstream ss;
            ss << framerate_float;
            framerate_string = ss.str();
            fps.SetText(framerate_string + " FPS");
            Clock.Reset();
        }

        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
            }

            // Escape key pressed : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            {
                App.Close();
            }

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F))
            {
                fps_enabled = !fps_enabled;
            }
        }

        // Create an input event handler for keyboard presses
        const sf::Input& Input = App.GetInput();
        bool a_down = Input.IsKeyDown(sf::Key::A);
        bool z_down = Input.IsKeyDown(sf::Key::Z);
        bool slash_down = Input.IsKeyDown(sf::Key::Slash);
        bool quote_down = Input.IsKeyDown(sf::Key::Quote);
        bool space_down = Input.IsKeyDown(sf::Key::Space);
        int mouse_x = Input.GetMouseX();
        int mouse_y = Input.GetMouseY();

        //Draw menu if that's where we are.
        if (in_menu)
        {
            if (!title_music.GetLoop())
            {
                title_music.SetLoop(true);
                title_music.Play();
            }
            App.Clear(sf::Color(255, 255, 255));
            App.Draw(logo_sprite);
            App.Display();
            if ((Input.IsKeyDown(sf::Key::Space) || (Input.IsMouseButtonDown(sf::Mouse::Left))) && in_menu)
            {
                step.Play();
                in_menu = false;
            }

        }
        else
        {
            // Player 1 controls

            if (space_down) //Meant to be used for testing.
            {
                std::cout << "Duration: " << step_sb.GetDuration() << "Status: " << step.GetStatus();
                p1.update_score(1);
                std::cout << "Space Pressed.\n";
                step.Play();
            }
            if (a_down)
            {
                if (p1.sprite.GetPosition().y != 0)
                {
                    p1.sprite.Move(0, -20);
                }
            }

            if (z_down)
            {
                if (p1.sprite.GetPosition().y != App.GetHeight()-p1.sprite.GetSize().y)
                {
                    p1.sprite.Move(0, 20);
                }
            }

            // Player 2 controls
            if (quote_down)
            {
                if (p2.sprite.GetPosition().y != 0)
                {
                    p2.sprite.Move(0, -20);
                }
            }

            if (slash_down)
            {
                if (p2.sprite.GetPosition().y != App.GetHeight()-p2.sprite.GetSize().y)
                {
                    p2.sprite.Move(0, 20);
                }
            }

            // Clear screen
            App.Clear();

            // Draw entities
            App.Draw(p1.score_string);
            App.Draw(p2.score_string);
            App.Draw(p1.sprite);
            App.Draw(p2.sprite);
            if (fps_enabled)
            {
                App.Draw(fps);
            }

            // Update the window
            App.Display();
        }
    }

    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::soundbuffer plays no sound
« Reply #5 on: September 15, 2011, 07:44:22 am »
Code: [Select]
if (!hit_sb.LoadFromFile("audio/hit.wav") && !step_sb.LoadFromFile("audio/step.wav"))
Still wrong, that should be a || not a && ;)

And is there any message in the standard output (console)?
Laurent Gomila - SFML developer

Captain Lightning

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::soundbuffer plays no sound
« Reply #6 on: September 16, 2011, 01:56:11 am »
Wow. That one change from '&&' to '||' fixed my issue. Thanks Laurent! :D

 

anything