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

📄 text.cpp

📁 游戏编程精粹2第六章源码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* Copyright (C) Dante Treglia II, 2000.  * All rights reserved worldwide. * * This software is provided "as is" without express or implied * warranties. You may freely copy and compile this source into * applications you distribute provided that the copyright text * below is included in the resulting source code, for example: * "Portions Copyright (C) Dante Treglia II, 2000" */#ifdef _WIN32#include <windows.h>#endif#include <GL/gl.h>#include <malloc.h>#ifndef _WIN32#include <string.h>#include <assert.h>#endif#include "text.h"//---------------------------------------------------------------------------//---------------------------------------------------------------------------// The text texture only needs to be initialized oncestatic unsigned int fontID = 0;// Forward definitionextern unsigned char fontData[];#define FONT_W 8#define FONT_H 8#define FONT_DRAW_W 8#define FONT_DRAW_H 9#define FONT_TEX_W 64#define FONT_TEX_H 128//---------------------------------------------------------------------------// A text box constructor//---------------------------------------------------------------------------TextBox::TextBox(int inL, int inR, int inT, int inB) {	// Calculate the Text box boundaries	boxL = inL;	boxR = inL + ((inR - inL) - ((inR - inL) % FONT_DRAW_W));	boxT = inT;	boxB = inT + ((inB - inT) - ((inB - inT) % FONT_DRAW_H));	// Initialize the Buffers and Pointers	buffer = (char *)malloc(MAX_STR);	buffer[0] = '\0';	drawBufferPtr = buffer;	// Calculate the number of lines per Page	pageLineCnt = (boxB - boxT) / FONT_DRAW_H;	scrollCnt = 0;	currX = boxL;	if (!fontID) {		// Create Texture		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);		glGenTextures(1, &fontID);		glBindTexture(GL_TEXTURE_2D, fontID);		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);		glTexImage2D(			GL_TEXTURE_2D,			0,			GL_LUMINANCE_ALPHA,			FONT_TEX_W,			FONT_TEX_H,			0,			GL_LUMINANCE_ALPHA,			GL_UNSIGNED_BYTE,			(void *)fontData);	}	// Set Color	SET_COLOR(color, 255, 255, 255, 255);	// Set Mode	mode = 0;}//---------------------------------------------------------------------------// Load Font Texture and set current Matrix to Screen Space//---------------------------------------------------------------------------void TextBox::Begin() {	// Push the neccessary Matrices on the stack	glMatrixMode(GL_PROJECTION);	glPushMatrix();		glLoadIdentity();		glOrtho(0.0, 640.0, 480.0, 0.0, -1.0, 1.0);	glMatrixMode(GL_MODELVIEW);	glPushMatrix();		glLoadIdentity();	// Push the neccessary Attributes on the stack	glPushAttrib(GL_TEXTURE_BIT|GL_ENABLE_BIT);	glBindTexture(GL_TEXTURE_2D, fontID);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 	glEnable(GL_TEXTURE_2D);	// Always Draw in Front	glDisable(GL_DEPTH_TEST);	glDisable(GL_CULL_FACE);	if (!(mode & TEXT_DRAW_BACKGROUND)) {		glEnable(GL_BLEND);		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	}}//---------------------------------------------------------------------------// Remove Screen Matrix//---------------------------------------------------------------------------void TextBox::End() {	// Return to previous Matrix and Attribute states. Easy cleanup!	glMatrixMode(GL_PROJECTION);	glPopMatrix();	glMatrixMode(GL_MODELVIEW);	glPopMatrix();	glPopAttrib();}//---------------------------------------------------------------------------// Draw a string at a given coordinate//---------------------------------------------------------------------------void TextBox::DrawStr(int x, int y, char * str) {	Begin();	DrawStr(x, y, 0, 0, str);	End();}//---------------------------------------------------------------------------// Draw the Text given to TextBoxPrintf()//---------------------------------------------------------------------------void TextBox::Draw() {	Paginate();	Begin();	if(mode & TEXT_DRAW_BOX) DrawBoundingBox();	DrawStr(boxL, boxT, 1, boxB, drawBufferPtr);	End();}//---------------------------------------------------------------------------// Draw a string at a given position in screen space//---------------------------------------------------------------------------void TextBox::DrawStr(int x, int y, int maxFlag, int maxY, char* string) {	int cursorX;	int cursorY;	char* cP = string;	int index;	int s, t;	cursorX = x;	cursorY = y;	glColor4ub(color.r, color.g, color.b, color.a);	glBegin(GL_QUADS);	// Parse the String	while (*cP != '\0') {		// Visible characters		if ( ' ' == *cP && !(mode & TEXT_DRAW_SPACES)) {			cursorX += FONT_DRAW_W;		}		else if ( ' ' <= *cP && *cP <= '~' ) {			index = *cP - ' ';			s = ( index % 8 ) * FONT_W;			t = ( index / 8 ) * FONT_H;			glTexCoord2f((float)(s       )/FONT_TEX_W, (float)(t           )/FONT_TEX_H);			glVertex3s(cursorX            , cursorY            , 0);			glTexCoord2f((float)(s       )/FONT_TEX_W, (float)(t+FONT_H + 1)/FONT_TEX_H);			glVertex3s(cursorX            , cursorY+FONT_DRAW_H, 0);			glTexCoord2f((float)(s+FONT_W)/FONT_TEX_W, (float)(t+FONT_H + 1)/FONT_TEX_H);			glVertex3s(cursorX+FONT_DRAW_W, cursorY+FONT_DRAW_H, 0);			glTexCoord2f((float)(s+FONT_W)/FONT_TEX_W, (float)(t           )/FONT_TEX_H);			glVertex3s(cursorX+FONT_DRAW_W, cursorY            , 0);			cursorX += FONT_DRAW_W;		}		if ( *cP == '\n' ) {			cursorX = x;			cursorY += FONT_DRAW_H;			if (maxFlag && cursorY + FONT_DRAW_H > maxY) break;		}		cP++;	}	glEnd( );}//---------------------------------------------------------------------------// Draw Bounding Box//---------------------------------------------------------------------------void TextBox::DrawBoundingBox() {	glColor4f(0.0, 0.0, 0.0, 1.0);	glBegin(GL_QUADS);	glVertex3s(boxL, boxT, 0);	glVertex3s(boxR, boxT, 0);	glVertex3s(boxR, boxB, 0);	glVertex3s(boxL, boxB, 0);	glEnd( );}//---------------------------------------------------------------------------// This function will concatinate a formated string the a TextBox//---------------------------------------------------------------------------void TextBox::FormatStrCat(char * str) {	char buff[MAX_STR];	char buff2[MAX_STR];	char * cP;	char * cP2;	strcpy(buff, str);	cP = buff;	// Format String for caption	// Parse the String	while (*cP != '\0') {		// Visible characters		if ( ' ' <= *cP && *cP <= '~' ) {			currX += FONT_DRAW_W;		}		else if ( *cP == '\n' ) {			currX = boxL; 		}		// Wrap 		if (currX >= boxR) {			cP++;			// Find the last space			if (mode & TEXT_WRAP_SPACES) {				while(cP > buff && *cP != ' ') cP--;			}			strcpy(buff2, cP);			*cP++ = '\n';			*cP = '\0';			// Eliminate Space			if (mode & TEXT_WRAP_SPACES && buff2[0] == ' ') cP2 = &buff2[1];			else cP2 = buff2;			strcat(cP, cP2);			currX = boxL;		} 		else {			cP++;		}	}	// Make sure it fits in the text buffer by removing	// strings at the top of the buffer.	cP = buffer;	while (strlen(cP) + strlen(buff) > MAX_STR) {		cP = strchr(cP, '\n');		if( !cP ) {			assert( !"TextBox::FormatStrCat: string too long!!!" );		}		cP++;	}	// If there are strings that need to be removed. Do so.	if (cP != buffer) {		strcpy(buff2, cP);		strcpy(buffer, buff2);	}	// Now there is space. So, concatinate.	strcat(buffer, buff); }//---------------------------------------------------------------------------// Paginate the text Box//---------------------------------------------------------------------------void TextBox::Paginate() {	int newLineCnt = 0;	int pageNewLineCnt;	char * cP;	// Calculate the number of new lines from the bottom we want	pageNewLineCnt = pageLineCnt + scrollCnt;	// Starting at the end of the string count the number of newlines backwards	cP = strchr(buffer, '\0');	while(cP > buffer) {		if(*cP == '\n') newLineCnt++;		if (newLineCnt == pageNewLineCnt) {			cP++;			break;		}		cP--;	}	if (cP == buffer && newLineCnt > pageLineCnt) 		scrollCnt = newLineCnt - pageLineCnt + 1;

⌨️ 快捷键说明

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