Jypeli 10
The simple game programming library
View.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2009 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
25
26#endregion
27
28/*
29 * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
30 */
31
32using Microsoft.Xna.Framework;
33using Microsoft.Xna.Framework.Graphics;
34
35using XnaColor = Microsoft.Xna.Framework.Color;
36
37namespace Jypeli
38{
44 public class ScreenView : Dimensional
45 {
46 private RenderTarget2D _renderTarget = null;
47 private SpriteBatch renderBatch;
48 private Vector2 _center = Vector2.Zero;
49 private Vector3 _scale = Vector3.One;
50 private Vector2 _scale2 = Vector2.One;
51 private Vector3 _scaleInv = Vector3.One;
52 private Point _size;
53 private float _angle = 0;
54 private SpriteEffects _effect = SpriteEffects.None;
55 private bool _flipAndMirror;
56 private XnaColor _color = XnaColor.White;
57 private XnaColor _bgcolor = XnaColor.Black;
58 private Texture2D _bgTex = null;
59
63 internal GraphicsDevice device;
64
68 internal RenderTarget2D RenderTarget
69 {
70 get
71 {
72 if ( _renderTarget == null )
73 {
74 _renderTarget = new RenderTarget2D( device, _size.X, _size.Y, false, device.DisplayMode.Format, DepthFormat.Depth24Stencil8 );
76 }
77
78 return _renderTarget;
79 }
80 }
81
86 public ScreenView( GraphicsDevice device )
87 {
88 this.device = device;
89 this.renderBatch = new SpriteBatch( device );
90 this._bgTex = new Texture2D( device, 1, 1 );
91
92 PresentationParameters pp = device.PresentationParameters;
93 this._size = new Point( pp.BackBufferWidth, pp.BackBufferHeight );
94 }
95
99 public Image Image
100 {
101 get { return new Image( RenderTarget ); }
102 }
103
108 {
109 get
110 {
111 return new Image( _bgTex );
112 }
113 set
114 {
115 // Clone ensures that the image doesn't change unexpectedly
116 _bgTex = value.Clone().XNATexture;
117 }
118 }
119
125 {
126 get { return (Color)_bgcolor; }
127 set
128 {
129 _bgcolor = (XnaColor)value;
130 _bgTex = new Texture2D( RenderTarget.GraphicsDevice, 1, 1 );
131 _bgTex.SetData<XnaColor>( new XnaColor[] { (XnaColor)value } );
132 }
133 }
134
139 {
140 get { return (Vector)_center; }
141 set { _center = (Vector2)value; }
142 }
143
148 {
149 get { return (Color)_color; }
150 set { _color = (XnaColor)value; }
151 }
152
156 public bool IsFlipped
157 {
158 get { return _flipAndMirror || _effect == SpriteEffects.FlipVertically; }
159 set
160 {
161 if ( IsMirrored )
162 {
163 _effect = SpriteEffects.None;
164 _flipAndMirror = true;
165 }
166 else
167 {
168 _effect = SpriteEffects.FlipVertically;
169 _flipAndMirror = false;
170 }
171 }
172 }
173
177 public bool IsMirrored
178 {
179 get { return _flipAndMirror || _effect == SpriteEffects.FlipVertically; }
180 set
181 {
182 if ( IsFlipped )
183 {
184 _effect = SpriteEffects.None;
185 _flipAndMirror = true;
186 }
187 else
188 {
189 _effect = SpriteEffects.FlipHorizontally;
190 _flipAndMirror = false;
191 }
192 }
193 }
194
199 {
200 get { return Angle.FromRadians( -_angle ); }
201 set { _angle = -(float)value.Radians; }
202 }
203
208 {
209 get { return new Vector( _scale.X, _scale.Y ); }
210 set
211 {
212 _scale = new Vector3( (float)value.X, (float)value.Y, 1 );
213 _scale2 = new Vector2( _scale.X, _scale.Y );
214 _scaleInv = new Vector3( 1 / _scale.X, 1 / _scale.Y, 1 );
215 }
216 }
217
221 public double Width
222 {
223 get { return RenderTarget.Width; }
224 set
225 {
226 _size.X = (int)value;
227 _renderTarget = null;
228 }
229 }
230
234 public double Height
235 {
236 get { return RenderTarget.Height; }
237 set
238 {
239 _size.Y = (int)value;
240 _renderTarget = null;
241 }
242 }
243
248 {
249 get { return new Vector( RenderTarget.Width, RenderTarget.Height ); }
250 set
251 {
252 _size.X = (int)value.X;
253 _size.Y = (int)value.Y;
254 _renderTarget = null;
255 }
256 }
257
261 public double ViewportWidth
262 {
263 get
264 {
265 return Game.GraphicsDeviceManager.PreferredBackBufferWidth;
266 }
267 }
268
272 public double ViewportHeight
273 {
274 get
275 {
276 return Game.GraphicsDeviceManager.PreferredBackBufferHeight;
277 }
278 }
279
284 {
285 get { return new Vector( ViewportWidth, ViewportHeight ); }
286 }
287
291 public double Left
292 {
293 get { return -Width / 2; }
294 }
295
299 public double Right
300 {
301 get { return Width / 2; }
302 }
303
307 public double Top
308 {
309 get { return Height / 2; }
310 }
311
315 public double Bottom
316 {
317 get { return -Height / 2; }
318 }
319
323 public double LeftSafe { get { return Left + 10; } }
324
328 public double RightSafe { get { return Right - 10; } }
329
333 public double BottomSafe { get { return Bottom + 10; } }
334
338 public double TopSafe { get { return Top + 10; } }
339
343 public double WidthSafe { get { return RightSafe - LeftSafe; } }
344
348 public double HeightSafe { get { return TopSafe - BottomSafe; } }
349
353 public void ScaleToFit()
354 {
355 // TODO: Angle
356 this.Scale = new Vector( ViewportSize.X / Size.X, ViewportSize.Y / Size.Y );
357 }
358
366 internal static Vector FromXnaCoords( Vector2 position, Vector screenSize, Vector objectSize )
367 {
368 double x = ( -screenSize.X + objectSize.X ) / 2 + position.X;
369 double y = ( screenSize.Y - objectSize.Y ) / 2 - position.Y;
370 return new Vector( x, y );
371 }
372
373 private static float xToXna( float x, float scrW, float objW )
374 {
375 return ( scrW - objW ) / 2 + x;
376 }
377
378 private static float yToXna( float y, float scrH, float objH )
379 {
380 return ( scrH - objH ) / 2 - y;
381 }
382
390 internal static Vector2 ToXnaCoords( Vector position, Vector screenSize, Vector objectSize )
391 {
392 return new Vector2(
393 xToXna( (float)position.X, (float)screenSize.X, (float)objectSize.X ),
394 yToXna( (float)position.Y, (float)screenSize.Y, (float)objectSize.Y ) );
395 }
396
404 internal static Matrix ToXnaCoords(ref Matrix matrix, Vector screenSize, Vector scale)
405 {
406 // Keskitetään sprite ruudulla, mutta toteutetaan alkuperäinen muunnos Jypelin koordinaateissa.
407 var centralize = Matrix.CreateTranslation((screenSize - scale) / 2);
408 var toXna = Matrix.CreateScale(1, -1, 1) * Matrix.CreateTranslation(screenSize / 2);
409 var toJypeli = Matrix.Invert(toXna);
410
411 return centralize * toJypeli * matrix * toXna;
412 }
413
422 {
423 return Matrix.CreateScale( _scale )
424 * Matrix.CreateRotationZ( _angle )
425 * Matrix.CreateTranslation( -_center.X, -_center.Y, 0 );
426 }
427
436 {
437 return Matrix.CreateScale( _scaleInv )
438 * Matrix.CreateRotationZ( -_angle )
439 * Matrix.CreateTranslation( _center.X, _center.Y, 0 );
440 }
441
442 internal void Render()
443 {
444 float angle = _flipAndMirror ? _angle + Microsoft.Xna.Framework.MathHelper.Pi : _angle;
445
446 device.SetRenderTarget( null );
447 device.Clear( _bgcolor );
448
449 Matrix rotate = Matrix.CreateRotationZ(angle);
450 Vector2 devorigin = new Vector2( device.Viewport.Width, device.Viewport.Height ) / 2;
451 Vector2 rtorigin = new Vector2( RenderTarget.Width * _scale.X, RenderTarget.Height * _scale.Y ) / 2;
452 Vector2 diff = Vector2.Transform( -rtorigin, rotate );
453 var rectangle = new Microsoft.Xna.Framework.Rectangle( 0, 0, device.Viewport.Width, device.Viewport.Height );
454
455 renderBatch.Begin( SpriteSortMode.Immediate, BlendState.AlphaBlend, Graphics.GetDefaultSamplerState(), DepthStencilState.None, RasterizerState.CullCounterClockwise, null );
456 renderBatch.Draw( _bgTex, rectangle, XnaColor.White );
457 renderBatch.Draw( RenderTarget, devorigin + diff + _center, null, _color, angle, Vector2.Zero, _scale2, _effect, 1 );
458 renderBatch.End();
459 }
460 }
461
466 {
470 Center,
471
475 Left,
476
480 Right
481 }
482
487 {
491 Center,
492
496 Top,
497
501 Bottom
502 }
503}
504
System.Numerics.Vector2 Vector2
Microsoft.Xna.Framework.Color XnaColor
Definition: View.cs:35
Microsoft.Xna.Framework.Color XnaColor
static GraphicsDeviceManager GraphicsDeviceManager
XNA:n grafiikkakorttien hallintaolio.
Definition: Graphics.cs:61
Contains graphics resources.
Definition: Graphics.cs:36
static void ResetScreenSize()
Definition: Graphics.cs:213
static SamplerState GetDefaultSamplerState()
Definition: Graphics.cs:77
Kuva.
Definition: Image.cs:30
Sisältää näytön leveyden ja korkeuden sekä reunojen koordinaatit. Y-koordinaatti kasvaa ylöspäin....
Definition: View.cs:45
double LeftSafe
Vasemman reunan sijainti johon lisätty pieni marginaali
Definition: View.cs:323
XnaColor _color
Definition: View.cs:56
static float yToXna(float y, float scrH, float objH)
Definition: View.cs:378
Matrix GetScreenInverse()
Palauttaa käänteisen transformaatiomatriisin jolla voi ottaa huomioon ruudun kokoon,...
Definition: View.cs:435
Vector ViewportSize
Näytön todellinen koko.
Definition: View.cs:284
ScreenView(GraphicsDevice device)
Alustaa uuden näyttönäkymän.
Definition: View.cs:86
static Vector2 ToXnaCoords(Vector position, Vector screenSize, Vector objectSize)
Muuntaa Jypelin ruutukoordinaateista XNA:n ruutukoordinaateiksi.
Definition: View.cs:390
Vector Size
Näytön koko vektorina.
Definition: View.cs:248
Texture2D _bgTex
Definition: View.cs:58
double Width
Näytön leveys x-suunnassa.
Definition: View.cs:222
double TopSafe
Yläreunan sijainti johon lisätty pieni marginaali
Definition: View.cs:338
double WidthSafe
Leveys johon lisätty pieni marginaali
Definition: View.cs:343
static float xToXna(float x, float scrW, float objW)
Definition: View.cs:373
Vector Scale
Näytön skaalaus.
Definition: View.cs:208
Vector3 _scale
Definition: View.cs:49
Image Background
Näytön taustakuva.
Definition: View.cs:108
double Height
Näytön korkeus y-suunnassa.
Definition: View.cs:235
static Matrix ToXnaCoords(ref Matrix matrix, Vector screenSize, Vector scale)
Muuntaa matriisin Jypelin ruutukoordinaateista XNA:n ruutukoordinaatteihin.
Definition: View.cs:404
bool IsFlipped
Peilataanko kuva pystysuunnassa.
Definition: View.cs:157
Vector2 _scale2
Definition: View.cs:50
RenderTarget2D _renderTarget
Definition: View.cs:46
Point _size
Definition: View.cs:52
double Bottom
Näytön alareunan y-koordinaatti.
Definition: View.cs:316
Color BackgroundColor
Ruudun "taustalla" näkyvä väri jos ruutua on kierretty tai se on pienempi kuin ikkuna.
Definition: View.cs:125
double Top
Näytön yläreunan y-koordinaatti.
Definition: View.cs:308
Vector3 _scaleInv
Definition: View.cs:51
GraphicsDevice device
Näyttölaite.
Definition: View.cs:63
RenderTarget2D RenderTarget
Tekstuuri johon näkymä piirretään.
Definition: View.cs:69
float _angle
Definition: View.cs:53
bool IsMirrored
Peilataanko kuva vaakasuunnassa.
Definition: View.cs:178
double HeightSafe
Korkeus johon lisätty pieni marginaali
Definition: View.cs:348
bool _flipAndMirror
Definition: View.cs:55
double Left
Näytön vasemman reunan x-koordinaatti.
Definition: View.cs:292
Image Image
Ruudulla näkyvä kuva.
Definition: View.cs:100
double RightSafe
Oikean reunan sijainti johon lisätty pieni marginaali
Definition: View.cs:328
void Render()
Definition: View.cs:442
Matrix GetScreenTransform()
Palauttaa transformaatiomatriisin jolla voi ottaa huomioon ruudun kokoon, kiertoon ja paikkaan tehdyt...
Definition: View.cs:421
SpriteEffects _effect
Definition: View.cs:54
double ViewportHeight
Näytön todellinen korkeus.
Definition: View.cs:273
SpriteBatch renderBatch
Definition: View.cs:47
double BottomSafe
Alareunan sijainti johon lisätty pieni marginaali
Definition: View.cs:333
double ViewportWidth
Näytön todellinen leveys.
Definition: View.cs:262
void ScaleToFit()
Skaalaa näkymän mahtumaan ruudulle
Definition: View.cs:353
Vector2 _center
Definition: View.cs:48
Vector Center
Näytön keskipiste.
Definition: View.cs:139
static Vector FromXnaCoords(Vector2 position, Vector screenSize, Vector objectSize)
Muuntaa XNA:n ruutukoordinaateista Jypelin ruutukoordinaateiksi.
Definition: View.cs:366
double Right
Näytön oikean reunan x-koordinaatti.
Definition: View.cs:300
XnaColor _bgcolor
Definition: View.cs:57
Olio jolla on reunat.
Definition: Dimensional.cs:7
VerticalAlignment
Asemointi pystysuunnassa.
Definition: View.cs:487
HorizontalAlignment
Asemointi vaakasuunnassa.
Definition: View.cs:466
@ Right
Kallistetaan oikealle.
@ Left
Kallistetaan vasemalle.
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Suuntakulma (rajoitettu -180 ja 180 asteen välille) asteina ja radiaaneina. Tietoja kulmasta: http://...
Definition: Angle.cs:40
static Angle FromRadians(double radian)
Luo kulman annettujen radiaanien mukaan.
Definition: Angle.cs:315
Väri.
Definition: Color.cs:13
2D-vektori.
Definition: Vector.cs:67
double Y
Vektorin Y-komponentti
Definition: Vector.cs:339
double X
Vektorin X-komponentti.
Definition: Vector.cs:334