
 /* SV_CommonGfxSubs.c
    - Functions for use with various SVObjects -
    (c) 1993-94 by Andreas R. Kleinert
    Last changes : 27.03.1994
 */

#include "svobject.h"

ULONG __saveds __stdargs SVLI_GetBestModeID(long width, long height, long depth);
ULONG __saveds __stdargs SVLI_CopyScreenToBuffer8(struct SVObjectHandle *SVObjectHandle,
                                                  UBYTE *buffer, ULONG width,
                                                  ULONG height, ULONG depth);
void  __saveds __stdargs SVLI_SPrintf(APTR buffer, char *formatstring, ...);

#ifdef SV_CGFX_GETBESTMODE

/* *************************************************** */
/* *						     * */
/* * SVLI_GetBestModeID : (do that)                  * */
/* *						     * */
/* *************************************************** */

ULONG __saveds __stdargs SVLI_GetBestModeID(long width, long height, long depth)
{
 ULONG mode_id = N;

 if(OS_VER > 38)
  {
   mode_id = BestModeID(BIDTAG_NominalWidth,  width,
                        BIDTAG_NominalHeight, height,
                        BIDTAG_DesiredWidth,  width,
                        BIDTAG_DesiredHeight, height,
                        BIDTAG_Depth,         depth,
                        TAG_END);
  }

 if(!mode_id) /* if OS < V39 or BestModeID failed */
  {
   /* Uses OverScan values for checking. */
   /* Assumes an ECS-System.             */

        if(width > 724 && depth < 3)   mode_id = SUPER_KEY;
   else if(width > 362 && depth < 5)   mode_id = HIRES_KEY;
   else                                mode_id = LORES_KEY;

   if(!ModeNotAvailable(mode_id | PAL_MONITOR_ID))    /* for PAL  Systems */
    {
     if(height > 283) mode_id |= LACE;

     mode_id |= PAL_MONITOR_ID;
    }else
    {
     if(!ModeNotAvailable(mode_id | NTSC_MONITOR_ID)) /* for NTSC Systems */
      {
       if(height > 241) mode_id |= LACE;

       mode_id |= NTSC_MONITOR_ID;
      }
    }
  }

 return(mode_id);
}

#endif /* SV_CGFX_GETBESTMODE */

#ifdef SV_CGFX_COPYSCREEN

/* *************************************************** */
/* *						     * */
/* * SVLI_CopyScreenToBuffer8 : (do that)            * */
/* *						     * */
/* *************************************************** */

ULONG __saveds __stdargs SVLI_CopyScreenToBuffer8(struct SVObjectHandle *SVObjectHandle,
                                                  UBYTE *buffer, ULONG width,
                                                  ULONG height, ULONG depth)
{
 register struct RastPort *tmp_rp;
 register UWORD            j;
 register UBYTE           *bptr;
          APTR             bmap;
          struct RastPort *rp;
          ULONG            retval = SVERR_NO_ERROR;

 rp = (struct RastPort *) &(SVObjectHandle->ah_WriteScreen->RastPort);

 if(OS_VER > 38)
  {
   bmap = (APTR) AllocBitMap(width, 1, depth, BMF_CLEAR, N);
   if(bmap)
    {
     tmp_rp = (APTR) AllocVec(sizeof(struct RastPort), MEMF_CLEAR|MEMF_PUBLIC);
     if(tmp_rp)
      {
       InitRastPort(tmp_rp);
       tmp_rp->BitMap = bmap;

       bptr = buffer;

       for(j=0; j<height; j++, bptr+=width) ReadPixelLine8(rp, 0, j, width, bptr, tmp_rp);

       FreeVec(tmp_rp);

      }else retval = SVERR_NO_MEMORY;

     FreeBitMap(bmap);

    }else retval = SVERR_NO_MEMORY;
  }else
  {
   register struct RastPort *tmp_rp;
   register UWORD            i, j;
   register UBYTE           *bptr;

   tmp_rp = (APTR) AllocVec(sizeof(struct RastPort), MEMF_CLEAR|MEMF_PUBLIC);
   if(tmp_rp)
    {
     InitRastPort(tmp_rp);

     tmp_rp->BitMap = AllocVec(sizeof(struct BitMap), MEMF_CLEAR|MEMF_PUBLIC);
     if(tmp_rp->BitMap)
      {
       InitBitMap(tmp_rp->BitMap, depth, width, height);

       for(i=0; i<depth; i++)
        {
         tmp_rp->BitMap->Planes[i] = (APTR) AllocRaster(width, height);
         if(!tmp_rp->BitMap->Planes[i])
          {
           retval = SVERR_NO_MEMORY;

           break;
          }
        }

       if(!retval)
        {
         bptr = buffer;

         for(j=0; j<height; j++, bptr+=width) ReadPixelLine8(rp, 0, j, width, bptr, tmp_rp);
        }

       for(i=0; i<8; i++) if(tmp_rp->BitMap->Planes[i]) FreeRaster(tmp_rp->BitMap->Planes[i], width, height);

       FreeVec(tmp_rp->BitMap);
      }else retval = SVERR_NO_MEMORY;

     FreeVec(tmp_rp);
    }else retval = SVERR_NO_MEMORY;
  }

 return(retval);
}

#endif /* SV_CGFX_COPYSCREEN */

extern void Fmt(void); /* Assembler-Routine for RawDoFmt */

void __saveds __stdargs SVLI_SPrintf(APTR buffer, char *formatstring, ...)
{
 RawDoFmt((APTR) formatstring, (APTR) (((ULONG *)&formatstring)+1), (APTR) &Fmt, (APTR) buffer);
}
