Skip to content

Commit

Permalink
Refactor|libgui: Added the VertexBuilder template
Browse files Browse the repository at this point in the history
The VertexBuilder will contain various utilities for constructing
simple geometric shapes out of triangle strips. Strips can easily be
concatenated into a longer strip using the + and += operators.
  • Loading branch information
skyjake committed May 24, 2013
1 parent 78bc1a2 commit 1ce21e8
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 19 deletions.
2 changes: 1 addition & 1 deletion doomsday/client/include/ui/widgets/gltextcomposer.h
Expand Up @@ -35,7 +35,7 @@ class GLTextComposer
public:
typedef de::Vertex2TexRgba Vertex;
typedef de::GLBufferT<Vertex> VertexBuf;
typedef VertexBuf::Vertices Vertices;
typedef VertexBuf::Builder Vertices;

/**
* Flags for specifying alignment.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/client/src/ui/widgets/gltextcomposer.cpp
Expand Up @@ -212,7 +212,7 @@ void GLTextComposer::makeVertices(Vertices &triStrip,
Rectanglef const uv = d->atlas->imageRectf(d->lines[i].id);

Vertex v;
Vertices quad;
VertexBuf::Builder quad;

v.rgba = Vector4f(1, 1, 1, 1); // should be a param

Expand All @@ -233,7 +233,7 @@ void GLTextComposer::makeVertices(Vertices &triStrip,
v.pos = linePos + Vector2f(0, size.y); v.texCoord = uv.bottomLeft(); quad << v;
v.pos = linePos + Vector2f(size.x, size.y); v.texCoord = uv.bottomRight; quad << v;

VertexBuf::concatenate(quad, triStrip);
triStrip += quad;
}
p.y += d->font->lineSpacing().value();
}
Expand Down
6 changes: 3 additions & 3 deletions doomsday/client/src/ui/widgets/lineeditwidget.cpp
Expand Up @@ -159,7 +159,7 @@ DENG2_OBSERVES(Atlas, Reposition)

Vector4f bgColor = self.style().colors().colorf("background");

VertexBuf::Vertices verts;
VertexBuf::Builder verts;
VertexBuf::Type v;

v.rgba = bgColor;
Expand Down Expand Up @@ -192,7 +192,7 @@ DENG2_OBSERVES(Atlas, Reposition)
Vector2i start = wraps.charTopLeftInPixels(i, i == startPos.y? startPos.x : span.start) + offset;
Vector2i end = wraps.charTopLeftInPixels(i, i == endPos.y? endPos.x : span.end) + offset;

VertexBuf::Vertices quad;
VertexBuf::Builder quad;
v.rgba = Vector4f(1, 1, 1, 1);
v.texCoord = atlas().imageRectf(bgTex).middle();

Expand All @@ -201,7 +201,7 @@ DENG2_OBSERVES(Atlas, Reposition)
v.pos = start + Vector2i(0, 1); quad << v;
v.pos = end + Vector2i(0, 1); quad << v;

VertexBuf::concatenate(quad, verts);
verts += quad;
}
}

Expand Down
7 changes: 4 additions & 3 deletions doomsday/client/src/ui/widgets/logwidget.cpp
Expand Up @@ -29,6 +29,7 @@
#include <de/LogBuffer>
#include <de/AtlasTexture>
#include <de/Drawable>
#include <de/VertexBuilder>

#include <QImage>
#include <QPainter>
Expand Down Expand Up @@ -418,7 +419,7 @@ DENG2_PIMPL(LogWidget), public Font::RichFormat::IStyle
firstVisibleIndex = -1;
lastVisibleIndex = -1;

VertexBuf::Vertices verts;
VertexBuf::Builder verts;

// Copy all visible entries to the buffer.
for(int idx = sink.entryCount() - 1; yBottom > 0 && idx >= 0; --idx)
Expand Down Expand Up @@ -448,7 +449,7 @@ DENG2_PIMPL(LogWidget), public Font::RichFormat::IStyle
float const avail = contentSize.y - indHeight;
for(int i = 0; i < indHeight; ++i)
{
VertexBuf::Vertices quad;
VertexBuf::Builder quad;
VertexBuf::Type v;
v.rgba = Vector4f(1, 1, 1, scrollOpacity) * accentColor / 255.f;
v.texCoord = entryAtlas->imageRectf(scrollTex).middle();
Expand All @@ -461,7 +462,7 @@ DENG2_PIMPL(LogWidget), public Font::RichFormat::IStyle
v.pos = indRect.bottomLeft(); quad << v;
v.pos = indRect.bottomRight; quad << v;

VertexBuf::concatenate(quad, verts);
verts += quad;
}
}

Expand Down
1 change: 1 addition & 0 deletions doomsday/libgui/include/de/VertexBuilder
@@ -0,0 +1 @@
#include "gui/vertexbuilder.h"
11 changes: 2 additions & 9 deletions doomsday/libgui/include/de/gui/glbuffer.h
Expand Up @@ -28,6 +28,7 @@

#include "libgui.h"
#include "opengl.h"
#include "../VertexBuilder"

namespace de {

Expand Down Expand Up @@ -181,6 +182,7 @@ class GLBufferT : public GLBuffer
public:
typedef VertexType Type;
typedef QVector<VertexType> Vertices;
typedef typename VertexBuilder<VertexType>::Vertices Builder;

public:
GLBufferT() {
Expand All @@ -202,15 +204,6 @@ class GLBufferT : public GLBuffer
void setVertices(gl::Primitive primitive, Vertices const &vertices, gl::Usage usage) {
GLBuffer::setVertices(primitive, vertices.size(), vertices.constData(), sizeof(VertexType) * vertices.size(), usage);
}

static void concatenate(Vertices const &triStripSequence, Vertices &longTriStrip) {
if(!triStripSequence.size()) return;
if(!longTriStrip.isEmpty()) {
longTriStrip << longTriStrip.back();
longTriStrip << triStripSequence.front();
}
longTriStrip << triStripSequence;
}
};

} // namespace de
Expand Down
57 changes: 57 additions & 0 deletions doomsday/libgui/include/de/gui/vertexbuilder.h
@@ -0,0 +1,57 @@
/** @file vertexbuilder.h Utility for composing triangle strips.
*
* @authors Copyright (c) 2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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, see:
* http://www.gnu.org/licenses</small>
*/

#ifndef LIBGUI_VERTEXBUILDER_H
#define LIBGUI_VERTEXBUILDER_H

#include <QVector>

namespace de {

/**
* Utility for composing simple geometric constructs (using triangle strips).
*/
template <typename VertexType>
struct VertexBuilder
{
struct Vertices : public QVector<VertexType> {
Vertices &operator += (Vertices const &other) {
concatenate(other, *this);
return *this;
}
Vertices operator + (Vertices const &other) const {
Vertices v(*this);
return v += other;
}
};

static void concatenate(Vertices const &stripSequence, Vertices &destStrip)
{
if(!stripSequence.size()) return;
if(!destStrip.isEmpty())
{
destStrip << destStrip.back();
destStrip << stripSequence.front();
}
destStrip << stripSequence;
}
};

} // namespace de

#endif // LIBGUI_VERTEXBUILDER_H
4 changes: 3 additions & 1 deletion doomsday/libgui/libgui.pro
Expand Up @@ -71,6 +71,7 @@ HEADERS += \
include/de/MouseEventSource \
include/de/PersistentCanvasWindow \
include/de/RowAtlasAllocator \
include/de/VertexBuilder \
\
include/de/gui/atlas.h \
include/de/gui/atlastexture.h \
Expand Down Expand Up @@ -101,7 +102,8 @@ HEADERS += \
include/de/gui/mouseeventsource.h \
include/de/gui/opengl.h \
include/de/gui/persistentcanvaswindow.h \
include/de/gui/rowatlasallocator.h
include/de/gui/rowatlasallocator.h \
include/de/gui/vertexbuilder.h

# Sources and private headers.
SOURCES += \
Expand Down

0 comments on commit 1ce21e8

Please sign in to comment.