openspades/Sources/Client/Quake3Font.cpp
2013-08-18 16:18:06 +09:00

136 lines
2.6 KiB
C++

//
// Quake3Font.cpp
// OpenSpades
//
// Created by yvt on 7/18/13.
// Copyright (c) 2013 yvt.jp. All rights reserved.
//
#include "Quake3Font.h"
#include "IRenderer.h"
#include "../Core/Debug.h"
#include "../Core/Debug.h"
namespace spades {
namespace client {
Quake3Font::Quake3Font(IRenderer *r,
IImage *tex,
const int *mp,
int gh,
float sw):
renderer(r), tex(tex),
glyphHeight(gh), spaceWidth(sw){
SPADES_MARK_FUNCTION();
for(int i = 0; i < 128; i++){
int x = *(mp++);
int y = *(mp++);
int w = *(mp++);
GlyphInfo info;
if(w == -1){
info.type = Invalid;
glyphs.push_back(info);
continue;
}
if(w == PROP_SPACE_WIDTH){
info.type = Space;
glyphs.push_back(info);
continue;
}
info.type = Image;
info.imageRect = AABB2(x, y, w, gh);
glyphs.push_back(info);
}
}
Quake3Font::~Quake3Font(){
SPADES_MARK_FUNCTION();
}
Vector2 Quake3Font::Measure(const std::string &txt) {
SPADES_MARK_FUNCTION();
float x = 0.f, w = 0.f, h = (float)glyphHeight;
for(size_t i = 0; i < txt.size(); i++){
int ch = ((int)txt[i]) & 127;
if(ch == 13 || ch == 10){
// new line
x = 0.f;
h += (float)glyphHeight;
continue;
}
SPAssert(ch >= 0); SPAssert(ch < 128);
const GlyphInfo& info = glyphs[ch];
if(info.type == Invalid)
continue;
else if(info.type == Space){
x += spaceWidth;
}else if(info.type == Image ) {
x += info.imageRect.GetWidth();
}
if(x > w){
w = x;
}
}
return MakeVector2(w, h);
}
void Quake3Font::Draw(const std::string &txt,
spades::Vector2 offset,
float scale,
spades::Vector4 color) {
float x = 0.f, y = 0;
if(scale == 1.f){
offset.x = floorf(offset.x);
offset.y = floorf(offset.y);
}
renderer->SetColor(color);
for(size_t i = 0; i < txt.size(); i++){
int ch = ((int)txt[i]) & 127;
if(ch == 13 || ch == 10){
// new line
x = 0.f;
y += (float)glyphHeight;
continue;
}
SPAssert(ch >= 0); SPAssert(ch < 128);
const GlyphInfo& info = glyphs[ch];
if(info.type == Invalid)
continue;
else if(info.type == Space){
x += spaceWidth;
}else if(info.type == Image ) {
AABB2 rt(x * scale + offset.x, y * scale + offset.y,
info.imageRect.GetWidth() * scale,
info.imageRect.GetHeight() * scale);
renderer->DrawImage(tex, rt, info.imageRect);
x += info.imageRect.GetWidth();
}
}
}
}
}