Maybe make some GL code easier to understand
authorDavid Gow <[email protected]>
Sat, 31 May 2014 13:15:20 +0000 (21:15 +0800)
committerDavid Gow <[email protected]>
Sat, 31 May 2014 13:15:20 +0000 (21:15 +0800)
bin/ipdf
src/bufferbuilder.h [new file with mode: 0644]
src/screen.cpp

index 3941958..4143438 100755 (executable)
Binary files a/bin/ipdf and b/bin/ipdf differ
diff --git a/src/bufferbuilder.h b/src/bufferbuilder.h
new file mode 100644 (file)
index 0000000..385369c
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef _BUFFERBUILDER_H
+#define _BUFFERBUILDER_H
+
+namespace IPDF
+{
+       /*
+        * Utility to add stuff to a buffer. 
+        */
+       template <typename T>
+       class BufferBuilder
+       {
+       public:
+               BufferBuilder(void *data, size_t size) : m_bufferData((T*)data), m_bufferSize(size), m_bufferOffset(0) {};
+               // Append an item to the buffer, returning its index.
+               size_t Add(const T& item) {m_bufferData[m_bufferOffset] = item; m_bufferOffset++; return m_bufferOffset-1;}
+               bool Free(size_t num = 1) const {return ((m_bufferOffset + num) * sizeof(T)) < m_bufferSize;}
+       private:
+               T *m_bufferData;
+               size_t m_bufferSize; // In bytes, 'cause why make things easy?
+               size_t m_bufferOffset; // In elements, 'cause why make things consistant?
+       };
+
+}
+
+#endif // _SCREEN_H
index 7817482..2d63bc9 100644 (file)
@@ -5,6 +5,7 @@
 #include <fcntl.h> // for access(2)
 #include <unistd.h> // for access(2)
 
+#include "bufferbuilder.h"
 #include "shaderprogram.h"
 
 #define BASICTEX_VERT \
@@ -415,8 +416,8 @@ void Screen::DebugFontFlush()
        glEnableVertexAttribArray(1);
        glEnable(GL_PRIMITIVE_RESTART);
        glPrimitiveRestartIndex(65535);
-       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(2*sizeof(float)));
-       glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
+       glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
+       glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(2*sizeof(float)));
        glDrawElements(GL_TRIANGLE_STRIP, m_debug_font_index_head, GL_UNSIGNED_SHORT, 0);
        glDisable(GL_PRIMITIVE_RESTART);
        glDisableVertexAttribArray(1);
@@ -432,11 +433,17 @@ void Screen::DebugFontPrint(const char* str)
 {
        if (!m_debug_font_atlas) return;
 
-       float *vertexData = (float*)m_debug_font_vertices.MapRange(m_debug_font_vertex_head*sizeof(float), m_debug_font_vertices.GetSize() - m_debug_font_vertex_head*sizeof(float), false, true, true);
-       uint16_t *indexData = (uint16_t*)m_debug_font_indices.MapRange(m_debug_font_index_head*sizeof(uint16_t), m_debug_font_indices.GetSize() - m_debug_font_index_head*sizeof(uint16_t), false, true, true);
+       struct fontvertex
+       {
+               float x, y, s, t;
+       };
+
+       BufferBuilder<fontvertex> vertexData(m_debug_font_vertices.MapRange(m_debug_font_vertex_head*sizeof(float), m_debug_font_vertices.GetSize() - m_debug_font_vertex_head*sizeof(float), false, true, true), m_debug_font_vertices.GetSize() - m_debug_font_vertex_head*sizeof(float));
+       BufferBuilder<uint16_t> indexData(m_debug_font_indices.MapRange(m_debug_font_index_head*sizeof(uint16_t), m_debug_font_indices.GetSize() - m_debug_font_index_head*sizeof(uint16_t), false, true, true), m_debug_font_indices.GetSize() - m_debug_font_index_head*sizeof(uint16_t));
+
+       size_t baseVertex = m_debug_font_vertex_head/4;
        while (*str) {
-               if ((m_debug_font_vertex_head*sizeof(float) + 16*sizeof(float) >= m_debug_font_vertices.GetSize() ) ||
-                       (m_debug_font_index_head*sizeof(uint16_t) + 5*sizeof(uint16_t) >= m_debug_font_indices.GetSize()))
+               if (!vertexData.Free(4) || !indexData.Free(5))
                {
                        m_debug_font_indices.UnMap();
                        m_debug_font_vertices.UnMap();
@@ -447,28 +454,19 @@ void Screen::DebugFontPrint(const char* str)
                if (*str >= 32 && *str < 128) {
                        stbtt_aligned_quad q;
                        stbtt_GetBakedQuad(m_debug_font_rects, 1024,1024, *str-32, &m_debug_font_x,&m_debug_font_y,&q,1);
-                       *vertexData = q.s0; vertexData++;
-                       *vertexData = q.t0; vertexData++;
-                       *vertexData = q.x0; vertexData++;
-                       *vertexData = q.y0; vertexData++;
-                       *vertexData = q.s1; vertexData++;
-                       *vertexData = q.t0; vertexData++;
-                       *vertexData = q.x1; vertexData++;
-                       *vertexData = q.y0; vertexData++;
-                       *vertexData = q.s1; vertexData++;
-                       *vertexData = q.t1; vertexData++;
-                       *vertexData = q.x1; vertexData++;
-                       *vertexData = q.y1; vertexData++;
-                       *vertexData = q.s0; vertexData++;
-                       *vertexData = q.t1; vertexData++;
-                       *vertexData = q.x0; vertexData++;
-                       *vertexData = q.y1; vertexData++;
-
-                       *indexData = m_debug_font_vertex_head/4; indexData++;
-                       *indexData = m_debug_font_vertex_head/4+1; indexData++;
-                       *indexData = m_debug_font_vertex_head/4+3; indexData++;
-                       *indexData = m_debug_font_vertex_head/4+2; indexData++;
-                       *indexData = 65535; indexData++;
+                       size_t index = vertexData.Add({q.x0, q.y0, q.s0, q.t0});
+                       index += baseVertex;
+                       indexData.Add(index);
+                       index = vertexData.Add({q.x1, q.y0, q.s1, q.t0});
+                       index += baseVertex;
+                       indexData.Add(index);
+                       index = vertexData.Add({q.x0, q.y1, q.s0, q.t1});
+                       index += baseVertex;
+                       indexData.Add(index);
+                       index = vertexData.Add({q.x1, q.y1, q.s1, q.t1});
+                       index += baseVertex;
+                       indexData.Add(index);
+                       indexData.Add(65535);
 
                        m_debug_font_vertex_head += 16;
                        m_debug_font_index_head += 5;

UCC git Repository :: git.ucc.asn.au