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