1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// ==============================================================
// This file is part of Glest Shared Library (www.glest.org)
//
// Copyright (C) 2001-2008 Martio Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
#include "text_renderer_gl.h"
#include "opengl.h"
#include "font_gl.h"
#include "leak_dumper.h"
namespace Shared{ namespace Graphics{ namespace Gl{
// =====================================================
// class TextRenderer2DGl
// =====================================================
TextRenderer2DGl::TextRenderer2DGl(){
rendering= false;
}
void TextRenderer2DGl::begin(const Font2D *font){
assert(!rendering);
rendering= true;
this->font= static_cast<const Font2DGl*>(font);
}
void TextRenderer2DGl::render(const string &text, int x, int y, bool centered){
assert(rendering);
assertGl();
int line=0;
int size= font->getSize();
const unsigned char *utext= reinterpret_cast<const unsigned char*>(text.c_str());
Vec2f rasterPos;
const FontMetrics *metrics= font->getMetrics();
if(centered){
rasterPos.x= x-metrics->getTextWidth(text)/2.f;
rasterPos.y= y+metrics->getHeight()/2.f;
}
else{
rasterPos= Vec2f(static_cast<float>(x), static_cast<float>(y));
}
glRasterPos2f(rasterPos.x, rasterPos.y);
for (int i=0; utext[i]!='\0'; ++i) {
switch(utext[i]){
case '\t':
rasterPos= Vec2f((rasterPos.x/size+3.f)*size, y-(size+1.f)*line);
glRasterPos2f(rasterPos.x, rasterPos.y);
break;
case '\n':
line++;
rasterPos= Vec2f(static_cast<float>(x), y-(metrics->getHeight()*2.f)*line);
glRasterPos2f(rasterPos.x, rasterPos.y);
break;
default:
glCallList(font->getHandle()+utext[i]);
}
}
assertGl();
}
void TextRenderer2DGl::end(){
assert(rendering);
rendering= false;
}
// =====================================================
// class TextRenderer3DGl
// =====================================================
TextRenderer3DGl::TextRenderer3DGl(){
rendering= false;
}
void TextRenderer3DGl::begin(const Font3D *font){
assert(!rendering);
rendering= true;
this->font= static_cast<const Font3DGl*>(font);
assertGl();
//load color
glPushAttrib(GL_TRANSFORM_BIT);
assertGl();
}
void TextRenderer3DGl::render(const string &text, float x, float y, float size, bool centered){
assert(rendering);
assertGl();
const unsigned char *utext= reinterpret_cast<const unsigned char*>(text.c_str());
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glPushAttrib(GL_POLYGON_BIT);
float scale= size/10.f;
if(centered){
const FontMetrics *metrics= font->getMetrics();
glTranslatef(x-scale*metrics->getTextWidth(text)/2.f, y-scale*metrics->getHeight()/2.f, 0);
}
else{
glTranslatef(x-scale, y-scale, 0);
}
glScalef(scale, scale, scale);
for (int i=0; utext[i]!='\0'; ++i) {
glCallList(font->getHandle()+utext[i]);
}
glPopMatrix();
glPopAttrib();
assertGl();
}
void TextRenderer3DGl::end(){
assert(rendering);
rendering= false;
assertGl();
glPopAttrib();
assertGl();
}
}}}//end namespace
|