/// <summary>
        /// Initializes the game. Loads all resources.
        /// </summary>
        protected override void InitializeGame()
        {
            Cursor.Hide();

            mouse = new MouseDevice(this);

            renderer.ProjectionMode = ProjectionMode.Orthogonal;
            renderer.ViewMatrix = Matrix.LookAtLH(new Vector3(0, 0, 5.0f), new Vector3(),
                new Vector3(0, 1, 0));

            effect = GlobalResourceCache.CreateEffectFromFile(renderer,
                "Effect Files\\Dynamic2DLightingEffect.fx");

            rectMesh = Mesh.Rectangle(renderer, Color.White, renderer.FullscreenSize.Width,
                renderer.FullscreenSize.Height, 1.0f);

            material = GlobalResourceCache.CreateMaterialFromFile(renderer, "Materials\\roughWallMaterial.xml");

            light = new Light(renderer, 350, 1.0f, new Vector2(), Color.Red);

            sceneImage = new Texture(renderer, renderer.FullscreenSize.Width, renderer.FullscreenSize.Height,
                true);

            lightMesh = Mesh.Circle(renderer, Color.Yellow, Color.Yellow, 6, 16);

            bloomPostProcessor = new BloomPostProcessor(renderer);
            bloomPostProcessor.Blur = 3.5f;
            bloomPostProcessor.BloomScale = 1.5f;
            bloomPostProcessor.BrightPassThreshold = 0.4f;

            poly1 = new ConvexHull(renderer, Mesh.Circle(renderer, Color.Blue, Color.Blue, 65, 8));
            poly1.Position = new Vector2(-150.0f, 150.0f);

            poly2 = new ConvexHull(renderer, Mesh.Circle(renderer, Color.Red, Color.Purple, 50, 4));
            poly2.Position = new Vector2(200.0f, 0.0f);

            poly3 = new ConvexHull(renderer, Mesh.Circle(renderer, Color.SaddleBrown, Color.SeaGreen,
                60, 32));
            poly3.Position = new Vector2(-250.0f, -200.0f);

            this.KeyDown += new KeyEventHandler(OnKeyDown);
        }
示例#2
0
        public static Material FromFile(Renderer renderer, string xmlFilename)
        {
            if (renderer == null)
                throw new ArgumentNullException("renderer",
                    "Cannot create a material with a null renderer reference.");

            if (String.IsNullOrEmpty(xmlFilename))
                throw new ArgumentNullException("xmlFilename",
                    "Cannot load a material without a valid filename.");

            if (!File.Exists(xmlFilename))
                throw new FileNotFoundException(xmlFilename);

            Material material = new Material(renderer);
            XmlTextReader reader = new XmlTextReader(xmlFilename);

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.LocalName == "Ambient")
                    {
                        int a = int.Parse(reader.GetAttribute(0));
                        int r = int.Parse(reader.GetAttribute(1));
                        int g = int.Parse(reader.GetAttribute(2));
                        int b = int.Parse(reader.GetAttribute(3));

                        material.Ambient = Color.FromArgb(a, r, g, b);
                    }
                    else if (reader.LocalName == "Diffuse")
                    {
                        int a = int.Parse(reader.GetAttribute(0));
                        int r = int.Parse(reader.GetAttribute(1));
                        int g = int.Parse(reader.GetAttribute(2));
                        int b = int.Parse(reader.GetAttribute(3));

                        material.Diffuse = Color.FromArgb(a, r, g, b);
                    }
                    else if (reader.LocalName == "Specular")
                    {
                        int a = int.Parse(reader.GetAttribute(0));
                        int r = int.Parse(reader.GetAttribute(1));
                        int g = int.Parse(reader.GetAttribute(2));
                        int b = int.Parse(reader.GetAttribute(3));

                        material.Specular = Color.FromArgb(a, r, g, b);
                    }
                    else if (reader.LocalName == "Shininess")
                    {
                        float shininess = float.Parse(reader.ReadString());
                        material.Shininess = shininess;
                    }
                    else if (reader.LocalName == "DiffuseMap")
                    {
                        string filename = reader.ReadString();
                        material.DiffuseMap = GlobalResourceCache.CreateTextureFromFile(renderer,
                            filename);
                    }
                    else if (reader.LocalName == "NormalMap")
                    {
                        string filename = reader.ReadString();
                        material.NormalMap = GlobalResourceCache.CreateTextureFromFile(renderer,
                            filename);
                    }
                    else if (reader.LocalName == "HeightMap")
                    {
                        string filename = reader.ReadString();
                        material.HeightMap = GlobalResourceCache.CreateTextureFromFile(renderer,
                            filename);
                    }
                }
            }

            return material;
        }