27 using Microsoft.Xna.Framework;
28 using Microsoft.Xna.Framework.Graphics;
36 internal static class Graphics
38 public static BasicEffect BasicTextureEffect;
39 public static BasicEffect BasicColorEffect;
41 public static SpriteBatch SpriteBatch;
46 public static ImageBatch ImageBatch =
new ImageBatch();
47 public static ShapeBatch ShapeBatch =
new ShapeBatch();
48 public static LineBatch LineBatch =
new LineBatch();
50 public static Canvas Canvas =
new Canvas();
53 static Effect LightingEffect;
56 private static Matrix ViewMatrix;
57 private static Matrix ProjectionMatrix;
58 private static Matrix viewProjectionMatrix;
62 private static bool is_PS_2_0_supported =
true;
65 private static bool is_PS_2_0_supported =
false;
68 public static SamplerState GetDefaultSamplerState()
70 return Game.SmoothTextures ? SamplerState.LinearClamp : SamplerState.PointClamp;
73 internal static readonly TextureCoordinates DefaultTextureCoords =
new TextureCoordinates()
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 ),
81 public static void Initialize()
83 GraphicsDevice device = Game.GraphicsDevice;
85 device.SamplerStates[0] = GetDefaultSamplerState();
87 BasicTextureEffect =
new BasicEffect( device );
89 BasicTextureEffect.VertexColorEnabled =
false;
90 BasicTextureEffect.TextureEnabled =
true;
92 BasicColorEffect =
new BasicEffect( device );
93 BasicColorEffect.VertexColorEnabled =
true;
94 BasicColorEffect.TextureEnabled =
false;
97 LightingEffect = Game.ResourceContent.Load<Effect>(
"Lighting" );
100 SpriteBatch =
new SpriteBatch( device );
101 ImageBatch.Initialize();
102 ShapeBatch.Initialize();
105 Game.GraphicsDevice.DeviceReset += GraphicsDevice_DeviceReset;
108 public static DepthFormat SelectStencilMode()
110 GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter;
111 SurfaceFormat format = adapter.CurrentDisplayMode.Format;
112 return DepthFormat.Depth24Stencil8;
125 private static void GraphicsDevice_DeviceReset(
object sender, EventArgs e )
130 public static Effect GetTextureEffect( ref Matrix worldMatrix, Texture2D texture,
bool lightingEnabled )
133 if ( lightingEnabled && is_PS_2_0_supported )
135 Effect effect = GetLightingEffect( ref worldMatrix );
136 effect.CurrentTechnique = effect.Techniques[
"TextureLighting"];
137 effect.Parameters[
"xTexture"].SetValue( texture );
143 BasicEffect effect = BasicTextureEffect;
145 effect.World = worldMatrix;
146 effect.Texture = texture;
151 public static Effect GetColorEffect( ref Matrix worldMatrix,
bool lightingEnabled )
154 if ( lightingEnabled && is_PS_2_0_supported )
156 Effect effect = GetLightingEffect( ref worldMatrix );
157 effect.CurrentTechnique = effect.Techniques[
"ColorLighting"];
163 BasicEffect effect = BasicColorEffect;
164 effect.World = worldMatrix;
170 private static Effect GetLightingEffect( ref Matrix worldMatrix )
172 Effect effect = LightingEffect;
174 Vector3 lightPos =
new Vector3( 0, 0, 40 );
175 float lightPower = 0.0f;
177 if ( Game.Lights.Count > 0 )
179 Light light = Game.Lights[0];
184 Vector3 transformedLightPos;
185 Vector3.Transform( ref lightPos, ref worldMatrix, out transformedLightPos );
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 );
197 private static void ResetScreenSize()
199 GraphicsDevice device = Game.GraphicsDevice;
201 ViewMatrix = Matrix.CreateLookAt(
202 new Vector3( 0.0f, 0.0f, 1.0f ),
206 ProjectionMatrix = Matrix.CreateOrthographic(
207 (
float)device.Viewport.Width,
208 (
float)device.Viewport.Height,
212 viewProjectionMatrix = ViewMatrix * ProjectionMatrix;
214 BasicColorEffect.View = ViewMatrix;
215 BasicColorEffect.Projection = ProjectionMatrix;
216 BasicTextureEffect.View = ViewMatrix;
217 BasicTextureEffect.Projection = ProjectionMatrix;
double Distance
Etäisyys kohtisuoraan 2D-tasosta. Mitä kauempana valo on, sitä laajemman alueen se valaisee...
double Intensity
Voimakkuus väliltä [0.0, 1.0].