Exemple #1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="maxSprites">The maximum number of sprites which can be batched.</param>
        public SpriteRenderer(GraphicsContext graphics, int maxSprites)
        {
            if (graphics == null)
                throw new ArgumentNullException("graphics");

            if (maxSprites <= 0)
                throw new ArgumentOutOfRangeException("maxSprites", "MaxSprites must be >= 1.");

            this.graphics = graphics;

            this.vertices = new Vertex[maxSprites * 4];

            this.vertexBuffer = new DynamicVertexBuffer<Vertex>(this.graphics);

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

            this.indexBuffer = new StaticIndexBuffer<ushort>(this.graphics, indices);

            this.transform = new Matrix4()
            {
                M33 = 1f,
                M44 = 1f,
                M41 = -1f,
                M42 = 1f
            };
        }
        public WavingFlagSample()
            : base()
        {
            this.Window.Title = "Samurai Waving Flag Sample";

            this.shader = new ShaderProgram(
                this.Graphics,
                VertexShader.Compile(this.Graphics, File.ReadAllText("WavingFlagSample.vert")),
                FragmentShader.Compile(this.Graphics, File.ReadAllText("WavingFlagSample.frag")));

            this.Graphics.ShaderProgram = this.shader;

            this.texture = Texture2D.LoadFromFile(this.Graphics, "Flag.png", TextureParams.Default);

            int totalChunks = 100;
            int chunkSize = this.texture.Width / totalChunks;
            Rectangle destination = new Rectangle(
                (this.Window.Width - this.texture.Width) / 2,
                (this.Window.Height - this.texture.Height) / 2,
                chunkSize,
                this.texture.Height);

            Rectangle source = new Rectangle(0, 0, chunkSize, this.texture.Height);

            Vertex[] vertexData = new Vertex[totalChunks * 4];

            for (int i = 0; i < totalChunks * 4; i += 4)
            {
                vertexData[i] = new Vertex() { Position = new Vector2(destination.X, destination.Y), UV = new Vector2(source.X / (float)texture.Width, 0) };
                vertexData[i + 1] = new Vertex() { Position = new Vector2(destination.X + chunkSize, destination.Y), UV = new Vector2((source.X + chunkSize) / (float)texture.Width, 0) };
                vertexData[i + 2] = new Vertex() { Position = new Vector2(destination.X + chunkSize, destination.Y + this.texture.Height), UV = new Vector2((source.X + chunkSize) / (float)texture.Width, 1.0f) };
                vertexData[i + 3] = new Vertex() { Position = new Vector2(destination.X, destination.Y + this.texture.Height), UV = new Vector2(source.X / (float)texture.Width, 1.0f) };

                destination.X += chunkSize;
                source.X += chunkSize;
            }

            this.vertexBuffer = new StaticVertexBuffer<Vertex>(this.Graphics, vertexData);

            ushort[] indexData = new ushort[vertexData.Length * 6];

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

            this.indexBuffer = new StaticIndexBuffer<ushort>(this.Graphics, indexData);
        }
Exemple #3
0
        protected override void DisposeManagedResources()
        {
            this.vertexBuffer.Dispose();
            this.vertexBuffer = null;

            this.indexBuffer.Dispose();
            this.indexBuffer = null;
        }
Exemple #4
0
        public DemoGame()
        {
            this.Window.Title = "Samurai Demo";

            this.Graphics.BlendEnabled = true;
            this.Graphics.SourceBlendFactor = SourceBlendFactor.SourceAlpha;
            this.Graphics.DestinationBlendFactor = DestinationBlendFactor.OneMinusDestinationAlpha;

            this.Graphics.DepthBufferEnabled = true;
            this.Graphics.DepthFunction = DepthFunction.LessThanOrEqual;

            this.Graphics.FrontFace = FrontFace.Clockwise;
            this.Graphics.CullMode = CullMode.Back;

            this.shaderProgram = new ShaderProgram(
                this.Graphics,
                VertexShader.Compile(this.Graphics, File.ReadAllText("Shader.vert")),
                FragmentShader.Compile(this.Graphics, File.ReadAllText("Shader.frag")));

            this.vertexBuffer = new StaticVertexBuffer<Vertex>(this.Graphics, this.vertexData);
            this.indexBuffer = new StaticIndexBuffer<byte>(this.Graphics, this.indexData);

            this.texture0 = Texture2D.LoadFromFile(this.Graphics, "Texture0.png", new TextureParams()
            {
                WrapS = TextureWrap.Repeat,
                WrapT = TextureWrap.Repeat
            });

            this.texture1 = Texture2D.LoadFromFile(this.Graphics, "Texture1.png", new TextureParams()
            {
                WrapS = TextureWrap.Clamp,
                WrapT = TextureWrap.Clamp
            });

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

            this.controlInputHandler = new ControlInputHandler(this.Window);

            this.controlRenderer = new ControlRenderer(this.Graphics);
            this.controlRenderer.DefaultFont = TextureFont.Build(this.Graphics, "Segoe UI", 72, new TextureFontParams()
            {
                BackgroundColor = Color4.Transparent
            });

            this.panel = new Panel()
            {
                Position = new Vector2(100, 100),
                Size = new Size(100, 100)
            };

            this.label = new Label()
            {
                Text = "Hello World!",
                Font = TextureFont.Build(this.Graphics, "Segoe UI", 24, new TextureFontParams() { BackgroundColor = Color4.Transparent })
            };

            this.label.CursorEnter += (s, e) => { this.label.ForegroundColor = Color4.Black; };
            this.label.CursorLeave += (s, e) => { this.label.ForegroundColor = Color4.White; };

            this.panel.Controls.Add(label);
        }