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

Author Topic: No sound inside custom class  (Read 3959 times)

0 Members and 1 Guest are viewing this topic.

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
No sound inside custom class
« on: March 18, 2012, 10:52:59 pm »
I have some problem with my weapon class to fire when it's costructed.
This is what i made in Weapon constructor:
Code: [Select]
Weapon::Weapon(std::string n, const int rate, const int speed):bullets("data/bullets/"+n+"bullet.png")
{
name=n;
fireRate=rate;
bullets.setSpeed(speed);
buffer.loadFromFile("data/sounds/weapons/"+n+".wav"); //SF BUFFER
weaponSound.setBuffer(buffer); // SF SOUND
weaponSound.play();
}

If i made this in my main:
Code: [Select]
Weapon test("M60",80,1500);
It plays the sound well!
Now, i tested a little weapon vector initializer, so this is what i made:
Code: [Select]
bool loadWeaponList(std::vector<Weapon> &weaponVector){
std::string tempName;
int tempRate, tempSpeed;
std::ifstream weaponFile("data/weapons/weaponlist.txt", std::ios::in);
if (!weaponFile){
std::cerr << "Weapon List File Error!"<<std::endl;
return false;
}
while(!weaponFile.eof()){
weaponFile >> tempName >> tempRate >> tempSpeed;
weaponVector.push_back(Weapon(tempName,tempRate,tempSpeed));
}
return true;
}

The function wroks for bullets and fire, but seems sound doesn't want to start! What can be the error?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No sound inside custom class
« Reply #1 on: March 19, 2012, 08:16:18 am »
When using a vector, elements are moved in memory so that the vector can grow. Therefore, the pointers that the sounds had on their sound buffer are invalid.
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
No sound inside custom class
« Reply #2 on: March 19, 2012, 11:37:31 am »
Quote from: "Laurent"
When using a vector, elements are moved in memory so that the vector can grow. Therefore, the pointers that the sounds had on their sound buffer are invalid.

So what can be a solution to my problem? Use pointers?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No sound inside custom class
« Reply #3 on: March 19, 2012, 11:41:29 am »
First, you must decide whether your Weapon class should have value semantics, or entity semantics. In other words, if it is copyable or not.

Then:
- if you decide that it must be copyable, implement the copy constructor and assignment operator correctly, and your problem will be solved
- if you decide that it should not be copyable because it makes no sense to copy a weapon, explicitely disable the copy constructor and assignment operator, and use either pointers or a container that doesn't store copy of elements; that will also solve your problem
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
No sound inside custom class
« Reply #4 on: March 19, 2012, 12:46:32 pm »
Quote from: "Laurent"
First, you must decide whether your Weapon class should have value semantics, or entity semantics. In other words, if it is copyable or not.

Then:
- if you decide that it must be copyable, implement the copy constructor and assignment operator correctly, and your problem will be solved
- if you decide that it should not be copyable because it makes no sense to copy a weapon, explicitely disable the copy constructor and assignment operator, and use either pointers or a container that doesn't store copy of elements; that will also solve your problem


Uhm i've a Hero class that can equip a Weapon, and i used something like this:
Code: [Select]
void Hero::equipWeapon(Weapon newWeapon){
heroWeapon=newWeapon;
}

How can i manage this code without copy constructor and assignment operator? :O

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
No sound inside custom class
« Reply #5 on: March 19, 2012, 01:08:32 pm »
By using references/pointers.
Laurent Gomila - SFML developer

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
No sound inside custom class
« Reply #6 on: March 19, 2012, 05:48:11 pm »
I was thinking, is ti a good idea to use pointers? Nexus some month ago gives me an advice: "Don't use pointers!" xD
I'm trying to make it non-copyable putting Copy constructor and Assignement  operator in private without implementation but i can't compile it :(
For test i've created copy constructor and assignement operator like this:
Code: [Select]

Weapon::Weapon(const Weapon & copyWeapon):bullets(copyWeapon.bullets){
name=copyWeapon.name;
fireRate=copyWeapon.fireRate;
fireOrigin=copyWeapon.fireOrigin;
buffer.loadFromFile("data/sounds/weapons/"+name+".wav");
weaponSound.setBuffer(buffer);
}

Weapon & Weapon::operator=(const Weapon & rightWeapon){
name=rightWeapon.name;
fireRate=rightWeapon.fireRate;
fireOrigin=rightWeapon.fireOrigin;
buffer.loadFromFile("data/sounds/weapons/"+name+".wav");
weaponSound.setBuffer(buffer);
bullets=rightWeapon.bullets;
return *this;
}


Now when i fire the first time it load the right .wav, but when i switch weapon i get this OpenAL error:
"An internal OpenAL call failed in SoundBuffer.cpp (253): AN_INVALID_VALUE, a numeric value is out of range"

This is the code in line 253:
Code: [Select]
alCheck(alBufferData(m_buffer, format, &m_samples[0], size, sampleRate));
EDIT: i was thinking, if i use sf::Music instead of sf::Sound?

TheEnigmist

  • Full Member
  • ***
  • Posts: 119
    • View Profile
No sound inside custom class
« Reply #7 on: March 19, 2012, 08:44:03 pm »
Modified:
sf::Sound -> sf::Music!
Now all works fine!
Ah i've some problem when i close my application for sf::music/sf::sound!
Quote
Eccezione non gestita a 0x7680a4b9 in bug.exe: 0xC0000005: Violazione di accesso nella lettura del percorso 0xfeeefeee.

Code: [Select]
void __cdecl __crtExitProcess (
        int status
        )
{
        __crtCorExitProcess(status);

        /*
         * Either mscoree.dll isn't loaded,
         * or CorExitProcess isn't exported from mscoree.dll,
         * or CorExitProcess returned (should never happen).
         * Just call ExitProcess.
         */

        ExitProcess(status);
-> }