objarni's picture

TextureFont v0.1

I finished a rough version of the TextureFont class. The WriteCharacter/WriteString got a little tidier aswell as more optimized. For example I moved out the "Begin/End"-calls from WriteCharacter to WriteString. WriteString is the only public API for the class, example code:

    private void DrawText()
    {
      string text = string.Format("Hello! FPS={0:000.0}", fps);
 
      // Assume orthogonal 0,0,width,height projection matrix
      GL.Color3(Color.Khaki);
      GL.Translate(w / 2, h / 2, 0); // place text at center of view
      double scale = h * 0.05; // make it 5% of the height of the view
      GL.Scale(scale, scale, scale);
      GL.Rotate(rotation, 0, 0, 1);
      TextureFont texFont = new TextureFont(texture);
      texFont.WriteString(text);
    }

The TexUtil.cs file can be downloaded here, I'm releasing it under same license as OpenTK, if you are interesting in using this functionality.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

Note: TextureFont assumes that the texture bitmap grid are "encoded" in latin-1. It uses the first byte of the unicode character to lookup the piece to draw from the bitmap.

Some texture fonts I've found on the net (some might need some retouching, eg. lack alpha-channel or glyph above code 127).

If you make them work please share.







A whole batch of bitmap fonts can be found here. Some of them are multi-coloured. Sadly they are not layed out in latin-1 :(

Mincus's picture

There's a few programs out there that create font sheets for you, with an XML file alongside that lists where the characters are. Might or might not be useful.
Quick google has found me Font Studio (http://www.nitrogen.za.org/projectinfo.asp?id=12) which is free.
I've used others in the past when working with SDL/other libraries though (can't remember their names unfortunately).

objarni's picture

Thanks, if the program generates all positions + config file then the TextureFont class can load that information to render correctly. Will have to check out how advanced it is. (the whole point of this "project" is to provide a simple way of printing text for OpenTK, until the TextPrinter implementation is a little more stable..)

Entropy's picture

Looking good, Objarni.

I like characters 0-127 in the last texture font - especially the two lines with all those characters which would be perfect for a roguelike...

*Yoink*

I'll put that image in my "Inspiration" pile, in the big mixing pot with all my other half-formed ideas, to see what comes out (if only I could code fast enough to bring just a handful of my ideas to fruition)...

objarni's picture

Entropy - Thanks! About the ideas you are having, try to jot them down in Wordpad or whatever, just some sentences describing the idea.

It is a great source of inspiration rainy days ;)

objarni's picture

OK I've made an update to the TextureFont class. It's a new method called "WriteStringAt" that takes a "percent coordinate" and a rotation, and writes a text string at the specified location. Example code:

    private void DrawText()
    {
      string text = string.Format("Hello! FPS={0:000.0}", fps);
      GL.Color3(Color.Khaki);
      TextureFont texFont = new TextureFont(texture);
 
      // Draw 10% high text at x,y=50%,50% (center of screen) upside-down (180 degrees rotated)
      double height = 10;
      double xpercent = 50;
      double ypercent = 50;
      double rotation = 180;
      texFont.WriteStringAt(text, height, x, y, rotation);
    }

This is of course less general (and slower) than the ordinary "WriteString" method - which uses ordinary GL matrices to render the text - but it is simpler to use.

New version of TexUtil.cs file can be found here.

objarni's picture

Just tried running my TexUtilDemo program under Ubuntu 9.04. OpenTK is quite impressive in Linux!! 1200 FPS?!?!

(not so impressive is mono-winforms. The form just does not work as it did under WinXP. The GLControl was anchored with equal amount of space on all four sides in WinXP - in mono it's a different story .. too bad.)

Entropy's picture
objarni wrote:

Entropy - Thanks! About the ideas you are having, try to jot them down in Wordpad or whatever, just some sentences describing the idea.

It is a great source of inspiration rainy days ;)

Yeah, I have a folder full of images that inspired me/plain text files with half-formed ideas etc.

The real difficulty, of course, is staying focussed on one idea!

Serinus's picture

I don't suppose there's any example code for this? I'm just trying to play around with opentk and your texturefont, but I can't seem to get it running. In all the code on this page, "texture" is never defined. I'm not at all familiar with opengl (which is the reason I'm playing with this now).

Can you tell me what i'm doing wrong here? Where's my text? (Edit: I've moved the "CreateTextureFromFile" line into On_Load.) font1.png is from your post "If you make them work, please share", so I'm thinking that's my issue. The code I'm trying to apply this to is the "QuickStart" solution that's included in the "Download OpenTK" link.

        /// <summary>
        /// Called when it is time to setup the next frame. Add you game logic here.
        /// </summary>
        /// <param name="e">Contains timing information for framerate independent logic.</param>
        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
 
            if (Keyboard[Key.Escape])
                Exit();
            if (Keyboard[Key.Up]) y += speed;
            if (Keyboard[Key.Down]) y -= speed;
            if (Keyboard[Key.Left]) x += speed;
            if (Keyboard[Key.Right]) x -= speed;
 
            int texture = TexLib.TexUtil.CreateTextureFromFile("D:\\Desktop\\font1.png");
            TexLib.TextureFont texfont = new TexLib.TextureFont(texture);
            texfont.WriteString("testing");
 
        }
the Fiddler's picture

Try downloading the latest version from here: http://www.opentk.com/project/TexLib

The project page includes an example.