コード例 #1
0
        // TODO Support Kerning Pairs

        internal static TextureFont CreateFromFile(string filename)
        {
            // TODO: Convert to binary format at some stage for improved loadtimes
            Font      font        = null;
            Texture2D fontTexture = null;

            XmlSerializer serializer = new XmlSerializer(typeof(Font));
            var           fi         = new FileInfo(filename);

            using (FileStream stream = new FileStream(filename, FileMode.Open))
            {
                font = (Font)serializer.Deserialize(stream);

                var texture = font.Info.Face;
                var pages   = font.Pages.Count();
                if (pages != 1)
                {
                    throw new NotImplementedException("TODO Support multiple pages!");
                }

                var dir = fi.DirectoryName;
                fontTexture = Texture2D.CreateFromFile(dir + "/" + font.Pages[0].File);
            }

            var rv = new TextureFont
            {
                Font        = font,
                FontTexture = fontTexture
            };

            // Construct char dictionary
            for (int i = 0; i < font.Chars.Length; i++)
            {
                rv.CharLookup.Add(font.Chars[i].Id, font.Chars[i]);
            }

            return(rv);
        }
コード例 #2
0
        public void DrawText(string text, Vector2 position, TextureFont font, Vector4 color, TextureUnits textureUnit = TextureUnits.GL_TEXTURE0, int layer = 0)
        {
            // TODO? Make a seperate FontBatcher?
            // See http://www.angelcode.com/products/bmfont/doc/render_text.html
            // To improve the rendering...

            var fontSheetWidth  = (float)font.Font.Common.ScaleW;
            var fontSheetHeight = (float)font.Font.Common.ScaleH;

            for (int i = 0; i < text.Length; i++)
            {
                var quad = new QuadRecord();
                quad.X = 32 + i * 32 + 16 * i + position.X;
                quad.Y = 32 + (2 * i) + position.Y;
                quad.W = 32;
                quad.H = 32;

                // Compute the U, V and UW, UH values
                var charId   = (byte)text[i];
                var fontChar = font.CharLookup[charId];

                quad.U = fontChar.X / fontSheetWidth;
                quad.V = fontChar.Y / fontSheetHeight;

                quad.UW = fontChar.Width / fontSheetWidth;
                quad.VH = fontChar.Height / fontSheetHeight;



                PushQuad(quad);
            }

            var device = GameWindow.GraphicsDevice;

            var sX = 2 / (float)GameWindow.ScreenWidth;
            var sY = 2 / (float)GameWindow.ScreenHeight;

            var oX = -1;
            var oY = 1;

            var quadCount = m_quadRecordCount;

            // Edit Vertex Data
            for (int i = 0; i < quadCount; i++)
            {
                var record = quadRecords[i];
                m_Verticies[i * 4 + 0].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y + record.H) * sY, 0.0f);       // Top Right
                m_Verticies[i * 4 + 1].Position = new Vector3(oX + (record.X + record.W) * sX, oY - (record.Y * sY), 0.0f);                  // Bottom Right
                m_Verticies[i * 4 + 2].Position = new Vector3(oX + record.X * sX, oY - (record.Y * sY), 0.0f);                               // Bottom Left
                m_Verticies[i * 4 + 3].Position = new Vector3(oX + record.X * sX, oY - (record.Y + record.H) * sY, 0.0f);                    // Top Left


                m_Verticies[i * 4 + 0].Texture = new Vector2(record.U + record.UW, record.V + record.VH);                      // Note: Inverted Y
                m_Verticies[i * 4 + 1].Texture = new Vector2(record.U + record.UW, record.V);
                m_Verticies[i * 4 + 2].Texture = new Vector2(record.U, record.V);
                m_Verticies[i * 4 + 3].Texture = new Vector2(record.U, record.V + record.VH);
            }

            m_VertexArrayObject.UpdateVertexData <DefaultQuadBatchVertex>(m_Verticies, 0, quadCount * 6);


            // TODO Make a font.glsl shader
            //    GlBindings.PolygonMode(Face.GL_FRONT_AND_BACK, Mode.GL_LINE);

            GameWindow.GraphicsDevice.BindTexture2D(font.FontTexture.TextureId, OpenGL.TextureUnits.GL_TEXTURE0);
            GameWindow.GraphicsDevice.BindTexture2D(0, OpenGL.TextureUnits.GL_TEXTURE1);

            GameWindow.GraphicsDevice.BindShaderProgram(GameWindow.QuadBatchShader.ShaderProgramId);

            GameWindow.QuadBatchShader.SetUniform("texture1", 0);

            GlBindings.BindVertexArray(m_VertexArrayObject.VaoId);
            GlBindings.DrawElements(PrimitiveType.TriangleList, quadCount * 6, DrawElementsType.UnsignedInt, 0);

            GlBindings.BindVertexArray(0);

            m_quadRecordCount = 0;
        }