1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-25 14:15:46 +00:00
Aquaria/BBGE/BitmapFont.cpp

458 lines
8.4 KiB
C++
Raw Normal View History

/*
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"
#include "Core.h"
//#include "DSQ.h"
using namespace glfont;
BmpFont::BmpFont()
{
scale = 1;
loaded = false;
overrideTexture = 0;
fontTopColor = Vector(1,1,1);
fontBtmColor = Vector(1,1,1);
}
BmpFont::~BmpFont()
{
destroy();
}
void BmpFont::destroy()
{
if (loaded)
{
font.Destroy();
loaded = false;
}
if (overrideTexture)
{
overrideTexture->removeRef();
overrideTexture = 0;
}
}
void BmpFont::load(const std::string &file, float scale, bool loadTexture)
{
if (loaded)
font.Destroy();
this->scale = scale;
#ifdef BBGE_BUILD_OPENGL
GLuint id=0;
glGenTextures(1, &id);
if (!font.Create(file.c_str(), id, loadTexture))
return;
#endif
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)
{
this->bmpFont = bmpFont;
bfePass =0;
bfe = BFE_NONE;
currentScrollLine = currentScrollChar = 0;
scrollDelay = 0;
scrolling = false;
align = ALIGN_CENTER;
textWidth = 600;
this->fontDrawSize = 24;
//color = Vector(0.5,0.5,1);
cull = false;
//setTexture(font);
alignWidth = 0;
//fontTextureTest = core->addTexture("font");
}
void BitmapText::autoKern()
{
}
void BitmapText::loadSpacingMap(const std::string &file)
{
spacingMap.clear();
[vfs, #3] All file reading code goes through the VFS now, new mod downloader & mod selector in place. Also a bunch of other stuff. (...) - HTTP networking support, mods can be downloaded via the builtin downloader. All network activity runs in a seperate thread, which is started as soon as any network activity is requested. - The master server is hard-coded to fg.wzff.de/aqmods/ if not specified otherwise; this setting can be overridden in the config file. - The mod selector screen is now a grid-view for much better navigation; also works with joystick. - VFS code is functionally similar to the old molebox-packed release for win32. The game could also have its data shipped in a Zip file or any other kind of archive. - It is still possible to build without VFS support, but then the mod downloader and soft-patching will not be available. The full commit history can be found here: https://github.com/fgenesis/Aquaria_clean/compare/master...vfs The most important commit messages follow: [...] This replaces all std::ifstream with InStream, and fopen(), ... with vfopen(), ... Some code is #ifdef'd for better performance and less memory-copying. VFILE is defined to whatever type of file is in use: - FILE if BBGE_BUILD_VFS is not defined - tttvfs::VFSFile if it is. Other changes: - [un]packFile() is now unused and obsolete. That code has not been adjusted to use VFILE. - glpng can now load from a memory buffer. - TinyXML uses the VFS for reading operations now. - The rather clunky binary stream loading of glfont2 got replaced with ByteBuffer, which gets its data in one block (necessary to use the VFS without implementing a somewhat STL-compliant std::ifstream replacement.) ------------- Implement loading mods from zip files. ------------- Implement soft-patching game data files. (Replacing textures/audio/... on the fly) ------------- Misc bits: - Extended GUI focus handling a bit - Fixed weirdness in texture loading... not sure but this seems more correct to me. Actually, considering that the texture will have its native size after restarting the game, the lines removed with this commit seem pretty useless.
2012-06-01 15:52:19 +00:00
InStream inFile(file.c_str());
std::string line;
while (std::getline(inFile, line))
{
if (!line.empty())
{
char c = line[0];
line = line.substr(2, line.length());
std::istringstream is(line);
is >> spacingMap[c];
}
}
}
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();
}
void BitmapText::setWidth(int width)
{
textWidth = width;
}
int BitmapText::getSetWidth()
{
return textWidth;
}
float BitmapText::getHeight()
{
float sz = bmpFont->font.GetCharHeight('A') * bmpFont->scale;
return lines.size()*sz;
}
void BitmapText::formatText()
{
std::string text;
text = this->text;
lines.clear();
std::string currentLine;
int lastSpace = -1;
int currentWidth = 0;
alignWidth = 0;
for (int i = 0; i < text.size(); i++)
{
//currentWidth += spacingMap[text[i]]*fontDrawSize;
float sz = bmpFont->font.GetCharWidth(text[i])*bmpFont->scale;
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;
currentWidth = 0;
lastSpace = 0;
continue;
}
if (text[i] == ' ')
{
lastSpace = i;
}
}
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);
}
}
/*
for (int i = 0; i < lines.size(); i++)
{
int c = 0;
for (int t = 0; t < dsq->continuity.wordColoring.size(); t++)
{
WordColoring *w = &dsq->continuity.wordColoring[t];
if ((c = lines[i].find(w->word)) != std::string::npos)
{
for (int j = c; j < c + w->word.size(); j++)
{
colorIndices[i][j] = w->color;
}
}
}
}
*/
//lines.push_back(text);
}
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;
}
void BitmapText::setFontSize(int sz)
{
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};
#ifdef BBGE_BUILD_OPENGL
glEnable(GL_TEXTURE_2D);
/*
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
*/
glDisable(GL_CULL_FACE);
//glScalef(1, -1, 0);
bmpFont->font.Begin();
if (fontTextureTest) fontTextureTest->apply();
if (bmpFont->overrideTexture) bmpFont->overrideTexture->apply();
float y=0;
float x=0;
float adj = bmpFont->font.GetCharHeight('A') * bmpFont->scale;
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;
bmpFont->font.GetStringSize(lines[i], &sz);
x = -sz.first*0.5f*bmpFont->scale;
}
float la = 1.0f-(scrollDelay/scrollSpeed);
/*
std::ostringstream os;
os << "la: " << la;
debugLog(os.str());
*/
bmpFont->font.DrawString(theLine, bmpFont->scale, x, y, top_color, bottom_color, alpha.x, la);
y += adj;
}
}
else
{
for (int i = 0; i < lines.size(); i++)
{
x=0;
if (align == ALIGN_CENTER)
{
std::pair<int, int> sz;
bmpFont->font.GetStringSize(lines[i], &sz);
x = -sz.first*0.5f*bmpFont->scale;
}
bmpFont->font.DrawString(lines[i], bmpFont->scale, x, y, top_color, bottom_color, alpha.x, 1);
y += adj;
}
}
glEnable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, 0);
#endif
}
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();
}
/*
BitmapText::BitmapText() : RenderObject()
{
cull = false;
followCamera = 1;
scrollSpeed = 0.1f;
}
void BitmapText::scrollText(const std::string &text, float scrollSpeed)
{
setText(text);
this->scrollSpeed = scrollSpeed;
}
void BitmapText::setText(const std::string &text)
{
this->text = text;
}
std::string BitmapText::getText()
{
return text;
}
void BitmapText::onRender()
{
CTextDrawer::GetSingleton().SetColor(color.x, color.y, color.z, alpha.getValue());
dsq->print(position.x, 600 - (position.y + 16*2), text);
}
*/