Jump to content
  • Advertisement
Sign in to follow this  
Mawr

Problem using Windows Fonts - c++

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The problem I'm having is pretty strange, atleast from my point of view. I'm trying to use the created font from CreateFont(...) , but the first calling of CreateFont(...) doesn't quite work correctly. Any string I give it to draw is several sizes too small, has the first letter after every space missing (ex. "Click Here" would be "lick ere"). Calling it again, and storing a new font object allows the orignal font object to work properly, which is really confusing. newFont.h
#ifndef GUARD_NEWFONT_H
#define GUARD_NEWFONT_H

class mainClass;

#include <windows.h>
#include <gl/gl.h>
#include <string>

#define TEXT_RIGHT 0
#define TEXT_LEFT 1
#define TEXT_CENTER 2

using namespace std;

class newFont
{
public:
  int currBase;
  int fontSize;
  mainClass *mainPtr;
  
  newFont(char *FONTNAME, int FONTSIZE, HDC hDC, mainClass *MAIN);
  newFont();
  ~newFont();
  int createBitmapFont(char *FONTNAME, int FONTSIZE, HDC hDC);
  void renderFontAt(string MSG, float X, float Y, int TEXTALIGN, float R, float G, float B);
};
#include "mainClass.h"
#endif

newFont.cpp

#ifndef GUARD_NEWFONT_CPP
#define GUARD_NEWFONT_CPP

#include "newFont.h"

using namespace std;

newFont::newFont(char* FONTNAME, int FONTSIZE, HDC hDC, mainClass *MAIN)
: currBase(0), fontSize(FONTSIZE)
{                       
  currBase=createBitmapFont(FONTNAME, FONTSIZE, hDC); 
  mainPtr=MAIN;
}

newFont::newFont()
{}

int newFont::createBitmapFont(char* FONTNAME, int FONTSIZE, HDC hDC)
{
  HFONT hFont;
  unsigned int base;          //only positive numbers can be used for a base number
                     //but 0 can be used too, how do we know we got a valid range?
  
  base = glGenLists(96-32);
  
  if(stricmp(FONTNAME, "symbol") == 0)
  {
    hFont = CreateFont(FONTSIZE, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, 
                       SYMBOL_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
                       ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, FONTNAME);
  }
  else
  {
    hFont= CreateFont(FONTSIZE, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, 
                      ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
                      ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, FONTNAME);
  }
  
  
  if(!hFont)
    return 0;
  
  SelectObject(hDC, hFont);
  wglUseFontBitmaps(hDC, 32, 96, base);
  return base;
}

void newFont::renderFontAt(string MSG, float X, float Y, int TEXTALIGN, float R, float G, float B)
{
  float size = MSG.size();
  if(size<1)
    return;
  glColor3f(R,G,B);
  switch(TEXTALIGN)
  {
    case TEXT_LEFT:
         glRasterPos2f(mainPtr->TransXpt(X+3),mainPtr->TransYpt(Y));
         break;
    case TEXT_RIGHT:
         glRasterPos2f(mainPtr->TransXpt(X-(size*(fontSize-1))),mainPtr->TransYpt(Y));
         break;
    case TEXT_CENTER:
         X= mainPtr->TransXpt(X-(size*fontSize/2));
         Y= mainPtr->TransYpt(Y);
         
         glRasterPos2f(X,Y);
         break;
    default:
         glRasterPos2f(mainPtr->TransXpt(X),mainPtr->TransYpt(Y));
         break;
  }
  
  glPushAttrib(GL_LIST_BIT);
    glListBase(currBase-32);
    glCallLists((int)size, GL_UNSIGNED_BYTE, &MSG[0]);
  glPopAttrib();
}

newFont::~newFont()
{
  if(currBase !=0)
  {
    glDeleteLists(currBase, 96-32);
  }
  fontSize=0;
  mainPtr=NULL;
}


I create the newFont object when I call the constructor for mainClass. In there I do: fe("Courier New", 12, hDC, this); with fe being a newFont object. Later, after I call mainClass's constructor, I call do: newFont nf = newFont("Courier New", 12, hDC, this); and the orignal font works, even though I'm not calling the new font object anywhere else. If someone could help me with this I'd be very appreciative. As always, thank you for your time.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!