/*FILE HEADER**************************************************************
 *
 *  File name		: $Id$
 *  RCS Revision Number	: $Revision: 1.1 $
 *  Last Modified	: $Date: 94/10/28 09:23:28 $
 *  CSCI name		: Real-Time Graphics System - Applications Interface
 *  CSC name		: All functions
 *  Notes:
 *      This file contains code used to debug and test the SVME-770 RTGS.
 *      This code is provided as an example of how to call the RTGS functions
 *      from a host application.
 *  History		:
 *      Created 94/01/17 by Jeff Sanderson
 *
 *END FILE HEADER*********************************************************/

#include "dy4std.h"
#include "es.h"
#include "css.h"

#include "rtgs_ai.h"
#include "video.h"
#include "colours.h"

#include "test.h"

#include "lady2.h"    /* file containing decl of bitmap */

/*-----------------------------------------------------------------------*/
#define LADY_X         288
#define LADY_Y         192
#define BITS_PER_BYTE  8
#define PIX_PER_BYTE   1

#define LEFT    0
#define RIGHT   1
#define UP      2
#define DOWN    3

extern uint32  *symbols[ 4 ];
void test_bitblt( void );
void scroll( int dir, int stp, int x, int y, int w, int h , int *buf, int ptch);

/*-----------------------------------------------------------------------*/

void test_bitblt( void )
{
    int   counter;
    int   *lady_gcc1;
    int   *lady_gcc2;
    int   *buffer;

    init( "Bitblt test" );

    Clear_screen( BLACK, 0, 0 );
    /* Init the memory. */
    Set_transparency_mode( OPAQUE );
    Set_background_colour( BLACK );

    lady_gcc1 = ( int * )Gcc_malloc(( LADY_X * LADY_Y / BITS_PER_BYTE ), 0 );
    malloc_check( lady_gcc1 );

    lady_gcc2 = ( int * )Gcc_malloc(( LADY_X * LADY_Y / BITS_PER_BYTE ), 0 );
    malloc_check( lady_gcc2 );

    buffer = ( int * )Gcc_malloc(( LADY_X * LADY_Y / PIX_PER_BYTE ), 0 );
    malloc_check( buffer );

    /* set up images */

    Copy_bitmap_to_gcc(( char * )( lady2 ), ( char * )( lady_gcc1 ), 
                       ( LADY_X * LADY_Y / BITS_PER_BYTE ));

    Bitblt_linear_to_linear( lady_gcc1, (LADY_X / BITS_PER_BYTE ),
                             lady_gcc2, (LADY_X / BITS_PER_BYTE ),
                             (LADY_X /BITS_PER_BYTE), LADY_Y    );

    Set_foreground_colour( WHITE );
    Bitblt_bit_expand( lady_gcc2,  64,  32, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( RED );
    Bitblt_bit_expand( lady_gcc2,  64, 224, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( GREEN );
    Bitblt_bit_expand( lady_gcc2,  64, 416, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( BLUE );
    Bitblt_bit_expand( lady_gcc2,  64, 608, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( WHITE );
    Bitblt_bit_expand( lady_gcc2,  64, 800, LADY_X, LADY_Y, LADY_X );

    Set_foreground_colour( RED );
    Bitblt_bit_expand( lady_gcc2, 352,  32, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( GREEN );
    Bitblt_bit_expand( lady_gcc2, 352, 224, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( BLUE );
    Bitblt_bit_expand( lady_gcc2, 352, 416, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( WHITE );
    Bitblt_bit_expand( lady_gcc2, 352, 608, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( RED );
    Bitblt_bit_expand( lady_gcc2, 352, 800, LADY_X, LADY_Y, LADY_X );

    Set_foreground_colour( GREEN );
    Bitblt_bit_expand( lady_gcc2, 640,  32, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( BLUE );
    Bitblt_bit_expand( lady_gcc2, 640, 224, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( WHITE );
    Bitblt_bit_expand( lady_gcc2, 640, 416, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( RED );
    Bitblt_bit_expand( lady_gcc2, 640, 608, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( GREEN );
    Bitblt_bit_expand( lady_gcc2, 640, 800, LADY_X, LADY_Y, LADY_X );

    Set_foreground_colour( BLUE );
    Bitblt_bit_expand( lady_gcc2, 928,  32, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( WHITE );
    Bitblt_bit_expand( lady_gcc2, 928, 224, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( RED );
    Bitblt_bit_expand( lady_gcc2, 928, 416, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( GREEN );
    Bitblt_bit_expand( lady_gcc2, 928, 608, LADY_X, LADY_Y, LADY_X );
    Set_foreground_colour( BLUE );
    Bitblt_bit_expand( lady_gcc2, 928, 800, LADY_X, LADY_Y, LADY_X );

    Key_wait();

    counter = ( 200 );
    while( counter-- )
    {
	scroll( LEFT ,  1,  64,  32, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( DOWN ,  2,  64, 224, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( RIGHT,  3,  64, 416, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( UP   ,  4,  64, 608, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( LEFT ,  5,  64, 800, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));

	scroll( DOWN ,  3, 352,  32, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( RIGHT,  6, 352, 224, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( UP   ,  9, 352, 416, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( LEFT , 12, 352, 608, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( DOWN , 15, 352, 800, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));

	scroll( RIGHT,  5, 640,  32, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( UP   , 10, 640, 224, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( LEFT , 15, 640, 416, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( DOWN , 20, 640, 608, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( RIGHT, 25, 640, 800, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));

	scroll( UP   ,  7, 928,  32, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( LEFT , 14, 928, 224, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( DOWN , 21, 928, 416, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( RIGHT, 28, 928, 608, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
	scroll( UP   , 35, 928, 800, LADY_X, LADY_Y , buffer, ( LADY_X * 8 ));
    }
    Key_wait();
}

/*-----------------------------------------------------------------------*/

void scroll( int dir, int stp, int x, int y, int w, int h , int *buf, int ptch )
{
    switch( dir )
    {
        case LEFT :
        {
	    Bitblt_xy_to_linear( x, y, buf, stp, h, ptch );
	    Bitblt_xy_to_xy(( x + stp ), y, x, y, ( w - stp ), h );
	    Bitblt_linear_to_xy( buf, ( x + w - stp ), y, stp, h, ptch );
            break;
        }
        case RIGHT :
        {
	    Bitblt_xy_to_linear(( x + w - stp ), y, buf, stp, h, ptch );
	    Bitblt_xy_to_xy( x, y, ( x + stp ), y, ( w - stp ), h );
	    Bitblt_linear_to_xy( buf, x, y, stp, h, ptch );
            break;
        }
        case UP :
        {
	    Bitblt_xy_to_linear( x, y, buf, w, stp, ptch );
	    Bitblt_xy_to_xy( x, ( y + stp ), x, y, w, ( h - stp ));
	    Bitblt_linear_to_xy( buf, x, ( y + h - stp ), w, stp, ptch );
            break;
        }
        case DOWN :
        {
	    Bitblt_xy_to_linear( x, ( y + h - stp ), buf, w, stp, ptch );
	    Bitblt_xy_to_xy( x, y, x, ( y + stp ), w, ( h - stp ));
	    Bitblt_linear_to_xy( buf, x, y, w, stp, ptch );
            break;
        }
    }
}

/*-----------------------------------------------------------------------*/
