diff options
Diffstat (limited to 'opl/examples')
-rw-r--r-- | opl/examples/.gitignore | 5 | ||||
-rw-r--r-- | opl/examples/droplay.c | 94 |
2 files changed, 77 insertions, 22 deletions
diff --git a/opl/examples/.gitignore b/opl/examples/.gitignore new file mode 100644 index 00000000..49bb1af8 --- /dev/null +++ b/opl/examples/.gitignore @@ -0,0 +1,5 @@ +Makefile.in +Makefile +.deps +droplay + diff --git a/opl/examples/droplay.c b/opl/examples/droplay.c index 5f09fe11..89cf6862 100644 --- a/opl/examples/droplay.c +++ b/opl/examples/droplay.c @@ -116,44 +116,51 @@ void Shutdown(void) OPL_Shutdown(); } -void PlayFile(char *filename) +struct timer_data { - FILE *stream; - char buf[8]; - - stream = fopen(filename, "rb"); + int running; + FILE *fstream; +}; - if (fread(buf, 1, 8, stream) < 8) - { - fprintf(stderr, "failed to read raw OPL header\n"); - exit(-1); - } +void TimerCallback(void *data) +{ + struct timer_data *timer_data = data; + int delay; - if (strncmp(buf, HEADER_STRING, 8) != 0) + if (!timer_data->running) { - fprintf(stderr, "Raw OPL header not found\n"); - exit(-1); + return; } - fseek(stream, 28, SEEK_SET); + // Read data until we must make a delay. - while (!feof(stream)) + for (;;) { int reg, val; - reg = fgetc(stream); - val = fgetc(stream); + // End of file? - // Delay? + if (feof(timer_data->fstream)) + { + timer_data->running = 0; + return; + } + + reg = fgetc(timer_data->fstream); + val = fgetc(timer_data->fstream); + + // Register value of 0 or 1 indicates a delay. if (reg == 0x00) { - SDL_Delay(val); + delay = val; + break; } else if (reg == 0x01) { - val |= (fgetc(stream) << 8); - SDL_Delay(val); + val |= (fgetc(timer_data->fstream) << 8); + delay = val; + break; } else { @@ -161,7 +168,50 @@ void PlayFile(char *filename) } } - fclose(stream); + // Schedule the next timer callback. + + OPL_SetCallback(delay, TimerCallback, timer_data); +} + +void PlayFile(char *filename) +{ + struct timer_data timer_data; + int running; + char buf[8]; + + timer_data.fstream = fopen(filename, "rb"); + + if (fread(buf, 1, 8, timer_data.fstream) < 8) + { + fprintf(stderr, "failed to read raw OPL header\n"); + exit(-1); + } + + if (strncmp(buf, HEADER_STRING, 8) != 0) + { + fprintf(stderr, "Raw OPL header not found\n"); + exit(-1); + } + + fseek(timer_data.fstream, 28, SEEK_SET); + timer_data.running = 1; + + // Start callback loop sequence. + + OPL_SetCallback(0, TimerCallback, &timer_data); + + // Sleep until the playback finishes. + + do + { + OPL_Lock(); + running = timer_data.running; + OPL_Unlock(); + + SDL_Delay(100); + } while (running); + + fclose(timer_data.fstream); } int main(int argc, char *argv[]) |