Code archives/Graphics/OGL bitmap font

This code has been declared by its author to be Public Domain code.

Download source code

OGL bitmap font by Jesse2007
a simple way of displaying bitmap fonts in ogl.
SuperStrict


Type TTexture

	Field texID:Int = 0



	Method CDelete()
		If(glIsTexture(texID)) glDeleteTextures(1, Varptr texID);
	End Method

	Method LoadTexture:Int(filename:String)

		Local glimage:TPixmap =LoadPixmap(filename);
		If(glimage = Null)Return False;
		Local data:Byte[,,]= New Byte[glimage.Width,glimage.Height,3]
		Local pp:Int = 0

		glGenTextures(1, Varptr texID);
		glBindTexture(GL_TEXTURE_2D, texID);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glimage.Width, glimage.Height, 0, GL_RGB, GL_UNSIGNED_BYTE, glimage.pixels)'data);

		Return True;
	End Method

	Method BindTexture()

		glBindTexture(GL_TEXTURE_2D, texID);
	End Method

	Method GetTexID:Int()
		Return texID;
	End Method

End Type

Type TFont

	
		Field tex:TTexture;
		Field FontSize:Float;
		Field fontColor:Float[4];

	
	Method Create()
		FontSize = 1.0;
		fontColor[0] = 1;
		fontColor[1] = 1;
		fontColor[2] = 1;
		fontColor[3] = 1;
		tex = New TTexture
		tex.LoadTexture("mypng.png");
	End Method


	Method DrawText(x:Int, y:Int, text:String)
		If(text=Null) Return;
		Local x1:Int=x
		Local y1:Int=y
		Local cx:Float
		Local cy:Float
		Local  i:Int
		Local offset:Int = 0
		glEnable(GL_BLEND);
		glDisable(GL_DEPTH_TEST);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		tex.BindTexture();
		glColor4fv(fontColor);
		For i=0 Until text.length
			cx=(((text[i]+offset)/16.0) Mod 1);
			cy=((text[i]+offset)/16)/16.0;
			
			glBegin(GL_QUADS);
								
				glTexCoord2f(cx,cy);
				glVertex2i(x1,y1);
				
				glTexCoord2f(cx,(cy+0.0625));
				glVertex2i(x1,Int(16*FontSize)+y1);
				
				glTexCoord2f(cx+0.0625,(cy+0.0625))
				glVertex2i(Int(16*FontSize)+x1,Int(16*FontSize)+y1)
				
				glTexCoord2f(cx+0.0625,cy);
				glVertex2i(Int(16*FontSize)+x1,y1);
			
			glEnd();
			x1:+Int(13.0*(FontSize));

		Next
		glDisable(GL_BLEND);
		glEnable(GL_DEPTH_TEST);
	End Method

	Method SetSize(size:Float )
	
		FontSize = size;
	End Method

	Method SetColor(r:Float, g:Float, b:Float, a:Float)
		fontColor[0] = r;
		fontColor[1] = g;
		fontColor[2] = b;
		fontColor[3] = a;
	End Method
End Type

	GLGraphics 800,600
'	glEnable(GL_DEPTH_TEST);
	
'	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0,800,600,0);
'	glMatrixMode(GL_MODELVIEW);

	glEnable(GL_CULL_FACE);
	glEnable(GL_TEXTURE_2D);
	Local font:Tfont = New Tfont
	font.Create()
Repeat
	Local alpha:Float = Abs(Sin(MilliSecs()/10.0))
	Local red:Float = Abs(Sin(MilliSecs()/100))
	Local green:Float = Abs(Cos(MilliSecs()/100))
	Local blue:Float = Abs(Sin(MilliSecs()/1000))
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
	font.SetColor(red,green,blue,alpha)
	font.SetSize(2.0)
	font.drawtext(10,270,"abcdefghijklmnopqrstuvwxyz")

	Flip()
Until KeyDown(key_escape)

Comments

Jesse2007
Strict

Graphics 800,600

SetBlend(alphablend)
Local size:Int = 32
Local r:Timagefont = LoadImageFont("c:\windows\fonts\courbd.TTF",size)
If r = Null Notify "windows font not found";End
SetImageFont(r)

For Local y:Int = 0 Until 16
	For Local x:Int = 0 Until 16
		DrawText Chr(y*16+x),x*size,y*size
	Next
Next
Local pixmap:TPixmap = GrabPixmap(0,5,512,512)
If SavePixmapPNG(pixmap,"mypng.png") Notify "ok mypng.png file created"
Cls
DrawPixmap(pixmap,30,30)
SetAlpha .2
DrawRect(30,30,512,512)
SetAlpha 1.0
DrawText "press a key to continue",200,550
Flip()
WaitKey()
End


this file creates a bitmap to be used in the example
also a 512 x 512 bitmap may be used using the same text format


Jesse2007
by mistake I put this in under bb can someone at brl change this to bmax.


Code Archives Forum