⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glfont.cpp

📁 Font class which can create, destroy and manage font
💻 CPP
字号:
#include "GLFont.h"

#include <GL/gl.h>
#include <GL/glu.h>



CGLFont::CGLFont(char* fontFace, int width, int height, int weight)
{
	SetFont(GetWindowDC(GetForegroundWindow()), fontFace, width, height, weight);	
}

CGLFont::CGLFont()
{
	SetFont(GetWindowDC(GetForegroundWindow()), "Calibri", 0, 0, 0);	
}

void CGLFont::SetFont(HDC hDC, char* fontFace, int width, int height, int weight)
{
	this->hDC = hDC;
	HFONT hFont = CreateFontA(height, width, 0/*aspect angle*/,
							  0/*angle*/,weight, FALSE/*italic*/, FALSE/*underline*/, FALSE/*strike-out*/, 
							  RUSSIAN_CHARSET, OUT_TT_PRECIS/*render precision*/, CLIP_DEFAULT_PRECIS/*clip precision*/,
							  ANTIALIASED_QUALITY/*quality*/, FF_DONTCARE|DEFAULT_PITCH, fontFace);
	if( hFont == NULL )
		return;
	fontOffset = glGenLists( 256 );
	SelectObject( hDC, hFont );
	wglUseFontBitmaps(hDC, 0, 256, fontOffset);
	DeleteObject( hFont );
}

CGLFont::~CGLFont()
{
	glDeleteLists(fontOffset, 256);
}

void CGLFont::put(float x, float y, float z, const char *str, const RGBColor& textColor, unsigned int len)
{
	glColor3d(textColor.r, textColor.g, textColor.b);
	unsigned int realLen = strlen(str);
	if (len > realLen || len == -1)
		len = realLen;
	glRasterPos3f(x, y, z);
	glPushAttrib(GL_LIST_BIT);
	glListBase(fontOffset);
	glCallLists((GLsizei)len, GL_UNSIGNED_BYTE, str);
	glPopAttrib();
}

void CGLFont::putHighlighted(float x, float y, float z, const char *str, int sHighlight, int fHighlight, int charCnt, const RGBColor& textColor, const RGBColor& bgColor)
{
	if (sHighlight < 0)
	{
		fHighlight += sHighlight;
		sHighlight = 0;
	}
	if (fHighlight < 0)
	{
		sHighlight += fHighlight;
		fHighlight = -1*fHighlight;
	}
	if (fHighlight > charCnt - sHighlight)
		fHighlight = charCnt - sHighlight;
	if (sHighlight != 0)
	{
		put(x,y,z,str,textColor,sHighlight);
	}
	TSize size;
	GetTextSize(str, sHighlight,size);
	int sPos = size.cx;
	GetTextSize(str + sHighlight, fHighlight, size);
	int fPos = size.cx;
	glColor3d(1 - bgColor.r, 1 - bgColor.g, 1.7 - bgColor.b);
	glBegin(GL_QUADS);
		glVertex3d(x + sPos, y - 0.2*size.cy, z);
		glVertex3d(x + sPos + fPos, y - 0.2*size.cy, z);
		glVertex3d(x + sPos + fPos, y + 0.8*size.cy, z);
		glVertex3d(x + sPos, y + 0.8*size.cy, z);
	glEnd();
	RGBColor invertedText(1 - textColor.r, 1 - textColor.g, 1 - textColor.b);
	put(x + sPos,y,z,str + sHighlight, invertedText, fHighlight);
	glColor3d(textColor.r, textColor.g, textColor.b);
	put(x + sPos + fPos,y,z,str + sHighlight + fHighlight, textColor, charCnt - sHighlight - fHighlight);
}

void CGLFont::GetTextSize(const char* str, unsigned int len, TSize& size)
{
	GetTextExtentPoint32(hDC, str, len, &size);
}


int CGLFont::GetSymbolCnt(const char* str, unsigned int len, unsigned int pixels, TSize& size)
{
	int cnt;
	GetTextExtentExPoint(hDC, str, len, pixels, &cnt, NULL, &size);
	return cnt;
}

void CGLFont::WrapText(const char* text, int len, int area, TIntVec& wrapPos)
{
	wrapPos.push_back(0);
	TSize size;
	int cnt = 0;
	int oldCnt = 0;
	int tmp = 0;
	int enter = 0;
	while (oldCnt < len)
	{
		tmp = cnt = oldCnt + GetSymbolCnt(text + oldCnt, len - oldCnt, area, size);
		if (oldCnt == 0 && cnt == 0)
			throw std::logic_error("Control width too small. Text cannot be fit");
		while (cnt > oldCnt && text[cnt] != ' ' && text[cnt] != '\n' && cnt != len) --cnt;
		++cnt;
		enter = oldCnt;
		while (enter < cnt && text[enter] != '\n') ++enter;
		if (text[enter] == '\n')
			cnt = enter + 1;
		if (cnt == 0 || cnt <= oldCnt)
			oldCnt = tmp;
		else
			oldCnt = cnt;
		wrapPos.push_back(oldCnt);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -