Nikhil Malhotra's picture

Render simple 2d text using C sharp in opentk with example

Project:The Open Toolkit library
Version:1.0-2010-10-06
Component:Code
Category:task
Priority:critical
Assigned:Unassigned
Status:duplicate
Description

hi, i want to display text using C sharp in opentk. it is plain text nothing special functionality required. please help with an example. thanks


Comments

Comment viewing options

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

#1

flopoloco's picture

#2

Sorry for not seeing this message earlier, here's a sample.

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
 
namespace OpenTKSimpleText
{
	class OpenTKApplication : GameWindow
	{
		OpenTK.Graphics.TextPrinter textPrinter = new OpenTK.Graphics.TextPrinter();
		Font font = new Font("Arial Black", 20, FontStyle.Regular);
 
		protected override void OnRenderFrame(FrameEventArgs e)
		{
			GL.Clear(ClearBufferMask.ColorBufferBit);
 
			textPrinter.Begin();
 
			// For multiple lines GL.Translate same as the font height
			textPrinter.Print("Hello", font, Color.Yellow);
			GL.Translate(0f, 20, 0f);
			textPrinter.Print("world", font, Color.Yellow);
			GL.Translate(0f, 20, 0f);
			textPrinter.Print("and", font, Color.Yellow);
			GL.Translate(0f, 20, 0f);
			textPrinter.Print("good bye", font, Color.Yellow);
 
			// You can do OpenGL tricks also
			GL.Translate(200f, 200f, 0f);
			GL.Rotate(-40f, Vector3.UnitZ);
			textPrinter.Print("Is this real life?", font, Color.Yellow);
 
			textPrinter.End();
 
			SwapBuffers();
		}
	}
 
	class Program
	{
		public static void Main(string[] args)
		{
			using (OpenTKApplication app = new OpenTKApplication())
			{
				app.Run();
			}
		}
	}
}