From a111bfc17f4d6b88c12f63d1c4ee8b582e4926dc Mon Sep 17 00:00:00 2001 From: fgenesis Date: Sat, 26 Aug 2023 00:14:05 +0200 Subject: [PATCH] use VBO to render tile borders Also fix oversight in DynamicGPUBuffer::drawElements(); first wasn't used --- BBGE/Core.cpp | 5 ++++- BBGE/Core.h | 2 ++ BBGE/TileRender.cpp | 16 ++++------------ BBGE/VertexBuffer.cpp | 12 ++++++++---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index 65abe60..61fc609 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -158,6 +158,8 @@ void Core::setup_opengl() TexCoordBox defaultTC; defaultTC.setStandard(); defaultQuadGrid.init(2, 2, defaultTC); + + defautQuadBorder.initQuadVertices(defaultTC, GPUACCESS_DEFAULT); } void Core::resizeWindow(int w, int h, int full, int bpp, int vsync, int display, int hz) @@ -281,7 +283,7 @@ static bool checkWritable(const std::string& path, bool warn, bool critical) Core::Core(const std::string &filesystem, const std::string& extraDataDir, int numRenderLayers, const std::string &appName, int particleSize, std::string userDataSubFolder) -: ActionMapper(), StateManager(), appName(appName) +: ActionMapper(), StateManager(), appName(appName), defautQuadBorder(GPUBUF_STATIC | GPUBUF_VERTEXBUF) { window = NULL; sound = NULL; @@ -1922,6 +1924,7 @@ void Core::shutdown() debugLog("OK"); defaultQuadGrid.dropBuffers(); + defautQuadBorder.dropBuffer(); debugLog("Shutdown Graphics Library..."); shutdownGraphicsLibrary(); diff --git a/BBGE/Core.h b/BBGE/Core.h index 29509dd..e4ca150 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -413,6 +413,7 @@ public: TextureMgr texmgr; inline const RenderGrid *getDefaultQuadGrid() const { return &defaultQuadGrid; } + inline const DynamicGPUBuffer *getDefaultQuadBorderBuf() const { return &defautQuadBorder; } protected: @@ -492,6 +493,7 @@ protected: void clearActionButtons(); RenderGrid defaultQuadGrid; + DynamicGPUBuffer defautQuadBorder; public: // inclusive! diff --git a/BBGE/TileRender.cpp b/BBGE/TileRender.cpp index 8c18265..a7df141 100644 --- a/BBGE/TileRender.cpp +++ b/BBGE/TileRender.cpp @@ -62,6 +62,7 @@ void TileRender::onRender(const RenderState& rs) const unsigned lastTexId = 0; const bool renderExtras = renderBorders || RenderObject::renderCollisionShape; + const TileEffectData *prevEff = ((TileEffectData*)NULL)+1; // initial value is different from anything else for(size_t i = 0; i < storage.tiles.size(); ++i) @@ -183,20 +184,11 @@ void TileRender::onRender(const RenderState& rs) const color *= getTagColor(tile.tag); glColor4f(color.x, color.y, color.z, 1.0f); + core->getDefaultQuadBorderBuf()->apply(); glPointSize(16); - glBegin(GL_POINTS); - glVertex2f(0,0); - glEnd(); - - // TODO: move this to the IBO + glDrawArrays(GL_POINTS, 4, 1); glLineWidth(2); - glBegin(GL_LINE_STRIP); - glVertex2f(0.5f, 0.5f); - glVertex2f(0.5f, -0.5f); - glVertex2f(-0.5f, -0.5f); - glVertex2f(-0.5f, 0.5f); - glVertex2f(0.5f, 0.5f); - glEnd(); + glDrawArrays(GL_LINE_LOOP, 0, 4); } } diff --git a/BBGE/VertexBuffer.cpp b/BBGE/VertexBuffer.cpp index 4d3fdb6..ff87ea1 100644 --- a/BBGE/VertexBuffer.cpp +++ b/BBGE/VertexBuffer.cpp @@ -279,17 +279,17 @@ void DynamicGPUBuffer::drawElements(unsigned glmode, size_t n, size_t first) con glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, id); //} - void *p = id ? NULL : _h_data; + const unsigned short *p = (unsigned short*)(id ? NULL : _h_data); assert(p || id); - glDrawElements(glmode, n, GL_UNSIGNED_SHORT, p); + glDrawElements(glmode, n, GL_UNSIGNED_SHORT, p + first); } void DynamicGPUBuffer::initQuadVertices(const TexCoordBox& tc, unsigned access) { do { - float *p = (float*)beginWrite(GPUBUFTYPE_VEC2_TC, (4*4) * sizeof(float), access); + float *p = (float*)beginWrite(GPUBUFTYPE_VEC2_TC, (4*4 + 4) * sizeof(float), access); *p++ = -0.5f; *p++ = +0.5f; // xy *p++ = tc.u1; *p++ = tc.v1; // uv *p++ = +0.5f; *p++ = +0.5f; // xy @@ -298,6 +298,9 @@ void DynamicGPUBuffer::initQuadVertices(const TexCoordBox& tc, unsigned access) *p++ = tc.u2; *p++ = tc.v2; // uv *p++ = -0.5f; *p++ = -0.5f; // xy *p++ = tc.u1; *p++ = tc.v2; // uv + + for(size_t i = 0; i < 4; ++i) + *p++ = 0; // zero/center xy uv (uv isn't used) } while(!commitWrite()); } @@ -318,9 +321,10 @@ size_t DynamicGPUBuffer::initGridIndices_Triangles(size_t w, size_t h, bool inve const size_t quadsx = w - 1; const size_t quadsy = h - 1; const size_t quads = quadsx * quadsy; + const size_t border = 4; // for GL_LINE_LOOP do { - unsigned short *p = (unsigned short*)beginWrite(GPUBUFTYPE_U16, 6*quads * sizeof(short), access); + unsigned short *p = (unsigned short*)beginWrite(GPUBUFTYPE_U16, (6*quads + border) * sizeof(short), access); if(!invert) {