////////////////////////////////////////////////////////////////////////////////////////// glutText 1.0 Description: C++ object to hold not more than 79 chars and render them using GLUT. Author: V'lion Contact Info: pdn@rmci.net http://thefivelions.tripod.com/bugle4d/ Notes: I wrote this for a program I was writing. This implementaion uses GLUT_STROKE_ROMAN for its font. There are other fonts GLUT supplies, I just like this one. Entire string will be on the same Z, X is the right-to-left var, and Y is the top-to-bottom car. Features: Minimal error checking. RGB or RGBA color 3D point for bottom left. Scaling- dynamic if you feel like. Linefeed changing. Functions: PrintMe()- Call in your render loop. SetLowLeft(x,y,z)- Sets 3D point for bottom left. SetText(text)- Sets the text. SetScaleNline()- Sets scale and newline. SetColor(r,g,b) SetColor(r,g,b,a)- Switches to RGB if A is not set. Header Files required: glut.h gl.h Licence This file and code copyright 2002 by Paul "V'lion" Nathan This code is made to be used, however, all responsibilities using this code entails rest on the user; the user is responisible for ensuring it is bugfree and harmless. ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// Code ////////////////////////////////////////////////////////////////////////////////////////// class glutText { float x,y,z; float r,g,b,a; char str[80]; float scale; float nrow; public: bool PrintMe() { //Basic error check if(str == NULL) { //Just returns false. Implement error check elsewhere. return false; } //temporary sting used for pointer arithmatic char *temp = str; //index for automatic linefeeds int linefeed = 0; //Counts linefeeds int k = 0; //tenporary Y, so that it does not get pushed down more each render float tempy = y; glPushMatrix(); { glLoadIdentity(); if(a < 0) //means that A has not been set. { glColor3f(r,g,b); } else { glColor4f(r,g,b,a); } //Move to start co-ord. Specified in current world co-ords. glTranslatef(x,tempy,z); //Scale to a resonable size...0.04 works well. glScalef(scale,scale,scale); //indexes thru the string. string _must_ be null-terminated. :-D while(*temp != 0) { //If its a newline, linefeed if(*temp == '\n') { glLoadIdentity(); glTranslatef(x, tempy -= nrow, z); glScalef(scale,scale,scale); k++; } //output char. glutStrokeCharacter(GLUT_STROKE_ROMAN,*temp); temp++; linefeed++; } } glPopMatrix(); return true; } void SetLowLeft(float x, float y,float z) { glutText::x = x; glutText::y = y; glutText::z = z; } void SetText(char *text) { strcpy(str,text); } void SetScaleNline(float scale, float nrow) { glutText::nrow = nrow; glutText::scale = scale; } void SetColor(float r, float g, float b, float a) { glutText::r = r; glutText::g = g; glutText::b = b; glutText::a = a; } void SetColor(float r, float g, float b) { glutText::r = r; glutText::g = g; glutText::b = b; } glutText() { scale= 0.005; nrow = .5; a = -1; //So that when it is time to render, it can check to see if a == -1, //if it is, a is unsed } }; ////////////////////////////////////////////////////////////////////////////////////////// Sample initialization ////////////////////////////////////////////////////////////////////////////////////////// thetext.SetColor(1,1,1); thetext.SetLowLeft(0,0,0); thetext.SetScaleNline(.005,10); thetext.SetText("Hello World"); ////////////////////////////////////////////////////////////////////////////////////////// Sample rendering; ////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////// thetext.PrintMe();