#ifndef SE_OPENAL_H
#define SE_OPENAL_H

#include <iostream>
#include <AL/al.h>
#include <AL/alc.h>
#include <set>

#include "se_iface.h"

#include <mpg123.h>

using namespace std;

namespace se
{
	#define MAX_MUSIC 16
	#define MAX_SOUND 64
	#define BUFFER_SIZE 32768

	class ZSoundEngineOpenAL;

	class ZSoundOpenAL : public XSound
	{
		friend class ZSoundEngineOpenAL;
		ALuint _source;
	protected:
		ZSoundOpenAL(ALuint source);
		~ZSoundOpenAL();
	public:
		void kill();
		void set_volume(float volume);
	};

	class ZSoundEngineOpenAL : public XSoundEngine
	{
		ALCdevice * _device;
		ALCcontext * _context;

		ALuint _buffer[MAX_SOUND];
		set< ZSoundOpenAL * > _sounds;
		set< int > _cur_frame_sounds;

		string _path[MAX_MUSIC];
		FILE * _m_f;
		int _cur_music;
		float _cur_volume;

		ALuint _music_source;
		ALuint _music_buffer[2];
		int _cur_buf;

		mpg123_handle * _mpg_handle;
		unsigned char _in_buf[BUFFER_SIZE];
		unsigned char _out_buf[BUFFER_SIZE];
		int _mpg_state;
		int decode_part( int _buf );
	public:
		ZSoundEngineOpenAL();
		~ZSoundEngineOpenAL();

		bool	load_music(int num, const char * path);
		bool	load_sound(int num, const char * path);

		void	run_music(int num=-1, float volume=1.f, bool sudo=false);
		int		get_cur_music();
		void	set_volume(float volume);
		XSound * play_sound(int num, float volume=1.f, bool loop=false);
		void	stop_all_sounds();
		void	check_time();
	};
};

#endif
