/*  Siteswap v1.2 beta 2
 *
 *  pilotgr.c contains all of the graphics related functions
 *  Originally written by Stuart Macmillan, modified by Nathan Peterson
 *  Based off of Glenn Hutchings Juggle 1.0
 *  Contact info: Nathan Peterson (nathan@juggler.net)
 *  Download the latest version at http://www.siteswap.org/palm/
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
*/

/*
** PalmOS includes
*/
#include <Window.h>
#include <PalmCompatibility.h>
#include <UIResources.h>
#include <StringMgr.h>
#include <DataMgr.h>
#include <System/ErrorMgr.h>

#include "globals.h"
#include "juggle.h"

#define X_BORDER	15.		// left margin for juggle area
#define Y_BASELINE	130.	// bottom edge of juggle area
#define X_SCALE         100.
#define Y_SCALE         115.
#define BITMAP_SIZE_2   6	// bitmap size / 2

#define OFSCRN_WIN_Y 140
#define OFSCRN_WIN_X 130


BitMapSize bitmapsize = {
  6, // xball
  6, // yball
  { 8,8,8,8,8,8,16 }, // xProp[]
  { 8,8,8,8,8,8,16 }, // yProp[]
};



//#undef NEW_ANIMATION

WinHandle backWin;		// back buffer
WinHandle frontWin;		// front buffer

Boolean first = true;	// used in juggle_init()

// global variables
int jugProp = 0;	// defines which type of juggling prop is currently used
Boolean pref_Draw = false;
Boolean pref_BackBuff = true;

void juggle_set_draw_back();
void juggle_set_draw_front();
void printwchar(int inyg, int cnt, int y);

/**** juggle_setdp()
 * change the objects used for balls
 **************************************/
void juggle_setdp() {
  Boolean temp;
  temp = pref_Draw;
//  txtrbmp = !txtrbmp;
  jugProp = (jugProp + 1) % 3;
  pref_Draw = false;
  juggle_frame();	// erase back buffer
  pref_Draw = temp;
}


/**** juggle_init()
 * creates front and back buffers
 ***********************************/
void juggle_init() {
  Word error;

  if (first )  {
	frontWin = WinGetDrawWindow();
	juggle_set_draw_front();

	backWin = WinCreateOffscreenWindow( OFSCRN_WIN_X, OFSCRN_WIN_Y, screenFormat, &error );
	ErrFatalDisplayIf( error,"Error creating window");
	first = false;
//  juggle_set_draw_back();
  }

  if ( pref_BackBuff ) {
	// clear back buffer if in non erase mode
	if (pref_Draw == true) {
	  pref_Draw = false;
	  juggle_frame();
	  pref_Draw = true;
	}
  }
}


/**** juggle_frame()
 * Erases back buffer
 ***********************/
void juggle_frame() {

  if ( pref_BackBuff ) {
	RectangleType bounds;
	juggle_set_draw_back();

	if ( !pref_Draw ) {
	  bounds.topLeft.x = 0;
	  bounds.topLeft.y = 0;
	  bounds.extent.x = OFSCRN_WIN_X;
	  bounds.extent.y = OFSCRN_WIN_Y;
	  WinEraseRectangle( &bounds ,0 );
	}

/* uncomment to check layout */
//	WinDrawRectangle( &bounds, 2);
	juggle_set_draw_front();

  }
  else if ( !pref_Draw ) {
	RectangleType clrit = { 0, 0, OFSCRN_WIN_X, OFSCRN_WIN_Y };
	WinEraseRectangle(&clrit ,0);//$$4
  }
}


/*
void juggle_label( Char *jlabel){

      int lenj;
      RectangleType clrit = { 1, 145, 159, 14 };

// WinEraseRectangle(&clrit ,4);
      lenj = StrLen( jlabel);
      WinDrawChars( jlabel,lenj,5,148);
}
*/


/**** juggle_set_draw_front()
 * set the current window to front buffer
 *******************************************/
void juggle_set_draw_front() {
    WinSetDrawWindow( frontWin );
 }


/**** juggle_set_draw_back()
 * set the current window to back buffer
 ******************************************/
void juggle_set_draw_back() {
   if ( pref_BackBuff )
    WinSetDrawWindow( backWin );
 }


/**** juggle_ball()
 * draw a ball on buffer
 * (note: throw and catch not used)
 *************************************/
void juggle_ball(int num, int throw, int catch, float x, float y)
{
  int usex,usey;
  char string[8];
  int lenj, icon2Use;
  VoidHand handle;
  VoidPtr ptr;

  if ( pref_BackBuff )
	juggle_set_draw_back();

  usex  = (int) X_BORDER+(x*X_SCALE);
  if ( pref_BackBuff )
	usey  = (int) Y_BASELINE-(y*Y_SCALE);
  else
	usey  = ((int) Y_BASELINE-(y*Y_SCALE));

  switch ( jugProp ){

	case 1: // use text

	  StrIToA( string, num+1);
	  lenj = StrLen( string);
	  FntSetFont( largeFont );
	  usey = usey-6; // font is 12 pixels high
	  if (num < 9) { usex = usex-3; }  // 6 pixels wide if single digit
	  else { usex = usex-7; }  // 14 pixels wide if two digits
	  WinDrawChars( string,lenj,usex,usey); // draw number
	  break;

	case 0: // use balls
	case 2: // use props

	  icon2Use = 7010; //-- Use ball.bmp

	  if ( jugProp == 2 ) {  //-- Use other bmp
		icon2Use = 7002+( num % 7);
		usex -= bitmapsize.xProp[num%7];  // set offset for bitmap
		usey -= bitmapsize.yProp[num%7];  //
	  }
	  else {
		usex -= bitmapsize.xball;  // set offset for ball
		usey -= bitmapsize.yball;  //
	  }

	  // this is to keep old palms from crashing
	  if (usex < 0) usex = 0;
	  if (usey < 0) usey = 0;

//	  if (usex>=0 && usey>=0) { // this is to keep old palms from crashing

		handle = DmGetResource( bitmapRsc , icon2Use );
		if ( handle != 0 ) {
		  ptr = MemHandleLock( handle );
		  if ( ptr != 0 ) {
			WinDrawBitmap( ptr, usex , usey);
			MemHandleUnlock( handle);
		  }
		}
		DmReleaseResource (handle);

//	  }

	  break;
  }

  if ( pref_BackBuff )
	juggle_set_draw_front();
}


/**** juggle_update()
 * copy back buffer to front buffer
 *************************************/
void juggle_update() {

  if ( pref_BackBuff ) {
	RectangleType scrnBnds;
	int offset;

	scrnBnds.topLeft.x = 0;
	scrnBnds.topLeft.y = 0;
	scrnBnds.extent.x = OFSCRN_WIN_X;
	scrnBnds.extent.y = OFSCRN_WIN_Y;

	// Align the x coordinate with the nearest word to speed up the WinCopyRectangle.
	offset = scrnBnds.topLeft.x & 0x000F;

	if (offset) {
	  scrnBnds.topLeft.x -= offset;
	  scrnBnds.extent.x += offset;
	}

	// Push the extent out to fill the entire word.
	scrnBnds.extent.x = (scrnBnds.extent.x + 0x0F) & 0xFFF0;

	WinCopyRectangle( backWin, frontWin, &scrnBnds , 5 , 0 , scrCopy  );
  }
}


//int juggle_quit() { }


/**** juggle_end()
 * delete back buffer
 ***********************/
void juggle_end() {

 if ( pref_BackBuff ) // delete backbuffer if there is one
   WinDeleteWindow( backWin ,false );
}

