GLUT to display text

hey guys,
i was trying to work this out but i cant seem to get it working… what i want is to use glut to the keyboardfunc to get any text i type in then display it onscreen using renderBitmapString … it seems that im failing in converting unsinged char into *char…

Hi !

Hmmmm, a little bit more info would help, in what way are you failing ? does the program compile ? does it run but doesn’t display anything ? does it crash ?

Mikael

thanx for the reply…
no the program doesnt crash… but insteat of the code that i input it gives out rubbish on the screen… what im trying to do is this

char *text;
void processNormalKeys(unsigned char key, int x, int y) {

text = key;
}

void renderBitmapString(float x,float y, GLUT_BITMAP_TIMES_ROMAN_10
, text) {
char *c;
glRasterPos3f(x, y,z);
for (c=text; *c != ‘\0’; c++) {
glutBitmapCharacter(font, *c);
}
}

sth like that… i will post the actuall code when i get back to my pc … this is just something i based my code on from an online tutorial.

char *text;
void processNormalKeys(unsigned char key, int x, int y)
{
*text = key; <== don’t forget the *
}

text have been allocated ?

you should do:

char *text;
void processNormalKeys(unsigned char key, int x, int y)
{
int len = strlen(text);
text[len]=key;
text[len+1]=0;
}

The only problem I see with the way you are doing it, is that if you had to switch to double buffers or re-draw the scene you would loose past charactors you typed.

Best create a variable,

char keyboardbuffer[64];
int keyboardindex;

void processNormalKeys(unsigned char key, int x, int y) {

keyboardbuffer[keyboardindex] = key;
    keyboardindex++; increase with each key stroke

}

// This prints a string to the screen
void Sprint( int x, int y, char *st)
{
int l,i;

l=strlen( st ); // see how many characters are in text string.
glRasterPos2i( x, y); // location to start printing text
for( i=0; i &lt; l; i++)  // loop until i is greater then l
	{
	glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen
}

}

void Display(void)
{

Sprint( 0, 0, keyboardbuffer ); // Print text to screen

}

Hello,

I display my text in GLUT as the following:

void displayText( float x, float y, int r, int g, int b, const char *string ) {
int j = strlen( string );

glColor3f( r, g, b );
glRasterPos2f( x, y );
for( int i = 0; i < j; i++ ) {
glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, string[i] );
}
}

<IMG SRC="http://www.bluedev.net/images/gamedev/header_1.jpg">

thanx nexusone, thats exactly what i was looking for.

ive added that code so i could like type at the position where my mouse is… so if my mouse hasent moved it would append what im writing to the string and if i move it it will clear the keyboard buffer and start from scrach… but somehow its not working correctly… here is the code if anyone can see anything wrong plz point me to it…

void renderBitmapString(float x,float y,float z,void *font, char *string) {
char *c;
c=string;
glRasterPos3f(x, y,z);
for (int count=0; count <= keyboardindex; count++) {
glutBitmapCharacter(font, c[count]);
}
}

void processNormalKeys(unsigned char key, int x, int y) {

keyboardbuffer[keyboardindex] = key;
cout<<keyboardbuffer[keyboardindex]<<endl;
cout<<x << " " << y << endl;
if (checked ==false)
{LastX=x;
LastY=y;
checked=true;}
if (LastX != x | | LastY !=y)
{
keyboardindex=0; LastX=x;
LastY=y;}

renderBitmapString(x,600-y,0,GLUT_BITMAP_TIMES_ROMAN_24, keyboardbuffer);
keyboardindex++;
glutPostRedisplay();
}

thanx nexusone, thats exactly what i was looking for.

ive added that code so i could like type at the position where my mouse is… so if my mouse hasent moved it would append what im writing to the string and if i move it it will clear the keyboard buffer and start from scrach… but somehow its not working correctly… here is the code if anyone can see anything wrong plz point me to it…

void renderBitmapString(float x,float y,float z,void *font, char *string) {
char *c;
c=string;
glRasterPos3f(x, y,z);
for (int count=0; count <= keyboardindex; count++) {
glutBitmapCharacter(font, c[count]);
}
}

void processNormalKeys(unsigned char key, int x, int y) {

keyboardbuffer[keyboardindex] = key;
cout<<keyboardbuffer[keyboardindex]<<endl;
cout<<x << " " << y << endl;
if (checked ==false)
{LastX=x;
LastY=y;
checked=true;}
if (LastX != x | | LastY !=y)
{
keyboardindex=0; LastX=x;
LastY=y;}

renderBitmapString(x,600-y,0,GLUT_BITMAP_TIMES_ROMAN_24, keyboardbuffer);
keyboardindex++;
glutPostRedisplay();
}

You problem is that you draw your text then call update screen, which in turn erases it.

You should draw you text inside of the display routine like my example.

renderBitmapString(x,600-y,0,GLUT_BITMAP_TIMES_ROMAN_24, keyboardbuffer);  // This should be in the display routine, which is the routine called by glutPostRedisplay.
keyboardindex++;
glutPostRedisplay(); 

}

no it doesnt get cleared coz i got GL_COLOR_BUFFER_BIT disabled after the first draw.

nexusone is right that you should be doing the display of your text in the display function. You don’t show your display function, or your setup code, but if you’re using double buffering, you would especially want to make sure that the display function is the only callback function doing the rendering.

// Glutwindow.c
// By Eric Stringer 2003
// Simple examples of OpenGL and Glut usage.
// Keyboard input to screen

#include <windows.h> // This header file will be needed for some windows compilers
//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers
//#include <GL/glu.h>
#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse.
#include <stdio.h> // standard (I/O library)
#include <stdlib.h> // standard library (set of standard C functions
#include <math.h> // Math library (Higher math functions )
#include <ctype.h>

char keyboardbuffer[20][40];
int keyboardindex_x[20];
int keyboardindex_y;

// I use this to put text on the screen
void Sprint( int x, int y, char *st)
{
int l,i;

l=strlen( st ); // see how many characters are in text string.
glRasterPos2i( x, y); // location to start printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen
}

}

static void TimeEvent(int te)
{
glutPostRedisplay(); // Update screen with new data
glutTimerFunc( 100, TimeEvent, 1); // Reset our timmer.
}

// Setup our Opengl world, called once at startup.
void init(void)
{
int i;
glClearColor (0.0, 0.0, 0.0, 0.0); // When screen cleared, use black.
glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat
glEnable(GL_DEPTH_TEST); // Check depth when rendering
for (i = 0; i < 20; i++)
{
keyboardindex_x[i] = 0;
}
keyboardindex_y = 8;
}

// Draw our world
void display(void)
{
int i;

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix

glColor3f(1.0, 1.0, 1.0);
for ( i = 0; i < 20; i++)
{
Sprint(-8, 8 - i, keyboardbuffer[i]);
}

glutSwapBuffers();
}

// This is called when the window has been resized.
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}

void arrows( int key, int x, int y)
{

if (key == GLUT_KEY_UP)
{
keyboardindex_y–;
if (keyboardindex_y < 0) keyboardindex_y = 0;
}

if (key == GLUT_KEY_DOWN)
{
keyboardindex_y++;
if (keyboardindex_y > 19) keyboardindex_y = 19;
}
}

// Read the keyboard
void keyboard (unsigned char key, int x, int y)
{

if ( isalnum(key) | | key == ’ ') // Check for a letter of the alphabet
{
keyboardbuffer[keyboardindex_y][keyboardindex_x[keyboardindex_y]] = key;
keyboardindex_x[keyboardindex_y]++;
if (keyboardindex_x[keyboardindex_y] > 39 ) keyboardindex_x[keyboardindex_y] = 39; // Stop buffer overflow
}
if ( key == 8 )
{
keyboardindex_x[keyboardindex_y]–;
if ( keyboardindex_x[keyboardindex_y] < 0) keyboardindex_x[keyboardindex_y] = 0; // Stop index from being negitive
keyboardbuffer[keyboardindex_y][keyboardindex_x[keyboardindex_y]] = 0;
}

switch (key)
{
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}
}

// Main program
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
glutSetWindowTitle(“GlutWindow”);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutSpecialFunc(arrows);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}

[This message has been edited by nexusone (edited 08-12-2003).]

[This message has been edited by nexusone (edited 08-12-2003).]

thanx alot for the great replies… im very thankful…
i found out what the problem was… i put the if statements below the ones that add the characters to the array :stuck_out_tongue: dumb very dumb… anyways thanx alot …

ohhh btww… i got one more question about the font thing… can i change its color?! if so how?

thanx alot for the great replies… im very thankful…
i found out what the problem was… i put the if statements below the ones that add the characters to the array :stuck_out_tongue: dumb very dumb… anyways thanx alot …

ohhh btww… i got one more question about the font thing… can i change its color?! if so how?

Originally posted by stealthmaker:
[b]thanx alot for the great replies… im very thankful…
i found out what the problem was… i put the if statements below the ones that add the characters to the array :stuck_out_tongue: dumb very dumb… anyways thanx alot …

ohhh btww… i got one more question about the font thing… can i change its color?! if so how?[/b]

glColor, see my example code

hmm i thought i tried that and it seemed like it didnt work before… but now its wroking great! … thanx alot man… u have been a great help…