This is my first time posting, and I appreciate you taking the time to read and potentially help me find a solution to my question.
I am trying to figure out a way to do a loop and load all of the sound files in the directory where I am storing them using a key value array, instead of hard coding the sound files as I am currently (example below). I am hoping this will reduce the number of lines of code I have, and if I ever decided to add more sounds I could just drop a new sound in the directory.
I hope I am communicating clear enough and i'm not breaking any cplusplus.com rules with the formatting of my post. Once again thank you all for taking the time to look at my post and possibly helping! Also, I would be happy to hear any and all feedback about my code, I am only about 2 months into learning C++ so I basically have little to no idea what I am doing. Any opportunity to learn will be met with and open mind and heart.
Thanks!
[code]
#include <windows.h>
//-----------------------------------------------------------------------------
// Engine Includes
//-----------------------------------------------------------------------------
#include "..\Engine\Engine.h"
//-----------------------------------------------------------------------------
// Test State Class
//-----------------------------------------------------------------------------
class TestState : public State
{
public:
virtual void Load()
{
m_sound = new Sound("./Assets/Sound.wav");
m_sound1 = new Sound("./Sounds/note1.wav");
m_sound2 = new Sound("./Sounds/note2.wav");
m_sound3 = new Sound("./Sounds/note3.wav");
m_sound4 = new Sound("./Sounds/note4.wav");
m_sound5 = new Sound("./Sounds/note5.wav");
m_sound6 = new Sound("./Sounds/note6.wav");
m_sound7 = new Sound("./Sounds/note7.wav");
}
using KeyType = /*...*/;
std::unordered_map<KeyType, Sound> sounds;
virtualvoid Load()
{
std::ifstream sounds_file(sound.cfg);
std::string line;
while(std::getline(sounds_file, line)) {
KeyType key;
std::string path;
//Parse string somehow (there are many libraries for parsing config files. Ini parser for example)
//key will contain code for your key, and path — path to file
//http://en.cppreference.com/w/cpp/container/unordered_map/emplace
sounds.emplace(key, path); //construct a sound directly in map
}
}
if(!recording)
{
std::vector<KeyType> keys;
//Fill vector with all keys pressed currently
for(constauto& key: keys)
if(sounds.find(key) != sounds.end()) //If key has corresponding sound
sounds[key].Play(); //Play it
}