Jypeli  5
The simple game programming library
Graphics.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 #endregion
25 
26 using System;
27 using Microsoft.Xna.Framework;
28 using Microsoft.Xna.Framework.Graphics;
29 using Jypeli.Effects;
30 
31 namespace Jypeli
32 {
36  internal static class Graphics
37  {
38  public static BasicEffect BasicTextureEffect;
39  public static BasicEffect BasicColorEffect;
40 
41  public static SpriteBatch SpriteBatch;
42 
43  // Having global batch objects saves memory.
44  // The same batch object must not be used from more
45  // than one place at a time, though.
46  public static ImageBatch ImageBatch = new ImageBatch();
47  public static ShapeBatch ShapeBatch = new ShapeBatch();
48  public static LineBatch LineBatch = new LineBatch();
49 
50  public static Canvas Canvas = new Canvas();
51 
52 #if !WINDOWS_PHONE
53  static Effect LightingEffect;
54 #endif
55 
56  private static Matrix ViewMatrix;
57  private static Matrix ProjectionMatrix;
58  private static Matrix viewProjectionMatrix;
59 
60 #if !WINDOWS_PHONE
61  // XNA 4.0 requires PS 2.0
62  private static bool is_PS_2_0_supported = true;
63 #else
64  // ...except on windows phone.
65  private static bool is_PS_2_0_supported = false;
66 #endif
67 
68  public static SamplerState GetDefaultSamplerState()
69  {
70  return Game.SmoothTextures ? SamplerState.LinearClamp : SamplerState.PointClamp;
71  }
72 
73  internal static readonly TextureCoordinates DefaultTextureCoords = new TextureCoordinates()
74  {
75  TopLeft = new Vector2( 0.0f, 0.0f ),
76  TopRight = new Vector2( 1.0f, 0.0f ),
77  BottomLeft = new Vector2( 0.0f, 1.0f ),
78  BottomRight = new Vector2( 1.0f, 1.0f ),
79  };
80 
81  public static void Initialize()
82  {
83  GraphicsDevice device = Game.GraphicsDevice;
84 
85  device.SamplerStates[0] = GetDefaultSamplerState();
86 
87  BasicTextureEffect = new BasicEffect( device );
88  // This must be set to false for textures to work with BasicEffect.
89  BasicTextureEffect.VertexColorEnabled = false;
90  BasicTextureEffect.TextureEnabled = true;
91 
92  BasicColorEffect = new BasicEffect( device );
93  BasicColorEffect.VertexColorEnabled = true;
94  BasicColorEffect.TextureEnabled = false;
95 
96 #if !WINDOWS_PHONE
97  LightingEffect = Game.ResourceContent.Load<Effect>( "Lighting" );
98 #endif
99 
100  SpriteBatch = new SpriteBatch( device );
101  ImageBatch.Initialize();
102  ShapeBatch.Initialize();
103 
104  ResetScreenSize();
105  Game.GraphicsDevice.DeviceReset += GraphicsDevice_DeviceReset;
106  }
107 
108  public static DepthFormat SelectStencilMode()
109  {
110  GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter;
111  SurfaceFormat format = adapter.CurrentDisplayMode.Format;
112  return DepthFormat.Depth24Stencil8;
113  /*if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth24Stencil8 ) )
114  return DepthFormat.Depth24Stencil8;
115  else if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth24Stencil8Single ) )
116  return DepthFormat.Depth24Stencil8Single;
117  else if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth24Stencil4 ) )
118  return DepthFormat.Depth24Stencil4;
119  else if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth15Stencil1 ) )
120  return DepthFormat.Depth15Stencil1;
121  else
122  throw new ApplicationException( "Could Not Find Stencil Buffer for Default Adapter" );*/
123  }
124 
125  private static void GraphicsDevice_DeviceReset( object sender, EventArgs e )
126  {
127  ResetScreenSize();
128  }
129 
130  public static Effect GetTextureEffect( ref Matrix worldMatrix, Texture2D texture, bool lightingEnabled )
131  {
132 #if !WINDOWS_PHONE
133  if ( lightingEnabled && is_PS_2_0_supported )
134  {
135  Effect effect = GetLightingEffect( ref worldMatrix );
136  effect.CurrentTechnique = effect.Techniques["TextureLighting"];
137  effect.Parameters["xTexture"].SetValue( texture );
138  return effect;
139  }
140  else
141 #endif
142  {
143  BasicEffect effect = BasicTextureEffect;
144  effect.Alpha = 1.0f;
145  effect.World = worldMatrix;
146  effect.Texture = texture;
147  return effect;
148  }
149  }
150 
151  public static Effect GetColorEffect( ref Matrix worldMatrix, bool lightingEnabled )
152  {
153 #if !WINDOWS_PHONE
154  if ( lightingEnabled && is_PS_2_0_supported )
155  {
156  Effect effect = GetLightingEffect( ref worldMatrix );
157  effect.CurrentTechnique = effect.Techniques["ColorLighting"];
158  return effect;
159  }
160  else
161 #endif
162  {
163  BasicEffect effect = BasicColorEffect;
164  effect.World = worldMatrix;
165  return effect;
166  }
167  }
168 
169 #if !WINDOWS_PHONE
170  private static Effect GetLightingEffect( ref Matrix worldMatrix )
171  {
172  Effect effect = LightingEffect;
173 
174  Vector3 lightPos = new Vector3( 0, 0, 40 );
175  float lightPower = 0.0f;
176 
177  if ( Game.Lights.Count > 0 )
178  {
179  Light light = Game.Lights[0];
180  lightPos = new Vector3( (float)light.Position.X, (float)light.Position.Y, (float)light.Distance );
181  lightPower = (float)light.Intensity;
182  }
183 
184  Vector3 transformedLightPos;
185  Vector3.Transform( ref lightPos, ref worldMatrix, out transformedLightPos );
186 
187  effect.Parameters["xWorldViewProjection"].SetValue( worldMatrix * viewProjectionMatrix );
188  effect.Parameters["xWorld"].SetValue( worldMatrix );
189  effect.Parameters["xLightPos"].SetValue( transformedLightPos );
190  effect.Parameters["xLightPower"].SetValue( lightPower );
191  effect.Parameters["xAmbient"].SetValue( (float)Game.Instance.Level.AmbientLight );
192 
193  return effect;
194  }
195 #endif
196 
197  private static void ResetScreenSize()
198  {
199  GraphicsDevice device = Game.GraphicsDevice;
200 
201  ViewMatrix = Matrix.CreateLookAt(
202  new Vector3( 0.0f, 0.0f, 1.0f ),
203  Vector3.Zero,
204  Vector3.Up
205  );
206  ProjectionMatrix = Matrix.CreateOrthographic(
207  (float)device.Viewport.Width,
208  (float)device.Viewport.Height,
209  1.0f, 2.0f
210  );
211 
212  viewProjectionMatrix = ViewMatrix * ProjectionMatrix;
213 
214  BasicColorEffect.View = ViewMatrix;
215  BasicColorEffect.Projection = ProjectionMatrix;
216  BasicTextureEffect.View = ViewMatrix;
217  BasicTextureEffect.Projection = ProjectionMatrix;
218  }
219  }
220 }
double Distance
Etäisyys kohtisuoraan 2D-tasosta. Mitä kauempana valo on, sitä laajemman alueen se valaisee...
Definition: Light.cs:45
Vector Position
Paikka.
Definition: Light.cs:39
double Intensity
Voimakkuus väliltä [0.0, 1.0].
Definition: Light.cs:50
double Y
Definition: Vector.cs:275
Pistemäinen valonlähde.
Definition: Light.cs:34
double X
Definition: Vector.cs:274