/*
** B2M2C.c       : Interface für B2-MIDI-Playerroutinen für C
** Compiler      : SAS-C 5.10
** Date          : 21-Sep-1991
** Version       : 1.0
** Revision      : 1
**
** © 1991 by Wolfgang Küting  -  K·WARE - Hard- & Software
** Es gelten die gleichen Kopierbestimmungen wie für den original
** Assembler-Source von Jörg Schließer
*/

#include <exec/types.h>
#include <exec/libraries.h>
#include <exec/memory.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <dos.h>
#include <graphics/gfxmacros.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <libraries/dos.h>
#include <private/priv_defs.h>
#include "defs.h"
#include "B2Routines.h"

#include <proto/all.h>

extern struct Library *SysBase;

/*
**   FreeB2Score()  - Speicher für Score-File und Insts freigeben
**
**   Parameter:  SD          - Zeiger auf B2Data-Struktur
*/
void FreeB2Score(struct B2Data *SD)
{
  FREEB2SCORE(SD->ScoreData, SD->ScoreSize);
  FreeMem(SD,sizeof(SD));
}

/*
**   InitB2Score()  - Läd für ein B2-Midi-Score die benötigten
**                    Instrumente oder kopiert die Instrumente
**                    eines Full-I-Scores ins Chip-Ram
**
**   Parameter:  SD          - Zeiger auf B2Data-Struktur
**               InstDirName - Zeiger auf Name des Instrumentenverzeichn.
**   Rückgabe :  SD          - Zeiger auf ScoreData-Struktur oder
**                             0 wenn Fehler
**               B2_ERROR    - ist > 0 wenn Fehler!
*/
struct B2Data *InitB2Score(struct B2Data *SD, char *InstDirName)
{
  BPTR InstFL;
  B2Score ScoreRet;

  if((InstFL = Lock(InstDirName,ACCESS_READ)) != NULL)
  {
    INITB2SCORE(SD->ScoreData, SD->ScoreSize, InstFL);
    ScoreRet = (B2Score)getreg(REG_A0);
    B2_ERROR = (WORD)getreg(REG_D0);

    UnLock(InstFL);
  }
  return((ScoreRet == NULL)? NULL: (struct B2Data *)SD);
}

/*
**   FreeB2Insts()  - Speicher für Insts freigeben
**
**   Parameter:  SD          - Zeiger auf B2Data-Struktur
*/
void FreeB2Insts(struct B2Data *SD)
{
  FREEB2INSTS(SD->ScoreData, SD->ScoreSize);
}

/*
**   LoadB2Score()  - Läd ein B2-Midi-Scorefile und die
**                    benötigten Instrumente
**
**   Parameter:  SongName    - Zeiger auf Name des Songs
**               SongDirName - Zeiger auf Name des Songverzeichn.
**               InstDirName - Zeiger auf Name des Instrumentenverzeichn.
**   Rückgabe :  B2Data      - Zeiger auf B2Data-Struktur oder
**                             0 wenn Fehler
**               B2_ERROR    - ist > 0 wenn Fehler!
*/
struct B2Data *LoadB2Score(char *SongName, char *SongDirName, char *InstDirName)
{
  BPTR SongFL, InstFL;
  struct B2Data *SD;

  if((SD = (struct B2Data *)AllocMem(sizeof(struct B2Data),MEMF_CLEAR)))
  {
    if((SongFL = Lock(SongDirName,ACCESS_READ)) != NULL)
    {
      if((InstFL = Lock(InstDirName,ACCESS_READ)) != NULL)
      {
        LOADB2SCORE(SongName, SongFL, InstFL);
        SD->ScoreData = (B2Score)getreg(REG_A0);
        B2_ERROR      = (WORD)getreg(REG_D0);
        SD->ScoreSize = (LONG)getreg(REG_D1);

        UnLock(InstFL);
      }
      UnLock(SongFL);
    }
  }
  else
    B2_ERROR = 64; /* Kein Speicher! */

  return((struct B2Data *)SD);
}

/*
**   PlayB2Score()  - B2-Midi-Score abspielen
**
**   Parameter:  SD          - Zeiger auf B2Data-Struktur
**   Rückgabe :  B2_ERROR    - ist > 0 wenn Fehler!
*/
void PlayB2Score(struct B2Data *SD)
{
  PLAYB2SCORE(SD->ScoreData, SD->ScoreSize);
  B2_ERROR = (WORD)getreg(REG_D0);
}

/*
**   StopB2Score()  - B2-Midi-Score-Abspiel anhalten
*/
void StopB2Score(void)
{
  STOPB2SCORE();
}

