Files
love/src/modules/graphics/opengl/Text.cpp
T
Alex Szpakowski 4a3319245b Fix Windows build
--HG--
branch : minor
2017-01-28 23:19:45 -04:00

104 lines
3.3 KiB
C++

/**
* Copyright (c) 2006-2017 LOVE Development Team
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
**/
#include "Text.h"
#include "graphics/Graphics.h"
#include "OpenGL.h"
#include <algorithm>
namespace love
{
namespace graphics
{
namespace opengl
{
Text::Text(love::graphics::Graphics *gfx, love::graphics::Font *font, const std::vector<Font::ColoredString> &text)
: love::graphics::Text(gfx, font, text)
{
}
Text::~Text()
{
}
void Text::draw(Graphics *gfx, const Matrix4 &m)
{
if (vbo == nullptr || draw_commands.empty())
return;
gfx->flushStreamDraws();
OpenGL::TempDebugGroup debuggroup("Text object draw");
// Re-generate the text if the Font's texture cache was invalidated.
if (font->getTextureCacheID() != texture_cache_id)
regenerateVertices();
int totalverts = 0;
for (const Font::DrawCommand &cmd : draw_commands)
totalverts = std::max(cmd.startvertex + cmd.vertexcount, totalverts);
if ((size_t) totalverts / 4 > quadIndices.getSize())
quadIndices = QuadIndices(gfx, (size_t) totalverts / 4);
const size_t pos_offset = offsetof(Font::GlyphVertex, x);
const size_t tex_offset = offsetof(Font::GlyphVertex, s);
const size_t color_offset = offsetof(Font::GlyphVertex, color.r);
const size_t stride = sizeof(Font::GlyphVertex);
const GLenum gltype = quadIndices.getType();
const size_t elemsize = quadIndices.getElementSize();
Graphics::TempTransform transform(gfx, m);
gl.prepareDraw();
vbo->unmap(); // Make sure all pending data is flushed to the GPU.
gl.bindBuffer(BUFFER_VERTEX, (GLuint) vbo->getHandle());
glVertexAttribPointer(ATTRIB_POS, 2, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET(pos_offset));
glVertexAttribPointer(ATTRIB_TEXCOORD, 2, GL_UNSIGNED_SHORT, GL_TRUE, stride, BUFFER_OFFSET(tex_offset));
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, BUFFER_OFFSET(color_offset));
gl.useVertexAttribArrays(ATTRIBFLAG_POS | ATTRIBFLAG_TEXCOORD | ATTRIBFLAG_COLOR);
gl.bindBuffer(BUFFER_INDEX, (GLuint) quadIndices.getBuffer()->getHandle());
// We need a separate draw call for every section of the text which uses a
// different texture than the previous section.
for (const Font::DrawCommand &cmd : draw_commands)
{
GLsizei count = (cmd.vertexcount / 4) * 6;
size_t offset = (cmd.startvertex / 4) * 6 * elemsize;
// TODO: Use glDrawElementsBaseVertex when supported?
gl.bindTextureToUnit((GLuint) cmd.texture, 0, false);
gl.drawElements(GL_TRIANGLES, count, gltype, BUFFER_OFFSET(offset));
}
}
} // opengl
} // graphics
} // love