Ejemplo n.º 1
0
        public DemoGame()
            : base()
        {
            this.Window.Title = "Snowball Demo Game";

            this.graphicsDevice = new GraphicsDevice(this.Window);
            this.Services.AddService(typeof(IGraphicsDevice), this.graphicsDevice);

            this.keyboard = new Keyboard();
            this.Services.AddService(typeof(IKeyboard), this.keyboard);

            this.mouse = new Mouse(this.Window);
            this.Services.AddService(typeof(IMouse), this.mouse);

            this.gamePad = new GamePad(GamePadIndex.One);

            this.soundDevice = new SoundDevice();
            this.Services.AddService(typeof(ISoundDevice), this.soundDevice);

            this.starfield = new Starfield(this.graphicsDevice);

            this.ship = new Ship(this.graphicsDevice, this.keyboard, this.gamePad);

            this.console = new GameConsole(this.Window, this.graphicsDevice);
            this.console.InputEnabled = true;
            this.console.InputColor = Color.Blue;
            this.console.InputReceived += (s, e) => { this.console.WriteLine(e.Text); };
            this.console.IsVisibleChanged += (s, e) => { this.console.WriteLine("Console toggled."); };

            this.contentManager = new ContentManager<DemoGameContent>(this.Services);

            this.RegisterContent();
        }
        public GraphicsDeviceEventArgs(GraphicsDevice graphicsDevice)
            : base()
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            this.GraphicsDevice = graphicsDevice;
        }
        public TurtleGraphicsRenderer(GraphicsDevice graphicsDevice)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            this.graphicsDevice = graphicsDevice;
            this.renderer = new Renderer(this.graphicsDevice);

            this.positionStack = new Stack<Vector2>();
        }
Ejemplo n.º 4
0
        public WavingFlagSample()
            : base()
        {
            this.Window.Title = "Snowball Waving Flag Sample";

            this.graphicsDevice = new GraphicsDevice(this.Window);
            this.Services.AddService(typeof(IGraphicsDevice), this.graphicsDevice);

            this.contentLoader = new ContentLoader(this.Services);
        }
Ejemplo n.º 5
0
        public TextBlockSample()
            : base()
        {
            this.Window.Title = "TextBlock Sample";

            this.graphicsDevice = new GraphicsDevice(this.Window);
            this.Services.AddService(typeof(IGraphicsDevice), this.graphicsDevice);

            this.contentLoader = new ContentLoader(this.Services);
            this.Services.AddService(typeof(IContentLoader), this.contentLoader);
        }
Ejemplo n.º 6
0
        public GamePadReader()
            : base()
        {
            this.Window.Title = "Snowball GamePad Reader";

            this.gamePad = new GamePad(GamePadIndex.One);

            this.graphicsDevice = new GraphicsDevice(this.Window);
            this.Services.AddService(typeof(IGraphicsDevice), this.graphicsDevice);

            this.contentLoader = new ContentLoader(this.Services);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="vertexBufferSize"></param>
        /// <param name="matrixStackSize"></param>
        /// <param name="colorStackSize"></param>
        public GraphicsBatch(GraphicsDevice graphicsDevice, int vertexBufferSize, int matrixStackSize)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            graphicsDevice.EnsureDeviceCreated();

            this.GraphicsDevice = graphicsDevice;

            this.vertexDeclaration = new D3D.VertexDeclaration(this.GraphicsDevice.InternalDevice, new[] {
                new D3D.VertexElement(0, 0, D3D.DeclarationType.Float4, D3D.DeclarationMethod.Default, D3D.DeclarationUsage.Position, 0),
                new D3D.VertexElement(0, 16, D3D.DeclarationType.Float4, D3D.DeclarationMethod.Default, D3D.DeclarationUsage.Color, 0),
                new D3D.VertexElement(0, 32, D3D.DeclarationType.Float2, D3D.DeclarationMethod.Default, D3D.DeclarationUsage.TextureCoordinate, 0),
                D3D.VertexElement.VertexDeclarationEnd
            });

            this.vertices = new Vertex[vertexBufferSize * 4];
            this.vertexCount = 0;

            this.indices = new short[vertexBufferSize * 6];

            for (short i = 0, vertex = 0; i < this.indices.Length; i += 6, vertex += 4)
            {
                this.indices[i] = vertex;
                this.indices[i + 1] = (short)(vertex + 1);
                this.indices[i + 2] = (short)(vertex + 2);
                this.indices[i + 3] = vertex;
                this.indices[i + 4] = (short)(vertex + 2);
                this.indices[i + 5] = (short)(vertex + 3);
            }

            this.basicEffect = new BasicEffect(this.GraphicsDevice);

            this.pixel = D3DHelper.CreateTexture(this.GraphicsDevice.InternalDevice, 1, 1, TextureUsage.None);

            SharpDX.DataRectangle dataRectangle = this.pixel.LockRectangle(0, D3D.LockFlags.None);

            using (SharpDX.DataStream dataStream = new SharpDX.DataStream(dataRectangle.DataPointer, dataRectangle.Pitch, true, true))
            {
                dataStream.WriteRange(new Color[] { Color.White });
            }

            this.pixel.UnlockRectangle(0);

            this.pixelSource = new Rectangle(0, 0, 1, 1);

            this.matrixStack = new Matrix[matrixStackSize];
            this.matrixStackCount = 0;

            this.texture = null;
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        public BasicEffect(GraphicsDevice graphicsDevice)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            graphicsDevice.EnsureDeviceCreated();

            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(@"Snowball.Graphics.BasicEffect.fx");

            if (stream == null)
                throw new FileNotFoundException("Failed to load BasicEffect.fx.");

            this.effect = Effect.FromStream(graphicsDevice, stream);
        }
Ejemplo n.º 9
0
        public WalkingWizardSample()
            : base()
        {
            this.Window.Title = "Snowball Walking Wizard Sample";

            this.graphicsDevice = new GraphicsDevice(this.Window);

            // Add the GraphicsDevice to the list of services. This is how ContentLoader finds the GraphicsDevice.
            this.Services.AddService(typeof(IGraphicsDevice), this.graphicsDevice);

            this.keyboard = new Keyboard();

            this.content = new ContentLoader(this.Services);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Internal constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="texture"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        internal Texture(GraphicsDevice graphicsDevice, D3D.Texture texture, int width, int height)
            : base()
        {
            if (texture == null)
                throw new ArgumentNullException("texture");

            graphicsDevice.EnsureDeviceCreated();

            this.Width = width;
            this.Height = height;
            this.Usage = TextureUsage.None;

            this.CreateInternalTexture(graphicsDevice, texture);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="usage"></param>
        public Texture(GraphicsDevice graphicsDevice, int width, int height, TextureUsage usage)
            : base()
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            graphicsDevice.EnsureDeviceCreated();

            this.Width = width;
            this.Height = height;
            this.Usage = usage;

            this.CalculateInternalSize(graphicsDevice);

            this.InternalTexture = D3DHelper.CreateTexture(graphicsDevice.InternalDevice, this.InternalWidth, this.InternalHeight, this.Usage);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Loads a Texture from a Stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="colorKey"></param>
        /// <returns></returns>
        public static Texture FromStream(GraphicsDevice graphicsDevice, Stream stream, Color? colorKey)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            graphicsDevice.EnsureDeviceCreated();

            int width;
            int height;

            using (Image image = Image.FromStream(stream))
            {
                width = image.Width;
                height = image.Height;
            }

            stream.Position = 0;

            int argb = 0;
            if (colorKey != null)
                argb = colorKey.Value.ToArgb();

            D3D.Texture texture = D3DHelper.TextureFromStream(graphicsDevice.InternalDevice, stream, width, height, argb);
            return new Texture(graphicsDevice, texture, width, height);
        }
Ejemplo n.º 13
0
 static GraphicsDeviceControl()
 {
     GraphicsDevice = new GraphicsDevice();
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="graphicsDevice"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 public Texture(GraphicsDevice graphicsDevice, int width, int height)
     : this(graphicsDevice, width, height, TextureUsage.None)
 {
 }
Ejemplo n.º 15
0
        private void CreateInternalTexture(GraphicsDevice graphicsDevice, D3D.Texture texture)
        {
            this.CalculateInternalSize(graphicsDevice);

            // Check if we need to resize
            if (this.InternalWidth == this.Width && this.InternalHeight == this.Height)
            {
                this.InternalTexture = texture;
                return;
            }

            this.InternalTexture = D3DHelper.CreateTexture(graphicsDevice.InternalDevice, this.InternalWidth, this.InternalHeight, TextureUsage.None);

            SharpDX.DataRectangle input = texture.LockRectangle(0, D3D.LockFlags.ReadOnly);
            SharpDX.DataStream inputStream = new SharpDX.DataStream(input.DataPointer, this.Height * input.Pitch, true, false);

            SharpDX.DataRectangle output = this.InternalTexture.LockRectangle(0, D3D.LockFlags.None);
            SharpDX.DataStream outputStream = new SharpDX.DataStream(output.DataPointer, this.InternalHeight * output.Pitch, true, true);

            byte[] buffer = new byte[4];

            for (int y = 0; y < this.Height; y++)
            {
                for (int x = 0; x < this.Width; x++)
                {
                    inputStream.Seek((y * input.Pitch) + (x * 4), SeekOrigin.Begin);
                    inputStream.Read(buffer, 0, 4);

                    outputStream.Seek((y * output.Pitch) + (x * 4), SeekOrigin.Begin);
                    outputStream.Write(buffer, 0, 4);
                }
            }

            texture.UnlockRectangle(0);
            this.InternalTexture.UnlockRectangle(0);

            texture.Dispose(); // Get rid of old texture
        }
Ejemplo n.º 16
0
        private void CalculateInternalSize(GraphicsDevice graphicsDevice)
        {
            this.InternalWidth = this.Width;
            this.InternalHeight = this.Height;

            if (graphicsDevice.TexturesMustBePowerOf2)
            {
                this.InternalWidth = (int)MathHelper.NextPowerOf2((uint)this.InternalWidth);
                this.InternalHeight = (int)MathHelper.NextPowerOf2((uint)this.InternalHeight);
            }

            if (graphicsDevice.TexturesMustBeSquare)
            {
                if (this.InternalWidth > this.InternalHeight)
                    this.InternalHeight = this.InternalWidth;
                else if (this.InternalHeight > this.InternalWidth)
                    this.InternalWidth = this.InternalHeight;
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="graphicsDevice"></param>
 public GraphicsBatch(GraphicsDevice graphicsDevice)
     : this(graphicsDevice, 1024, 8)
 {
 }
Ejemplo n.º 18
0
        /// <summary>
        /// Loads a Texture from a file.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="fileName"></param>
        /// <param name="colorKey"></param>
        /// <returns></returns>
        public static Texture FromFile(GraphicsDevice graphicsDevice, string fileName, Color? colorKey)
        {
            if (String.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");

            if (!File.Exists(fileName))
                throw new FileNotFoundException("Unable to load file \"" + fileName + "\".");

            using (Stream stream = File.OpenRead(fileName))
                return FromStream(graphicsDevice, stream, colorKey);
        }
Ejemplo n.º 19
0
        /// <summary>
        /// Loads a TextureFont from a stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static SpriteSheet FromStream(GraphicsDevice graphicsDevice, Stream stream, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if (loadTextureFunc == null)
                throw new ArgumentNullException("loadTextureFunc");

            graphicsDevice.EnsureDeviceCreated();

            List<Rectangle> rectangles = new List<Rectangle>();
            string textureFile = null;
            Color colorKey = Color.Transparent;

            try
            {
                using (var xml = new XmlTextReader(stream))
                {
                    xml.WhitespaceHandling = WhitespaceHandling.None;

                    xml.Read();

                    if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        xml.Read();

                    if (xml.NodeType != XmlNodeType.Element && xml.Name != "SpriteSheet")
                        throw new XmlException("Invalid SpriteSheet xml file.");

                    textureFile = xml.ReadRequiredAttributeValue("Texture");
                    colorKey = Color.FromHexString(xml.ReadAttributeValueOrDefault("BackgroundColor", "00000000"));

                    xml.Read();
                    while (xml.Name == "Frame")
                    {
                        Rectangle rectangle = new Rectangle(
                            xml.ReadRequiredAttributeValue<int>("X"),
                            xml.ReadRequiredAttributeValue<int>("Y"),
                            xml.ReadRequiredAttributeValue<int>("Width"),
                            xml.ReadRequiredAttributeValue<int>("Height"));

                        rectangles.Add(rectangle);
                        xml.Read();
                    }
                }
            }
            catch (XmlException ex)
            {
                throw new GraphicsException("An error occured while parsing the SpriteSheet xml file.", ex);
            }

            Texture texture = loadTextureFunc(textureFile, colorKey);

            if (texture == null)
                throw new InvalidOperationException("loadTextureFunc returned null.");

            return new SpriteSheet(texture, rectangles);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Loads a SpriteSheet from a file.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="fileName"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static SpriteSheet FromFile(GraphicsDevice graphicsDevice, string fileName, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (String.IsNullOrEmpty(fileName))
                throw new ArgumentNullException("fileName");

            if (!File.Exists(fileName))
                throw new FileNotFoundException("Unable to load file " + fileName + ".");

            using (Stream stream = File.OpenRead(fileName))
                return FromStream(graphicsDevice, stream, loadTextureFunc);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Loads a TextureFont from a stream.
        /// </summary>
        /// <param name="graphicsDevice"></param>
        /// <param name="stream"></param>
        /// <param name="loadTextureFunc"></param>
        /// <returns></returns>
        public static TextureFont FromStream(GraphicsDevice graphicsDevice, Stream stream, Func<string, Color?, Texture> loadTextureFunc)
        {
            if (graphicsDevice == null)
                throw new ArgumentNullException("graphicsDevice");

            if (stream == null)
                throw new ArgumentNullException("stream");

            if (loadTextureFunc == null)
                throw new ArgumentNullException("loadTextureFunc");

            graphicsDevice.EnsureDeviceCreated();

            Dictionary<char, Rectangle> rectangles = new Dictionary<char, Rectangle>();
            string textureFile = null;
            Color backgroundColor = Color.Transparent;
            string fontName = TextureFont.DefaultFontName;
            int fontSize = TextureFont.DefaultFontSize;
            int characterSpacing;
            int lineSpacing;

            try
            {
                using (var xml = new XmlTextReader(stream))
                {
                    xml.WhitespaceHandling = WhitespaceHandling.None;

                    xml.Read();

                    if (xml.NodeType == XmlNodeType.XmlDeclaration)
                        xml.Read();

                    if (xml.NodeType != XmlNodeType.Element && xml.Name != "TextureFont")
                        throw new XmlException("Invalid TextureFont xml file.");

                    textureFile = xml.ReadRequiredAttributeValue("Texture");

                    backgroundColor = Color.FromHexString(xml.ReadAttributeValueOrDefault("BackgroundColor", "FFFFFFFF"));
                    fontName = xml.ReadAttributeValueOrDefault("FontName", DefaultFontName);
                    fontSize = xml.ReadAttributeValueOrDefault<int>("FontSize", DefaultFontSize);
                    characterSpacing = xml.ReadAttributeValueOrDefault<int>("CharacterSpacing", DefaultCharacterSpacing);
                    lineSpacing = xml.ReadAttributeValueOrDefault<int>("LineSpacing", DefaultLineSpacing);

                    xml.Read();
                    while (xml.Name == "Character")
                    {
                        Rectangle rectangle = new Rectangle(
                            xml.ReadRequiredAttributeValue<int>("X"),
                            xml.ReadRequiredAttributeValue<int>("Y"),
                            xml.ReadRequiredAttributeValue<int>("Width"),
                            xml.ReadRequiredAttributeValue<int>("Height"));

                        rectangles.Add(xml.ReadRequiredAttributeValue("Value")[0], rectangle);
                        xml.Read();
                    }
                }
            }
            catch (XmlException ex)
            {
                throw new GraphicsException("An error occured while parsing the TextureFont xml file.", ex);
            }

            Texture texture = loadTextureFunc(textureFile, backgroundColor);

            if (texture == null)
                throw new InvalidOperationException("loadTextureFunc returned null.");

            return new TextureFont(texture, rectangles, fontName, fontSize)
            {
                CharacterSpacing = characterSpacing,
                LineSpacing = lineSpacing
            };
        }