Help with Key Value Array

Hello cplusplus.com-ians!

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");
}

-----------------

virtual void Close()
{
SAFE_DELETE(m_font);
SAFE_DELETE(m_fontbig);
SAFE_DELETE(m_sound);
SAFE_DELETE(m_sound1);
SAFE_DELETE(m_sound2);
SAFE_DELETE(m_sound3);
SAFE_DELETE(m_sound4);
SAFE_DELETE(m_sound5);
SAFE_DELETE(m_sound6);
SAFE_DELETE(m_sound7);
SAFE_DELETE(m_sound8);
}

------------------

if(!recording)
{
if( g_engine->GetInput()->GetKeyPress(DIK_1))
m_sound1->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_2))
m_sound2->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_3))
m_sound3->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_4))
m_sound4->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_5))
m_sound5->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_6))
m_sound6->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_7))
m_sound7->Play();
if( g_engine->GetInput()->GetKeyPress(DIK_8))
m_sound8->Play();
}

-----------------

private:
Sound *m_sound, *m_sound1, *m_sound2,*m_sound3, *m_sound4, *m_sound5, *m_sound6, *m_sound7, *m_sound8;
};
You might want to move filename asociations into external configuration file and load stuff into a map:
sound.cfg:

DIK_1 = ./Sounds/note1.wav
DIK_2 = ./Sounds/note2.wav
//...


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
using KeyType = /*...*/;

std::unordered_map<KeyType, Sound> sounds;
virtual void 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(const auto& key: keys)
        if(sounds.find(key) != sounds.end()) //If key has corresponding sound
            sounds[key].Play(); //Play it
}
See if your supports readdir() to read the contents of a directory. Be sure to filter out any subdirectories from the list. (like "." and "..").
Thank you MiiNiPaa and dhayden! Much appreciated!
Topic archived. No new replies allowed.