//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// DS Opera SDK - Drawing Class v2.6.16 2009-05-26                          //
// (c) 2007-2009 Daniel Gump. All Rights Reserved.                          //
// http://dsoperasdk.com, http://hullbreachonline.com                       //
// hullbreach@hullbreachonline.com                                          //
//                                                                          //
//  Nintendo DS / DSi are trademarks of Nintendo Co., Ltd.                  //
//  Opera is a trademark of Opera, ASA.                                     //
//  This software package is not associated with either company             //
//  but was created to support users of both.  Its alternative name         //
//  when supporting other products is the HULLBREACH LITE SDK.              //
//                                                                          //
//  Redistribution and use in source and binary forms, with or without      //
//  modification, are permitted provided that the following conditions      //
//  are met:                                                                //
//    * Redistributions of source code must retain the above copyright      //
//      notice, this list of conditions and the following disclaimer.       //
//    * Redistributions in binary form must reproduce the above copyright   //
//      notice, this list of conditions and the following disclaimer in     //
//      the documentation and/or other materials provided with the          //
//      distribution.                                                       //
//    * Neither the names HULLBREACH ONLINE nor DS OPERA SDK nor the names  //
//      of its contributors may be used to endorse or promote products      //
//      derived from this software without specific prior written           //
//      permission.                                                         //
//    * If the explicit purpose of the software is not to support the       //
//      Nintendo Wii or the Opera Web browser, then the names of such must  //
//      not be used in any derived product. The name shall be the           //
//      HULLBREACH LITE SDK with a reference link to                        //
//      http://hullbreachonline.                                            //
//                                                                          //
//  THIS SOFTWARE IS PROVIDED BY Daniel Gump ''AS IS'' AND ANY EXPRESS OR   //
//  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED          //
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE  //
//  DISCLAIMED. IN NO EVENT SHALL Daniel Gump BE LIABLE FOR ANY DIRECT,     //
//  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES      //
//  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      //
//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)      //
//  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,     //
//  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING   //
//  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE      //
//  POSSIBILITY OF SUCH DAMAGE.                                             //
//////////////////////////////////////////////////////////////////////////////

function Drawing(){
	this.WIREFRAME=1; this.FILL=2;
	CANVAS = null;
}
Drawing.prototype.initialize = function(canvas){ CANVAS = canvas.getContext('2d'); }
Drawing.prototype.setFillColor = function(r,g,b){ CANVAS.fillStyle = "rgb("+(r>255?255:(r<0?0:r|0))+", "+(g>255?255:(g<0?0:g|0))+", "+(b>255?255:(b<0?0:b|0))+")"; }
Drawing.prototype.setLineColor = function(r,g,b){ CANVAS.strokeStyle = "rgb("+(r>255?255:(r<0?0:r|0))+", "+(g>255?255:(g<0?0:g|0))+", "+(b>255?255:(b<0?0:b|0))+")"; }
Drawing.prototype.drawCircle = function(centerx,centery,radius,style){
	CANVAS.beginPath();
	CANVAS.arc(centerx, centery, radius, 0, Math.PI*2, false);
	if(style & this.FILL) CANVAS.fill();
	if(style & this.WIREFRAME) CANVAS.stroke();
}
Drawing.prototype.drawLine = function(x1, y1, x2, y2){ CANVAS.beginPath(); CANVAS.moveTo(x1|0, y1|0); CANVAS.lineTo(x2|0, y2|0); CANVAS.stroke(); }
Drawing.prototype.drawRectangle = function(x1, y1, x2, y2, style, angle){
	x1|=0; y1|=0; x2|=0; y2|=0;
	
	if(!angle){
		if(style & this.FILL) CANVAS.fillRect(x1, y1, x2-x1, y2-y1);
		if(style & this.WIREFRAME) CANVAS.strokeRect(x1, y1, x2-x1, y2-y1);
	}else{
		var centerx = (x1+x2)>>1; var centery = (y1+y2)>>1;

		CANVAS.save();
		CANVAS.translate(centerx, centery);
		CANVAS.rotate(angle*DEGTORAD);
		if(style & this.FILL) CANVAS.fillRect(x1-centerx, y1-centery, x2-x1, y2-y1);
		if(style & this.WIREFRAME) CANVAS.strokeRect(x1-centerx, y1-centery, x2-x1, y2-y1);
		CANVAS.restore();
	}
}
Drawing.prototype.drawScene = function(array,style){
	for(tri=array.length;--tri>=0;){
		var x1=array[tri][0], y1=array[tri][1];
		var x2=array[tri][2], y2=array[tri][3];
		var x3=array[tri][4], y3=array[tri][5];
		shade=array[tri][10];

		var red=array[tri][6], green=array[tri][7], blue=array[tri][8];
		if(shade!=0){
			red=(red*=++shade)>255?255:(red<0?0:red);
			green=(green*=shade)>255?255:(green<0?0:green);
			blue=(blue*=shade)>255?255:(blue<0?0:blue);
		}
		var color = "rgb("+(red|0)+","+(green|0)+","+(blue|0)+")";
		CANVAS.beginPath();
		CANVAS.moveTo(x1,y1);
		CANVAS.lineTo(x2,y2);
		CANVAS.lineTo(x3,y3);
		CANVAS.lineTo(x1,y1);
		if(style & this.FILL){ CANVAS.fillStyle = color; CANVAS.fill(); }
		if(style & this.WIREFRAME){ CANVAS.strokeStyle = color; CANVAS.stroke(); }
	}
}
Drawing.prototype.clear = function(x1,y1,x2,y2){ CANVAS.clearRect(x1, y1, x2-x1, y2-y1); }