source: 2014/24/EemeliK/Zombieland/Jypeli/Graphics/Graphics.cs @ 5974

Revision 5974, 8.0 KB checked in by empaheik, 4 years ago (diff)
Line 
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;
29using Jypeli.Effects;
30
31namespace Jypeli
32{
33    /// <summary>
34    /// Contains graphics resources.
35    /// </summary>
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        public static void Initialize()
74        {
75            GraphicsDevice device = Game.GraphicsDevice;
76
77            device.SamplerStates[0] = GetDefaultSamplerState();
78
79            BasicTextureEffect = new BasicEffect( device );
80            // This must be set to false for textures to work with BasicEffect.
81            BasicTextureEffect.VertexColorEnabled = false;
82            BasicTextureEffect.TextureEnabled = true;
83
84            BasicColorEffect = new BasicEffect( device );
85            BasicColorEffect.VertexColorEnabled = true;
86            BasicColorEffect.TextureEnabled = false;
87
88#if !WINDOWS_PHONE
89            LightingEffect = Game.ResourceContent.Load<Effect>( "Lighting" );
90#endif
91
92            SpriteBatch = new SpriteBatch( device );
93            ImageBatch.Initialize();
94            ShapeBatch.Initialize();
95
96            ResetScreenSize();
97            Game.GraphicsDevice.DeviceReset += GraphicsDevice_DeviceReset;
98        }
99
100        public static DepthFormat SelectStencilMode()
101        {
102            GraphicsAdapter adapter = GraphicsAdapter.DefaultAdapter;
103            SurfaceFormat format = adapter.CurrentDisplayMode.Format;
104            return DepthFormat.Depth24Stencil8;
105            /*if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth24Stencil8 ) )
106                return DepthFormat.Depth24Stencil8;
107            else if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth24Stencil8Single ) )
108                return DepthFormat.Depth24Stencil8Single;
109            else if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth24Stencil4 ) )
110                return DepthFormat.Depth24Stencil4;
111            else if ( adapter.CheckDepthStencilMatch( DeviceType.Hardware, format, format, DepthFormat.Depth15Stencil1 ) )
112                return DepthFormat.Depth15Stencil1;
113            else
114                throw new ApplicationException( "Could Not Find Stencil Buffer for Default Adapter" );*/
115        }
116
117        private static void GraphicsDevice_DeviceReset( object sender, EventArgs e )
118        {
119            ResetScreenSize();
120        }
121
122        public static Effect GetTextureEffect( ref Matrix worldMatrix, Texture2D texture, bool lightingEnabled )
123        {
124#if !WINDOWS_PHONE
125            if ( lightingEnabled && is_PS_2_0_supported )
126            {
127                Effect effect = GetLightingEffect( ref worldMatrix );
128                effect.CurrentTechnique = effect.Techniques["TextureLighting"];
129                effect.Parameters["xTexture"].SetValue( texture );
130                return effect;
131            }
132            else
133#endif
134            {
135                BasicEffect effect = BasicTextureEffect;
136                effect.Alpha = 1.0f;
137                effect.World = worldMatrix;
138                effect.Texture = texture;
139                return effect;
140            }
141        }
142
143        public static Effect GetColorEffect( ref Matrix worldMatrix, bool lightingEnabled )
144        {
145#if !WINDOWS_PHONE
146            if ( lightingEnabled && is_PS_2_0_supported )
147            {
148                Effect effect = GetLightingEffect( ref worldMatrix );
149                effect.CurrentTechnique = effect.Techniques["ColorLighting"];
150                return effect;
151            }
152            else
153#endif
154            {
155                BasicEffect effect = BasicColorEffect;
156                effect.World = worldMatrix;
157                return effect;
158            }
159        }
160
161#if !WINDOWS_PHONE
162        private static Effect GetLightingEffect( ref Matrix worldMatrix )
163        {
164            Effect effect = LightingEffect;
165
166            Vector3 lightPos = new Vector3( 0, 0, 40 );
167            float lightPower = 0.0f;
168
169            if ( Game.Lights.Count > 0 )
170            {
171                Light light = Game.Lights[0];
172                lightPos = new Vector3( (float)light.Position.X, (float)light.Position.Y, (float)light.Distance );
173                lightPower = (float)light.Intensity;
174            }
175
176            Vector3 transformedLightPos;
177            Vector3.Transform( ref lightPos, ref worldMatrix, out transformedLightPos );
178
179            effect.Parameters["xWorldViewProjection"].SetValue( worldMatrix * viewProjectionMatrix );
180            effect.Parameters["xWorld"].SetValue( worldMatrix );
181            effect.Parameters["xLightPos"].SetValue( transformedLightPos );
182            effect.Parameters["xLightPower"].SetValue( lightPower );
183            effect.Parameters["xAmbient"].SetValue( (float)Game.Instance.Level.AmbientLight );
184
185            return effect;
186        }
187#endif
188
189        private static void ResetScreenSize()
190        {
191            GraphicsDevice device = Game.GraphicsDevice;
192
193            ViewMatrix = Matrix.CreateLookAt(
194                new Vector3( 0.0f, 0.0f, 1.0f ),
195                Vector3.Zero,
196                Vector3.Up
197                );
198            ProjectionMatrix = Matrix.CreateOrthographic(
199                (float)device.Viewport.Width,
200                (float)device.Viewport.Height,
201                1.0f, 2.0f
202                );
203
204            viewProjectionMatrix = ViewMatrix * ProjectionMatrix;
205
206            BasicColorEffect.View = ViewMatrix;
207            BasicColorEffect.Projection = ProjectionMatrix;
208            BasicTextureEffect.View = ViewMatrix;
209            BasicTextureEffect.Projection = ProjectionMatrix;
210        }
211    }
212}
Note: See TracBrowser for help on using the repository browser.