Jypeli  9
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 
30 namespace Jypeli
31 {
35  internal static class Graphics
36  {
37  public static BasicEffect BasicTextureEffect;
38  public static BasicEffect BasicColorEffect;
39 
40  public static SpriteBatch SpriteBatch;
41 
42  // Having global batch objects saves memory.
43  // The same batch object must not be used from more
44  // than one place at a time, though.
45  internal static ImageBatch ImageBatch = new ImageBatch();
46  internal static ShapeBatch ShapeBatch = new ShapeBatch();
47  internal static LineBatch LineBatch = new LineBatch();
48 
49  public static Canvas Canvas = new Canvas();
50 
52  {
53  TopLeft = new Vector2( 0.0f, 0.0f ),
54  TopRight = new Vector2( 1.0f, 0.0f ),
55  BottomLeft = new Vector2( 0.0f, 1.0f ),
56  BottomRight = new Vector2( 1.0f, 1.0f ),
57  };
58 
59 #if !WINDOWS_PHONE && !DISABLE_LIGHTING_EFFECT
60  static Effect LightingEffect;
61 #endif
62 
63  private static Matrix ViewMatrix;
64  private static Matrix ProjectionMatrix;
65  private static Matrix viewProjectionMatrix;
66 
67 #if !WINDOWS_PHONE && !DISABLE_LIGHTING_EFFECT
68  // XNA 4.0 requires PS 2.0
69  private static bool is_PS_2_0_supported = true;
70 #elif !DISABLE_LIGHTING_EFFECT
71  // ...except on windows phone.
72  private static bool is_PS_2_0_supported = false;
73 #endif
74 
75  public static SamplerState GetDefaultSamplerState()
76  {
77  return Game.SmoothTextures ? SamplerState.LinearClamp : SamplerState.PointClamp;
78  }
79 
80  public static void Initialize()
81  {
82  GraphicsDevice device = Game.GraphicsDevice;
83 
84  BasicTextureEffect = new BasicEffect( device );
85  // This must be set to false for textures to work with BasicEffect.
86  BasicTextureEffect.VertexColorEnabled = false;
87  BasicTextureEffect.TextureEnabled = true;
88 
89  BasicColorEffect = new BasicEffect( device );
90  BasicColorEffect.VertexColorEnabled = true;
91  BasicColorEffect.TextureEnabled = false;
92 
93 #if !WINDOWS_PHONE && !DISABLE_LIGHTING_EFFECT
94  // A hack until we can use Game.ResourceContent.Load<Effect>( "Lighting" )
96 #endif
97 
98  SpriteBatch = new SpriteBatch( device );
101 
102  ResetScreenSize();
103 #if !LINUX
105 #endif
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 SamplerState storedSamplerState;
126 
127  public static void SetSamplerState()
128  {
129  storedSamplerState = Game.GraphicsDevice.SamplerStates[0];
130  Game.GraphicsDevice.SamplerStates[0] = GetDefaultSamplerState();
131  }
132 
133  public static void ResetSamplerState()
134  {
135  Game.GraphicsDevice.SamplerStates[0] = storedSamplerState;
136  }
137 
138  private static void GraphicsDevice_DeviceReset( object sender, EventArgs e )
139  {
140  ResetScreenSize();
141  }
142 
143  public static Effect GetTextureEffect( ref Matrix worldMatrix, Texture2D texture, bool lightingEnabled )
144  {
145 #if !WINDOWS_PHONE && !DISABLE_LIGHTING_EFFECT
146  if ( lightingEnabled && is_PS_2_0_supported )
147  {
148  Effect effect = GetLightingEffect( ref worldMatrix );
149  effect.CurrentTechnique = effect.Techniques["TextureLighting"];
150  effect.Parameters["xTexture"].SetValue( texture );
151  return effect;
152  }
153  else
154 #endif
155  {
156  BasicEffect effect = BasicTextureEffect;
157  effect.Alpha = 1.0f;
158  effect.World = worldMatrix;
159  effect.Texture = texture;
160  return effect;
161  }
162  }
163 
164  public static Effect GetColorEffect( ref Matrix worldMatrix, bool lightingEnabled )
165  {
166 #if !WINDOWS_PHONE && !DISABLE_LIGHTING_EFFECT
167  if ( lightingEnabled && is_PS_2_0_supported )
168  {
169  Effect effect = GetLightingEffect( ref worldMatrix );
170  effect.CurrentTechnique = effect.Techniques["ColorLighting"];
171  return effect;
172  }
173  else
174 #endif
175  {
176  BasicEffect effect = BasicColorEffect;
177  effect.World = worldMatrix;
178  return effect;
179  }
180  }
181 
182 #if !WINDOWS_PHONE && !DISABLE_LIGHTING_EFFECT
183  private static Effect GetLightingEffect( ref Matrix worldMatrix )
184  {
185  Effect effect = LightingEffect;
186 
187  Vector3 lightPos = new Vector3( 0, 0, 40 );
188  float lightPower = 0.0f;
189 
190  if ( Game.Lights.Count > 0 )
191  {
192  Light light = Game.Lights[0];
193  lightPos = new Vector3( (float)light.Position.X, (float)light.Position.Y, (float)light.Distance );
194  lightPower = (float)light.Intensity;
195  }
196 
197  Vector3 transformedLightPos;
198  Vector3.Transform( ref lightPos, ref worldMatrix, out transformedLightPos );
199 
200  effect.Parameters["xWorldViewProjection"].SetValue( worldMatrix * viewProjectionMatrix );
201  effect.Parameters["xWorld"].SetValue( worldMatrix );
202  effect.Parameters["xLightPos"].SetValue( transformedLightPos );
203  effect.Parameters["xLightPower"].SetValue( lightPower );
204  effect.Parameters["xAmbient"].SetValue( (float)Game.Instance.Level.AmbientLight );
205 
206  return effect;
207  }
208 #endif
209 
210  public static void ResetScreenSize()
211  {
212  GraphicsDevice device = Game.GraphicsDevice;
213 
214  ViewMatrix = Matrix.CreateLookAt(
215  new Vector3( 0.0f, 0.0f, 1.0f ),
216  Vector3.Zero,
217  Vector3.Up
218  );
219  ProjectionMatrix = Matrix.CreateOrthographic(
220  (float)Game.Screen.Width,
221  (float)Game.Screen.Height,
222  1.0f, 2.0f
223  );
224 
226 
228  BasicColorEffect.Projection = ProjectionMatrix;
231  }
232  }
233 }
Jypeli.Game.Lights
static List< Light > Lights
Valoefektit.
Definition: Effects.cs:15
Jypeli.Graphics.BasicColorEffect
static BasicEffect BasicColorEffect
Definition: Graphics.cs:38
Jypeli.Graphics.GetColorEffect
static Effect GetColorEffect(ref Matrix worldMatrix, bool lightingEnabled)
Definition: Graphics.cs:164
Jypeli.ShapeBatch.Initialize
void Initialize()
Definition: ShapeBatch.cs:53
Jypeli.Graphics.ImageBatch
static ImageBatch ImageBatch
Definition: Graphics.cs:45
Jypeli.Matrix
Microsoft.Xna.Framework.Matrix Matrix
Definition: Mouse.cs:36
Microsoft.Xna
Definition: JypeliContentManager.cs:6
Jypeli.Content.Lighting.rawData
static byte[] rawData
Definition: Lighting.cs:12
Jypeli.Graphics.ShapeBatch
static ShapeBatch ShapeBatch
Definition: Graphics.cs:46
Jypeli.Graphics.DefaultTextureCoords
static readonly TextureCoordinates DefaultTextureCoords
Definition: Graphics.cs:51
Jypeli
Definition: Automobile.cs:5
Jypeli.Graphics.LineBatch
static LineBatch LineBatch
Definition: Graphics.cs:47
Jypeli.ShapeBatch
Draws simple shapes efficiently. Draw() calls should be made only between Begin() and End() calls....
Definition: ShapeBatch.cs:20
Microsoft
Definition: JypeliContentManager.cs:6
Microsoft.Xna.Framework
Definition: JypeliContentManager.cs:6
Jypeli.ImageBatch.Initialize
void Initialize()
Definition: ImageBatch.cs:65
Jypeli.Graphics.GetLightingEffect
static Effect GetLightingEffect(ref Matrix worldMatrix)
Definition: Graphics.cs:183
Jypeli.Level.AmbientLight
double AmbientLight
Definition: Level.cs:79
Jypeli.ScreenView.Width
double Width
Näytön leveys x-suunnassa.
Definition: View.cs:222
Jypeli.Content.Lighting
Definition: Lighting.cs:11
Jypeli.Canvas
Piirtoalusta.
Definition: Canvas.cs:39
Jypeli.Graphics.ResetScreenSize
static void ResetScreenSize()
Definition: Graphics.cs:210
Jypeli.Graphics.ProjectionMatrix
static Matrix ProjectionMatrix
Definition: Graphics.cs:64
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Graphics.SelectStencilMode
static DepthFormat SelectStencilMode()
Definition: Graphics.cs:108
Jypeli.Graphics.GetDefaultSamplerState
static SamplerState GetDefaultSamplerState()
Definition: Graphics.cs:75
Jypeli.Graphics.is_PS_2_0_supported
static bool is_PS_2_0_supported
Definition: Graphics.cs:69
Jypeli.TextureCoordinates
Definition: ImageBatch.cs:14
Jypeli.ScreenView.Height
double Height
Näytön korkeus y-suunnassa.
Definition: View.cs:235
Jypeli.Graphics.GetTextureEffect
static Effect GetTextureEffect(ref Matrix worldMatrix, Texture2D texture, bool lightingEnabled)
Definition: Graphics.cs:143
Jypeli.Game.Screen
static ScreenView Screen
Näytön dimensiot, eli koko ja reunat.
Definition: Graphics.cs:90
Jypeli.Graphics.BasicTextureEffect
static BasicEffect BasicTextureEffect
Definition: Graphics.cs:37
Jypeli.Graphics.storedSamplerState
static SamplerState storedSamplerState
Definition: Graphics.cs:125
Jypeli.Graphics.GraphicsDevice_DeviceReset
static void GraphicsDevice_DeviceReset(object sender, EventArgs e)
Definition: Graphics.cs:138
Jypeli.Graphics.SpriteBatch
static SpriteBatch SpriteBatch
Definition: Graphics.cs:40
Jypeli.Game.SmoothTextures
static bool SmoothTextures
Tekstuurien (kuvien) reunanpehmennys skaalattaessa (oletus päällä).
Definition: Graphics.cs:95
Jypeli.Graphics
Contains graphics resources.
Definition: Graphics.cs:36
System
Definition: CFFauxAttributes.cs:29
Jypeli.Graphics.ResetSamplerState
static void ResetSamplerState()
Definition: Graphics.cs:133
Jypeli.Game.GraphicsDevice
static new GraphicsDevice GraphicsDevice
XNA:n grafiikkakortti.
Definition: Graphics.cs:49
Jypeli.ImageBatch
Draws images efficiently. Draw() calls should be made only between Begin() and End() calls....
Definition: ImageBatch.cs:32
Jypeli.Graphics.ViewMatrix
static Matrix ViewMatrix
Definition: Graphics.cs:63
Jypeli.Graphics.viewProjectionMatrix
static Matrix viewProjectionMatrix
Definition: Graphics.cs:65
Jypeli.LineBatch
Definition: LineBatch.cs:8
Jypeli.Game
Definition: Content.cs:46
Jypeli.Graphics.SetSamplerState
static void SetSamplerState()
Definition: Graphics.cs:127
Jypeli.Graphics.Initialize
static void Initialize()
Definition: Graphics.cs:80
Jypeli.Graphics.LightingEffect
static Effect LightingEffect
Definition: Graphics.cs:60
Jypeli.Content
Definition: Lighting.cs:8
Jypeli.Graphics.Canvas
static Canvas Canvas
Definition: Graphics.cs:49
Jypeli.Game.Level
Level Level
Aktiivinen kenttä.
Definition: Game.cs:133