Example #1
0
 protected void OnLoad(object sender, EventArgs e)
 {
     spriteBatch = new SpriteBatch();
     texture = new Texture(new Bitmap(@"Content/test.png"));
     sprite = new Sprite(texture);
     arial = new BitmapFont("Content/arial_test");
     text = new Text(arial, "The quick brown fox \njumps over the lazy dog.", new Vector2(0, 256), Color.Black);
     fpsFont = new BitmapFont(@"Content/fps_font");
     fpsCounter = new ClockDisplay(Game.Clock, fpsFont, Vector2.Zero, Color.Black); // White is also default
     camera = Camera.CreateOrthographic(Game.ClientSize.Width, Game.ClientSize.Height, -1, 1);
 }
        /// <summary>
        /// Draw all batched sprites.
        /// </summary>
        /// <param name="camera">Optional camera.</param>
        public void Draw(OrthographicCamera camera = null)
        {
            GL.Disable(EnableCap.DepthTest);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

            DrawSprites(camera);

            GL.Disable(EnableCap.Blend);
            GL.Enable(EnableCap.DepthTest);
        }
        private void DrawSprites(OrthographicCamera camera = null)
        {
            Matrix4 MVP, lastMVP = Matrix4.Identity;
            camera = camera ?? DefaultCamera;

            Vector4 lastTint = Vector4.One;
            int lastTexID = -1;

            spriteShader.Use();
            GL.BindVertexArray(0);
            GL.ActiveTexture(TextureUnit.Texture0);
            spriteShader.SetUniform("tint", lastTint);

            foreach (var spr in spritesToDraw)
            {
                GL.BindVertexArray(spr.VAO);
                MVP = spr.Transform.Matrix * camera.ViewMatrix * camera.ProjectionMatrix;

                if (MVP != lastMVP)
                {
                    lastMVP = MVP;
                    spriteShader.SetUniform("MVP", lastMVP);
                }
                if (spr.Tint != lastTint)
                {
                    lastTint = spr.Tint;
                    spriteShader.SetUniform("tint", lastTint);
                }
                if (spr.Texture.TexID != lastTexID)
                {
                    lastTexID = spr.Texture.TexID;
                    GL.BindTexture(TextureTarget.Texture2D, lastTexID);
                    GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, new int[] { (int)TextureMinFilter.Linear });
                }

                GL.DrawArrays(PrimitiveType.TriangleStrip, 0, 4);
            }

            GL.BindVertexArray(0);
            spritesToDraw.Clear();
        }
 private static void Default_Camera_Resize_Hook(object sender, EventArgs e)
 {
     DefaultCamera = Camera.CreateOrthographicTopLeftOrigin(Game.ClientSize.Width, Game.ClientSize.Height, -1, 1);
 }