mirror of
https://github.com/love2d/love.git
synced 2026-08-16 00:02:12 +02:00
Simplified some code by removing an unnecessary/useless optimization.
This commit is contained in:
@@ -398,7 +398,7 @@ float Font::getHeight() const
|
||||
return (float) height;
|
||||
}
|
||||
|
||||
Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, std::vector<GlyphVertex> &vertices, float extra_spacing, Vector offset, TextInfo *info)
|
||||
std::vector<Font::DrawCommand> Font::generateVertices(const ColoredCodepoints &codepoints, std::vector<GlyphVertex> &vertices, float extra_spacing, Vector offset, TextInfo *info)
|
||||
{
|
||||
// Spacing counter and newline handling.
|
||||
float dx = offset.x;
|
||||
@@ -408,7 +408,7 @@ Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, s
|
||||
int maxwidth = 0;
|
||||
|
||||
// Keeps track of when we need to switch textures in our vertex array.
|
||||
DrawCommands drawcmds;
|
||||
std::vector<DrawCommand> commands;
|
||||
|
||||
// Pre-allocate space for the maximum possible number of vertices.
|
||||
size_t vertstartsize = vertices.size();
|
||||
@@ -420,11 +420,6 @@ Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, s
|
||||
int curcolori = -1;
|
||||
int ncolors = (int) codepoints.colors.size();
|
||||
|
||||
if (ncolors == 0 || (ncolors == 1 && codepoints.colors[0].color == curcolor))
|
||||
drawcmds.usecolors = false;
|
||||
else
|
||||
drawcmds.usecolors = true;
|
||||
|
||||
for (int i = 0; i < (int) codepoints.cps.size(); i++)
|
||||
{
|
||||
uint32 g = codepoints.cps[i];
|
||||
@@ -454,7 +449,7 @@ Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, s
|
||||
maxwidth = 0;
|
||||
dx = offset.x;
|
||||
dy = offset.y;
|
||||
drawcmds.commands.clear();
|
||||
commands.clear();
|
||||
vertices.resize(vertstartsize);
|
||||
prevglyph = 0;
|
||||
curcolori = -1;
|
||||
@@ -477,17 +472,17 @@ Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, s
|
||||
}
|
||||
|
||||
// Check if glyph texture has changed since the last iteration.
|
||||
if (drawcmds.commands.empty() || drawcmds.commands.back().texture != glyph.texture)
|
||||
if (commands.empty() || commands.back().texture != glyph.texture)
|
||||
{
|
||||
// Add a new draw command if the texture has changed.
|
||||
DrawCommand cmd;
|
||||
cmd.startvertex = (int) vertices.size() - 4;
|
||||
cmd.vertexcount = 0;
|
||||
cmd.texture = glyph.texture;
|
||||
drawcmds.commands.push_back(cmd);
|
||||
commands.push_back(cmd);
|
||||
}
|
||||
|
||||
drawcmds.commands.back().vertexcount += 4;
|
||||
commands.back().vertexcount += 4;
|
||||
}
|
||||
|
||||
// Advance the x position for the next glyph.
|
||||
@@ -510,7 +505,7 @@ Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, s
|
||||
return a.startvertex < b.startvertex;
|
||||
};
|
||||
|
||||
std::sort(drawcmds.commands.begin(), drawcmds.commands.end(), drawsort);
|
||||
std::sort(commands.begin(), commands.end(), drawsort);
|
||||
|
||||
if (dx > maxwidth)
|
||||
maxwidth = (int) dx;
|
||||
@@ -521,24 +516,24 @@ Font::DrawCommands Font::generateVertices(const ColoredCodepoints &codepoints, s
|
||||
info->height = (int) dy + (dx > 0.0f ? floorf(getHeight() * getLineHeight() + 0.5f) : 0) - offset.y;
|
||||
}
|
||||
|
||||
return drawcmds;
|
||||
return commands;
|
||||
}
|
||||
|
||||
Font::DrawCommands Font::generateVertices(const std::string &text, std::vector<GlyphVertex> &vertices, float extra_spacing, Vector offset, TextInfo *info)
|
||||
std::vector<Font::DrawCommand> Font::generateVertices(const std::string &text, std::vector<GlyphVertex> &vertices, float extra_spacing, Vector offset, TextInfo *info)
|
||||
{
|
||||
ColoredCodepoints codepoints;
|
||||
getCodepointsFromString(text, codepoints.cps);
|
||||
return generateVertices(codepoints, vertices, extra_spacing, offset, info);
|
||||
}
|
||||
|
||||
Font::DrawCommands Font::generateVerticesFormatted(const ColoredCodepoints &text, float wrap, AlignMode align, std::vector<GlyphVertex> &vertices, TextInfo *info)
|
||||
std::vector<Font::DrawCommand> Font::generateVerticesFormatted(const ColoredCodepoints &text, float wrap, AlignMode align, std::vector<GlyphVertex> &vertices, TextInfo *info)
|
||||
{
|
||||
|
||||
wrap = std::max(wrap, 0.0f);
|
||||
|
||||
uint32 cacheid = textureCacheID;
|
||||
|
||||
DrawCommands drawcmds;
|
||||
std::vector<DrawCommand> drawcommands;
|
||||
vertices.reserve(text.cps.size() * 4);
|
||||
|
||||
std::vector<int> widths;
|
||||
@@ -581,29 +576,27 @@ Font::DrawCommands Font::generateVerticesFormatted(const ColoredCodepoints &text
|
||||
break;
|
||||
}
|
||||
|
||||
DrawCommands cmds = generateVertices(line, vertices, extraspacing, offset);
|
||||
std::vector<DrawCommand> newcommands = generateVertices(line, vertices, extraspacing, offset);
|
||||
|
||||
if (!cmds.commands.empty())
|
||||
if (!newcommands.empty())
|
||||
{
|
||||
auto firstcmd = cmds.commands.begin();
|
||||
auto firstcmd = newcommands.begin();
|
||||
|
||||
// If the first draw command in the new list has the same texture
|
||||
// as the last one in the existing list we're building and its
|
||||
// vertices are in-order, we can combine them (saving a draw call.)
|
||||
if (!drawcmds.commands.empty())
|
||||
if (!drawcommands.empty())
|
||||
{
|
||||
auto prevcmd = drawcmds.commands.back();
|
||||
auto prevcmd = drawcommands.back();
|
||||
if (prevcmd.texture == firstcmd->texture && (prevcmd.startvertex + prevcmd.vertexcount) == firstcmd->startvertex)
|
||||
{
|
||||
drawcmds.commands.back().vertexcount += firstcmd->vertexcount;
|
||||
drawcommands.back().vertexcount += firstcmd->vertexcount;
|
||||
++firstcmd;
|
||||
}
|
||||
}
|
||||
|
||||
// Append the new draw commands to the list we're building.
|
||||
drawcmds.commands.insert(drawcmds.commands.end(), firstcmd, cmds.commands.end());
|
||||
|
||||
drawcmds.usecolors = drawcmds.usecolors || cmds.usecolors;
|
||||
drawcommands.insert(drawcommands.end(), firstcmd, newcommands.end());
|
||||
}
|
||||
|
||||
y += getHeight() * getLineHeight();
|
||||
@@ -618,19 +611,19 @@ Font::DrawCommands Font::generateVerticesFormatted(const ColoredCodepoints &text
|
||||
if (cacheid != textureCacheID)
|
||||
{
|
||||
vertices.clear();
|
||||
drawcmds = generateVerticesFormatted(text, wrap, align, vertices);
|
||||
drawcommands = generateVerticesFormatted(text, wrap, align, vertices);
|
||||
}
|
||||
|
||||
return drawcmds;
|
||||
return drawcommands;
|
||||
}
|
||||
|
||||
void Font::drawVertices(const DrawCommands &drawcommands)
|
||||
void Font::drawVertices(const std::vector<DrawCommand> &drawcommands)
|
||||
{
|
||||
// Vertex attribute pointers need to be set before calling this function.
|
||||
// This assumes that the attribute pointers are constant for all vertices.
|
||||
|
||||
int totalverts = 0;
|
||||
for (const DrawCommand &cmd : drawcommands.commands)
|
||||
for (const DrawCommand &cmd : drawcommands)
|
||||
totalverts = std::max(cmd.startvertex + cmd.vertexcount, totalverts);
|
||||
|
||||
if ((size_t) totalverts / 4 > quadIndices.getSize())
|
||||
@@ -645,7 +638,7 @@ void Font::drawVertices(const DrawCommands &drawcommands)
|
||||
|
||||
// We need a separate draw call for every section of the text which uses a
|
||||
// different texture than the previous section.
|
||||
for (const DrawCommand &cmd : drawcommands.commands)
|
||||
for (const DrawCommand &cmd : drawcommands)
|
||||
{
|
||||
GLsizei count = (cmd.vertexcount / 4) * 6;
|
||||
size_t offset = (cmd.startvertex / 4) * 6 * elemsize;
|
||||
@@ -656,9 +649,9 @@ void Font::drawVertices(const DrawCommands &drawcommands)
|
||||
}
|
||||
}
|
||||
|
||||
void Font::printv(const Matrix4 &t, const DrawCommands &drawcommands, const std::vector<GlyphVertex> &vertices)
|
||||
void Font::printv(const Matrix4 &t, const std::vector<DrawCommand> &drawcommands, const std::vector<GlyphVertex> &vertices)
|
||||
{
|
||||
if (vertices.empty() || drawcommands.commands.empty())
|
||||
if (vertices.empty() || drawcommands.empty())
|
||||
return;
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Font print");
|
||||
@@ -668,16 +661,9 @@ void Font::printv(const Matrix4 &t, const DrawCommands &drawcommands, const std:
|
||||
|
||||
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, sizeof(GlyphVertex), &vertices[0].x);
|
||||
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_UNSIGNED_SHORT, GL_TRUE, sizeof(GlyphVertex), &vertices[0].s);
|
||||
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(GlyphVertex), &vertices[0].color.r);
|
||||
|
||||
uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
|
||||
|
||||
if (drawcommands.usecolors)
|
||||
{
|
||||
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(GlyphVertex), &vertices[0].color.r);
|
||||
enabledattribs |= ATTRIBFLAG_COLOR;
|
||||
}
|
||||
|
||||
gl.useVertexAttribArrays(enabledattribs);
|
||||
gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD | ATTRIBFLAG_COLOR);
|
||||
|
||||
drawVertices(drawcommands);
|
||||
}
|
||||
@@ -688,7 +674,7 @@ void Font::print(const std::vector<ColoredString> &text, float x, float y, float
|
||||
getCodepointsFromString(text, codepoints);
|
||||
|
||||
std::vector<GlyphVertex> vertices;
|
||||
DrawCommands drawcommands = generateVertices(codepoints, vertices);
|
||||
std::vector<DrawCommand> drawcommands = generateVertices(codepoints, vertices);
|
||||
|
||||
Matrix4 t(x, y, angle, sx, sy, ox, oy, kx, ky);
|
||||
|
||||
@@ -701,7 +687,7 @@ void Font::printf(const std::vector<ColoredString> &text, float x, float y, floa
|
||||
getCodepointsFromString(text, codepoints);
|
||||
|
||||
std::vector<GlyphVertex> vertices;
|
||||
DrawCommands drawcommands = generateVerticesFormatted(codepoints, wrap, align, vertices);
|
||||
std::vector<DrawCommand> drawcommands = generateVerticesFormatted(codepoints, wrap, align, vertices);
|
||||
|
||||
Matrix4 t(x, y, angle, sx, sy, ox, oy, kx, ky);
|
||||
|
||||
|
||||
@@ -100,22 +100,16 @@ public:
|
||||
int vertexcount;
|
||||
};
|
||||
|
||||
struct DrawCommands
|
||||
{
|
||||
std::vector<DrawCommand> commands;
|
||||
bool usecolors;
|
||||
};
|
||||
|
||||
Font(love::font::Rasterizer *r, const Texture::Filter &filter = Texture::getDefaultFilter());
|
||||
|
||||
virtual ~Font();
|
||||
|
||||
DrawCommands generateVertices(const ColoredCodepoints &codepoints, std::vector<GlyphVertex> &vertices, float extra_spacing = 0.0f, Vector offset = {}, TextInfo *info = nullptr);
|
||||
DrawCommands generateVertices(const std::string &text, std::vector<GlyphVertex> &vertices, float extra_spacing = 0.0f, Vector offset = Vector(), TextInfo *info = nullptr);
|
||||
std::vector<DrawCommand> generateVertices(const ColoredCodepoints &codepoints, std::vector<GlyphVertex> &vertices, float extra_spacing = 0.0f, Vector offset = {}, TextInfo *info = nullptr);
|
||||
std::vector<DrawCommand> generateVertices(const std::string &text, std::vector<GlyphVertex> &vertices, float extra_spacing = 0.0f, Vector offset = Vector(), TextInfo *info = nullptr);
|
||||
|
||||
DrawCommands generateVerticesFormatted(const ColoredCodepoints &text, float wrap, AlignMode align, std::vector<GlyphVertex> &vertices, TextInfo *info = nullptr);
|
||||
std::vector<DrawCommand> generateVerticesFormatted(const ColoredCodepoints &text, float wrap, AlignMode align, std::vector<GlyphVertex> &vertices, TextInfo *info = nullptr);
|
||||
|
||||
void drawVertices(const DrawCommands &drawcommands);
|
||||
void drawVertices(const std::vector<DrawCommand> &drawcommands);
|
||||
|
||||
static void getCodepointsFromString(const std::string &str, Codepoints &codepoints);
|
||||
static void getCodepointsFromString(const std::vector<ColoredString> &strs, ColoredCodepoints &codepoints);
|
||||
@@ -233,7 +227,7 @@ private:
|
||||
const Glyph &addGlyph(uint32 glyph);
|
||||
const Glyph &findGlyph(uint32 glyph);
|
||||
float getKerning(uint32 leftglyph, uint32 rightglyph);
|
||||
void printv(const Matrix4 &t, const DrawCommands &drawcommands, const std::vector<GlyphVertex> &vertices);
|
||||
void printv(const Matrix4 &t, const std::vector<DrawCommand> &drawcommands, const std::vector<GlyphVertex> &vertices);
|
||||
|
||||
std::vector<StrongRef<love::font::Rasterizer>> rasterizers;
|
||||
|
||||
|
||||
@@ -111,7 +111,7 @@ void Text::regenerateVertices()
|
||||
void Text::addTextData(const TextData &t)
|
||||
{
|
||||
std::vector<Font::GlyphVertex> vertices;
|
||||
Font::DrawCommands new_commands;
|
||||
std::vector<Font::DrawCommand> new_commands;
|
||||
|
||||
// We only have formatted text if the align mode is valid.
|
||||
if (t.align == Font::ALIGN_MAX_ENUM)
|
||||
@@ -127,37 +127,34 @@ void Text::addTextData(const TextData &t)
|
||||
if (!t.append_vertices)
|
||||
{
|
||||
voffset = 0;
|
||||
draw_commands.commands.clear();
|
||||
draw_commands.usecolors = false;
|
||||
draw_commands.clear();
|
||||
}
|
||||
|
||||
uploadVertices(vertices, voffset);
|
||||
|
||||
if (!new_commands.commands.empty())
|
||||
if (!new_commands.empty())
|
||||
{
|
||||
// The start vertex should be adjusted to account for the vertex offset.
|
||||
for (Font::DrawCommand &cmd : new_commands.commands)
|
||||
for (Font::DrawCommand &cmd : new_commands)
|
||||
cmd.startvertex += (int) voffset;
|
||||
|
||||
auto firstcmd = new_commands.commands.begin();
|
||||
auto firstcmd = new_commands.begin();
|
||||
|
||||
// If the first draw command in the new list has the same texture as the
|
||||
// last one in the existing list we're building and its vertices are
|
||||
// in-order, we can combine them (saving a draw call.)
|
||||
if (!draw_commands.commands.empty())
|
||||
if (!draw_commands.empty())
|
||||
{
|
||||
auto prevcmd = draw_commands.commands.back();
|
||||
auto prevcmd = draw_commands.back();
|
||||
if (prevcmd.texture == firstcmd->texture && (prevcmd.startvertex + prevcmd.vertexcount) == firstcmd->startvertex)
|
||||
{
|
||||
draw_commands.commands.back().vertexcount += firstcmd->vertexcount;
|
||||
draw_commands.back().vertexcount += firstcmd->vertexcount;
|
||||
++firstcmd;
|
||||
}
|
||||
}
|
||||
|
||||
// Append the new draw commands to the list we're building.
|
||||
draw_commands.commands.insert(draw_commands.commands.end(), firstcmd, new_commands.commands.end());
|
||||
|
||||
draw_commands.usecolors = draw_commands.usecolors || new_commands.usecolors;
|
||||
draw_commands.insert(draw_commands.end(), firstcmd, new_commands.end());
|
||||
}
|
||||
|
||||
vert_offset = voffset + vertices.size();
|
||||
@@ -210,8 +207,7 @@ void Text::addf(const std::vector<Font::ColoredString> &text, float wrap, Font::
|
||||
void Text::clear()
|
||||
{
|
||||
text_data.clear();
|
||||
draw_commands.commands.clear();
|
||||
draw_commands.usecolors = false;
|
||||
draw_commands.clear();
|
||||
texture_cache_id = font->getTextureCacheID();
|
||||
text_info = {};
|
||||
vert_offset = 0;
|
||||
@@ -219,7 +215,7 @@ void Text::clear()
|
||||
|
||||
void Text::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
|
||||
{
|
||||
if (vbo == nullptr || draw_commands.commands.empty())
|
||||
if (vbo == nullptr || draw_commands.empty())
|
||||
return;
|
||||
|
||||
OpenGL::TempDebugGroup debuggroup("Text object draw");
|
||||
@@ -243,17 +239,10 @@ void Text::draw(float x, float y, float angle, float sx, float sy, float ox, flo
|
||||
// Font::drawVertices expects AttribPointer calls to be done already.
|
||||
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, stride, vbo->getPointer(pos_offset));
|
||||
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_UNSIGNED_SHORT, GL_TRUE, stride, vbo->getPointer(tex_offset));
|
||||
|
||||
if (draw_commands.usecolors)
|
||||
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, vbo->getPointer(color_offset));
|
||||
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, vbo->getPointer(color_offset));
|
||||
}
|
||||
|
||||
uint32 enabledattribs = ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD;
|
||||
|
||||
if (draw_commands.usecolors)
|
||||
enabledattribs |= ATTRIBFLAG_COLOR;
|
||||
|
||||
gl.useVertexAttribArrays(enabledattribs);
|
||||
gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD | ATTRIBFLAG_COLOR);
|
||||
|
||||
font->drawVertices(draw_commands);
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ private:
|
||||
StrongRef<Font> font;
|
||||
GLBuffer *vbo;
|
||||
|
||||
Font::DrawCommands draw_commands;
|
||||
std::vector<Font::DrawCommand> draw_commands;
|
||||
|
||||
std::vector<TextData> text_data;
|
||||
Font::TextInfo text_info;
|
||||
|
||||
Reference in New Issue
Block a user