Advertisement

problem with font being "cut off?" any ideas?

Started by January 19, 2003 01:48 AM
0 comments, last by camcbri 22 years, 1 month ago
hello all! i'm having an issue occur wherein the bottom portion of letters such as y, g, j, p, etc are not drawn. for example, the y looks like a v, the j like an i, the p and g like circles with a line on one side. when i resize the window, the issue is fixed, but continued resizing can make the issue reappear. Also, sometimes the portions may be drawn partially... Heres the font code:
    
#ifdef WIN32
#include <windows.h>
#else

#include <gl/glx.h>
#include <cstdlib>
#endif

#include "font.hxx"
#include <gl/gl.h>
#include <cstdio>
#include <string>

//* ---------------------------------------------------------------------------

//* Constructor

//* ---------------------------------------------------------------------------

Font::Font(std::string face, GLint size)
: _face(face),
  _size(size),
  _red(0.0f),
  _green(0.0f),
  _blue(0.0f),
  _xpos(0),
  _ypos(0),
  _base(0)
{
   if(_face == "")
#ifdef WIN32
      _face = "Courier New";
#else
      _face = "adobe-helvetica-medium";
#endif

   Create();

   return;
}

//* ---------------------------------------------------------------------------

//* THE destructor

//* ---------------------------------------------------------------------------

Font::~Font()
{
   if(_base)
      KillFont();

   _base = 0;

   return;
}

//* ---------------------------------------------------------------------------

//* Create

//* Allocate the memory for the font & create some display lists.

//* ---------------------------------------------------------------------------

int Font::Create()
{
   int retVal = 0;

   _base = glGenLists(96);

#ifdef WIN32
   HDC   hdc  = GetDC(0);
   HFONT font;
   HFONT oldFont;

   font = CreateFont(_size, 
                     0, 0, 0,
                     FW_BOLD,
                     false, false, false,
                     ANSI_CHARSET,
                     OUT_TT_PRECIS,
                     CLIP_DEFAULT_PRECIS,
                     ANTIALIASED_QUALITY,
                     FF_DONTCARE | DEFAULT_PITCH,
                     _face.c_str());

   if(font == 0)
      retVal = -1;

   oldFont = (HFONT)SelectObject(hdc, font);
   wglUseFontBitmaps(hdc, 32, 96, _base);
   SelectObject(hdc, oldFont);
   DeleteObject(font);

#else
   Display* display;
   XFontStruct* fontInfo;
   
   char convert[2] = {0};
   itoa(_size, convert, 10);

   std::string face = "-" + _face +
                      "-r-normal--" +
                      convert + 
                      "-*-*-*-p-*-iso8859-1";

   display = XOpenDisplay(0);
   fontInfo = XLoadQueryFont(display, face.c_str());

   if(fontInfo == 0)
      fontInfo = XLoadQueryFont(display, "fixed");
   
   if(fontInfo == 0)
      retVal = -1;

   glXUseXFont(fontInfo->fid, 32, 96, base);
   XFreeFont(display, fontInfo);
   XCloseDisplay(display);
#endif
   return retVal;
}

//* ---------------------------------------------------------------------------

//* KillFont

//* Deallocate the memory used by the font.

//* ---------------------------------------------------------------------------

void Font::KillFont()
{
   glDeleteLists(_base, 96);
   return;
}

//* ---------------------------------------------------------------------------

//* Print

//* Print out any string to the screen.

//* ---------------------------------------------------------------------------

void Font::Print(const char* fmt, ...)
{
	static char text[256] = {'\0'};
   va_list ap = 0;

   memset(text, '\0', 256);

   if(fmt != 0)
   {
      va_start(ap, fmt);
         vsprintf(text, fmt, ap);
      va_end(ap);

      glPushAttrib(GL_LIST_BIT);
         glColor3f(_red, _green, _blue);
         glListBase(_base - 32);
         glPushMatrix();
            glLoadIdentity();
            //glTranslatef(0.0f, 0.0f, -10.0f);

            glRasterPos2i(_xpos, _ypos);
            glCallLists((GLsizei)strlen(text), 
                        GL_UNSIGNED_BYTE, text);
         glPopMatrix();
      glPopAttrib();
   }


   return;
}

//* ---------------------------------------------------------------------------

//* SetFace

//* ---------------------------------------------------------------------------

void Font::SetFace(const char* face)
{
   _face = face;
   return;
}

//* ---------------------------------------------------------------------------

//* SetSize

//* ---------------------------------------------------------------------------

void Font::SetSize(GLint size)
{
   _size = size;
   return;
}
   
//* ---------------------------------------------------------------------------

//* SetColor

//* ---------------------------------------------------------------------------

void Font::SetColor(GLfloat r, GLfloat g, GLfloat b)
{
   _red   = r;
   _green = g;
   _blue  = b;
   return;
}

//* ---------------------------------------------------------------------------

//* SetPosition

//* ---------------------------------------------------------------------------

void Font::SetPosition(GLint x, GLint y)
{
   _xpos = x;
   _ypos = y;
   return;
}

//* ---------------------------------------------------------------------------

//* Rebuild

//* Delete the old font and allocate a new one with a new size or font face.

//* Color is not changed.

//* ---------------------------------------------------------------------------

void Font::Rebuild()
{
   KillFont();
   Create();
   return;
}
  
and here is the interface drawing (the error occurs in the function StartMenu):
  

#include "interface.hxx"
#include "gamestates.hxx"

Interface::Interface(GLuint w, GLuint h, TextureManager* tm)
: _font(new Font()),
  _bigFont(new Font("creepy", 100)),
  _width(w),
  _height(h),
  _cubeList(0)
{
   _cubeList = glGenLists(1);
   BuildCubeList();
}

Interface::~Interface()
{
   glDeleteTextures(2, _textures);
   glDeleteLists(_cubeList, 1);

	delete _font;
	_font = 0;

	delete _bigFont;
	_bigFont = 0;

}

void Interface::DrawCube(GLuint tex)
{}

void Interface::BuildCubeList()
{
   glNewList(_cubeList, GL_COMPILE);
		glBegin(GL_QUADS);	// top face

         glNormal3f(0.0f, 1.0f, 0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f);	
			glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -0.5f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f);
         glNormal3f(0.0f, 0.0f, 1.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);	
			glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
		glNormal3f(1.0f, 0.0f, 0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, -0.5f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);	
			glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, -0.5f, 0.5f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(0.5f, -0.5f, -0.5f);
		glNormal3f(-1.0f, 0.0f, 0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.5f);	
			glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f); 
			glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
		glNormal3f(0.0f, -1.0f, 0.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(-0.5f, -0.5f, 0.5f); 
			glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(0.5f, -0.5f, -0.5f);	
		glNormal3f(0.0f, 0.0f, -1.0f);
			glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, 0.5f, -0.5f);
			glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -0.5f);
			glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, -0.5f, -0.5f);
			glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -0.5f);	
		glEnd();
   glEndList();
}

void Interface::SetDimensions(GLuint width, GLuint height)
{
	_width  = width;
	_height = height;
}

void Interface::ReadyForInterface()
{
	glPushMatrix();

	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
		glLoadIdentity();

		glOrtho(0, _width, 0, _height, 110, -110);
		glMatrixMode(GL_MODELVIEW);
		glTranslatef(0.0f, 0.0f, 10.0f);
		glLoadIdentity();
}

void Interface::ReadyForDrawing()
{
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		   
		glMatrixMode(GL_MODELVIEW);
   glPopMatrix();
}

void Interface::DisplayFrameRate(GLfloat fps)
{
	ReadyForInterface();

	_font->SetPosition(_width-300, _height-50);
	_font->SetColor(1.0f, 1.0f, 0.0f);
	_font->Print("Frame Rate: %i", (GLuint)fps);

   ReadyForDrawing();
}

void Interface::TemporaryShite(GLfloat fps, GLint rockets, GLint hits)
{
	ReadyForInterface();

	_font->SetPosition(25, 30);  
	_font->SetColor(0.0f, 0.0f, 0.0f);
   _font->Print("Rockets Fired: %i", rockets);

	_font->SetPosition(_width-250, 30);
	_font->SetColor(0.0f, 0.0f, 0.0f);
   _font->Print("Targets Hit: %i", hits);
	
	_font->SetPosition(_width-300, _height-50);
	_font->SetColor(1.0f, 1.0f, 0.0f);
	_font->Print("Frame Rate: %i", (GLuint)fps);

   ReadyForDrawing();
}

GLint Interface::StartMenu(GLint& choice)
{
	GLint retVal = STATE_MENU;

	ReadyForInterface();
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	_bigFont->SetColor(1.0f, 0.0f, 0.0f);
	_bigFont->SetPosition(50, _height - 101);
	_bigFont->Print("Multi-Pong!");

   _font->SetPosition(200, _height - 150);
   _font->SetColor(1.0f, 1.0f, 0.0f);
   _font->Print(" by Kevin Greenan & Chris McBride");

	if(choice == 0)
	{
		_font->SetColor(1.0f, 0.0f, 0.0f);

		if(GetAsyncKeyState(VK_RETURN))
			retVal = STATE_RUN;
	}
	else
	{
		_font->SetColor(0.0f, 1.0f, 0.0f);
	}	
	_font->SetPosition(_width / 2, _height / 2);
	_font->Print("Start Game");

	if(choice == 1)
		_font->SetColor(1.0f, 0.0f, 0.0f);
	else
		_font->SetColor(0.0f, 1.0f, 0.0f);

	_font->SetPosition(_width / 2, _height / 2 - 50);
	_font->Print("Exit Game");

	if(choice == 2)
		_font->SetColor(1.0f, 0.0f, 0.0f);
	else
		_font->SetColor(0.0f, 1.0f, 0.0f);

	_font->SetPosition(_width / 2, _height / 2 - 100);
	_font->Print("See Credits");

	ReadyForDrawing();

	glutSwapBuffers();

	return retVal;
}

GLint Interface::SplashScreen()
{
	return STATE_RUN;
}

//GLint Loading()

//{

//}


GLint Interface::Pause()
{
	return STATE_RUN;
}

GLint Interface::Running()
{
	return STATE_RUN;
}
  
and here is the code calling the interface:
  
GLint Game::Go()
{
   //std::cout << "Going..." << std::endl;  


   switch(_state)
   {
   case STATE_INIT:
      Init();
      _state = STATE_MENU;
      break;

   case STATE_MENU:
	   _interface->SetDimensions(_width, _height);
	  _state = _interface->StartMenu(_choice);
      break;

   case STATE_RUN:
      _state = Run();
      break;

   case STATE_CLOSE:      
      _state = Shutdown();
      break;

   case STATE_PAUSE:
      _state = Pause();
      break;
   }

   return _state;
}
    
thanks for any help! [edited by - camcbri on January 19, 2003 2:54:13 AM]
Don''t use Bitmap fonts... they VERY slow down the app...
PM Times change... Excuse my poor english!

This topic is closed to new replies.

Advertisement