I'm having a problem with a function I made and I was wondering if anyone could help me figure out the problem.I have a file called playsound.h,and a .cpp file for that. When the function runs,a sound plays as wanted but it causes a problem. I'm not sure if it causes a lag or a line skip,or a frame skip or what.All I know is that it causes image skips and it causes the program to get its variables all messed up so I assume it has something to do with skipping some code. So the question is" Why does it do this?".
playsound.h
#pragma once
#include <SFML\Graphics.hpp>
#include <SFML/Audio.hpp>
using namespace sf;
#include <string>
using namespace std;
extern Sound channel[50];
extern SoundBuffer noise[50];
extern string lastsound[50];
void playsound(int channelnumber,string soundDirectory);
playsound.cpp
#include <SFML\Audio.hpp>
using namespace sf;
#include "playsound.h"
#include <string>
using namespace std;
Sound channel[50];
SoundBuffer noise[50];
string lastsound[50];
void playsound(int channelnumber,string soundDirectory)
{
if(soundDirectory!=lastsound[channelnumber])
{
noise[channelnumber].loadFromFile(soundDirectory);
channel[channelnumber].setBuffer(noise[channelnumber]);
if(channel[channelnumber].getStatus()!=Sound::Playing)
{
channel[channelnumber].play();
lastsound[channelnumber]=soundDirectory;
}
}
if(channel[channelnumber].getStatus()!=Sound::Playing)
{
lastsound[channelnumber]="";
}
}
I'm thinkin it gets the variables messed up because it causes large variable changing sections of code to be skipped.I tried making it local but that didn't work.I did however find out that moving that calling function fixes it.It would be good though to know why this changes anything.
So I moved it directly past the function it was skipping the rest of.
bool click(int left,int top,int width,int height)
{
if(Mouse::isButtonPressed(Mouse::Left))
{
if(IntRect(left,top,width,height).contains(MPosition.x,MPosition.y))
{
if(released)
{
released=false;
//playsound() was here
return true;
}
}
else
{
if(released)
{
}
}
released=false;
}
else
{
released=true;
return false;
}
}
void loop()
{
MPosition=window.mapPixelToCoords(Mouse::getPosition(window));
if(room==1)
{
if(leave)
tback.loadFromFile("1.png");
leave=false;
if(click(501,256,104,54))//click function
{
playsound(0,"room.wav");//moved to here
room=2;
leave=true;
}
}
}