1 /*--License:
2 	Kyra Sprite Engine
3 	Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
4 	www.grinninglizard.com/kyra
5 	www.sourceforge.net/projects/kyra
6 
7 	Kyra is provided under the LGPL.
8 
9 	I kindly request you display a splash screen (provided in the HTML documentation)
10 	to promote Kyra and acknowledge the software and everyone who has contributed to it,
11 	but it is not required by the license.
12 
13 --- LGPL License --
14 
15     This library is free software; you can redistribute it and/or
16     modify it under the terms of the GNU Lesser General Public
17     License as published by the Free Software Foundation; either
18     version 2.1 of the License, or (at your option) any later version.
19 
20     This library is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23     Lesser General Public License for more details.
24 
25     You should have received a copy of the GNU Lesser General Public
26     License along with this library; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 
29 	The full text of the license can be found in lgpl.txt
30 */
31 
32 #include "canvas.h"
33 #include "SDL.h"
34 #include "engine.h"
35 #include "../../grinliz/glgeometry.h"
36 
37 using namespace grinliz;
38 
39 
KrCanvas(KrCanvasResource * _resource)40 KrCanvas::KrCanvas( KrCanvasResource* _resource )
41 {
42 	resource = _resource;
43 	resource->AddCanvas( this );
44 }
45 
46 
~KrCanvas()47 KrCanvas::~KrCanvas()
48 {
49 	resource->RemoveCanvas( this );
50 }
51 
52 
Draw(KrPaintInfo * paintInfo,const Rectangle2I & clip,int win)53 void KrCanvas::Draw(	KrPaintInfo* paintInfo,
54 						const Rectangle2I& clip,
55 						int win )
56 {
57 	GLASSERT( IsVisible( win ) );
58 	GLASSERT( CompositeQuality( win ) != KrQualityNone );
59 
60 	if ( bounds[win].Intersect( clip ) )
61 	{
62 		resource->Draw(	paintInfo,
63 						CompositeXForm(win),
64 						CompositeCForm(win),
65 						clip,
66 						CompositeQuality(win) );
67 	}
68 }
69 
70 
CalcTransform(int win)71 void KrCanvas::CalcTransform( int win )
72 {
73 	// Calculate our new coordinates, and then bounding info.
74 	KrImNode::CalcTransform( win );
75 
76 	// Tiles don't have hotspots, so this is easier than the sprite.
77 	bounds[win].min.x = CompositeXForm(win).x.ToIntRound();
78 	bounds[win].min.y = CompositeXForm(win).y.ToIntRound();
79 	bounds[win].max.x = ( CompositeXForm(win).x + CompositeXForm(win).xScale * resource->Width()  ).ToIntRound() - 1;
80 	bounds[win].max.y = ( CompositeXForm(win).y + CompositeXForm(win).yScale * resource->Height() ).ToIntRound() - 1;
81 }
82 
83 
HitTest(int x,int y,int flags,std::vector<KrImage * > * results,int window)84 bool KrCanvas::HitTest( int x, int y, int flags, std::vector<KrImage*>* results, int window )
85 {
86 	int i = window;
87 
88 	if (    IsVisible( i )
89 		 && CompositeCForm(i).Alpha() != 0
90 		 && bounds[i].Intersect( x, y ) )
91 	{
92 		Vector2< GlFixed > object;
93 		ScreenToObject( x, y, &object );
94 
95 		// Transform to local, and query the resource:
96 		if(	resource->HitTestTransformed(	object.x.ToIntRound(), object.y.ToIntRound(),
97 											flags ) )
98 		{
99 			results->push_back( this );
100 //			#ifdef DEBUG
101 //			GLOUTPUT( "HIT: Canvas %x (size=%dx%d)\n", this, Width(), Height() );
102 //			#endif
103 			return true;
104 		}
105 	}
106 	return false;
107 }
108 
109 
QueryBoundingBox(Rectangle2I * boundingBox,int window)110 void KrCanvas::QueryBoundingBox( Rectangle2I* boundingBox, int window )
111 {
112 	resource->CalculateBounds( CompositeXForm( window ), boundingBox );
113 }
114 
115 
Clone()116 KrImNode* KrCanvas::Clone()
117 {
118 	return new KrCanvas( resource );
119 }
120