
//
// Modifications History
//
//$LB (20/12/2002) : 16bits to 24bits
//


#include <stdlib.h>
#include <stdarg.h>
#include "../x/objstr.h"
#include "../x/scolplugin.h"



int getDifferentColor( int nbColors, ... )
{
	int i, j;
	va_list list_param;
	int *colors = (int *)malloc( sizeof( int ) * nbColors );
	int colorFound = 0;

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\ngetDifferentColor");
#endif
//***********************************

	if ( nbColors != 0 )
	{
		va_start( list_param, nbColors );
		for ( i = 0; i < nbColors; i++ )
			colors[i] =  va_arg( list_param, int );
		va_end(list_param);
	}

	i = 0;
	while ( !colorFound )
	{
		int notYet = 1;

		for ( j = 0; j < nbColors && notYet; j++ )
			if ( colors[j] == i )
				notYet = 0;

		if ( notYet )
			colorFound = 1;
		else
			i++;
	}

	free( colors );

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\ngetDifferentColor end");
#endif
//***********************************

	return i;
}





/*********************************************************************************************************/
/*                                                                                                       */
/*  void ClipBlit ( int dtaillew , int dtailleh , int staillew , int stailleh ,                          */
/* int dposx , int dposy , int sposx , int sposy , int taillew , int tailleh ) ;                         */
/*                                                                                                       */
/*  verifie que les coordonnees sont bonne, et effectue le clipping si necessaire                        */
/*                                                                                                       */
/*********************************************************************************************************/


int ClipBlit ( int dtaillew , int dtailleh , int staillew , int stailleh ,
                int *dposx , int *dposy , int *sposx , int *sposy , int *taillew , int *tailleh ) 
{       

//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nClipBlit");
#endif
//***********************************

    /* step 1 verification de dposx */
    if ( *dposx < 0 )
    {
        *sposx -= *dposx ;
        *taillew += *dposx ;
        *dposx = 0 ;
    } else if ( *dposx >= dtaillew ) return 0 ;
   

    /* step 2 verification de dposy */
    if ( *dposy < 0 )
    {
        *sposy -= *dposy ;
        *tailleh += *dposy ;
        *dposy = 0 ;
    } else if ( *dposy >= dtailleh ) return 0 ;

    /* step 3 verification de sposx */
    if ( *sposx < 0 )
    {
        *taillew += *sposx ;
        *dposx -= *sposx;
        *sposx = 0 ;
       
    } else if ( *sposx >= staillew ) return 0 ;

    /* step 4 verification de sposy */
    if ( *sposy < 0 )
    {
        *tailleh  += *sposy ;
        *dposy -= *sposy;
        *sposy = 0 ;
    } else if ( *sposy >= stailleh ) return 0 ;

    /* step 4 verification de taillew */
    if ( *taillew < 0 ) return 0 ;
    if ( *taillew + *sposx >= staillew )
    {   
         *taillew -=  ( *taillew + *sposx ) - staillew ;     
         if ( * taillew < 0 ) return 0 ;
    }
    if ( *taillew + *dposx >= dtaillew )
    {
        *taillew -= ( *taillew + *dposx ) - dtaillew ;
        if ( * taillew  < 0 ) return 0 ;
    }

    /* step 5 verification de tailleh */
    if ( *tailleh < 0 ) return 0 ;
    if ( *tailleh + *sposy >= stailleh )
    {
        *tailleh -= ( *tailleh + *sposy ) - stailleh ;
        if ( *tailleh < 0 ) return 0 ;
    }
    if ( *tailleh + *dposy >= dtailleh ) 
    {
        *tailleh -= ( *tailleh + *dposy ) -dtailleh ;
        if ( *tailleh < 0 ) return 0 ;
    }


//***********************************
#if DEBUG_LIB2D
MMechostr (0, "\nClipBlit end");
#endif
//***********************************

    return 1 ;    
}
