PDA

View Full Version : [codesnippet] The most easiest way to draw a string in opengl ! (without glut etc)



herc
05-25-2006, 10:22 AM
hy gyus, just wanted to show you my fresh new code tiny_gl_string that draws a vector font on screen. its completely dependency free, just plain opengl no external libs required.
hope that solves the problem for most beginners to draw some text on screen.
you can draw in 3d mode or in 2d mode. dont forget to scale correctly, otherwise the font may be not visible. if you have perspective projection and camera is 20 units away, you need to scale down to glScalef(0.1,0.1,0.1);
on the other hand, if you draw in ortho mode (you may use my glbegin2d() function) , you may scale by 2.0 to increase the font a little bit.

yes, the font is quite ugly, but the big advantage is that you can just include a single header, and start using it. no initialisation, no file loading etc. you may for example use it to print information while starting up your application/game, print loading progress of resources and textures etc.
please give me your feedback.

http://www.coreloop.com/dump/tiny_gl_string.gif

gl_tiny_string.h:

#ifndef __GL_TINY_STRING__
#define __GL_TINY_STRING__
//////////////////////////////////////////////////////////////////////////
//
// tiny opengl string drawing.
// just call gl_tiny_string_draw_char(xpos, ypos, 'C'); to draw char c
// or call gl_tiny_string(xpos,ypos, "Hello World!\nThis the easiest way to draw a string!");
// code based on vector font from otk.sourceforge.net, kindman1@users.sourceforge.net
//
//////////////////////////////////////////////////////////////////////////

// sample code
/*
#include <opengl2d.h>
gl_begin2d();
glPushMatrix();
glDisable(GL_TEXTURE_2D);
glDisable (GL_LINE_SMOOTH);
glLineWidth(1.0);
glColor3f(1,1,1);
//glScalef(2,2,2);
glBegin(GL_LINES);
gl_tiny_string(20, 160, "Hello\nWorld!");
glEnd();
glPopMatrix();
gl_end2d();
*/


// unimportant helper function _gl_tiny_string_draw_line
inline void _gtsl(float y_offset, float x_offset, float y1, float x1, float y2, float x2)
{
glVertex2d(x_offset+x1, y_offset-y1);
glVertex2d(x_offset+x2, y_offset-y2);
}

// this draws a single char at the specified position. z position = 0
inline void gl_tiny_string_draw_char(float x, float y, unsigned char c)
{
unsigned int chari = (unsigned int)c;
switch(chari)
{
case 32: ; /*(space)*/
break;
case 33: ; /* ! */
_gtsl(y, x, 0, 4, 7, 4 );
_gtsl(y, x, 8, 4, 9, 4 );
break;
case 34: /* " */
_gtsl(y, x, 2, 2, 0, 4 );
_gtsl(y, x, 2, 4, 0, 6 );
break;
case 37: /*%*/
_gtsl(y, x, 0, 7, 9, 1 );
_gtsl(y, x, 0, 2, 1, 3 );
_gtsl(y, x, 1, 1, 2, 2 );
_gtsl(y, x, 7, 6, 8, 7 );
_gtsl(y, x, 8, 5, 9, 6 );
break;
case 39: /*'*/
_gtsl(y, x, 2, 3, 0, 5 );
break;
case 41: /*)*/
_gtsl(y, x, 0, 5, 3, 7 );
_gtsl(y, x, 3, 7, 6, 7 );
_gtsl(y, x, 6, 7, 9, 6 );
break;
case 40: /*(*/
_gtsl(y, x, 0, 3, 3, 1 );
_gtsl(y, x, 3, 1, 6, 1 );
_gtsl(y, x, 6, 1, 9, 3 );
break;
case 42: /***/
_gtsl(y, x, 2, 1, 8, 7 );
_gtsl(y, x, 2, 4, 8, 4 );
_gtsl(y, x, 2, 7, 8, 1 );
_gtsl(y, x, 5, 1, 5, 7 );
break;
case 43: /*+*/
_gtsl(y, x, 2, 4, 8, 4 );
_gtsl(y, x, 5, 1, 5, 7 );
break;
case 44: /*,*/
_gtsl(y, x, 8, 5, 10, 3 );
break;
case 45: /*-*/
_gtsl(y, x, 5, 1, 5, 7 );
break;
case 46: /*.*/
_gtsl(y, x, 8, 4, 9,5 );
_gtsl(y, x, 8,5,9,4 );
break;
case 47: /* / */
_gtsl(y, x, 0, 7, 9, 1 );
break;
case 48: /*0*/
_gtsl(y, x, 0,4,0,5 );
_gtsl(y, x, 0,5,2,7 );
_gtsl(y, x, 2,7,7,7 );
_gtsl(y, x, 7,7,9,5 );
_gtsl(y, x, 9,5,9,4 );
_gtsl(y, x, 9,4,7,2 );
_gtsl(y, x, 7,2,2,2 );
_gtsl(y, x, 2,2,0,4 );
break;
case 49: /*1*/
_gtsl(y, x, 1,4,0,5 );
_gtsl(y, x, 0,5,9,5 );
break;
case 50: /*2*/
_gtsl(y, x, 2,2,0,3.5 );
_gtsl(y, x, 0,3.5,0,5.5 );
_gtsl(y, x, 0,5.5,2,7 );
_gtsl(y, x, 2,7,4,7 );
_gtsl(y, x, 4,7,5,5 );
_gtsl(y, x, 5,5,9,1 );
_gtsl(y, x, 9,1,9,7 );
break;
case 51: /*3*/
_gtsl(y, x, 2,2,0,3.5 );
_gtsl(y, x, 0,3.5,0,5.5 );
_gtsl(y, x, 0,5.5,2,7 );
_gtsl(y, x, 2,7,4,7 );
_gtsl(y, x, 4,7,5,6 );
_gtsl(y, x, 5,6,5,4 );
_gtsl(y, x, 5,6,6,7 );
_gtsl(y, x, 6,7,8,7 );
_gtsl(y, x, 8,7,9,6 );
_gtsl(y, x, 9,6,9,3 );
_gtsl(y, x, 9,3,8,2 );
break;
case 52: /*4*/
_gtsl(y, x, 6,1,0,6 );
_gtsl(y, x, 0,6,9,6 );
_gtsl(y, x, 6,1,6,6 );
break;
case 53: /*5*/
_gtsl(y, x, 0,2,0,7 );
_gtsl(y, x, 0,2,4,2 );
_gtsl(y, x, 4,2,3,4 );
_gtsl(y, x, 3,4,3,5 );
_gtsl(y, x, 3,5,5,7 );
_gtsl(y, x, 5,7,7,7 );
_gtsl(y, x, 7,7,9,5 );
_gtsl(y, x, 9,5,9,3 );
_gtsl(y, x, 9,3,8,2 );
break;
case 54: /*6*/
_gtsl(y, x, 1,7,0,6 );
_gtsl(y, x, 0,6,0,4 );
_gtsl(y, x, 0,4,2,2 );
_gtsl(y, x, 2,2,8,2 );
_gtsl(y, x, 8,2,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
_gtsl(y, x, 8,7,6,7 );
_gtsl(y, x, 6,7,5,6 );
_gtsl(y, x, 5,6,5,2 );
break;
case 55: /*7*/
_gtsl(y, x, 0,2,0,7 );
_gtsl(y, x, 0,7,9,3 );
break;
case 56: /*8*/
_gtsl(y, x, 0,4,0,5 );
_gtsl(y, x, 0,5,2,7 );
_gtsl(y, x, 2,7,5,5 );
_gtsl(y, x, 5,5,5,4 );
_gtsl(y, x, 5,4,7,2 );
_gtsl(y, x, 7,2,8,2 );
_gtsl(y, x, 8,2,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
_gtsl(y, x, 8,7,7,7 );
_gtsl(y, x, 7,7,5,5 );
_gtsl(y, x, 5,5,5,4 );
_gtsl(y, x, 5,4,3,2 );
_gtsl(y, x, 3,2,2,2 );
_gtsl(y, x, 2,2,0,4 );
break;
case 57: /*9*/
_gtsl(y, x, 0,4,0,6 );
_gtsl(y, x, 0,6,1,7 );
_gtsl(y, x, 1,7,5,7 );
_gtsl(y, x, 5,7,9,5 );
_gtsl(y, x, 4,7,4,4 );
_gtsl(y, x, 4,4,2,2 );
_gtsl(y, x, 2,2,0,4 );
break;
case 58: /*:*/
_gtsl(y, x, 3,4,4,5 );
_gtsl(y, x, 4,4,3,5 );
_gtsl(y, x, 8,4,9,5 );
_gtsl(y, x, 9,4,8,5 );
break;
case 59: /*;*/
_gtsl(y, x, 3,4,4,5 );
_gtsl(y, x, 4,4,3,5 );
_gtsl(y, x, 8,4,8.5,4.5 );
_gtsl(y, x, 10,3,8,5 );
break;
case 60: /*<*/
_gtsl(y, x, 1,7,5,1);
_gtsl(y, x, 5,1,9,7 );
break;
case 61: /*=*/
_gtsl(y, x, 3,2,3,6 );
_gtsl(y, x, 6,2,6,6 );
break;
case 62: /*>*/
_gtsl(y, x, 1,1,5,7);
_gtsl(y, x, 5,7,9,1 );
break;
case 63: /*?*/
_gtsl(y, x, 2,2,0,4 );
_gtsl(y, x, 0,4,0,6 );
_gtsl(y, x, 0,6,1,7 );
_gtsl(y, x, 1,7,3,7 );
_gtsl(y, x, 3,7,4,6 );
_gtsl(y, x, 4,6,4,5 );
_gtsl(y, x, 4,5,5,4 );
_gtsl(y, x, 5,4,6,4 );
_gtsl(y, x, 8,4,9,4 );
break;
case 65: /*A*/
_gtsl(y, x, 9,1,0,4 );
_gtsl(y, x, 0,4,9,7 );
_gtsl(y, x, 7,2,7,6 );
break;
case 66: /*B*/
_gtsl(y, x, 0,1,0,5 );
_gtsl(y, x, 0,5,2,7 );
_gtsl(y, x, 2,7,3,7 );
_gtsl(y, x, 3,7,4,6 );
_gtsl(y, x, 4,6,4,1 );
_gtsl(y, x, 4,5,6,7 );
_gtsl(y, x, 6,7,8,7 );
_gtsl(y, x, 8,7,9,6 );
_gtsl(y, x, 9,6,9,1 );
_gtsl(y, x, 9,1,0,1 );
break;
case 67: /*C*/
_gtsl(y, x, 1,7,0,6 );
_gtsl(y, x, 0,6,0,3 );
_gtsl(y, x, 0,3,2,1 );
_gtsl(y, x, 2,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
break;
case 68: /*D*/
_gtsl(y, x, 0,1,0,5 );
_gtsl(y, x, 0,5,2,7 );
_gtsl(y, x, 2,7,7,7 );
_gtsl(y, x, 7,7,9,5 );
_gtsl(y, x, 9,5,9,1 );
_gtsl(y, x, 9,1,0,1 );
break;
case 69: /*E*/
_gtsl(y, x, 0,7,0,1 );
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 9,1,9,7 );
_gtsl(y, x, 5,1,5,6 );
break;
case 70: /*F*/
_gtsl(y, x, 0,7,0,1 );
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 5,1,5,5 );
break;
case 71: /*G*/
_gtsl(y, x, 1,7,0,6 );
_gtsl(y, x, 0,6,0,3 );
_gtsl(y, x, 0,3,2,1 );
_gtsl(y, x, 2,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,7,7 );
_gtsl(y, x, 7,7,5,7 );
_gtsl(y, x, 5,7,5,5 );
_gtsl(y, x, 5,5,6,5 );
break;
case 72: /*H*/
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 5,1,5,7 );
_gtsl(y, x, 0,7,9,7 );
break;
case 73: /*I*/
_gtsl(y, x, 0,3,0,5 );
_gtsl(y, x, 0,4,9,4 );
_gtsl(y, x, 9,3,9,5 );
break;
case 74: /*J*/
_gtsl(y, x, 0,5,0,7 );
_gtsl(y, x, 0,6,7,6 );
_gtsl(y, x, 7,6,9,4 );
_gtsl(y, x, 9,4,9,3 );
_gtsl(y, x, 9,3,7,1 );
break;
case 75: /*K*/
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 0,7,4,1 );
_gtsl(y, x, 4,1,9,7 );
break;
case 76: /*L*/
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 9,1,9,7 );
break;
case 77: /*M*/
_gtsl(y, x, 9,1,0,1 );
_gtsl(y, x, 0,1,4,4 );
_gtsl(y, x, 4,4,0,7 );
_gtsl(y, x, 0,7,9,7 );
break;
case 78: /*N*/
_gtsl(y, x, 9,1,0,1 );
_gtsl(y, x, 0,1,9,7 );
_gtsl(y, x, 9,7,0,7 );
break;
case 79: /*O*/
_gtsl(y, x, 2,7,0,5 );
_gtsl(y, x, 0,5,0,3 );
_gtsl(y, x, 0,3,2,1 );
_gtsl(y, x, 2,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,5 );
_gtsl(y, x, 9,5,7,7 );
_gtsl(y, x, 7,7,2,7 );
break;
case 80: /*P*/
_gtsl(y, x, 9,1,0,1 );
_gtsl(y, x, 0,1,0,6 );
_gtsl(y, x, 0,6,1,7 );
_gtsl(y, x, 1,7,3.5,7 );
_gtsl(y, x, 3.5,7,5,5 );
_gtsl(y, x, 5,5,5,1 );
break;
case 81: /*Q*/
_gtsl(y, x, 2,7,0,5 );
_gtsl(y, x, 0,5,0,3 );
_gtsl(y, x, 0,3,2,1 );
_gtsl(y, x, 2,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,5 );
_gtsl(y, x, 9,5,7,7 );
_gtsl(y, x, 7,7,2,7 );
_gtsl(y, x, 7,5,9,7 );
break;
case 82: /*R*/
_gtsl(y, x, 9,1,0,1 );
_gtsl(y, x, 0,1,0,6 );
_gtsl(y, x, 0,6,1,7 );
_gtsl(y, x, 1,7,3.5,7 );
_gtsl(y, x, 3.5,7,5,5 );
_gtsl(y, x, 5,5,5,1 );
_gtsl(y, x, 5,4.5,9,7 );
break;
case 83: /*S*/
_gtsl(y, x, 1,7,0,6 );
_gtsl(y, x, 0,6,0,3 );
_gtsl(y, x, 0,3,2,1 );
_gtsl(y, x, 2,1,3,1 );
_gtsl(y, x, 3,1,4,2 );
_gtsl(y, x, 4,2,4,6 );
_gtsl(y, x, 4,6,5,7 );
_gtsl(y, x, 5,7,8,7 );
_gtsl(y, x, 8,7,9,6 );
_gtsl(y, x, 9,6,9,3 );
_gtsl(y, x, 9,3,7,1 );
break;
case 84: /*T*/
_gtsl(y, x, 0,0,0,9 );
_gtsl(y, x, 0,4,9,4 );
break;
case 85: /*U*/
_gtsl(y, x, 0,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,5 );
_gtsl(y, x, 9,5,7,7 );
_gtsl(y, x, 7,7,0,7 );
break;
case 86: /*V*/
_gtsl(y, x, 0,1,9,4 );
_gtsl(y, x, 9,4,0,7 );
break;
case 87: /*W*/
_gtsl(y, x, 0,1,9,2 );
_gtsl(y, x, 9,2,4,4 );
_gtsl(y, x, 4,4,9,6 );
_gtsl(y, x, 9,6,0,7 );
break;
case 88: /*X*/
_gtsl(y, x, 0,1,9,7 );
_gtsl(y, x, 0,7,9,1 );
break;
case 89: /*Y*/
_gtsl(y, x, 0,1,4,4 );
_gtsl(y, x, 4,4,9,4 );
_gtsl(y, x, 4,4,0,7 );
break;
case 90: /*Z*/
_gtsl(y, x, 0,1,0,7 );
_gtsl(y, x, 0,7,9,1 );
_gtsl(y, x, 9,1,9,7 );
break;
case 91: /*[*/
_gtsl(y, x, 0,5,0,3 );
_gtsl(y, x, 0,3,9,3 );
_gtsl(y, x, 9,3,9,5 );
break;
case 92: /* \ */
_gtsl(y, x, 9, 7, 0, 1 );
break;
case 93: /*]*/
_gtsl(y, x, 0,3,0,5 );
_gtsl(y, x, 0,5,9,5 );
_gtsl(y, x, 9,5,9,3 );
break;
case 94: /*^*/
_gtsl(y, x, 5,1,0,4 );
_gtsl(y, x, 0,4,5.5,7 );
break;
case 95: /*_*/
_gtsl(y, x, 9,1,9,7 );
break;
case 97: /*a*/
_gtsl(y, x, 4,3,4,6 );
_gtsl(y, x, 4,6,5,7 );
_gtsl(y, x, 5,7,9,7 );
_gtsl(y, x, 7,7,6,5 );
_gtsl(y, x, 6,5,6,3 );
_gtsl(y, x, 6,3,7,2 );
_gtsl(y, x, 7,2,8,2 );
_gtsl(y, x, 8,2,9,3 );
_gtsl(y, x, 9,3,9,5 );
_gtsl(y, x, 9,5,8,7 );
break;
case 98: /*b*/
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 9,1,9,5 );
_gtsl(y, x, 9,5,7,7 );
_gtsl(y, x, 7,7,6,7 );
_gtsl(y, x, 6,7,4,5 );
_gtsl(y, x, 4,5,4,3 );
_gtsl(y, x, 4,3,5.5,1 );
break;
case 99: /*c*/
_gtsl(y, x, 5,7,4,6 );
_gtsl(y, x, 4,6,4,3 );
_gtsl(y, x, 4,3,6,1 );
_gtsl(y, x, 6,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
break;
case 100: /*d*/
_gtsl(y, x, 5,7,4,6 );
_gtsl(y, x, 4,6,4,3 );
_gtsl(y, x, 4,3,6,1 );
_gtsl(y, x, 6,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
_gtsl(y, x, 9,7,0,7 );
break;
case 101: /*e*/
_gtsl(y, x, 6,7,4,6 );
_gtsl(y, x, 4,6,4,3 );
_gtsl(y, x, 4,3,6,1 );
_gtsl(y, x, 6,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
_gtsl(y, x, 6,1,6,7 );
break;
case 102: /*f*/
_gtsl(y, x, 2,7,0,5 );
_gtsl(y, x, 0,5,0,4 );
_gtsl(y, x, 0,4,2,2 );
_gtsl(y, x, 2,2,9,2 );
_gtsl(y, x, 4,1,4,4 );
break;
case 103: /*g*/
_gtsl(y, x, 5,7,4,6 );
_gtsl(y, x, 4,6,4,3 );
_gtsl(y, x, 4,3,6,1 );
_gtsl(y, x, 6,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,6 );
_gtsl(y, x, 9,6,8,7 );
_gtsl(y, x, 5,7,11,7 );
_gtsl(y, x, 11,7,12,6 );
_gtsl(y, x, 12,6,12,3 );
break;
case 104: /*h*/
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 6,1,4,4 );
_gtsl(y, x, 4,4,4,5 );
_gtsl(y, x, 4,5,6,7 );
_gtsl(y, x, 6,7,9,7 );
break;
case 105: /*i*/
_gtsl(y, x, 1.5,4,2.5,4 );
_gtsl(y, x, 4,3,4,4 );
_gtsl(y, x, 4,4,9,4 );
_gtsl(y, x, 9,3,9,5 );
break;
case 106: /*j*/
_gtsl(y, x, 1.5,5,2.5,5 );
_gtsl(y, x, 4,4,4,5 );
_gtsl(y, x, 4,5,10,5 );
_gtsl(y, x, 10,5,12,4 );
_gtsl(y, x, 12,4,12,3 );
_gtsl(y, x, 12,3,9,1.5 );
break;
case 107: /*k*/
_gtsl(y, x, 0,1,9,1 );
_gtsl(y, x, 2,7,4.5,1 );
_gtsl(y, x, 4.5,1,9,7 );
break;
case 108: /*l*/
_gtsl(y, x, 0,4,0,5 );
_gtsl(y, x, 0,5,9,5 );
_gtsl(y, x, 9,4,9,6 );
break;
case 109: /*m*/
_gtsl(y, x, 4,1,9,1 );
_gtsl(y, x, 5.5,1,4,3 );
_gtsl(y, x, 4,3,5,4 );
_gtsl(y, x, 5,4,9,4 );
_gtsl(y, x, 5,4,4,6 );
_gtsl(y, x, 4,6,5,7 );
_gtsl(y, x, 5,7,9,7 );
break;
case 110: /*n*/
_gtsl(y, x, 4,1, 9,1 );
_gtsl(y, x, 5.5,1, 4,3 );
_gtsl(y, x, 4,3, 4,5 );
_gtsl(y, x, 4,5, 6,7 );
_gtsl(y, x, 6,7, 9,7 );
break;
case 111: /*o*/
_gtsl(y, x, 4,4,4,5 );
_gtsl(y, x, 4,5,6,7 );
_gtsl(y, x, 6,7,7,7 );
_gtsl(y, x, 7,7,9,5 );
_gtsl(y, x, 9,5,9,4 );
_gtsl(y, x, 9,4,7,2 );
_gtsl(y, x, 7,2,6,2 );
_gtsl(y, x, 6,2,4,4 );
break;
case 112: /*p*/
_gtsl(y, x, 4,1,12,1 );
_gtsl(y, x, 5.5,1,4,3 );
_gtsl(y, x, 4,3,4,5 );
_gtsl(y, x, 4,5,6,7 );
_gtsl(y, x, 6,7,7,7 );
_gtsl(y, x, 7,7,9,5 );
_gtsl(y, x, 9,5,9,3 );
_gtsl(y, x, 9,3,7,1 );
break;
case 113: /*q*/
_gtsl(y, x, 4,2.5,4,4 );
_gtsl(y, x, 4,4,6,6 );
_gtsl(y, x, 6,6,12,6 );
_gtsl(y, x, 12,6,12,7 );
_gtsl(y, x, 7,6,9,4 );
_gtsl(y, x, 9,4,9,3 );
_gtsl(y, x, 9,2.5,7,1 );
_gtsl(y, x, 7,1,6,1 );
_gtsl(y, x, 6,1,4,2.5 );
break;
case 114: /*r*/
_gtsl(y, x, 4,1,9,1 );
_gtsl(y, x, 6,1,4,4 );
_gtsl(y, x, 4,4,4,5 );
_gtsl(y, x, 4,5,6,7 );
break;
case 115: /*s*/
_gtsl(y, x, 5,7,4,6 );
_gtsl(y, x, 4,6,4,3 );
_gtsl(y, x, 4,3,5,2 );
_gtsl(y, x, 5,2,5.8,2 );
_gtsl(y, x, 5.8,2, 7,6 );
_gtsl(y, x, 7,6, 7.5,7 );
_gtsl(y, x, 7.5,7, 8,7 );
_gtsl(y, x, 8,7, 9,6 );
_gtsl(y, x, 9,6,9,3 );
_gtsl(y, x, 9,3,8,2 );
break;
case 116: /*t*/
_gtsl(y, x, 0,3.5,8,3.5 );
_gtsl(y, x, 2,0,2,8 );
_gtsl(y, x, 8,3.5,9,5 );
_gtsl(y, x, 9,5,9,6 );
_gtsl(y, x, 9,6,8,7 );
break;
case 117: /*u*/
_gtsl(y, x, 4,1,7,1 );
_gtsl(y, x, 7,1,9,3 );
_gtsl(y, x, 9,3,9,5 );
_gtsl(y, x, 9,5,8,7 );
_gtsl(y, x, 4,7,9,7 );
break;
case 118: /*v*/
_gtsl(y, x, 4,1,9,4 );
_gtsl(y, x, 9,4,4,7 );
break;
case 119: /*w*/
_gtsl(y, x, 4,1,9,2.5 );
_gtsl(y, x, 9,2.5,5,4 );
_gtsl(y, x, 5,4,9,5.5 );
_gtsl(y, x, 9,5.5,4,7 );
break;
case 120: /*x*/
_gtsl(y, x, 4,1,9,7 );
_gtsl(y, x, 4,7,9,1 );
break;
case 121: /*y*/
_gtsl(y, x, 4,1,7.5,4.5 );
_gtsl(y, x, 4,7,12,1 );
break;
case 122: /*z*/
_gtsl(y, x, 4,1,4,7 );
_gtsl(y, x, 4,7,9,1 );
_gtsl(y, x, 9,1,9,7 );
break;
case '|': ; /* | */
_gtsl(y, x, 0, 4, 9, 4 );
break;
default:
_gtsl(y, x, 2,2,0,4 );
_gtsl(y, x, 0,4,0,6 );
_gtsl(y, x, 0,6,1,7 );
_gtsl(y, x, 1,7,3,7 );
_gtsl(y, x, 3,7,4,6 );
_gtsl(y, x, 4,6,4,5 );
_gtsl(y, x, 4,5,5,4 );
_gtsl(y, x, 5,4,6,4 );
_gtsl(y, x, 8,4,9,4 );
}
}



// draws a complete string. be aware that you need to enclose calls
// to this function - or multiple calls into
// glBegin(GL_LINES);
// gl_tiny_string(10,10,"Hello!");
// glEnd();
//
void gl_tiny_string(float x, float y, char* str)
{
float xpos=x;
float ypos=y;
while(*str != '\0')
{
if(*str=='\n')
{
ypos-=13;
xpos=x;
}
else
{
gl_tiny_string_draw_char(xpos, ypos, *str);
xpos+=8;
}
str++;
}
}


#endifopengl2d.h:

#ifndef __OPENGL2D__
#define __OPENGL2D__

inline void gl_begin2d()
{
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
int vPort[4];
glGetIntegerv(GL_VIEWPORT, vPort); // get current viewport
//glOrtho(0,vPort[2],0,vPort[3],-1,1);// Set Up An Ortho Screen
glOrtho(vPort[0], vPort[0]+vPort[2]-1, vPort[1], vPort[1]+vPort[3]-1,-1,1);// Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPushMatrix(); // Store The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix


// Save GL state to be restored afterwards
glPushAttrib( GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT |
GL_CURRENT_BIT | GL_TEXTURE_BIT |
GL_LIST_BIT | GL_DEPTH_BUFFER_BIT);
//glPushAttrib(GL_ALL_ATTRIB_BITS);

glDisable(GL_CULL_FACE); // important!
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST); // Disables Depth Testing
glDepthMask(GL_FALSE); // probably a little speedup on some opengl drivers

glEnable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_GEN_R);// disable automatic texture coordinate generation
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

//glEnable(GL_BLEND);

}
inline void gl_end2d()
{
glPopAttrib();
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPopMatrix(); // Restore The Old Projection Matrix
}



#endif

jide
05-25-2006, 10:28 AM
Es geht mir gut !

herc
05-25-2006, 10:58 AM
;-)

Zengar
05-25-2006, 12:47 PM
wow, did you auto-generate it or was it written by hand?

herc
05-25-2006, 01:02 PM
as i mentioned - i took the code from the
otk.sourceforge.net -
an opengl widget kit - and modified it to make it completely independent from any other stuff.
i dont know how the otk programmer generated that font. i think someone designed this font by hand, because its so minimalistic (or better say: not that pretty).
but thats the advantage: its very small! i think if compiled its probably only 1kbyte of binary code added to your project.

i also have a small, selfcontained header + cpp file that builds a texture font (verdana 10pt bold). if someone is interested, i could post that too. i convertet the font into a binary bitmap (using angelfont), then converted that into an array and put that c-array into the source file.

vizowl
05-25-2006, 06:34 PM
If you have access to the freetype2 library then this is a great way to draw a string too http://gpwiki.org/index.php/OpenGL:Tutorials:Font_System

It does not add the freetype library as a runtime dependency rather it uses a freetype too to load the font you want into a binary file that you then load directly into a texture.