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.


Messages - GreenyUk

Pages: [1]
1
Audio / Re: sf::Sound stopping playing on function return
« on: October 02, 2015, 02:40:48 pm »
After more playing it seems wrapping sf::Sound or sf::SoundBuffer in a unique_ptr is what's causing this behaviour.

I don't know enough to know what's happening behind the scenes here, but using raw pointers instead works just fine. I presume it's something to do with the scope a unique_ptr enforces?

Anywho, raw pointers on both my sf::Sound and sf::SoundBuffer fixed this issue. I now just need to make sure they get deleted!

2
Audio / sf::Sound stopping playing on function return
« on: October 02, 2015, 02:27:50 pm »
Hi,

Having some problems with playing a sound. I have a small audio component that handles the playing of sounds:

AudioComponent.h

#pragma once

#include "Component.h"
#include <SFML\Audio.hpp>
#include <memory>

class AudioComponent : public Component
{
public:

        /**
         * Default Constructor.
         */

        AudioComponent();

        /**
         * Sets the sound buffer.
         * @param buffer An existing buffer wrapped in a unique pointer.
         * @return True if successfully.
         */

        bool SetSoundBuffer(std::unique_ptr<sf::SoundBuffer> buffer);

        /**
        * Sets the sound buffer.
        * @param filePath The location of the sound file to load.
        * @return True if successfully.
        */

        bool SetSoundBuffer(std::string filePath);

        /**
         * Plays the sound that's currently set.
         */

        void Play();

private:
        /**
         * The components sound buffer.
         */

        std::unique_ptr<sf::SoundBuffer> buffer;

        /**
         * The main sound object.
         */

        sf::Sound sound;
};
 

AudioComponent.cpp

#include "AudioComponent.h"

/** Default Constructor. */
AudioComponent::AudioComponent()
{
        this->buffer = std::make_unique<sf::SoundBuffer>();
}

/** Sets the sound buffer. */
bool AudioComponent::SetSoundBuffer(std::unique_ptr<sf::SoundBuffer> buffer)
{
        this->buffer = std::move(buffer);
        this->sound.setBuffer(*this->buffer);

        return true;
}

/** Sets the sound buffer. */
bool AudioComponent::SetSoundBuffer(std::string filePath)
{
        if (this->buffer->loadFromFile(filePath))
        {
                this->sound.setBuffer(*this->buffer);
                return true;
        }
        else
        {
                return false;
        }
}

/** Plays the sound that's currently set. */
void AudioComponent::Play()
{
        this->sound.play();
}
 

This code doesn't work, and no sound is played. After some playing I've found it's returning from the Play() function that is stopping the sounds. It took me a while to look there as I understand sf::Sound and sf::Music both create their own threads. If I hang around in the Play() function by appending a while statement at the end:

/** Plays the sound that's currently set. */
void AudioComponent::Play()
{
        this->sound.play();

        while(true){};
}
 

As I understand it, a common reason for this is that the sound buffer goes out of scope and gets destroyed, but it's a member variable wrapped in a std::unique_ptr in this case.

Can anybody shed any light here? There must be something I'm missing as to why it's stopping when the function returns.

3
General / Re: What's causing this integer overflow?
« on: September 01, 2015, 01:49:44 pm »
Ahh, okay! Thank you both for the explanations.

And indeed Hapax! My bad. :)

4
General / What's causing this integer overflow?
« on: August 31, 2015, 01:00:22 am »
I have the following code:

this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE)) / 2;

When window->getSize().y - (GRID_HEIGHT * TILE_SIZE) < 0, it overflows.

this->position.y = -60 / 2;                                                  // reaches correct answer of -30
this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE));        // reaches correct answer of -60
this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE)) / 2;    // overflows

Either I'm missing something crazy obvious, or there's something weird going on here. How can line 1 work fine, but line 3 overflows when it's essentially the same equation?

If I replace all values with their actual values it works fine:

this->position.y = (window->getSize().y - (GRID_HEIGHT * TILE_SIZE)) / 2;   // overflow. result is 2147483618
this->position.y = (1080 - (19 * 60)) / 2;                                  // exact same values as the above line, just hard-coded, and results in -15

Any help would be greatly appretiated, I'm very confused right now! :)

5
Graphics / Re: Memory problem with std::vector<sf::Sprite>
« on: July 05, 2015, 10:28:26 pm »
Ahh, beat my edit.

Cheers, Kitteh-warrior. :)

6
Graphics / Re: Memory problem with std::vector<sf::Sprite>
« on: July 05, 2015, 10:25:29 pm »
Ahh, so each time I add to a vector it moves the entire thing to a new memory location?

Is this standard practice for C++? I would have thought I'd have run into this before?!


Never-mind, I read the C++ reference and found it is C++ practice. Many thanks for the quick reply! Learned something new about vectors. :)

7
Graphics / Memory problem with std::vector<sf::Sprite>
« on: July 05, 2015, 10:17:38 pm »
Hi all,

First post, new to SFML. Something is puzzling me about texture/sprite management and setup.

I'm managing my textures and sprites by loading all textures into a std::vector<sf::Texture>, and all sprites into a std::vector<sf::Sprite>.

The following code doesn't work:

1       this->tileTexures.push_back(sf::Texture());
2       this->tileTexures[0].loadFromFile("../resources/spr_tile_1.png");
3
4       this->tileSprites.push_back(sf::Sprite());
5       this->tileSprites[0].setTexture(this->tileTexures[0]);
6
7       this->tileTexures.push_back(sf::Texture());
8       this->tileTexures[1].loadFromFile("../resources/spr_tile_2.png");
9
10      this->tileSprites.push_back(sf::Sprite());
11      this->tileSprites[1].setTexture(this->tileTexures[1]);
 

When the code above runs, this->tileSprites[1] draws correctly, but this->tileSprites[0] has no texture.

I've followed this through with the debugger and observed the following:

Up to line 5 everything is fine. Once I call line 7, and call .push_back on the texture vector, the texture property of tileSprites[0] gets all messed up. Why is this?

If i change the order of this code however it works fine. The following code works a treat:

1       this->tileTexures.push_back(sf::Texture());
2       this->tileTexures.push_back(sf::Texture());
3
4       this->tileTexures[0].loadFromFile("../resources/spr_tile_1.png");
5       this->tileTexures[1].loadFromFile("../resources/spr_tile_2.png");
6
7       this->tileSprites.push_back(sf::Sprite());
8       this->tileSprites.push_back(sf::Sprite());
9
10      this->tileSprites[0].setTexture(this->tileTexures[0]);
11      this->tileSprites[1].setTexture(this->tileTexures[1]);
 

Same code, different order.

What's going on here? I've never run into this issue before where the order of my interactions with vectors seems to be altering memory somewhere.

Hopefully I've explained well enough, and thanks in advance for any input.

Pages: [1]