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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
#include "lc_global.h"
#include "lc_stringcache.h"
#include "lc_texture.h"
#include "lc_context.h"
lcStringCache gStringCache;
lcStringCache::lcStringCache()
: mTexture(nullptr)
{
}
lcStringCache::~lcStringCache()
{
delete mTexture;
}
void lcStringCache::Initialize(lcContext* Context)
{
Q_UNUSED(Context);
mTexture = new lcTexture();
}
void lcStringCache::Reset()
{
delete mTexture;
mTexture = nullptr;
}
void lcStringCache::CacheStrings(const QStringList& Strings)
{
bool Update = false;
for (const QString& String : Strings)
{
if (mStrings.find(String) == mStrings.end())
{
mStrings[String] = lcStringCacheEntry();
Update = true;
}
}
if (!Update)
return;
Image TextureImage;
TextureImage.Allocate(256, 256, lcPixelFormat::L8A8);
QImage Image(128, 128, QImage::Format_ARGB32);
QPainter Painter;
QFont Font("Helvetica", 20);
int DestX = 0, DestY = 0, DestHeight = 0;
memset(TextureImage.mData, 0, TextureImage.mWidth * TextureImage.mHeight * 2);
for (auto& Entry : mStrings)
{
QRect SourceRect;
Painter.begin(&Image);
Painter.fillRect(0, 0, Image.width(), Image.height(), QColor(0, 0, 0));
Painter.setBrush(QColor(255, 255, 255));
Painter.setPen(QColor(255, 255, 255));
Painter.setFont(Font);
Painter.drawText(0, 0, Image.width(), Image.height(), 0, Entry.first, &SourceRect);
Painter.end();
if (DestX + SourceRect.width() + 2 > TextureImage.mWidth)
{
DestX = 0;
DestY += DestHeight + 2;
DestHeight = 0;
}
lcStringCacheEntry& String = Entry.second;
if (SourceRect.width() + 2 > TextureImage.mWidth || DestY + SourceRect.height() + 2 > TextureImage.mHeight || DestY + SourceRect.height() + 2 > TextureImage.mHeight)
{
memset(&String, 0, sizeof(String));
break;
}
String.Top = TextureImage.mHeight - DestY - 2;
String.Bottom = String.Top - SourceRect.height() + 1;
String.Left = DestX + 1;
String.Right = String.Left + SourceRect.width() - 2;
for (int y = SourceRect.top(); y < SourceRect.bottom(); y++)
{
unsigned char* Dest = TextureImage.mData + ((String.Top - y) * TextureImage.mWidth + String.Left) * 2;
for (int x = SourceRect.left(); x < SourceRect.right(); x++)
{
*Dest = *(Dest + 1) = qRed(Image.pixel(x, y));
Dest += 2;
}
}
DestX += SourceRect.width() + 2;
DestHeight = qMax(DestHeight, SourceRect.height());
}
mTexture->SetImage(std::move(TextureImage), LC_TEXTURE_LINEAR);
}
void lcStringCache::GetStringDimensions(int* cx, int* cy, const QString& String) const
{
const auto& Entry = mStrings.find(String);
if (Entry != mStrings.end())
{
const lcStringCacheEntry& FontString = Entry->second;
*cx = FontString.Right - FontString.Left;
*cy = FontString.Top - FontString.Bottom;
}
else
*cx = *cy = 0;
}
void lcStringCache::DrawStrings(lcContext* Context, const lcMatrix44* Transforms, const QStringList& Strings) const
{
float* Verts = (float*)alloca(Strings.size() * 6 * 5 * sizeof(float));
float* Buffer = Verts;
for (int StringIdx = 0; StringIdx < Strings.size(); StringIdx++)
{
const auto& Entry = mStrings.find(Strings[StringIdx]);
if (Entry == mStrings.end())
continue;
const lcStringCacheEntry& FontString = Entry->second;
float u0 = (float)FontString.Left / (float)(mTexture->mWidth - 1);
float u1 = (float)FontString.Right / (float)(mTexture->mWidth - 1);
float v0 = (float)FontString.Bottom / (float)(mTexture->mHeight - 1);
float v1 = (float)FontString.Top / (float)(mTexture->mHeight - 1);
float Width = FontString.Right - FontString.Left;
float Height = FontString.Top - FontString.Bottom;
float Left = -Width / 2.0f;
float Top = Height / 2.0f;
float Z = 0.0f;
lcVector3 Points[4] =
{
lcVector3(Left, Top, Z),
lcVector3(Left, Top - Height, Z),
lcVector3(Left + Width, Top - Height, Z),
lcVector3(Left + Width, Top, Z),
};
for (int PointIdx = 0; PointIdx < 4; PointIdx++)
Points[PointIdx] = lcMul31(Points[PointIdx], Transforms[StringIdx]);
*Buffer++ = Points[0].x;
*Buffer++ = Points[0].y;
*Buffer++ = Points[0].z;
*Buffer++ = u0;
*Buffer++ = v1;
*Buffer++ = Points[1].x;
*Buffer++ = Points[1].y;
*Buffer++ = Points[1].z;
*Buffer++ = u0;
*Buffer++ = v0;
*Buffer++ = Points[2].x;
*Buffer++ = Points[2].y;
*Buffer++ = Points[2].z;
*Buffer++ = u1;
*Buffer++ = v0;
*Buffer++ = Points[2].x;
*Buffer++ = Points[2].y;
*Buffer++ = Points[2].z;
*Buffer++ = u1;
*Buffer++ = v0;
*Buffer++ = Points[3].x;
*Buffer++ = Points[3].y;
*Buffer++ = Points[3].z;
*Buffer++ = u1;
*Buffer++ = v1;
*Buffer++ = Points[0].x;
*Buffer++ = Points[0].y;
*Buffer++ = Points[0].z;
*Buffer++ = u0;
*Buffer++ = v1;
}
Context->SetVertexBufferPointer(Verts);
Context->SetVertexFormat(0, 3, 0, 2, 0, false);
Context->BindTexture2D(mTexture);
Context->SetColor(0.0f, 0.0f, 0.0f, 1.0f);
Context->DrawPrimitives(GL_TRIANGLES, 0, Strings.size() * 6);
}
|