Reputation: 193
I am finding it hard to figure out how to get my Score to appear on the screen. I currently have it working and printing to the console window but that's it.
When the ball hits the box the count increase by one one the NX_NOTIFY_ON_TOUCH event and is printed in the console window during the update function.
I have put the count variable in the .h file for the simulation code and then retireved it in the .cpp file for the visualisation and then tried to print it to screen using the HD function provided with line:
hud.AddDisplayString("Hit Count = ",0.0f,0.89f);
after looking it at i soon realsied that using that code i couldn't access the variable for count.
Does anyone know how in anyway it is possible to print to the visual screen.
EDIT: the function to draw the text to screen
void drawText(float *text, int length, int x, int y)
{
glMatrixMode(GL_PROJECTION);
double *matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x, y);
for(int i=0; i<length; i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
and then in the display function i write:
glColor3f(5,5,5);
drawText(&HITcount, HITcount, 0 , 535);
Upvotes: 1
Views: 398
Reputation: 162307
First problem here:
I have put the count variable in the .h file for the simulation code and then retireved it in the .cpp file for the visualisation and then tried to print it to screen using the HD function provided with line
Please inform yourself about variable scope and compilation units. For immediate relief change the variable declaration in the header (.h
) file to be extern, without an assignment, and in one of the compilation unit source files (.c*
) actually define the variable, ideally with initialization assignment.
Does anyone know how in anyway it is possible to print to the visual screen as i can't find any help online that works.
Switch to a projection that matches your HUDs needs, and draw the score over your scene. You can switch the projection and viewport anytime and anywhere you see fit. Pro-Tip: If you've got any code dealing with the projection matrix in a window's resize handler, move it do the drawing code.
Let me comment on that drawText
function
void drawText(float *text, int length, int x, int y)
{
glMatrixMode(GL_PROJECTION);
double *matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
This actually makes sense as the projection matrix stap may not nest deep enough for using glPushMatrix/glPopMatrix. But at the end of the function it misses to free the allocated memory. In fact using a statically allocated array is better. The number of elements is well known and getting memory of the stack is much more efficient than on the heap.
glLoadIdentity();
glOrtho(0, 800, 0, 600, -5, 5);
Problem here: The extents of the orthographic projection should match the viewport size. Here they don't.
glMatrixMode(GL_MODELVIEW);
The following glLoadIdentity will trash the modelview matrix, as it's not within a push/pop frame. Also it's redundant
glLoadIdentity();
The following is what's needed.
glPushMatrix();
glLoadIdentity();
The problem here is, that glRasterPos act like emitting a vertex, i.e. it gets clipped (we can do nothing about that) and depth tested. Also glutBitmapCharacter (glBitmap actually) may interfere with the depth buffer.
glRasterPos2i(x, y);
for(int i=0; i<length; i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}
The function can be fixed as following:
void drawText(float *text, int length, int x, int y)
{
double old_modelview[16];
int viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glMatrixMode(GL_PROJECTION);
glGetDoublev(GL_PROJECTION_MATRIX, old_modelview);
glLoadIdentity();
glOrtho(viewport[0], viewport[0]+viewport[2],
viewport[1], viewport[1]+viewport[3],
-1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// We're using glBitmap, i.e. legacy functionality anyway
// so we can use glPush/PopAttrib as well.
glPushAttrib(GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glRasterPos2i(x, y);
for(int i=0; i<length; i++) {
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
}
glPopAttrib(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(old_modelview);
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
Upvotes: 2