2011-08-03 20:05:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2007, 2010 - Bit-Blot
|
|
|
|
|
|
|
|
This file is part of Aquaria.
|
|
|
|
|
|
|
|
Aquaria is free software; you can redistribute it 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.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#include "BitmapFont.h"
|
2016-07-09 02:18:40 +00:00
|
|
|
#include "RenderBase.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
#include "Core.h"
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
#include "../ExternalLibs/glfont2/glfont2.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
using namespace glfont;
|
|
|
|
|
|
|
|
BmpFont::BmpFont()
|
2016-07-09 02:18:40 +00:00
|
|
|
: font(new GLFont)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
scale = 1;
|
|
|
|
loaded = false;
|
|
|
|
overrideTexture = 0;
|
|
|
|
fontTopColor = Vector(1,1,1);
|
|
|
|
fontBtmColor = Vector(1,1,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
BmpFont::~BmpFont()
|
|
|
|
{
|
2016-07-09 02:18:40 +00:00
|
|
|
delete font;
|
2011-08-03 20:05:33 +00:00
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BmpFont::destroy()
|
|
|
|
{
|
|
|
|
if (loaded)
|
|
|
|
{
|
2016-07-09 02:18:40 +00:00
|
|
|
font->Destroy();
|
2011-08-03 20:05:33 +00:00
|
|
|
loaded = false;
|
|
|
|
}
|
2015-03-23 23:06:51 +00:00
|
|
|
|
|
|
|
overrideTexture = NULL;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BmpFont::load(const std::string &file, float scale, bool loadTexture)
|
|
|
|
{
|
|
|
|
if (loaded)
|
2016-07-09 02:18:40 +00:00
|
|
|
font->Destroy();
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
this->scale = scale;
|
|
|
|
|
|
|
|
GLuint id=0;
|
|
|
|
glGenTextures(1, &id);
|
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
if (!font->Create(file.c_str(), id, loadTexture))
|
2011-08-03 20:05:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture *fontTextureTest = 0;
|
Script interface improvements & extensions.
- Pointer typechecks are now enabled by default.
- enabled all script warnings for non-FULL or DEMO builds by default
- Added generic obj_* functions that operate on any type of RenderObject.
These give quite low-level control about the renderer, and are quite
dangerous too.
Subsequently, many functions sharing the same code (*_setPosition, for example)
could be removed, and simply call the generic functions after a type check.
- Added interface function deathNotify(). The original logic of death
notifiers was never used, so i thought i'd make use of it.
This is useful in scripts to safely drop dangling pointers.
- removed sendEntityMessage, entity_setCollideWithAvatar, entity_setTouchDamage,
which were essentially no-ops.
- Replaced all unnecessary luaReturnNum(0) and luaReturnInt(0),
now it does only push a return value on the stack if the function
is actually supposed to have a retun value.
- Allow variadic calling of entity_msg(). Now any parameters can be passed
to the function, and the target entity will receive all of them.
Any values returned from the entity's msg() callback will be returned
by entity_msg() to the original caller. This allows nice RPC-like
entity communication.
- fixed possible crash in debugLog, bone_update, entity_debugText
- added an override function for loadfile() that is case-insensitive like dofile()
- entity_createEntity returns the created entity now
- spawnParticleEffect returns the associated RenderObject
- Added some text rendering functions
- Added beam_setFirer()
- removed the underflow check in avatar_decrLeaches() I added earlier
- added a panic function for Lua.
- added the Lua debug library
- fixed a stupid typo in ScriptObject::isType() that made the type checks a lot less accurate
- misc stuff I forgot
2012-02-05 19:22:54 +00:00
|
|
|
BitmapText::BitmapText(BmpFont *bmpFont)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
this->bmpFont = bmpFont;
|
|
|
|
bfePass =0;
|
|
|
|
bfe = BFE_NONE;
|
|
|
|
|
|
|
|
currentScrollLine = currentScrollChar = 0;
|
|
|
|
scrollDelay = 0;
|
|
|
|
scrolling = false;
|
|
|
|
align = ALIGN_CENTER;
|
|
|
|
textWidth = 600;
|
|
|
|
this->fontDrawSize = 24;
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
cull = false;
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
alignWidth = 0;
|
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::autoKern()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitmapText::getWidthOnScreen()
|
|
|
|
{
|
|
|
|
return text.size()*(fontDrawSize/2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::setAlign(Align align)
|
|
|
|
{
|
|
|
|
this->align = align;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string BitmapText::getText()
|
|
|
|
{
|
|
|
|
return this->text;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::setText(const std::string &text)
|
|
|
|
{
|
|
|
|
this->text = text;
|
|
|
|
formatText();
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:27:32 +00:00
|
|
|
void BitmapText::setWidth(float width)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
textWidth = width;
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:27:32 +00:00
|
|
|
float BitmapText::getSetWidth()
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
return textWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
float BitmapText::getHeight()
|
|
|
|
{
|
2016-07-09 02:18:40 +00:00
|
|
|
float sz = bmpFont->font->GetCharHeight('A') * bmpFont->scale;
|
2011-08-03 20:05:33 +00:00
|
|
|
return lines.size()*sz;
|
|
|
|
}
|
|
|
|
|
2015-11-16 02:59:47 +00:00
|
|
|
float BitmapText::getLineHeight()
|
|
|
|
{
|
2016-07-09 02:18:40 +00:00
|
|
|
return bmpFont->font->GetCharHeight('A') * bmpFont->scale;
|
2015-11-16 02:59:47 +00:00
|
|
|
}
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
void BitmapText::formatText()
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
text = this->text;
|
|
|
|
lines.clear();
|
|
|
|
std::string currentLine;
|
|
|
|
int lastSpace = -1;
|
2015-11-11 22:27:32 +00:00
|
|
|
float currentWidth = 0;
|
2011-08-03 20:05:33 +00:00
|
|
|
alignWidth = 0;
|
2015-11-11 22:27:32 +00:00
|
|
|
maxW = 0;
|
2011-08-03 20:05:33 +00:00
|
|
|
for (int i = 0; i < text.size(); i++)
|
2016-05-05 17:40:28 +00:00
|
|
|
{
|
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
float sz = bmpFont->font->GetCharWidth(text[i])*bmpFont->scale;
|
2011-08-03 20:05:33 +00:00
|
|
|
currentWidth += sz;
|
|
|
|
|
|
|
|
if (currentWidth+sz >= textWidth || text[i] == '\n')
|
|
|
|
{
|
|
|
|
if (text[i] == '\n')
|
|
|
|
{
|
|
|
|
lastSpace = i;
|
|
|
|
}
|
|
|
|
lines.push_back(text.substr(0, lastSpace));
|
|
|
|
int tsz = text.size();
|
|
|
|
text = text.substr(lastSpace+1, tsz);
|
|
|
|
i = 0;
|
|
|
|
alignWidth = currentWidth;
|
2015-11-11 22:27:32 +00:00
|
|
|
maxW = std::max(currentWidth, maxW);
|
2011-08-03 20:05:33 +00:00
|
|
|
currentWidth = 0;
|
|
|
|
lastSpace = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (text[i] == ' ')
|
|
|
|
{
|
|
|
|
lastSpace = i;
|
2015-11-11 22:27:32 +00:00
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2015-11-11 22:27:32 +00:00
|
|
|
maxW = std::max(currentWidth, maxW);
|
2011-08-03 20:05:33 +00:00
|
|
|
if (alignWidth == 0)
|
|
|
|
alignWidth = currentWidth;
|
|
|
|
if (!text.empty() && (text.size() > 1 || text[0] != ' '))
|
|
|
|
{
|
|
|
|
lines.push_back(text);
|
|
|
|
}
|
|
|
|
colorIndices.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::setBitmapFontEffect(BitmapFontEffect bfe)
|
|
|
|
{
|
|
|
|
this->bfe = bfe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::updateWordColoring()
|
|
|
|
{
|
|
|
|
colorIndices.resize(lines.size());
|
|
|
|
for (int i = 0; i < colorIndices.size(); i++)
|
|
|
|
{
|
|
|
|
colorIndices[i].resize(lines[i].size());
|
|
|
|
for (int j = 0; j < colorIndices[i].size(); j++)
|
|
|
|
{
|
|
|
|
colorIndices[i][j] = Vector(1,1,1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitmapText::isEmpty()
|
|
|
|
{
|
|
|
|
return lines.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::scrollText(const std::string &text, float scrollSpeed)
|
|
|
|
{
|
|
|
|
if (text.empty()) return;
|
|
|
|
this->scrollSpeed = scrollSpeed;
|
|
|
|
scrollDelay = scrollSpeed;
|
|
|
|
this->text = text;
|
|
|
|
scrolling = true;
|
|
|
|
formatText();
|
|
|
|
currentScrollLine = 0;
|
|
|
|
currentScrollChar = 0;
|
|
|
|
}
|
|
|
|
|
2015-11-11 22:27:32 +00:00
|
|
|
void BitmapText::setFontSize(float sz)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
this->fontDrawSize = sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::onUpdate(float dt)
|
|
|
|
{
|
|
|
|
bfePass = 0;
|
|
|
|
RenderObject::onUpdate(dt);
|
|
|
|
if (scrollDelay > 0 && scrolling)
|
|
|
|
{
|
|
|
|
if (lines.empty())
|
|
|
|
{
|
|
|
|
currentScrollLine = 0;
|
|
|
|
scrolling = false;
|
|
|
|
scrollDelay = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scrollDelay -= dt;
|
|
|
|
while (scrollDelay <= 0)
|
|
|
|
{
|
|
|
|
float diff = scrollDelay;
|
|
|
|
scrollDelay = scrollSpeed;
|
|
|
|
scrollDelay += diff;
|
|
|
|
currentScrollChar ++;
|
|
|
|
if (currentScrollChar >= lines[currentScrollLine].size())
|
|
|
|
{
|
|
|
|
currentScrollLine++;
|
|
|
|
currentScrollChar = 0;
|
|
|
|
if (currentScrollLine >= lines.size())
|
|
|
|
{
|
|
|
|
currentScrollLine = 0;
|
|
|
|
scrolling = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector BitmapText::getColorIndex(int i, int j)
|
|
|
|
{
|
|
|
|
Vector c(1,1,1);
|
|
|
|
if (!(i < 0 || j < 0))
|
|
|
|
{
|
|
|
|
if ( i < colorIndices.size() && j < colorIndices[i].size())
|
|
|
|
{
|
|
|
|
c = colorIndices[i][j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::onRender()
|
|
|
|
{
|
|
|
|
if (!bmpFont) return;
|
|
|
|
float top_color[3] = {bmpFont->fontTopColor.x*color.x, bmpFont->fontTopColor.y*color.y, bmpFont->fontTopColor.z*color.z};
|
|
|
|
float bottom_color[3] = {bmpFont->fontBtmColor.x*color.x, bmpFont->fontBtmColor.y*color.y, bmpFont->fontBtmColor.z*color.z};
|
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->Begin();
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
if (fontTextureTest) fontTextureTest->apply();
|
|
|
|
|
|
|
|
if (bmpFont->overrideTexture) bmpFont->overrideTexture->apply();
|
|
|
|
|
|
|
|
float y=0;
|
|
|
|
float x=0;
|
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
float adj = bmpFont->font->GetCharHeight('A') * bmpFont->scale;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
if (scrolling)
|
|
|
|
{
|
|
|
|
for (int i = 0; i <= currentScrollLine; i++)
|
|
|
|
{
|
|
|
|
std::string theLine = lines[i];
|
|
|
|
if (i == currentScrollLine)
|
|
|
|
theLine = theLine.substr(0, currentScrollChar);
|
|
|
|
|
|
|
|
x=0;
|
|
|
|
if (align == ALIGN_CENTER)
|
|
|
|
{
|
|
|
|
std::pair<int, int> sz;
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->GetStringSize(lines[i], &sz);
|
2011-08-03 20:05:33 +00:00
|
|
|
x = -sz.first*0.5f*bmpFont->scale;
|
|
|
|
}
|
|
|
|
float la = 1.0f-(scrollDelay/scrollSpeed);
|
2016-05-05 17:40:28 +00:00
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->DrawString(theLine, bmpFont->scale, x, y, top_color, bottom_color, alpha.x, la);
|
2011-08-03 20:05:33 +00:00
|
|
|
y += adj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < lines.size(); i++)
|
|
|
|
{
|
|
|
|
x=0;
|
|
|
|
if (align == ALIGN_CENTER)
|
|
|
|
{
|
|
|
|
std::pair<int, int> sz;
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->GetStringSize(lines[i], &sz);
|
2011-08-03 20:05:33 +00:00
|
|
|
x = -sz.first*0.5f*bmpFont->scale;
|
|
|
|
}
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->DrawString(lines[i], bmpFont->scale, x, y, top_color, bottom_color, alpha.x, 1);
|
2011-08-03 20:05:33 +00:00
|
|
|
y += adj;
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 17:40:28 +00:00
|
|
|
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::unloadDevice()
|
|
|
|
{
|
|
|
|
RenderObject::unloadDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::reloadDevice()
|
|
|
|
{
|
|
|
|
RenderObject::reloadDevice();
|
|
|
|
setText(this->text);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::render()
|
|
|
|
{
|
|
|
|
RenderObject::render();
|
|
|
|
if (!bfePass)
|
|
|
|
{
|
|
|
|
if (bfe == BFE_SHADOWBLUR)
|
|
|
|
{
|
|
|
|
InterpolatedVector oldAlpha = alpha, oldPos = position;
|
|
|
|
Vector adjust(rand()%5-2, rand()%5-2+2);
|
|
|
|
position += adjust;
|
|
|
|
alpha *= 0.4f;
|
|
|
|
bfePass = 1;
|
|
|
|
RenderObject::render();
|
|
|
|
alpha = oldAlpha;
|
|
|
|
position = oldPos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BitmapText::stopScrollingText()
|
|
|
|
{
|
|
|
|
scrolling = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BitmapText::isScrollingText()
|
|
|
|
{
|
|
|
|
return scrolling;
|
|
|
|
}
|
|
|
|
|
|
|
|
int BitmapText::getNumLines()
|
|
|
|
{
|
|
|
|
return lines.size();
|
|
|
|
}
|
|
|
|
|
2015-03-08 19:43:41 +00:00
|
|
|
float BitmapText::getStringWidth(const std::string& text)
|
|
|
|
{
|
|
|
|
std::string tmp;
|
|
|
|
int maxsize = 0;
|
|
|
|
tmp.reserve(text.length());
|
|
|
|
for (size_t i = 0; i < text.size(); i++)
|
|
|
|
{
|
|
|
|
if(text[i] == '\n')
|
|
|
|
{
|
|
|
|
std::pair<int, int> dim;
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->GetStringSize(tmp, &dim);
|
2015-03-08 19:43:41 +00:00
|
|
|
maxsize = std::max(maxsize, dim.first);
|
|
|
|
tmp.resize(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tmp += text[i];
|
|
|
|
}
|
|
|
|
std::pair<int, int> dim;
|
2016-07-09 02:18:40 +00:00
|
|
|
bmpFont->font->GetStringSize(tmp, &dim);
|
2015-03-08 19:43:41 +00:00
|
|
|
maxsize = std::max(maxsize, dim.first);
|
|
|
|
|
|
|
|
return maxsize * bmpFont->scale;
|
|
|
|
}
|
|
|
|
|
2011-08-03 20:05:33 +00:00
|
|
|
|