/*
** ICE BREAKER (CONSOLE VERSION) V1.0
** ----------------------------------
** Author:    Paul Manias
** Copyright: DreamWorld Productions 1997.
** Compile:   sc IceBreaker.c nolink
**            PhxAss /IceAssembler.asm
**            slink FROM lib:c.o,IceBreaker.o,/IceAssembler.o TO IceBreakerCon LIBRARY LIB:sc.lib,LIB:Amiga.lib
**
** Doc:       This is the entry code for IceBreaker, it sets things up
**            so that the assembler code is ready to intercept debug
**            messages.  This particular version outputs to a console
**            window - preferably KCON: but CON: is accepted as well.
**
*/

#include <proto/games.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <dos/dos.h>

extern struct ExecBase *SysBase;
extern struct GFXBase *GFXBase;

#define LVODEBUGMESSAGE -378
#define LVOERRORMESSAGE -384
#define LVOSTEPBACK     -390

extern ULONG (libDebugMessage)();
extern ULONG (libErrorMessage)();
extern ULONG (libStepBack)();

struct GMSBase *GMSBase;
struct Task *maintask;
struct Task *edittask = NULL;

BPTR conhandle=0;      /* Handle to output debug information */

/***************************** INITIALISATION CODE *********************************/

void mainroutine(void);
long getlen(char string[]);
void wtext(char string[]);
BPTR openconsole(void);

void main(void)
{
 void *oldDebugMessage;
 void *oldErrorMessage;
 void *oldStepBack;

 maintask = FindTask(0);

 if (GMSBase = (struct GMSBase *) OpenLibrary("GMS:libs/dpkernal.library",0)) {
  if (DebugActive() == ERR_OK) {
   if (GFXBase = (struct GFXBase *) OpenLibrary("graphics.library",0)) {
    if (conhandle = openconsole()) {

       Forbid();
       oldDebugMessage = SetFunction((struct Library *)GMSBase,LVODEBUGMESSAGE,&libDebugMessage);
       oldErrorMessage = SetFunction((struct Library *)GMSBase,LVOERRORMESSAGE,&libErrorMessage);
       oldStepBack     = SetFunction((struct Library *)GMSBase,LVOSTEPBACK,&libStepBack);
       SumLibrary((struct Library *)GMSBase);
       Permit();

       mainroutine();

       Forbid();
       SetFunction((struct Library *)GMSBase,LVODEBUGMESSAGE,oldDebugMessage);
       SetFunction((struct Library *)GMSBase,LVOERRORMESSAGE,oldErrorMessage);
       SetFunction((struct Library *)GMSBase,LVOSTEPBACK,oldStepBack);
       SumLibrary((struct Library *)GMSBase);
       Permit();

    Close(conhandle);
    }
   CloseLibrary((struct Library *)GFXBase);
   }
  DebugInactive();
  }
 CloseLibrary((struct Library *)GMSBase);
 }
}

/*********************************** MAIN LOOP *************************************/

void mainroutine(void)
{
  LONG result;

  wtext("Welcome to IceBreaker V1.0, the debugger for GMS programs.\n");
  wtext("This program will wait until a program using GMS is executed.  Any\n");
  wtext("debug messages that are sent while it is active will be intercepted\n");
  wtext("and printed out to this window.\n\n");
  wtext("Press CTRL-C at any time to exit.\n\n");
  wtext("Type                 | Data\n");
  wtext("---------------------+---------------------------------------------\n");

  do {
     result = Wait(SIGBREAKF_CTRL_C);
  } while (!(result & SIGBREAKF_CTRL_C));

}

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

void wtext(char string[]) {
  Write(conhandle,(ULONG)string,getlen(string));
}

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

long getlen(char string[]) {
  int length=0;

  while (string[length] != 0) {
    length++;
    if (length > 256) break;
  }
  return(length);
}

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

BPTR openconsole(void) {
  BPTR handle;

  if (handle = Open("KCON:0/20/640/200/Function Output/SCREEN IceBreaker",MODE_OLDFILE)) {
     return(handle);
  }
  else
     return(Open("CON:0/20/640/200/Function Output/SCREEN IceBreaker",MODE_OLDFILE));
}

