From: David Gow Date: Sat, 31 May 2014 13:15:20 +0000 (+0800) Subject: Maybe make some GL code easier to understand X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=280f0d7d34b9bd377fb49580b69aebcbd60cc440;p=ipdf%2Fcode.git Maybe make some GL code easier to understand --- diff --git a/bin/ipdf b/bin/ipdf index 3941958..4143438 100755 Binary files a/bin/ipdf and b/bin/ipdf differ diff --git a/src/bufferbuilder.h b/src/bufferbuilder.h new file mode 100644 index 0000000..385369c --- /dev/null +++ b/src/bufferbuilder.h @@ -0,0 +1,25 @@ +#ifndef _BUFFERBUILDER_H +#define _BUFFERBUILDER_H + +namespace IPDF +{ + /* + * Utility to add stuff to a buffer. + */ + template + 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 diff --git a/src/screen.cpp b/src/screen.cpp index 7817482..2d63bc9 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -5,6 +5,7 @@ #include // for access(2) #include // 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 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 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;