Beispiel #1
0
        public override void RenderText(Font font, Point pos, string text)
        {
            //m_Target.SaveGLStates();
            pos = Translate(pos);
            global::SFML.Graphics.Font sfFont = font.RendererData as global::SFML.Graphics.Font;

            // If the font doesn't exist, or the font size should be changed
            if (sfFont == null || Math.Abs(font.RealSize - font.Size * Scale) > 2)
            {
                FreeFont(font);
                LoadFont(font);
            }

            //if (sfFont == null)
            //    sfFont = global::SFML.Graphics.Font.DefaultFont;

            // todo: this is workaround for SFML.Net bug under mono
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                if (text[text.Length - 1] != '\0')
                {
                    text += '\0';
                }
            }

            Text sfText = new Text(text, sfFont);

            sfText.Font          = sfFont;
            sfText.Position      = new Vector2f(pos.X, pos.Y);
            sfText.CharacterSize = (uint)font.RealSize; // [omeg] round?
            sfText.Color         = m_Color;
            m_Target.Draw(sfText);
            sfText.Dispose();
            //m_Target.RestoreGLStates();
        }
Beispiel #2
0
        public IFont LoadFont(byte[] data)
        {
            // Note: This memory stream needs to be kept open for the font to be usable in SFML...
            MemoryStream stream = new MemoryStream();
            stream.Write(data, 0, data.Length);

            global::SFML.Graphics.Font sfFont;

            sfFont = new global::SFML.Graphics.Font(stream);

            Font font = new Font(sfFont);

            return font;
        }
Beispiel #3
0
        /*
         * public override void DrawLine(int x1, int y1, int x2, int y2)
         * {
         *  Translate(ref x1, ref y1);
         *  Translate(ref x2, ref y2);
         *
         *  Vertex[] line = {new Vertex(new Vector2f(x1, y1), m_Color), new Vertex(new Vector2f(x2, y2), m_Color)};
         *
         *  m_Target.Draw(line, PrimitiveType.Lines);
         * }
         */
        /// <summary>
        /// Loads the specified font.
        /// </summary>
        /// <param name="font">Font to load.</param>
        /// <returns>True if succeeded.</returns>
        public override bool LoadFont(Font font)
        {
            font.RealSize = font.Size * Scale;
            global::SFML.Graphics.Font sfFont;
            bool ret = true;

            Debug.Print("LoadFont: {0} {1}", font.FaceName, font.RendererData);
            try
            {
                sfFont = new global::SFML.Graphics.Font(font.FaceName);
                sfFont.GetTexture((uint)font.Size).Smooth = font.Smooth;
                font.RendererData = sfFont;
            }
            catch (LoadingFailedException)
            {
                // try to load windows font by this name
                string path = Platform.Windows.GetFontPath(font.FaceName);
                if (path != null)
                {
                    try
                    {
                        sfFont = new global::SFML.Graphics.Font(path);
                        sfFont.GetTexture((uint)font.Size).Smooth = font.Smooth;
                        font.RendererData = sfFont;
                    }
                    catch (LoadingFailedException)
                    {
                        // Ideally here we should be setting the font to a system default font here.
                        //sfFont = global::SFML.Graphics.Font.DefaultFont;
                        Debug.Print("LoadFont: failed");
                        ret = false;
                    }
                }
                else
                {
                    // Ideally here we should be setting the font to a system default font here.
                    //sfFont = global::SFML.Graphics.Font.DefaultFont;
                    Debug.Print("LoadFont: failed");
                    ret = false;
                }
            }

            return(ret);
        }
Beispiel #4
0
        /// <summary>
        /// Frees the specified font.
        /// </summary>
        /// <param name="font">Font to free.</param>
        public override void FreeFont(Font font)
        {
            if (font.RendererData == null)
            {
                return;
            }

            Debug.Print("FreeFont: {0} {1}", font.FaceName, font.RendererData);

            global::SFML.Graphics.Font sfFont = font.RendererData as global::SFML.Graphics.Font;

            // If this is the default font then don't delete it!
            //if (sfFont != global::SFML.Graphics.Font.DefaultFont)
            {
                sfFont.Dispose();
            }

            font.RendererData = null;
        }
Beispiel #5
0
        /// <summary>
        /// Returns dimensions of the text using specified font.
        /// </summary>
        /// <param name="font">Font to use.</param>
        /// <param name="text">Text to measure.</param>
        /// <returns>
        /// Width and height of the rendered text.
        /// </returns>
        public override Point MeasureText(Font font, string text)
        {
            // todo: cache results, this is slow
            global::SFML.Graphics.Font sfFont = font.RendererData as global::SFML.Graphics.Font;

            // If the font doesn't exist, or the font size should be changed
            if (sfFont == null || Math.Abs(font.RealSize - font.Size * Scale) > 2)
            {
                FreeFont(font);
                LoadFont(font);
            }

            sfFont = font.RendererData as global::SFML.Graphics.Font;

            // todo: this is workaround for SFML.Net bug under mono
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                if (text[text.Length - 1] != '\0')
                {
                    text += '\0';
                }
            }

            Point extents = new Point(0, sfFont.GetLineSpacing((uint)font.RealSize));
            char  prev    = '\0';

            for (int i = 0; i < text.Length; i++)
            {
                char cur = text[i];
                sfFont.GetKerning(prev, cur, (uint)font.RealSize);
                prev = cur;
                if (cur == '\n' || cur == '\v')
                {
                    continue;
                }
                extents.X += sfFont.GetGlyph(cur, (uint)font.RealSize, false).Advance;
            }

            return(extents);
        }
Beispiel #6
0
        /*
        public override void DrawLine(int x1, int y1, int x2, int y2)
        {
            Translate(ref x1, ref y1);
            Translate(ref x2, ref y2);

            Vertex[] line = {new Vertex(new Vector2f(x1, y1), m_Color), new Vertex(new Vector2f(x2, y2), m_Color)};

            m_Target.Draw(line, PrimitiveType.Lines);
        }
        */
        /// <summary>
        /// Loads the specified font.
        /// </summary>
        /// <param name="font">Font to load.</param>
        /// <returns>True if succeeded.</returns>
        public override bool LoadFont(Font font)
        {
            font.RealSize = font.Size*Scale;
            global::SFML.Graphics.Font sfFont;
            bool ret = true;
            
            Debug.Print("LoadFont: {0} {1}", font.FaceName, font.RendererData);
            try
            {
                sfFont = new global::SFML.Graphics.Font(font.FaceName);
            }
            catch (LoadingFailedException)
            {
                // try to load windows font by this name
                string path = Platform.Windows.GetFontPath(font.FaceName);
                if (path != null)
                {
                    try
                    {
                        sfFont = new global::SFML.Graphics.Font(path);
                        sfFont.GetTexture((uint)font.Size).Smooth = font.Smooth;
                        font.RendererData = sfFont;
                    }
                    catch (LoadingFailedException)
                    {
                        // Ideally here we should be setting the font to a system default font here.
                        //sfFont = global::SFML.Graphics.Font.DefaultFont;
                        Debug.Print("LoadFont: failed");
                        ret = false;
                    }
                }
                else
                {
                    // Ideally here we should be setting the font to a system default font here.
                    //sfFont = global::SFML.Graphics.Font.DefaultFont;
                    Debug.Print("LoadFont: failed");
                    ret = false;
                }
            }

            return ret;
        }
Beispiel #7
0
        public IFont LoadFontDirect(string fullFilePath)
        {
            global::SFML.Graphics.Font sfFont;

            //try {
            sfFont = new global::SFML.Graphics.Font(fullFilePath);
            Font font = new Font(sfFont);

            return font;
        }