Where to place sound files

Pages: 12
I'm trying to add a .wma sound file to my project. I can't figure out which folder to place it in. I figured it would be the same one for my textures and pictures but it's not working.

I know it has to go in the project folder.

Visual studios 2013 sfml 2.2
If your textures are being loaded from file just fine, it should be finding the audio files without problems as well.

SFML supports the audio formats that libsndfile supports, .wma might not be a supported format.
Edit: http://www.mega-nerd.com/libsndfile/
It doesn't look like it lists WMA as an available format.

Here's a code quote I found
1
2
3
/// Here is a complete list of all the supported audio formats:
/// ogg, wav, flac, aiff, au, raw, paf, svx, nist, voc, ircam,
/// w64, mat4, mat5 pvf, htk, sds, avr, sd2, caf, wve, mpc2k, rf64. 
Last edited on
Thanks Ganado. Doesn't make any sense... You would think a program to record sound that is MADE by microsoft would be usable...

Do you happen to know of any good programs to convert sound into a different format?
You would think a program to record sound that is MADE by microsoft would be usable...
?
SFML isn't made by Microsoft, and libsndfile probably doesn't support formats like WMA or MP3 because they're proprietary.

Anyway, no, sorry. Best I could do is say is look up a sound converter online.
Now I'm getting another issue. I converted the file but having this issue now:

1
2
3
4
5
6
7
8
9

		sf::SoundBuffer buffer;
		if (!buffer.loadFromFile("PunchSound1.wav")){
			sf::Sound sound;
			sound.setBuffer(buffer);
			if (POneFist){
				sound.play(sound);  // Issue is here (sound) is saying: Error: too many arguments in funcction call
			}
		}


Can someone help me with this? I followed the sfml tutorial to the T.
Well it says there's too many arguments.
Sound::play() shouldn't take any arguments, you have it taking in itself as an argument.
I think I'm losing it lol. Thanks Ganado.
1
2
3
4
5
6
7
		sf::SoundBuffer buffer;
		if (!buffer.loadFromFile("PunchSound1.wav"))
			sf::Sound sound;
			sound.setBuffer(buffer);
			if (POneFist){
				sound.play();
			}


No errors. It compiles. But getting a ugly long exception error now.
I haven't messed around with SFML's audio in a while, but the stuff inside your if statement will only be run if there's a problem loading PunchSound1.wav.

if (!buffer.loadFromFile("PunchSound1.wav"))
Is saying, in English, "if we could NOT correctly load the audio from file", then { do stuff }.

I would get rid of the !, that might fix it but I'm not positive.
1
2
3
4
5
6
if (!buffer.loadFromFile("PunchSound1.wav")) {
    std::cout << "Error, can't open file correctly" << std::endl;
}
else {
  // do whatever to play the sound.
}
Last edited on
if loadFromFile returns false, then it means the load failed. That means you shouldn't try to put it in a Sound.
Disch I'm no longer getting failed to load. I'm getting no errors now. But the sound is not playing.
play() is a nonblocking call. This means it will return immediately and your program will resume before the sound finishes playing (so your program can do other things without having to wait for the whole sound to play).

However the sound is tied to your sf::Sound object. And if your Sound object goes out of scope, then the playing sound is stopped.

1
2
3
4
5
6
7
8
9
if (buffer.loadFromFile("PunchSound1.wav"))
{
    sf::Sound sound;
    sound.setBuffer(buffer);
    if (POneFist)
    {
        sound.play();
    }
}  // <- 'sound' goes out of scope here 


If you have your code set up like this, then 'sound' will go out of scope immediately after you start playing it, which means SFML will stop and unload it before you ever hear anything.


You need to give your Sound object a wider scope.
Disch here is the code. I have spent about 2 and a half hours trying to get this sound to work. Can you please tell me what to do to fix this mess. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <iostream>
#include <Windows.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Network.hpp>

using namespace std;

void PlayerOneMove();
void PlayerTwoMove();
void FistSwingPOne();
void FistSwingPTwo();

bool SecondProg = true;
bool POneFist = false;
bool PTwoFist = false;

int run = 0;

sf::RectangleShape rectangle(sf::Vector2f(120, 50));
sf::RectangleShape rectangleTwo(sf::Vector2f(120, 50));

sf::Sound sound;

int main(){

	sf::RenderWindow window(sf::VideoMode(900, 900), "Fighting Game");

	sf::Thread P1Move(&PlayerOneMove);
	P1Move.launch();

	sf::Thread PunchiesOne(&FistSwingPOne);
	PunchiesOne.launch();

	sf::Thread P2Move(&PlayerTwoMove);
	P2Move.launch();

	sf::Thread PunchiesTwo(&FistSwingPTwo);
	PunchiesTwo.launch();



	while (window.isOpen()){
		sf::Event event;
		while (window.pollEvent(event))
			if (event.type == sf::Event::Closed){
				window.close();
				SecondProg = false;
			}

		window.clear(sf::Color::Cyan);

		sf::SoundBuffer buffer;
		if (!buffer.loadFromFile("PunchSound1.wav"))
			return -1;
		sf::Sound sound;
		sound.setBuffer(buffer);
		if (FistSwingPOne)
			sound.play();
			


			int x = rectangle.getPosition().x, y = rectangle.getPosition().y;
			int i = rectangleTwo.getPosition().x, p = rectangleTwo.getPosition().y;

			sf::Texture texture;
			if (!texture.loadFromFile("FightingFist1.png", sf::IntRect(0, 0, 67, 19)));
			sf::Sprite sprite;
			sprite.setTexture(texture);
			sprite.setPosition(95 + x, 50 + y);
			if (POneFist){
				window.draw(sprite);
			}

			sf::Texture textureTwo;
			if (!textureTwo.loadFromFile("FightingFist2.png", sf::IntRect(0, 0, 67, 19)));
			sf::Sprite spriteTwo;
			spriteTwo.setTexture(textureTwo);
			spriteTwo.setPosition(i - 60, p + 50);
			if (PTwoFist){
				window.draw(spriteTwo);
			}

			rectangleTwo.setSize(sf::Vector2f(100, 100));
			rectangleTwo.setFillColor(sf::Color::Blue);
			if (run == 1){
				rectangleTwo.setPosition(700, 650);
			}
			else{
				rectangleTwo.setPosition(i, p);
			}

			rectangle.setSize(sf::Vector2f(100, 100));
			rectangle.setFillColor(sf::Color::Black);
			if (run == 1){
				rectangle.setPosition(60, 650);
			}
			else{
				rectangle.setPosition(x, y);
			}
			window.draw(rectangleTwo);
			window.draw(rectangle);


			window.display();

			run++;
		}
	}

	void PlayerOneMove(){

		while (SecondProg){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
				rectangle.move(0, -5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
				rectangle.move(-5, 0);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
				rectangle.move(0, 5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
				rectangle.move(5, 0);
			}
			Sleep(10);
		}
	}

	void FistSwingPOne(){

		while (SecondProg){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::J)){
				POneFist = true;
				Sleep(500);
			}
			else{
				POneFist = false;
			}
			Sleep(10);
		}
	}

	void PlayerTwoMove(){

		while (SecondProg){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
				rectangleTwo.move(0, -5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
				rectangleTwo.move(-5, 0);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
				rectangleTwo.move(0, 5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
				rectangleTwo.move(5, 0);
			}
			Sleep(10);
		}
	}

	void FistSwingPTwo(){

		while (SecondProg){
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad0)){
				PTwoFist = true;
				Sleep(500);
			}
			else{
				PTwoFist = false;
			}
			Sleep(10);
		}
	
THREADS?!?

No, no, no. Get rid of those. You don't need them. They're almost certainly the cause of your problems.

Multithreading is a very complex topic and involves lots of extremely delicate synchronization issues that are very easy to get wrong. You don't appear to be doing any synchronization which is probably why your program isn't behaving how you'd expect.


You can drive everything from one thread. Typical behavior is:

1) Poll & process all pending events (pollEvent will tell you when there are no more events pending)

2) Update all game logic (ie, move the players, do collision checks, etc)

3) Update the screen

4) Repeat until the user exits.
I finally got the sound to play lol. I'm having issues with it being able to play while the game keeps going. I can only get it to work with a sleep. I will toy around with it some more and see what I can come up with.

I removed all the threads Disch and the program is running ALOT more smoothly. No lag and i'm getting less errors. Thanks!
Last edited on
Ok, i've put another 3 hours into trying to figure this out and no luck. Here's my new code:

I want the sound to play when the letter "j" is pressed. Which is the same key to throw a punch from player one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <Windows.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Network.hpp>

using namespace std;

bool POneFist = false;
bool PTwoFist = false;

int run = 0;

sf::RectangleShape rectangle(sf::Vector2f(120, 50));
sf::RectangleShape rectangleTwo(sf::Vector2f(120, 50));

int main(){

	sf::RenderWindow window(sf::VideoMode(900, 900), "Fighting Game");

	sf::SoundBuffer buffer;
	if (!buffer.loadFromFile("PunchSound1.wav")){
		return  -1;
		sf::Sound sound;
		sound.setBuffer(buffer);
		sound.play();
		Sleep(3000);
	}

	while (window.isOpen()){
		sf::Event event;
		while (window.pollEvent(event))
			if (event.type == sf::Event::Closed){
				window.close();
			}

		window.clear(sf::Color::Cyan);

			int x = rectangle.getPosition().x, y = rectangle.getPosition().y;
			int i = rectangleTwo.getPosition().x, p = rectangleTwo.getPosition().y;

			sf::Texture texture;
			if (!texture.loadFromFile("FightingFist1.png", sf::IntRect(0, 0, 67, 19)));
			sf::Sprite sprite;
			sprite.setTexture(texture);
			sprite.setPosition(95 + x, 50 + y);
			if (POneFist){
				window.draw(sprite);
			}

			sf::Texture textureTwo;
			if (!textureTwo.loadFromFile("FightingFist2.png", sf::IntRect(0, 0, 67, 19)));
			sf::Sprite spriteTwo;
			spriteTwo.setTexture(textureTwo);
			spriteTwo.setPosition(i - 60, p + 50);
			if (PTwoFist){
				window.draw(spriteTwo);
			}

			rectangleTwo.setSize(sf::Vector2f(100, 100));
			rectangleTwo.setFillColor(sf::Color::Blue);
			if (run == 1){
				rectangleTwo.setPosition(700, 650);
			}
			else{
				rectangleTwo.setPosition(i, p);
			}

			rectangle.setSize(sf::Vector2f(100, 100));
			rectangle.setFillColor(sf::Color::Black);
			if (run == 1){
				rectangle.setPosition(60, 650);
			}
			else{
				rectangle.setPosition(x, y);
			}
			window.draw(rectangleTwo);
			window.draw(rectangle);

			window.display();

			if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
				rectangle.move(0, -5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
				rectangle.move(-5, 0);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
				rectangle.move(0, 5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
				rectangle.move(5, 0);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::J)){
				POneFist = true;
				Sleep(10);
			}
			else{
				POneFist = false;
			}

			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
				rectangleTwo.move(0, -5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
				rectangleTwo.move(-5, 0);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
				rectangleTwo.move(0, 5);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
				rectangleTwo.move(5, 0);
			}
			if (sf::Keyboard::isKeyPressed(sf::Keyboard::Numpad0)){
				PTwoFist = true;
				Sleep(10);
			}
			else{
				PTwoFist = false;
			}
			Sleep(10);

			run++;
		}
	}
Side notes:

#1: Don't #include <Windows.h>. That is a non-portable header which defeats the point of using a portable lib like SFML.

#2: Don't use Sleep(). That is a non-portable WinAPI function. SFML offers a portable way to sleep (sf::sleep). Use that instead:

1
2
Sleep(100); // <- bad, nonportable
sf::sleep( sf::milliseconds(100) ); // <- good 


#3: You probably don't even need or want to sleep anyway. Set a framerate limit for your window and SFML will ensure the game runs at a fixed rate:
 
window.setFramerateLimit( 60 ); // <- ensures that your game runs no faster than 60 FPS 


Every time you sleep your program basically deadlocks for a brief period of time. You can't update enemy positions, you can't check input, you can't redraw the screen... you can't do anything while sleeping. You don't want to put sleeps all over your logic as it will make your program run slow/jerky and will make the timing unreliable.

#4: Go in your IDE and change your settings so that tabs are replaced with spaces. ;P I'm joking with this point, because it doesn't have any impact on anything other than personal preference. But seriously, tabs are stupid.


Your main problem:

If you want to play a sound, you need a Sound object. Nowhere in your code do you have an sf::Sound object.

The only thing you have is here:

1
2
3
4
5
6
7
8
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("PunchSound1.wav")){  // <- this block will only run if the load FAILS
    return  -1;                              // <- exit the program here
//  sf::Sound sound;            // <- this will never run, because you just exited the program
//  sound.setBuffer(buffer);    //     these lines do nothing and may as well not even exist
//  sound.play();
//  Sleep(3000);
}



So yeah. Step 1: Make an sf::Sound object.

This is easy:

1
2
sf::Sound sound;
sound.setBuffer(buffer);


Do that any time after the buffer is loaded.

Also, be sure that the object has a broad enough scope so that it doesn't die immediately (IE: don't put it inside an if block, or it will die when the if block ends). Putting it in main() will work for purposes of this small example... similar to how your 'window' object is defined.




Step 2: When the user presses J, play the sound:

1
2
3
4
5
6
if (sf::Keyboard::isKeyPressed(sf::Keyboard::J)){   // <- the user currently has the J button pressed
    if( !POneFist )     // <- if they just pressed it (IE:  it isn't being held down)
        sound.play();   // <- play the sound
    POneFist = true;
    //Sleep(10);   (get rid of these sleeps!)
}
Thanks Disch!

I was starting to lose my mind with this.

It kept saying sound is not defined yet I had sf::Sound sound;

Made me wanna throw my computer at a wall.

Another question for ya:

what does the return -1 do?

if (!buffer.loadFromFile("PunchSound1.wav"))

Anything in braces after an if statement means "do this if the if failed"? I still don't understand what the return -1 would be doing in this case.
It kept saying sound is not defined yet I had sf::Sound sound;


You must not have. Either you didn't define it, or it was defined in a different scope.

Remember that an object only exists within whatever {curly braces} you define it in:

1
2
3
4
5
6
7
8
if( blah )
{
    int foo = 5;  // <- foo's scope starts here
}  // <- foo's scope ends here

// <- foo no longer exists here

cout << foo;  // <-- ERROR, foo is undeclared 



what does the return -1 do?


return is used to exit the function and return control to whatever function called it.

When inside 'main', exiting main will also exit the program. So return -1 inside of main means "close the program, and tell the OS we've exited with code -1".

The -1 is of little significance. Typically an exit code of 0 means the program ran successfully and any non-zero value means there was some kind of failure.

Also note that this "exit code" is only true for main. It's kind of a special case. Any other function can return anything it wants and the return value can mean anything you want it to.

Anything in braces after an if statement means "do this if the if failed"?


No. The body of an if block (ie: what's in the following braces) gets executed only if the condition (inside the parenthesis) is true.

1
2
3
4
if( foo )  // 'foo' is our condition
{
    // code in this block will be executed only if 'foo' is true.
}


loadFromFile returns a value depending on whether or not the load was successful.

It will return true if the load was successful.
It will return false if it failed.

By putting the function call inside an if statement, you are examining the return value. The exclamation point (!) is the "not" operator... and basically is the same as checking to see if the value is false:

1
2
3
4
5
// this:
if( !foo )

// is the same as this:
if( foo == false )


So if loadFromFile fails, it will return false... which the ! operator will turn into true. And since that means the condition for the if statement is true, the {body} for that if statement will be executed.
Last edited on
Thanks for everything Disch.

Here's one more for ya lol:

sf::Music music;
if (!music.openFromFile("Music1.wav"))
music.play();

Any reason that this isn't running my music?

I followed SFML's tutorial and yet again no results. I put these 3 lines under sf::renderWindow window...

I also added the .wav music file inside the project folder. It runs fine yet nothing plays.
Pages: 12