
/*
 * Play.c
 * ------
 * (c) 1996 Jarkko Vatjus-Anttila <quaid@kempele.fi>
 * 
 * This is an example program to show how easy it's to use the
 * bfbplaymaster.library to playback different module formats. Take a look
 * at these sources, for example. This program is capable to replay 11
 * different moduleformats and the length of the program is *only* two and
 * a half kilobytes. This is only possible with the assist of
 * bfbplaymaster.library!
 *
 * This program has been compiled and tested using GCC 2.7.2. No enforcer
 * or mungwall hits happened during the test period.
 * 
 */

#include "exec/types.h"
#include "dos/dos.h"
#include "utility/tagitem.h"
#include "own/bfbplay.h"
#include "own/bfbplaybase.h"
#include "own/bfbplay_protos.h"
#include "own/bfbplay_inline.h"

#define SIGBIT_CTRL_C 1<<12

/* Variables: */
struct Library *BFBPlayBase;
struct ModInfo *modinfo = NULL;
ULONG tags[] = {
	BFBTAG_SongName, 0,
	BFBTAG_ModInfo, NULL,	/* define modinfo as NULL, so that the master */
	TAG_DONE				/* library allocates the structure for us.	  */
};


/* prototypes for functions: */
void cleanup(STRPTR t1);
void printinfotexts(STRPTR t1);


/***************************************************************************/

main (int argc, char *args[])
	{
	int result;

	if (argc == 1)
		cleanup("Unable to open file.");

	/*
	 * Store the filenameptr into taglist and open the master library.
	 * 
	 */

	tags[1] = (ULONG)args[1];

	BFBPlayBase = (struct Library *)
		OpenLibrary("bfbplaymaster.library", 1);
	if (BFBPlayBase == NULL)
		cleanup("Unable to open bfbplaymaster.library");

	/*
	 *  Load the module file into memory and check out the error code.
	 * If a negative value is returned, then convert it into string and
	 * exit displaying it.
	 *
	 */

	result = (int)BFBLoadModule( (struct TagItem *)tags );
	if (result < 0)
		cleanup((STRPTR)BFBGetError(result));

	/*
	 * Fetch the pointer to modinfo structure. BFBLoadModule stores it into
	 * the taglist because we passed NULL as the BFBTAG_ModInfo argument.
	 *
	 */ 

	modinfo = (APTR)tags[3];

	result = (int)BFBPlayModule(modinfo);
	if (result < 0)
		cleanup((STRPTR)BFBGetError(result));

	/*
	 * Now the module should be playing. Print out the texts and wait for
	 * CTRL+C signal. (This is a CLI program). Is there easier way to play
	 * module files than this??
	 *
	 */

	printinfotexts(args[1]);
	Wait (SIGBIT_CTRL_C);				/* Wait for CTRL+C */

	BFBStopModule(modinfo);

	cleanup(NULL);
	}

/***************************************************************************/

void cleanup(STRPTR t1)
	{
	if (modinfo != NULL)
		BFBUnLoadModule(modinfo);

	if (BFBPlayBase != NULL)
		CloseLibrary(BFBPlayBase);

	if (t1 != NULL)
		printf("%s\n", t1);

	exit(0);
	}

/***************************************************************************/

void printinfotexts(STRPTR t1)
	{
	printf("Name:    ");
	if (modinfo->mi_modulename != NULL)
		{
		printf("%s\n", (STRPTR)modinfo->mi_modulename);
		}
	else
		{
		printf("%s\n", t1);
		}
	printf("Format:  %s\n", modinfo->mi_moduletypename);
	printf("Length:  %ld ($%lx -> $%lx)\n", modinfo->mi_bufferlen,
		modinfo->mi_bufferptr, modinfo->mi_bufferptr+modinfo->mi_bufferlen);

	if (modinfo->mi_samplebufferptr != NULL)
	printf("Samples: %ld ($%lx -> $%lx)\n", modinfo->mi_samplebufferlen,
		modinfo->mi_samplebufferptr, modinfo->mi_samplebufferptr+modinfo->mi_samplebufferlen);

	printf("Status:  Replaying. Press CTRL+C to quit\n");
	}

/***************************************************************************/
