UNIT VTScreens;

INTERFACE

USES VTWins, VTCfg, VTPartitura,
     SongUnit,
     SwapManager,
     Output43, Vid43;


{
CONST
  SegTopLine      : PAoW = NIL;
  Seg2ndLine      : PAoW = NIL;
  SegSamples      : PAoW = NIL;
  SegOscillosc    : PAoW = NIL;
  SegMenus        : PAoW = NIL;
  SegSmallPart    : PAoW = NIL;
  SegBigPart      : PAoW = NIL;
  SegCredits      : PAoW = NIL;
}
VAR
  TopLineHandle   : TSwapHandle;
  _2ndLineHandle  : TSwapHandle;
  SamplesHandle   : TSwapHandle;
  OscilloscHandle : TSwapHandle;
  MenusHandle     : TSwapHandle;
  SmallPartHandle : TSwapHandle;
  BigPartHandle   : TSwapHandle;
  CreditsHandle   : TSwapHandle;

CONST
  wfTopLine   = $0001;
  wf2ndLine   = $0002;
  wfSamples   = $0004;
  wfOscillosc = $0008;
  wfMenus     = $0010;
  wfSmallPart = $0020;
  wfBigPart   = $0040;
  wfCredits   = $0080;

  ActiveWindows : WORD = 0;

  CreditWins          = wfTopLine + wfCredits;
  BigWins             = wfTopLine + wfBigPart;
  Small_SamplesWins   = wfTopLine + wf2ndLine + wfSamples   + wfMenus + wfSmallPart;
  Small_OscilloscWins = wfTopLine + wf2ndLine + wfOscillosc + wfMenus + wfSmallPart;




PROCEDURE InitVTScreens(VAR Song: TSong);
PROCEDURE RefreshVTScreens;

PROCEDURE SetCredits;
PROCEDURE SetBig;
PROCEDURE SetSmall_Samples;
PROCEDURE SetSmall_Oscillosc;
PROCEDURE SetNothing;
PROCEDURE SetUser(w: WORD);




IMPLEMENTATION

USES SongElements;


VAR
  VirtualScreen : ARRAY[1..80*43] OF WORD;
  TempBuffer    : ARRAY[1..80*43] OF WORD;

CONST
  NoDots : BOOLEAN = FALSE;



PROCEDURE PutDot;
  BEGIN
    IF NOT NoDots THEN
      Write('o');
  END;
  


PROCEDURE MySaveW(VAR Handle: TSwapHandle; x, y, w, h: WORD);
  BEGIN
    StoreWindow(@TempBuffer, x, y, w, h);
    IF NOT NoDots THEN
      Handle.Init;
    Handle.Write(TempBuffer, SavedWindowSize(@TempBuffer));
  END;


PROCEDURE InitVTScreens(VAR Song: TSong);
  VAR
    SaveSeg : WORD;
    SaveOfs : WORD;
  BEGIN
                                                                          PutDot;
    SaveSeg := ScrSegment;
    SaveOfs := ScrOffset;
    ScrSegment := SEG(VirtualScreen); 
    ScrOffset  := OFS(VirtualScreen);

    InitWinScreen;                                                        PutDot;
    InitWinTopLine;                                                       PutDot;
    InitWin2ndLine;                                                       PutDot;
    InitWinSamples;                                                       PutDot;
    RefreshModuleInfo(Song);                                              PutDot;
    WITH wTopLine   DO MySaveW(TopLineHandle,      x,   y, w,   h-1);     PutDot;
    WITH wSamples   DO MySaveW(SamplesHandle,      x,   y, w-1, h-1);     PutDot;

    IF NOT NoDots THEN
      BEGIN

        InitWinMenus;                                                     PutDot;
        InitWinPart;                                                      PutDot;

        WITH w2ndLine   DO MySaveW(_2ndLineHandle,     x,   y, w,   h-1); PutDot;
        WITH wMenus     DO MySaveW(MenusHandle,        x,   y, w,   h-1); PutDot;
        WITH wPartSmall DO MySaveW(SmallPartHandle,    x,   y, w,   h  ); PutDot;

        InitWinOscillosc;                                                 PutDot;
        InitWinPart;                                                      PutDot;
        WITH wOscillosc DO MySaveW(OscilloscHandle,    x,   y, w,   h-1); PutDot;

        InitWinBigPart;                                                   PutDot;
        WITH wPartBig   DO MySaveW(BigPartHandle,      x,   y, w,   h  ); PutDot;

        InitWinCredits;                                                   PutDot;
        WITH wCredits   DO MySaveW(CreditsHandle,      x,   y, w,   h  ); PutDot;

      END;

    ScrSegment := SaveSeg;
    ScrOffset  := SaveOfs;

    NoDots := TRUE;

  END;


(*
PROCEDURE InitVTScreens;
  VAR
    SaveSeg : WORD;
    SaveOfs : WORD;
  BEGIN
                                                                      PutDot;
    SaveSeg := ScrSegment;
    SaveOfs := ScrOffset;
    ScrSegment := SEG(VirtualScreen); 
    ScrOffset  := OFS(VirtualScreen);

    InitWinScreen;                                                    PutDot;
    InitWinTopLine;                                                   PutDot;
    InitWin2ndLine;                                                   PutDot;
    InitWinSamples;                                                   PutDot;
    RefreshModuleInfo;                                                PutDot;
    WITH wTopLine   DO SaveWindow(SegTopLine,      x,   y, w,   h-1); PutDot;
    WITH wSamples   DO SaveWindow(SegSamples,      x,   y, w-1, h-1); PutDot;

    IF SegOscillosc = NIL THEN
      BEGIN

        InitWinMenus;                                                     PutDot;
        InitWinPart;                                                      PutDot;

        WITH w2ndLine   DO SaveWindow(Seg2ndLine,      x,   y, w,   h-1); PutDot;
        WITH wMenus     DO SaveWindow(SegMenus,        x,   y, w,   h-1); PutDot;
        WITH wPartSmall DO SaveWindow(SegSmallPart,    x,   y, w,   h  ); PutDot;

        InitWinOscillosc;                                                 PutDot;
        InitWinPart;                                                      PutDot;
        WITH wOscillosc DO SaveWindow(SegOscillosc,    x,   y, w,   h-1); PutDot;

        InitWinBigPart;                                                   PutDot;
        WITH wPartBig   DO SaveWindow(SegBigPart,      x,   y, w,   h  ); PutDot;

        InitWinCredits;                                                   PutDot;
        WITH wCredits   DO SaveWindow(SegCredits,      x,   y, w,   h  ); PutDot;

      END;

    ScrSegment := SaveSeg;
    ScrOffset  := SaveOfs;

    NoDots := TRUE;

  END;
*)

PROCEDURE DoDeactivate(VAR w: TWindow);
  BEGIN
    w.vis  := FALSE;
    w.forz := FALSE;
    w.act  := FALSE;
  END;


PROCEDURE DoActivate(VAR w: TWindow; VAR Handle: TSwapHandle);
  BEGIN
    Handle.Read(TempBuffer, SizeOf(TempBuffer));
    RestoreWindow(@TempBuffer);
    w.vis  := TRUE;
    w.forz := TRUE;
    w.act  := TRUE;
  END;


{
PROCEDURE DoActivate(VAR w: TWindow; SavePtr: PAoW);
  BEGIN
    RestoreWindow(SavePtr);
    w.vis  := TRUE;
    w.forz := TRUE;
    w.act  := TRUE;
  END;
}

PROCEDURE DeactivateWins(NewWins: WORD);
  VAR
    f : WORD;
  BEGIN
    f := (NOT NewWins) AND ActiveWindows;
    IF (f AND wfTopLine)   <> 0 THEN DoDeactivate(wTopLine);
    IF (f AND wf2ndLine)   <> 0 THEN DoDeactivate(w2ndLine);
    IF (f AND wfSamples)   <> 0 THEN DoDeactivate(wSamples);
    IF (f AND wfOscillosc) <> 0 THEN DoDeactivate(wOscillosc);
    IF (f AND wfMenus)     <> 0 THEN DoDeactivate(wMenus);
    IF (f AND wfSmallPart) <> 0 THEN DoDeactivate(wPartSmall);
    IF (f AND wfBigPart)   <> 0 THEN DoDeactivate(wPartBig);
    IF (f AND wfCredits)   <> 0 THEN DoDeactivate(wCredits);

    ActiveWindows := ActiveWindows AND NewWins;
  END;


PROCEDURE ActivateWins(NewWins: WORD);
  VAR
    f : WORD;
  BEGIN
    f := NewWins AND (NOT ActiveWindows) ;
    IF (f AND wfTopLine)   <> 0 THEN   DoActivate(wTopLine,   TopLineHandle);
    IF (f AND wf2ndLine)   <> 0 THEN   DoActivate(w2ndLine,   _2ndLineHandle);
    IF (f AND wfSamples)   <> 0 THEN   DoActivate(wSamples,   SamplesHandle);
    IF (f AND wfOscillosc) <> 0 THEN   DoActivate(wOscillosc, OscilloscHandle);
    IF (f AND wfMenus)     <> 0 THEN   DoActivate(wMenus,     MenusHandle);
    IF (f AND wfSmallPart) <> 0 THEN BEGIN
                                       DoActivate(wPartSmall, SmallPartHandle);
                                       SetSmallPartWindow;
                                     END;
    IF (f AND wfBigPart)   <> 0 THEN BEGIN
                                       DoActivate(wPartBig,   BigPartHandle);
                                       SetBigPartWindow;
                                     END;
    IF (f AND wfCredits)   <> 0 THEN   DoActivate(wCredits,   CreditsHandle);

    ActiveWindows := ActiveWindows OR NewWins;
  END;


PROCEDURE SetCredits;
  BEGIN
    DeactivateWins(CreditWins);
    ActivateWins(CreditWins);
  END;


PROCEDURE SetBig;
  BEGIN
    DeactivateWins(BigWins);
    ActivateWins(BigWins);
  END;


PROCEDURE SetSmall_Samples;
  BEGIN
    DeactivateWins(Small_SamplesWins);
    ActivateWins(Small_SamplesWins);
  END;


PROCEDURE SetSmall_Oscillosc;
  BEGIN
    DeactivateWins(Small_OscilloscWins);
    ActivateWins(Small_OscilloscWins);
  END;


PROCEDURE SetNothing;
  BEGIN
    DeactivateWins(0);
    ActivateWins(0);
  END;


PROCEDURE SetUser(w: WORD);
  BEGIN
    DeactivateWins(w);
    ActivateWins(w);
  END;


PROCEDURE RefreshVTScreens;
  VAR
    i : WORD;
  BEGIN
    i := ActiveWindows;
    SetUser(0);
    SetUser(i);
  END;




END.
