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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - PizzaParty

Pages: [1]
1
Audio / sf::Music::openFromFile always return 0
« on: July 19, 2013, 12:22:50 am »
Here is the code, taken from the Udemy online course

//Libraries
#include <SFML/Graphics.hpp>
#include <iostream>
#include <SFML/Audio.hpp>

//Glboal variables, functions, classes

//C++ program entry point
int main()
{
        //Creating the window
        sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Game");

        //Settign the framerate limit to 60 FPS
        window.setFramerateLimit(60);

        window.setKeyRepeatEnabled(false);

        //Variable that keeps the game loop running
        bool play = true;

        //Event object holding all events
        sf::Event event;

        //States for button/events
        bool aPressed = false;
        bool aReleased = false;
        bool space = false;
        bool leftClick = false;

        //Variables
        int numberOfClicks = 0; //For counting the number of clicks
        int mouseX = 0, mouseY = 0;             //For storing the mouse position
        int rectXPosition = 0;  //Rectangles X position

        //Images
        sf::Texture image1; //Image/texture object
        if (image1.loadFromFile("Data/image2.png") == 0) //Load an image, if not close the program
        {
                return 1;
        }

        //Render shapes
        //Rectangle shape
        sf::RectangleShape rect;
        rect.setSize(sf::Vector2f(100, 100)); //Width and height
        rect.setPosition(0, 0); //Position
        rect.setFillColor(sf::Color::White); //White fill color so we don't change the images colors
        rect.setTexture(&image1); //Bind an image/texture to the rectangle

        //Circle shape
        sf::CircleShape circle;
        circle.setRadius(50); //Radius
        circle.setPosition(400,300); //Position
        circle.setFillColor(sf::Color::Blue); //Color

        //Font
        sf::Font font;  //Font object
        if (font.loadFromFile("Data/arial.ttf") == 0) //Safe way to load font
        {
                return 1;
        }

        //Text
        sf::Text title; //Text object
        title.setFont(font); //Which font will the text use
        title.setCharacterSize(30); //Character size
        title.setString("Hello World"); //Text/string to display
        title.setPosition(300, 50); //Position of the text
        title.setColor(sf::Color::Magenta); //Color of the text

        //Sounds
        sf::SoundBuffer ExplosionBuffer; //Sound buffor for the sound file
        if (ExplosionBuffer.loadFromFile("Data/explosion.wav") == 0) //Load the sound
        {
                return 1;
        }

        sf::Sound explosion; //Sound object
        explosion.setBuffer(ExplosionBuffer); //Bind the buffer to the object

        explosion.play(); //Play the sound

        //Music
        sf::Music drumLoop; //Music object
        if (drumLoop.openFromFile("Data/music.ogg") == 0) //Load the music file
        {
                return 1;
        }
       
        drumLoop.setLoop(true); //Loop the music file
        drumLoop.play(); //Play the music

        //Game loop
        while (play == true)
        {
                //EVENTS
                while (window.pollEvent(event))
                {
                        //Event type is key pressed and the key is 'A'
                        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A)
                        {
                                //Set the state to true
                                aPressed = true;
                        }

                        //Event type is key released and the key is 'A'
                        if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::A)
                        {
                                aReleased = true;
                        }

                        //Space key pressed
                        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
                        {
                                space = true;
                        }

                        //Space key released
                        if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
                        {
                                space = false;
                        }

                        //Left mouse button pressed down (clicked)
                        if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
                        {
                                leftClick = true;
                        }

                        //Mouse moved in the window
                        if (event.type == sf::Event::MouseMoved)
                        {
                                mouseX = event.mouseMove.x; //X position of the mouse
                                mouseY = event.mouseMove.y; //Y position of the mouse
                        }

                        //Event type is window closed
                        if (event.type == sf::Event::Closed)
                        {
                                //Set play to false in order to stop the game loop
                                play = false;
                        }
                }

                //LOGIC
                if (aPressed == true)
                {
                        //Print to the console
                        std::cout << "The A key has been pressed\n";

                        aPressed = false;
                }

                if (aReleased == true)
                {
                        std::cout << "The A key has been released\n";

                        aReleased = false;
                }

                //If the left mouse button was clicked
                if (leftClick == true)
                {
                        numberOfClicks++; //numberOfClicks = numberOfClicks + 1;

                        std::cout << "Number of clicks is " << numberOfClicks << "\n";

                        leftClick = false;
                }

                //Print out the position of the mouse.
                std::cout << "Mouse x: " << mouseX << " mouse y: " << mouseY << "\n";

                //Basic movement
                rectXPosition++; //X position variable of the rectangle
                rect.setPosition(rectXPosition, rectXPosition); //We set the rectangles X and Y position to the variable rectXPosition

                //RENDERING
                window.clear();

                window.draw(rect); //Draw the rectangle shape
                window.draw(circle); //Draw the circle shape
                window.draw(title); //Draw the text

                window.display();
        }
        ///////////

        //Clean up and close the window
        window.close();

        //Close the program
        return 0;
}

Now the problem is here :

        sf::Music drumLoop; //Music object
        if (drumLoop.openFromFile("Data/music.ogg") == 0) //Load the music file
        {
                return 1;
        }

I tried many different sound files, many different sound files sizes and made sure that the location and file name were correct. No matter what, the openFromFile function always returns 0, ending the program. Loading sounds is fine.

Pages: [1]