Quantcast
Channel: Aimbots.net - The #1 Community For All Your Gaming Needs!
Viewing all articles
Browse latest Browse all 1717

OpenGL Text Rendering

$
0
0
Drawing text in OpenGL could not be easier using this lovely class I have created from the knowledge passed down to me from @c5. You do not need GLUT to do this, it is very efficient and you have many options in the creation of your fonts.

gltext.h
Code:

//http://guidedhacking.com and c5
#pragma once
#include <windows.h>
#include <stdio.h>
#include <gl\GL.h>
#include "geom.h"

class GLText_t
{
public:
        unsigned int hackFont;
        bool bFontBuilt = false;
        HDC fontHDC = nullptr;

        void BuildFonts();
        void Print(float x, float y, const unsigned char color[3], const char *format, ...);

        //center on X and Y axes
        vec3_t centerText(float x, float y, float width, float height, float textWidth, float textHeight);

        //center on X axis only
        float centerText(float x, float width, float textWidth);
};

gltext.cpp
Code:

#include "gltext.h"

void GLText_t::BuildFonts()
{
        fontHDC = wglGetCurrentDC();
        hackFont = glGenLists(96);
        HFONT hFont = CreateFontA(-15, 0, 0, 0, FW_MEDIUM, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, FF_DONTCARE | DEFAULT_PITCH, "Consolas");
        HFONT hOldFont = (HFONT)SelectObject(fontHDC, hFont);
        wglUseFontBitmaps(fontHDC, 32, 96, hackFont);
        SelectObject(fontHDC, hOldFont);
        DeleteObject(hFont);

        bFontBuilt = true;
}

void GLText_t::Print(float x, float y, const unsigned char color[3], const char *format, ...)
{
        glColor3ub(color[0], color[1], color[2]);
        glRasterPos2f(x, y);

        char text[100];
        va_list        args;

        va_start(args, format);
        vsprintf_s(text, 100, format, args);
        va_end(args);

        glPushAttrib(GL_LIST_BIT);
        glListBase(hackFont - 32);
        glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
        glPopAttrib();
}

vec3_t GLText_t::centerText(float x, float y, float width, float height, float textWidth, float textHeight)
{
        vec3_t text;
        text.x = x + (width - textWidth) / 2;
        text.y = y + textHeight;
        return text;
}

float GLText_t::centerText(float x, float width, float textWidth)
{
        if (width > textWidth)
        {
                float difference = width - textWidth;
                return (x + (difference / 2));
        }

        else
        {
                float difference = textWidth - width;
                return (x - (difference / 2));
        }
}

You have to do it in an openGL hook because you need wglGetCurrentDC() to return the correct device context. If a new map is loaded or for some other reason the current device context changes, you need to rebuild the font for the new context. That will be accomplished if in your openGL hook you do this:

Code:

HDC currentHDC = wglGetCurrentDC();

if (!GLText->bFontBuilt || currentHDC != GLText->fontHDC)
{
        GLText->BuildFonts();
}

Original Thread Here

Viewing all articles
Browse latest Browse all 1717

Trending Articles