libsaria: Added callback framework
I have created generic functions for registering and triggering callbacks. New callbacks can be added by creating a new constant in the callback_t enum.
This commit is contained in:
parent
5dea1ae42a
commit
2888e741e8
|
@ -0,0 +1,9 @@
|
|||
#ifndef LIBSARIA_CALLBACKS_H
|
||||
#define LIBSARIA_CALLBACKS_H
|
||||
|
||||
enum callback_t {
|
||||
PLAY,
|
||||
PAUSE,
|
||||
};
|
||||
|
||||
#endif /* LIBSARIA_CALLBACKS_H */
|
|
@ -2,18 +2,21 @@
|
|||
#define LIBSARIA_H
|
||||
|
||||
#include <libsaria/audio.h>
|
||||
#include <libsaria/callback.h>
|
||||
#include <libsaria/print.h>
|
||||
|
||||
class Libsaria
|
||||
{
|
||||
private:
|
||||
Audio audio;
|
||||
void trigger_callback(callback_t);
|
||||
|
||||
public:
|
||||
Libsaria(int, char**);
|
||||
~Libsaria();
|
||||
|
||||
void load(string);
|
||||
void register_callback(callback_t, void (*)());
|
||||
|
||||
/* Control functions */
|
||||
void play();
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
|
||||
#include <libsaria/libsaria.h>
|
||||
#include <libsaria/callback.h>
|
||||
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
static map<callback_t, void (*)()> callbacks;
|
||||
|
||||
void Libsaria::trigger_callback(callback_t type)
|
||||
{
|
||||
map<callback_t, void (*)()>::iterator it;
|
||||
it=callbacks.find(type);
|
||||
if (it != callbacks.end())
|
||||
it->second();
|
||||
}
|
||||
|
||||
void Libsaria::register_callback(callback_t type, void (* func)())
|
||||
{
|
||||
callbacks[type] = func;
|
||||
}
|
|
@ -4,12 +4,14 @@
|
|||
|
||||
void Libsaria::play()
|
||||
{
|
||||
audio.play();
|
||||
if(audio.play())
|
||||
trigger_callback(PLAY);
|
||||
}
|
||||
|
||||
void Libsaria::pause()
|
||||
{
|
||||
audio.pause();
|
||||
if(audio.pause())
|
||||
trigger_callback(PAUSE);
|
||||
}
|
||||
|
||||
void Libsaria::stop()
|
||||
|
|
|
@ -15,5 +15,5 @@ Libsaria::~Libsaria()
|
|||
void Libsaria::load(string filepath)
|
||||
{
|
||||
audio.load(filepath);
|
||||
audio.play();
|
||||
play();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue