player: implement io-callbacks for loading tracks

Some demos use datafiles; let's allow these to easily work by
adding a callback-interface for loading tracks.

The callbacks are only present in player-mode, because tracks
aren't loaded from disk in client-mode.
This commit is contained in:
Erik Faye-Lund 2011-04-25 23:43:23 +02:00
parent 65c5c62e89
commit 217bed5131
3 changed files with 31 additions and 7 deletions

View File

@ -81,6 +81,16 @@ static SOCKET server_connect(const char *host, unsigned short nport)
closesocket(sock);
return INVALID_SOCKET;
}
#else
void sync_set_io_cb(struct sync_device *d, struct sync_io_cb *cb)
{
d->io_cb.open = cb->open;
d->io_cb.read = cb->read;
d->io_cb.close = cb->close;
}
#endif
struct sync_device *sync_create_device(const char *base)
@ -101,6 +111,10 @@ struct sync_device *sync_create_device(const char *base)
#ifndef SYNC_PLAYER
d->row = -1;
d->sock = INVALID_SOCKET;
#else
d->io_cb.open = fopen;
d->io_cb.read = fread;
d->io_cb.close = fclose;
#endif
return d;
@ -125,11 +139,11 @@ void sync_destroy_device(struct sync_device *d)
static int get_track_data(struct sync_device *d, struct sync_track *t)
{
int i;
FILE *fp = fopen(sync_track_path(d->base, t->name), "rb");
void *fp = d->io_cb.open(sync_track_path(d->base, t->name), "rb");
if (!fp)
return -1;
fread(&t->num_keys, sizeof(size_t), 1, fp);
d->io_cb.read(&t->num_keys, sizeof(size_t), 1, fp);
t->keys = malloc(sizeof(struct track_key) * t->num_keys);
if (!t->keys)
return -1;
@ -137,13 +151,13 @@ static int get_track_data(struct sync_device *d, struct sync_track *t)
for (i = 0; i < (int)t->num_keys; ++i) {
struct track_key *key = t->keys + i;
char type;
fread(&key->row, sizeof(size_t), 1, fp);
fread(&key->value, sizeof(float), 1, fp);
fread(&type, sizeof(char), 1, fp);
d->io_cb.read(&key->row, sizeof(size_t), 1, fp);
d->io_cb.read(&key->value, sizeof(float), 1, fp);
d->io_cb.read(&type, sizeof(char), 1, fp);
key->type = (enum key_type)type;
}
fclose(fp);
d->io_cb.close(fp);
return 0;
}

View File

@ -6,6 +6,7 @@
#define SYNC_DEVICE_H
#include "data.h"
#include "sync.h"
struct sync_device {
char *base;
@ -14,6 +15,8 @@ struct sync_device {
#ifndef SYNC_PLAYER
int row;
SOCKET sock;
#else
struct sync_io_cb io_cb;
#endif
};

View File

@ -25,7 +25,14 @@ struct sync_cb {
int sync_connect(struct sync_device *, const char *, unsigned short);
int sync_update(struct sync_device *, int, struct sync_cb *, void *);
void sync_save_tracks(const struct sync_device *);
#endif /* !defined(SYNC_PLAYER) */
#else /* defined(SYNC_PLAYER) */
struct sync_io_cb {
void *(*open)(const char *filename, const char *mode);
size_t (*read)(void *ptr, size_t size, size_t nitems, void *stream);
int (*close)(void *stream);
};
void sync_set_io_cb(struct sync_device *d, struct sync_io_cb *cb);
#endif /* defined(SYNC_PLAYER) */
const struct sync_track *sync_get_track(struct sync_device *, const char *);
float sync_get_val(const struct sync_track *, double);