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

Author Topic: Trouble with a load music function  (Read 4466 times)

0 Members and 1 Guest are viewing this topic.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Trouble with a load music function
« on: September 13, 2015, 01:39:45 am »
I'm having some trouble in my class of loading music.
I'm trying to make a function so I can load in Music simply by calling a function and tossing in some perameters, here is what I have so far:
Load.h
#pragma onc
#include <string>
class Load
{
public:
        void LoadMusic(sf::Music MusicLoaded, std::string FileName);
};
 
Load.cpp
#include <SFML/Audio.hpp>
#include "Load.h"
#include <string>
#include <iostream>
void LoadMusic(sf::Music MusicLoaded, std::string FileName){
        if (!MusicLoaded.openFromFile(FileName)){
                std::cout << "ERROR could not load " << FileName << std::endl;
        }
        else{
                std::cout << "Loaded" << FileName << std::endl;
        }
}
 
main()
//unimportant junk
sf::Music MenuMusic;
Load LoadOBJ;
        LoadOBJ.LoadMusic(MenuMusic, "Resources/MenuMusic");
//unimportant junk
 
I tried to show as little code as possible, also this is only my second time ever working with classes.
errors:
 \include\sfml\audio\inputsoundfile.hpp(203): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
sfml-2.3.1\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
sfml-2.3.1\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::InputSoundFile::InputSoundFile(const sf::InputSoundFile &)'
sfml-2.3.1\include\sfml\system\mutex.hpp(89): error C2248: 'sf::NonCopyable::NonCopyable' : cannot access private member declared in class 'sf::NonCopyable'
sfml-2.3.1\include\sfml\system\noncopyable.hpp(67) : see declaration of 'sf::NonCopyable::NonCopyable'
sfml-2.3.1\include\sfml\system\noncopyable.hpp(42) : see declaration of 'sf::NonCopyable'
1>          This diagnostic occurred in the compiler generated function 'sf::Mutex::Mutex(const sf::Mutex &)'

(I removed My Computer Name, and the name of the project from the error's as it is unimportant)
Thx in advance Y'all!!!

Nyrox

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Trouble with a load music function
« Reply #1 on: September 13, 2015, 01:55:26 am »
Ignoring the design in general for a second, thing number 1 would be you are passing the sf::Music object as a copy, where you really should as a reference. Also I really wouldn't use a class for that, you can instead just create a namespace with a static method.

namespace foo { // Insert a nifty name, util or something
    static void loadFromFile(sf::Music& file, std::string filepath){
        //.... load file and throw errors etc.
    }
}
 


GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Trouble with a load music function
« Reply #2 on: September 13, 2015, 02:00:56 am »
1, thank you for the quick reply, what do you mean make a namespace? I have never heard of doing such a thing, and I wouldn't know how to implement it. and also how would i reference sf music? with a pointer&?

Nyrox

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Trouble with a load music function
« Reply #3 on: September 13, 2015, 02:06:07 am »
Bit much too explain really. :P
I recommend you read up on scope's, pointer's and references and namespaces (namespaces really aren't a big topic, but an incredibly useful one for that.

As for now, that code will work for creating a namespace, you would just access it in the following way:

namespace foo {
  static void loadMusicFromFile(sf::Music& music, std::string filepath){
     // load music
  }
};
sf::Music menu_music;
foo::loadMusicFromFile(menu_music, "./assets/menuMusic.vav");

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Trouble with a load music function
« Reply #4 on: September 13, 2015, 02:08:18 am »
ok, thx and where would I put the code to make the namespace? just in the main()? the reason I ask is because I want a really clean main function.

Nyrox

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Trouble with a load music function
« Reply #5 on: September 13, 2015, 02:09:46 am »
You would put it /somewhere/ outside of any methods, for example in a header file or above your main method.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Trouble with a load music function
« Reply #6 on: September 13, 2015, 02:13:51 am »
ok, thx again, and I'm assuming by method you mean function?
1 more thing, I planned on doing this with sf Textures, and sf sounds, would it work more or less the same way?
« Last Edit: September 13, 2015, 02:19:18 am by GAMINGBACON97 »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Trouble with a load music function
« Reply #7 on: September 13, 2015, 04:17:48 am »
This may be inspirational as well: https://github.com/SFML/SFML/wiki/Source:-Jukebox

 

anything