/***************************************************************************/
/*                                                                         */
/*	    screen.c  -  v. 1.0  - 1.7.88  -  Antonio Ferrillo             */
/*                                                                         */
/*           funzioni di gestione di schermo sotto Intuition               */
/*                                                                         */
/*           versione Aztec v. 3.4 Lattice-compatibile                     */
/*                                                                         */
/***************************************************************************/ 

  #include <functions.h> /* rimuovere questa linea se si usano 
                            i compilatore Lattice  v. 3.XX  */

  #include <exec/types.h>
  #include <graphics/display.h>
  #include <intuition/intuition.h>

  static struct  Screen  *a_screen[] = {NULL, NULL};

  static struct  NewScreen   NewScreen[2];

  static int actscr;


/*--------------------------------------------------------------------------
-  int
-  Screen_Open(sn,siden,te,he,dp,bp,mode,bplanes)
- 
-     FUNZIONE  : apertura di schermo : e' possibile aprire 2 schermi
-                 contemporaneamente                        
-
-     ARGOMENTI : int sn;      - (valore = 1 o 2)  numero dello
-                                schermo da aprire.
-
-                 char *siden; - stringa col nome dello schermo
-
-                 int te;      - distanza dal top del display 
-                   
-                 int he;      - altezza dello schermo
-
-                 int dp;      - (valore da 0 a 31 dipendente dal numero
-                                dei bit-planes) detail pen  
-                   
-                 int bp;      - (valore da 0 a 31 dipendente dal numero
-                                dei bit-planes) block pen 
-                   
-                   primi 8 colori approssimativi : 0 -  blu,   1 - bianco,
-                                       2 - nero,   3 - giallo, 4 - azzurro,
-                                       5 - rosso,  6 - verde,  7 - bianco                                    
-                  
-                 int mode;    - (valore da 1 a 4) 
-                                1 = bassa ris. non interlacciato
-                                2 = alta ris. non interlacciato
-                                3 = bassa ris. interlacciato
-                                4 = alta ris. interlacciato
-
-                 int bplanes; - (valore da 1 a 5) n. bit-planes da associare
-                                allo schermo          
-
-      RITORNO   : TRUE se l'apertura e' andata a buon fine
-                  FALSE se c'e' stato qualche problema 
-      
-      NOTE      : richiede precedente apertura di intuition.library.
-                  ricordarsi di chiudere lo schermo a fine
-                  lavoro mediante la Screen_Close.
-
----------------------------------------------------------------------------*/

  int 
  Screen_Open(sn, siden, te, he,dp, bp,mode,bplanes)
  int     sn;
  char    *siden;
  int     te;
  int     he;
  int     dp;
  int     bp;
  int     mode;
  int     bplanes;
  {
    if (sn < 1 || sn > 2) return(FALSE);
    if (mode < 1 || mode > 4) return(FALSE);
    if (dp < 0 || dp > 31) return(FALSE);
    if (bp < 0 || bp > 31) return(FALSE); 
    if ((mode <= 2) && (te + he) > 256) return(FALSE);
    if ((mode > 2) && (te + he) > 512) return(FALSE);
 
    if (a_screen[sn-1] != NULL) return(FALSE);
   
    sn -= 1;

    NewScreen[sn].LeftEdge     = 0;
    NewScreen[sn].TopEdge      = te;
    NewScreen[sn].Width        = (mode == 1 || mode == 3) ? 320 : 640;
    NewScreen[sn].Height       = he;
    NewScreen[sn].Depth        = bplanes;
    NewScreen[sn].DetailPen    = dp;
    NewScreen[sn].BlockPen     = bp;
    NewScreen[sn].ViewModes = NULL;
    if (mode == 2) NewScreen[sn].ViewModes = HIRES;
    if (mode == 3) NewScreen[sn].ViewModes = INTERLACE;
    if (mode == 4) NewScreen[sn].ViewModes = HIRES | INTERLACE;
    NewScreen[sn].Type         = CUSTOMSCREEN;
    NewScreen[sn].Font         = NULL;
    NewScreen[sn].DefaultTitle = (UBYTE *)siden;
    NewScreen[sn].Gadgets      = NULL;
    NewScreen[sn].CustomBitMap = NULL;

    a_screen[sn] = (struct Screen *) OpenScreen(&NewScreen[sn]);

    if (a_screen[sn] == NULL) return(FALSE);

    actscr = sn;
  
    return(TRUE);
  }


/*---------------------------------------------------------------------------        
-    struct Screen *
-    Screen_Addr(sn)
-      
-      FUNZIONE  : restituisce l'indirizzo dello schermo sn
-                  oppure FALSE in caso di errore
-       
-      ARGOMENTI : int sn;  - (valore 1 o 2) numero dello schermo  
---------------------------------------------------------------------------*/                  
   
  struct  Screen  *
  Screen_Addr(sn)
  int     sn;
  {
    if (sn < 1 || sn > 2) return (FALSE);
    if (a_screen[sn-1] == NULL) return(FALSE);     
       return(a_screen[sn - 1]);
  }


/*---------------------------------------------------------------------------        
-    int
-    Screen_Front(sn)
-      
-      FUNZIONE  : porta lo schermo sul davanti del display
-   
-      RITORNO   : TRUE se ok, FALSE se no ok     
-      ARGOMENTI : int sn;  - (valore 1 o 2) numero dello schermo  
---------------------------------------------------------------------------*/                  
   
  int
  Screen_Front(sn)
  int     sn;
  {
    if (sn < 1 || sn > 2) return(FALSE);
    if (a_screen[sn-1] == NULL) return(FALSE);
    if (actscr == sn-1) return(TRUE); 
      ScreenToFront(a_screen[sn-1]);
      actscr = sn-1;
      return(TRUE);
  }
 

/*---------------------------------------------------------------------------
-    int
-    Screen_Close(sn)
-
-        FUNZIONE  : chiusura di schermo.
-
-        ARGOMENTI : int sn; - (valore 1 o 2) numero dello schermo  
---------------------------------------------------------------------------*/

  int
  Screen_Close(sn)
  int     sn;
  {
    if (sn < 1 || sn > 2) return(FALSE);
    if (a_screen[sn-1] == NULL) return(FALSE);
       CloseScreen(a_screen[sn - 1]);
       return(TRUE);
  }

