summary refs log tree commit diff stats
path: root/src/mixer.h
blob: 010bb0961699c8bd652a6f35593460c25fb72602 (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef MIXER_H_6DF82000
#define MIXER_H_6DF82000

#include <SDL_mixer.h>
#include <memory>
#include <stdexcept>
#include <map>
#include <string>

class mix_error : public std::logic_error {
public:

  mix_error() : std::logic_error(Mix_GetError())
  {
  }
};

class mix_wrapper {
public:

  mix_wrapper()
  {
    if (Mix_Init(0) != 0) {
      mix_error ex;
      Mix_Quit();

    	throw ex;
    }

    if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 2, 1024) != 0) {
      mix_error ex;
      Mix_Quit();

    	throw ex;
    }
  }

  ~mix_wrapper()
  {
    Mix_CloseAudio();
    Mix_Quit();
  }
};

class chunk_deleter {
public:

  void operator()(Mix_Chunk* chunk) {
    Mix_FreeChunk(chunk);
  }
};

using chunk_ptr = std::unique_ptr<Mix_Chunk, chunk_deleter>;

// MUST create the Renderer first!
class Mixer {
public:

  void playSound(std::string filename);

private:

  mix_wrapper mix_;
  std::map<std::string, chunk_ptr> sounds_;
};

#endif /* end of include guard: MIXER_H_6DF82000 */