1

I'm trying to draw string in GLUT. I need to convert int value to unsigned char* before I can use it in glutBitmapString. But when I do like this

glBegin(GL_POINTS);
    glRasterPos2i(15, 20);
    char text[10];
    sprintf(text, "%d", 5);
    glutBitmapString(GLUT_BITMAP_HELVETICA_18, (unsigned char*)text);   
glEnd();

There is not text on the screen. But in debugger text contains value of 5. What is wrong?

genpfault
  • 51,148
  • 11
  • 85
  • 139
lapots
  • 12,553
  • 32
  • 121
  • 242
  • Any chance this is the same problem as this question: http://stackoverflow.com/questions/16873561/glutbitmapstring-shows-nothing – Mats Petersson Oct 27 '13 at 17:19
  • 4
    Also, I think glutBitmapString can't be called between any `glBegin`/`glEnd` statements. – Tom Knapen Oct 27 '13 at 17:31
  • Yes. It seems it doesn't work between `glBegin`/`glEnd`. – lapots Oct 27 '13 at 17:49
  • The list of things that are valid between `glBegin`/`glEnd` can probably be counted on one hand. If it does not pertain to specifying vertex properties, it does not belong between them. `glutBitmapString (...)` is a raster function (completely different stage in the pipeline), so it has nothing to do with vertices. – Andon M. Coleman Oct 27 '13 at 19:44
  • Same problem http://stackoverflow.com/questions/15843832/how-can-i-write-a-text-in-glut/15852730#15852730 – Dinesh Subedi Oct 30 '13 at 11:54

1 Answers1

0

Don't know if this helps, but I personally use:

std::uint32_t Base = 0;

void SetupFonts(int FontSize)
{
    HFONT Font = CreateFont(-MulDiv(FontSize, GetDeviceCaps(DC, LOGPIXELSY), 72), 0, 0, 0, FW_BOLD, false, false, false, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Arial");
    HFONT OldFont = static_cast<HFONT>(SelectObject(DC, Font));
    wglUseFontBitmaps(DC, 32, 96, Base);
    //wglUseFontBitmaps(DC, 32, 96, Base); //May or may not need this line twice.
    SelectObject(DC, OldFont);
    DeleteObject(Font);
}

template<typename... Args>
void Print(float X, float Y, float R, float G, float B, const char* Text, Args... args)
{
    using namespace std;
    std::size_t Position = 0;
    std::string Container(Text);
    std::vector<std::string> Arguments;
    std::initializer_list<int> {(Arguments.push_back(to_string(args)), 0)...};

    for (auto it = Arguments.begin(); it != Arguments.end(); ++it)
    {
        if ((Position = Container.find("%")) != std::string::npos)
        {
            Container.replace(Position, 1, *it);
        }
    }

    glColor3f(R, G, B);
    glRasterPos2f(X, Y);
    glPushAttrib(GL_LIST_BIT);
    glListBase(this->Base - 32);
    glCallLists(Container.size(), GL_UNSIGNED_BYTE, Container.data());
    glPopAttrib();
    glFlush();
}

if (Base == 0) SetupFonts(12);
Print(200, 200, 1.0f, 0.0f, 0.0f, "Hello %", "World");
Print(200, 250, 0.0f, 1.0f, 0.0f, "Your FPS is: %", CalculateFPS());

etc..

Draws text that looks like: enter image description here

Brandon
  • 22,723
  • 11
  • 93
  • 186