PDA

View Full Version : Printing characters: help a newb!



Baltor
07-12-2004, 02:34 PM
When I run this code:

#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <algorithm>
#include <wchar.h>
#include <string>
#include <windows.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <game.hpp>

using namespace std;

// Non-specific functions
int randomRange(int lowestNumber, int highestNumber)
{
if(lowestNumber > highestNumber)
swap(lowestNumber, highestNumber);
int range = highestNumber - lowestNumber + 1;
return lowestNumber + int(range * rand()/(RAND_MAX + 1.0));
}

void printText(int startPosX, int startPosY, string text, float r, float g, float b)
{
int y = startPosY;
int x;
int fontWidth = 8;
for (int i=1; i<=text.length(); i++)
{
x = startPosX + (i-1)*(fontWidth);
glColor3f(r,g,b);
glRasterPos2i(x,y);
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, text[i-1]);
}
}
// End Non-specific functions

void displayDifficulty(void)
{
glClear(GL_COLOR_BUFFER_BIT);
string titleText("Asteroids - by Evan Patterson");
string difficultyText("Press the coresponding number to select the difficulty");
string difficulty1Text("1 -- What's Asteroids, anyway?");
string difficulty2Text("2 -- I've played once or twice...");
string difficulty3Text("3 -- I'm pretty darn good");
string difficulty4Text("3 -- I'm master, bring it on!");
printText(300, 150, titleText, 1.0, 1.0, 1.0);
printText(300, 170, difficultyText, 1.0, 1.0, 1.0);
printText(300, 180, difficulty1Text, 1.0, 1.0, 1.0);
printText(300, 190, difficulty1Text, 1.0, 1.0, 1.0);
printText(300, 200, difficulty1Text, 1.0, 1.0, 1.0);
printText(300, 200, difficulty1Text, 1.0, 1.0, 1.0);
glutSwapBuffers();
}

void keyboardDifficulty(unsigned char key, int x, int y)
{
if (key == 49)
Game::Game theGame(1);
if (key == 50)
Game::Game theGame(2);
if (key == 51)
Game::Game theGame(3);
if (key == 52)
Game::Game theGame(4);
if (key == 27)
exit(0);
}

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 0, 600, 0, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
ShowCursor(FALSE);
glutDisplayFunc(displayDifficulty);
glutKeyboardFunc(keyboardDifficulty);
glutReshapeFunc(reshape);
glShadeModel(GL_FLAT);
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Asteroids by Evan Patterson");
glutFullScreen();
init();
glutMainLoop();
return 0;
}I don't get an error, but the text is not displayed on the screen. I've used the printText function in previous programs and it has worked like a charm. Does anyone know how I might fix this problem?
Thanks in advance.

plasmonster
07-12-2004, 07:17 PM
Try a range of depth values in the Ortho() call, like

glOrtho(0, 800, 0, 600, -999, 999).

plasmonster
07-12-2004, 07:27 PM
Also, consider calling a function like this after a suspicious block of code. This may help you isolate problem code faster.


void checkGL( const char* msg = "GL ERROR" )
{
GLenum error = glGetError();
if( error != GL_NO_ERROR )
printf( "%s: %s\n", msg, gluErrorString(error) );
}

Baltor
07-13-2004, 11:58 AM
Thanks Q, got it working.