Jypeli  5
The simple game programming library
Canvas.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2009-2011 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tero Jäntti.
28  */
29 
30 using System;
31 using Microsoft.Xna.Framework;
32 
33 namespace Jypeli
34 {
38  public class Canvas
39  {
40  Image previousImage = null;
41  internal Matrix worldMatrix;
42 
46  public double Left { get; private set; }
47 
51  public double Right { get; private set; }
52 
56  public double Bottom { get; private set; }
57 
61  public double Top { get; private set; }
62 
66  public Vector TopLeft { get; private set; }
67 
71  public Vector TopRight { get; private set; }
72 
76  public Vector BottomLeft { get; private set; }
77 
81  public Vector BottomRight { get; private set; }
82 
86  public Color BrushColor { get; set; }
87 
88  internal Canvas()
89  {
90  Reset();
91  }
92 
93  public void Begin( ref Matrix worldMatrix, Dimensional dimensionSource )
94  {
95  Graphics.LineBatch.Begin( ref worldMatrix );
96  Graphics.ImageBatch.Begin( ref worldMatrix, null );
97  Graphics.Canvas.Reset( dimensionSource );
98  this.worldMatrix = worldMatrix;
99  }
100 
101  public void End()
102  {
103  Graphics.ImageBatch.End();
104  Graphics.LineBatch.End();
105  }
106 
107  private void Reset()
108  {
110  previousImage = null;
111  }
112 
113  internal void Reset( Dimensional dimensionSource )
114  {
115  Reset();
116  Left = dimensionSource.Left;
117  Right = dimensionSource.Right;
118  Bottom = dimensionSource.Bottom;
119  Top = dimensionSource.Top;
120  TopLeft = new Vector( Left, Top );
121  TopRight = new Vector( Right, Top );
122  BottomLeft = new Vector( Left, Bottom );
123  BottomRight = new Vector( Right, Bottom );
124  }
125 
131  public void DrawLine( Vector startPoint, Vector endPoint )
132  {
133  Graphics.LineBatch.Draw( startPoint, endPoint, BrushColor );
134  }
135 
143  public void DrawLine( double x1, double y1, double x2, double y2 )
144  {
145  Graphics.LineBatch.Draw( new Vector( x1, y1 ), new Vector( x2, y2 ), BrushColor );
146  }
147 
155  public void DrawImage( Vector point, Image image, Vector scale, Angle angle )
156  {
157  if ( !Object.ReferenceEquals( image, previousImage ) )
158  {
159  // Start a new batch with different image
160  Graphics.ImageBatch.End();
161  Graphics.ImageBatch.Begin( ref worldMatrix, image.XNATexture );
162  previousImage = image;
163  }
164 
165  Vector2 scaleV = new Vector2( (float)( image.Width * scale.X ), (float)( image.Height * scale.Y ) );
166  Graphics.ImageBatch.Draw( Graphics.DefaultTextureCoords, (Vector2)point, scaleV, (float)angle.Radians );
167  }
168 
174  public void DrawImage( Vector point, Image image )
175  {
176  DrawImage( point, image, Vector.Diagonal, Angle.Zero );
177  }
178  }
179 }
double Right
Oikea reuna.
Definition: Dimensional.cs:21
double Top
Yläreuna.
Definition: Dimensional.cs:26
static readonly Color Black
Musta.
Definition: Color.cs:494
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
void DrawLine(Vector startPoint, Vector endPoint)
Piirtää janan.
Definition: Canvas.cs:131
void Begin(ref Matrix worldMatrix, Dimensional dimensionSource)
Definition: Canvas.cs:93
double Bottom
Alareuna.
Definition: Canvas.cs:56
int Width
Leveys pikseleinä.
Definition: Image.cs:290
void DrawImage(Vector point, Image image)
Piirtää kuvan.
Definition: Canvas.cs:174
Kuva.
Definition: Image.cs:24
Vector TopLeft
Vasen ylänurkka.
Definition: Canvas.cs:66
double Bottom
Alareuna.
Definition: Dimensional.cs:31
void End()
Definition: Canvas.cs:101
double Y
Definition: Vector.cs:275
static readonly Angle Zero
Nollakulma.
Definition: Angle.cs:45
Color BrushColor
Pensselin väri.
Definition: Canvas.cs:86
int Height
Korkeus pikseleinä.
Definition: Image.cs:298
Vector TopRight
Oikea ylänurkka.
Definition: Canvas.cs:71
Vector BottomLeft
Vasen alanurkka.
Definition: Canvas.cs:76
double X
Definition: Vector.cs:274
void DrawImage(Vector point, Image image, Vector scale, Angle angle)
Piirtää kuvan.
Definition: Canvas.cs:155
static readonly Vector Diagonal
Diagonaalivektori (1,1)
Definition: Vector.cs:76
double Top
Yläreuna.
Definition: Canvas.cs:61
double Radians
Palauttaa tai asettaa kulman radiaaneina.
Definition: Angle.cs:86
Väri.
Definition: Color.cs:13
double Left
Vasen reuna.
Definition: Dimensional.cs:16
Piirtoalusta.
Definition: Canvas.cs:38
double Right
Oikea reuna.
Definition: Canvas.cs:51
2D-vektori.
Definition: Vector.cs:56
Vector BottomRight
Oikea alanurkka.
Definition: Canvas.cs:81
double Left
Vasen reuna.
Definition: Canvas.cs:46
void DrawLine(double x1, double y1, double x2, double y2)
Piirtää janan.
Definition: Canvas.cs:143
Olio jolla on reunat.
Definition: Dimensional.cs:11