From 5c0874f82f2100fe951945eff3f343a897d2a5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 6 Mar 2016 16:24:35 -0800 Subject: [PATCH] Cleanup. --- examples/19-oit/oit.cpp | 630 ++++++++++++++++++---------------- examples/20-nanovg/nanovg.cpp | 149 ++++---- 2 files changed, 426 insertions(+), 353 deletions(-) diff --git a/examples/19-oit/oit.cpp b/examples/19-oit/oit.cpp index 66baed03c..b9bbd0419 100644 --- a/examples/19-oit/oit.cpp +++ b/examples/19-oit/oit.cpp @@ -146,322 +146,370 @@ void screenSpaceQuad(float _textureWidth, float _textureHeight, bool _originBott } } -int _main_(int _argc, char** _argv) +class ExampleOIT : public entry::AppI { - Args args(_argc, _argv); - - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - uint32_t reset = BGFX_RESET_VSYNC; - - bgfx::init(args.m_type, args.m_pciId); - bgfx::reset(width, height, reset); - - // Create vertex stream declaration. - PosColorVertex::init(); - PosColorTexCoord0Vertex::init(); - - // Enable debug text. - bgfx::setDebug(debug); - - // Get renderer capabilities info. - const bgfx::Caps* caps = bgfx::getCaps(); - - // Setup root path for binary shaders. Shader binaries are different - // for each renderer. - switch (caps->rendererType) + void init(int _argc, char** _argv) BX_OVERRIDE { - default: - break; + Args args(_argc, _argv); - case bgfx::RendererType::OpenGL: - case bgfx::RendererType::OpenGLES: - s_flipV = true; - break; + m_width = 1280; + m_height = 720; + m_debug = BGFX_DEBUG_TEXT; + m_reset = BGFX_RESET_VSYNC; + + bgfx::init(args.m_type, args.m_pciId); + bgfx::reset(m_width, m_height, m_reset); + + // Enable debug text. + bgfx::setDebug(m_debug); + + // Create vertex stream declaration. + PosColorVertex::init(); + PosColorTexCoord0Vertex::init(); + + // Get renderer capabilities info. + const bgfx::Caps* caps = bgfx::getCaps(); + + // Setup root path for binary shaders. Shader binaries are different + // for each renderer. + switch (caps->rendererType) + { + default: + break; + + case bgfx::RendererType::OpenGL: + case bgfx::RendererType::OpenGLES: + s_flipV = true; + break; + } + + // Imgui. + imguiCreate(); + + // Create static vertex buffer. + m_vbh = bgfx::createVertexBuffer( + bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ) + , PosColorVertex::ms_decl + ); + + // Create static index buffer. + m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) ); + + // Create texture sampler uniforms. + s_texColor0 = bgfx::createUniform("s_texColor0", bgfx::UniformType::Int1); + s_texColor1 = bgfx::createUniform("s_texColor1", bgfx::UniformType::Int1); + u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4); + + m_blend = loadProgram("vs_oit", "fs_oit" ); + m_wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" ); + m_wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" ); + m_wbPass = loadProgram("vs_oit", "fs_oit_wb" ); + m_wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" ); + + m_fbtextures[0].idx = bgfx::invalidHandle; + m_fbtextures[1].idx = bgfx::invalidHandle; + m_fbh.idx = bgfx::invalidHandle; + + m_mode = 1; + m_scrollArea = 0; + m_frontToBack = true; + m_fadeInOut = false; + + m_oldWidth = 0; + m_oldHeight = 0; + m_oldReset = m_reset; + + m_timeOffset = bx::getHPCounter(); } - // Imgui. - imguiCreate(); - - const bgfx::Memory* mem; - - // Create static vertex buffer. - mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ); - bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, PosColorVertex::ms_decl); - - // Create static index buffer. - mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ); - bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem); - - // Create texture sampler uniforms. - bgfx::UniformHandle s_texColor0 = bgfx::createUniform("s_texColor0", bgfx::UniformType::Int1); - bgfx::UniformHandle s_texColor1 = bgfx::createUniform("s_texColor1", bgfx::UniformType::Int1); - bgfx::UniformHandle u_color = bgfx::createUniform("u_color", bgfx::UniformType::Vec4); - - bgfx::ProgramHandle blend = loadProgram("vs_oit", "fs_oit" ); - bgfx::ProgramHandle wbSeparatePass = loadProgram("vs_oit", "fs_oit_wb_separate" ); - bgfx::ProgramHandle wbSeparateBlit = loadProgram("vs_oit_blit", "fs_oit_wb_separate_blit" ); - bgfx::ProgramHandle wbPass = loadProgram("vs_oit", "fs_oit_wb" ); - bgfx::ProgramHandle wbBlit = loadProgram("vs_oit_blit", "fs_oit_wb_blit" ); - - bgfx::TextureHandle fbtextures[2] = { BGFX_INVALID_HANDLE, BGFX_INVALID_HANDLE }; - bgfx::FrameBufferHandle fbh = BGFX_INVALID_HANDLE; - - int64_t timeOffset = bx::getHPCounter(); - - uint32_t mode = 1; - int32_t scrollArea = 0; - bool frontToBack = true; - bool fadeInOut = false; - - uint32_t oldWidth = 0; - uint32_t oldHeight = 0; - uint32_t oldReset = reset; - - entry::MouseState mouseState; - while (!entry::processEvents(width, height, debug, reset, &mouseState) ) + int shutdown() BX_OVERRIDE { - if (oldWidth != width - || oldHeight != height - || oldReset != reset - || !bgfx::isValid(fbh) ) - { - // Recreate variable size render targets when resolution changes. - oldWidth = width; - oldHeight = height; - oldReset = reset; + // Cleanup. + imguiDestroy(); - if (bgfx::isValid(fbh) ) + bgfx::destroyFrameBuffer(m_fbh); + bgfx::destroyIndexBuffer(m_ibh); + bgfx::destroyVertexBuffer(m_vbh); + bgfx::destroyProgram(m_blend); + bgfx::destroyProgram(m_wbSeparatePass); + bgfx::destroyProgram(m_wbSeparateBlit); + bgfx::destroyProgram(m_wbPass); + bgfx::destroyProgram(m_wbBlit); + bgfx::destroyUniform(s_texColor0); + bgfx::destroyUniform(s_texColor1); + bgfx::destroyUniform(u_color); + + // Shutdown bgfx. + bgfx::shutdown(); + + return 0; + } + + bool update() BX_OVERRIDE + { + if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) + { + if (m_oldWidth != m_width + || m_oldHeight != m_height + || m_oldReset != m_reset + || !bgfx::isValid(m_fbh) ) { - bgfx::destroyFrameBuffer(fbh); + // Recreate variable size render targets when resolution changes. + m_oldWidth = m_width; + m_oldHeight = m_height; + m_oldReset = m_reset; + + if (bgfx::isValid(m_fbh) ) + { + bgfx::destroyFrameBuffer(m_fbh); + } + + m_fbtextures[0] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT); + m_fbtextures[1] = bgfx::createTexture2D(m_width, m_height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT); + m_fbh = bgfx::createFrameBuffer(BX_COUNTOF(m_fbtextures), m_fbtextures, true); } - fbtextures[0] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::RGBA16F, BGFX_TEXTURE_RT); - fbtextures[1] = bgfx::createTexture2D(width, height, 1, bgfx::TextureFormat::R16F, BGFX_TEXTURE_RT); - fbh = bgfx::createFrameBuffer(BX_COUNTOF(fbtextures), fbtextures, true); - } + imguiBeginFrame(m_mouseState.m_mx + , m_mouseState.m_my + , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0) + | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0) + | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0) + , m_mouseState.m_mz + , m_width + , m_height + ); - imguiBeginFrame(mouseState.m_mx - , mouseState.m_my - , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0) - | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0) - | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0) - , mouseState.m_mz - , width - , height - ); + imguiBeginScrollArea("Settings", m_width - m_width / 4 - 10, 10, m_width / 4, m_height / 3, &m_scrollArea); + imguiSeparatorLine(); - imguiBeginScrollArea("Settings", width - width / 4 - 10, 10, width / 4, height / 3, &scrollArea); - imguiSeparatorLine(); + imguiLabel("Blend mode:"); - imguiLabel("Blend mode:"); + m_mode = imguiChoose(m_mode + , "None" + , "Separate" + , "MRT Independent" + ); - mode = imguiChoose(mode - , "None" - , "Separate" - , "MRT Independent" - ); + imguiSeparatorLine(); - imguiSeparatorLine(); - - if (imguiCheck("Front to back", frontToBack) ) - { - frontToBack ^= true; - } - - if (imguiCheck("Fade in/out", fadeInOut) ) - { - fadeInOut ^= true; - } - - imguiEndScrollArea(); - imguiEndFrame(); - - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); - bgfx::setViewRect(1, 0, 0, width, height); - - int64_t now = bx::getHPCounter(); - static int64_t last = now; - const int64_t frameTime = now - last; - last = now; - const double freq = double(bx::getHPFrequency() ); - const double toMs = 1000.0/freq; - - float time = (float)( (now-timeOffset)/freq); - - // Use debug font to print information about this example. - bgfx::dbgTextClear(); - // Reference: - // Weighted, Blended Order-Independent Transparency - // http://jcgt.org/published/0002/02/09/ - // http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html - bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit"); - bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency."); - bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); - - float at[3] = { 0.0f, 0.0f, 0.0f }; - float eye[3] = { 0.0f, 0.0f, -7.0f }; - - float view[16]; - float proj[16]; - - // Set view and projection matrix for view 0. - bx::mtxLookAt(view, eye, at); - mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f); - - bgfx::setViewTransform(0, view, proj); - - // Set palette color for index 0 - bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f); - - // Set palette color for index 1 - bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f); - - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 1.0f // Depth - , 0 // Stencil - , 0 // FB texture 0, color palette 0 - , 1 == mode ? 1 : 0 // FB texture 1, color palette 1 - ); - - bgfx::setViewClear(1 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 1.0f // Depth - , 0 // Stencil - , 0 // Color palette 0 - ); - - bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE; - bgfx::setViewFrameBuffer(0, 0 == mode ? invalid : fbh); - - // Set view and projection matrix for view 1. - bx::mtxIdentity(view); - bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f); - bgfx::setViewTransform(1, view, proj); - - for (uint32_t depth = 0; depth < 3; ++depth) - { - uint32_t zz = frontToBack ? 2-depth : depth; - - for (uint32_t yy = 0; yy < 3; ++yy) + if (imguiCheck("Front to back", m_frontToBack) ) { - for (uint32_t xx = 0; xx < 3; ++xx) + m_frontToBack ^= true; + } + + if (imguiCheck("Fade in/out", m_fadeInOut) ) + { + m_fadeInOut ^= true; + } + + imguiEndScrollArea(); + imguiEndFrame(); + + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, m_width, m_height); + bgfx::setViewRect(1, 0, 0, m_width, m_height); + + int64_t now = bx::getHPCounter(); + static int64_t last = now; + const int64_t frameTime = now - last; + last = now; + const double freq = double(bx::getHPFrequency() ); + const double toMs = 1000.0/freq; + + float time = (float)( (now-m_timeOffset)/freq); + + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + // Reference: + // Weighted, Blended Order-Independent Transparency + // http://jcgt.org/published/0002/02/09/ + // http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/19-oit"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Weighted, Blended Order Independent Transparency."); + bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); + + float at[3] = { 0.0f, 0.0f, 0.0f }; + float eye[3] = { 0.0f, 0.0f, -7.0f }; + + float view[16]; + float proj[16]; + + // Set view and projection matrix for view 0. + bx::mtxLookAt(view, eye, at); + mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f); + + bgfx::setViewTransform(0, view, proj); + + // Set palette color for index 0 + bgfx::setPaletteColor(0, 0.0f, 0.0f, 0.0f, 0.0f); + + // Set palette color for index 1 + bgfx::setPaletteColor(1, 1.0f, 1.0f, 1.0f, 1.0f); + + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 1.0f // Depth + , 0 // Stencil + , 0 // FB texture 0, color palette 0 + , 1 == m_mode ? 1 : 0 // FB texture 1, color palette 1 + ); + + bgfx::setViewClear(1 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 1.0f // Depth + , 0 // Stencil + , 0 // Color palette 0 + ); + + bgfx::FrameBufferHandle invalid = BGFX_INVALID_HANDLE; + bgfx::setViewFrameBuffer(0, 0 == m_mode ? invalid : m_fbh); + + // Set view and projection matrix for view 1. + bx::mtxIdentity(view); + bx::mtxOrtho(proj, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f); + bgfx::setViewTransform(1, view, proj); + + for (uint32_t depth = 0; depth < 3; ++depth) + { + uint32_t zz = m_frontToBack ? 2-depth : depth; + + for (uint32_t yy = 0; yy < 3; ++yy) { - float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f }; - - if (fadeInOut - && zz == 1) + for (uint32_t xx = 0; xx < 3; ++xx) { - color[3] = sinf(time*3.0f)*0.49f+0.5f; + float color[4] = { xx*1.0f/3.0f, zz*1.0f/3.0f, yy*1.0f/3.0f, 0.5f }; + + if (m_fadeInOut + && zz == 1) + { + color[3] = sinf(time*3.0f)*0.49f+0.5f; + } + + bgfx::setUniform(u_color, color); + + BX_UNUSED(time); + float mtx[16]; + bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f); + //mtxIdentity(mtx); + mtx[12] = -2.5f + float(xx)*2.5f; + mtx[13] = -2.5f + float(yy)*2.5f; + mtx[14] = -2.5f + float(zz)*2.5f; + + // Set transform for draw call. + bgfx::setTransform(mtx); + + // Set vertex and index buffer. + bgfx::setVertexBuffer(m_vbh); + bgfx::setIndexBuffer(m_ibh); + + const uint64_t state = 0 + | BGFX_STATE_CULL_CW + | BGFX_STATE_RGB_WRITE + | BGFX_STATE_ALPHA_WRITE + | BGFX_STATE_DEPTH_TEST_LESS + | BGFX_STATE_MSAA + ; + + bgfx::ProgramHandle program = BGFX_INVALID_HANDLE; + switch (m_mode) + { + case 0: + // Set vertex and fragment shaders. + program = m_blend; + + // Set render states. + bgfx::setState(state + | BGFX_STATE_BLEND_ALPHA + ); + break; + + case 1: + // Set vertex and fragment shaders. + program = m_wbSeparatePass; + + // Set render states. + bgfx::setState(state + | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA) + ); + break; + + default: + // Set vertex and fragment shaders. + program = m_wbPass; + + // Set render states. + bgfx::setState(state + | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE) + | BGFX_STATE_BLEND_INDEPENDENT + , 0 + | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR) + ); + break; + } + + // Submit primitive for rendering to view 0. + bgfx::submit(0, program); } - - bgfx::setUniform(u_color, color); - - BX_UNUSED(time); - float mtx[16]; - bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f); - //mtxIdentity(mtx); - mtx[12] = -2.5f + float(xx)*2.5f; - mtx[13] = -2.5f + float(yy)*2.5f; - mtx[14] = -2.5f + float(zz)*2.5f; - - // Set transform for draw call. - bgfx::setTransform(mtx); - - // Set vertex and index buffer. - bgfx::setVertexBuffer(vbh); - bgfx::setIndexBuffer(ibh); - - const uint64_t state = 0 - | BGFX_STATE_CULL_CW - | BGFX_STATE_RGB_WRITE - | BGFX_STATE_ALPHA_WRITE - | BGFX_STATE_DEPTH_TEST_LESS - | BGFX_STATE_MSAA - ; - - bgfx::ProgramHandle program = BGFX_INVALID_HANDLE; - switch (mode) - { - case 0: - // Set vertex and fragment shaders. - program = blend; - - // Set render states. - bgfx::setState(state - | BGFX_STATE_BLEND_ALPHA - ); - break; - - case 1: - // Set vertex and fragment shaders. - program = wbSeparatePass; - - // Set render states. - bgfx::setState(state - | BGFX_STATE_BLEND_FUNC_SEPARATE(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_INV_SRC_ALPHA) - ); - break; - - default: - // Set vertex and fragment shaders. - program = wbPass; - - // Set render states. - bgfx::setState(state - | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_ONE, BGFX_STATE_BLEND_ONE) - | BGFX_STATE_BLEND_INDEPENDENT - , 0 - | BGFX_STATE_BLEND_FUNC_RT_1(BGFX_STATE_BLEND_ZERO, BGFX_STATE_BLEND_SRC_COLOR) - ); - break; - } - - // Submit primitive for rendering to view 0. - bgfx::submit(0, program); } } + + if (0 != m_mode) + { + bgfx::setTexture(0, s_texColor0, m_fbtextures[0]); + bgfx::setTexture(1, s_texColor1, m_fbtextures[1]); + bgfx::setState(0 + | BGFX_STATE_RGB_WRITE + | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA) + ); + screenSpaceQuad( (float)m_width, (float)m_height, s_flipV); + bgfx::submit(1 + , 1 == m_mode ? m_wbSeparateBlit : m_wbBlit + ); + } + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. + bgfx::frame(); + + return true; } - if (0 != mode) - { - bgfx::setTexture(0, s_texColor0, fbtextures[0]); - bgfx::setTexture(1, s_texColor1, fbtextures[1]); - bgfx::setState(0 - | BGFX_STATE_RGB_WRITE - | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_INV_SRC_ALPHA, BGFX_STATE_BLEND_SRC_ALPHA) - ); - screenSpaceQuad( (float)width, (float)height, s_flipV); - bgfx::submit(1 - , 1 == mode ? wbSeparateBlit : wbBlit - ); - } - - // Advance to next frame. Rendering thread will be kicked to - // process submitted rendering primitives. - bgfx::frame(); + return false; } - // Cleanup. - imguiDestroy(); + uint32_t m_width; + uint32_t m_height; + uint32_t m_debug; + uint32_t m_reset; - bgfx::destroyFrameBuffer(fbh); - bgfx::destroyIndexBuffer(ibh); - bgfx::destroyVertexBuffer(vbh); - bgfx::destroyProgram(blend); - bgfx::destroyProgram(wbSeparatePass); - bgfx::destroyProgram(wbSeparateBlit); - bgfx::destroyProgram(wbPass); - bgfx::destroyProgram(wbBlit); - bgfx::destroyUniform(s_texColor0); - bgfx::destroyUniform(s_texColor1); - bgfx::destroyUniform(u_color); + uint32_t m_mode; + int32_t m_scrollArea; + bool m_frontToBack; + bool m_fadeInOut; - // Shutdown bgfx. - bgfx::shutdown(); + uint32_t m_oldWidth; + uint32_t m_oldHeight; + uint32_t m_oldReset; - return 0; -} + entry::MouseState m_mouseState; + + int64_t m_timeOffset; + + bgfx::VertexBufferHandle m_vbh; + bgfx::IndexBufferHandle m_ibh; + + bgfx::UniformHandle s_texColor0; + bgfx::UniformHandle s_texColor1; + bgfx::UniformHandle u_color; + + bgfx::ProgramHandle m_blend; + bgfx::ProgramHandle m_wbSeparatePass; + bgfx::ProgramHandle m_wbSeparateBlit; + bgfx::ProgramHandle m_wbPass; + bgfx::ProgramHandle m_wbBlit; + + bgfx::TextureHandle m_fbtextures[2]; + bgfx::FrameBufferHandle m_fbh; +}; + +ENTRY_IMPLEMENT_MAIN(ExampleOIT); diff --git a/examples/20-nanovg/nanovg.cpp b/examples/20-nanovg/nanovg.cpp index f667ba180..206bca102 100644 --- a/examples/20-nanovg/nanovg.cpp +++ b/examples/20-nanovg/nanovg.cpp @@ -1201,80 +1201,105 @@ void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float he nvgRestore(vg); } -int _main_(int _argc, char** _argv) +class ExampleNanoVG : public entry::AppI { - Args args(_argc, _argv); - - uint32_t width = 1280; - uint32_t height = 720; - uint32_t debug = BGFX_DEBUG_TEXT; - uint32_t reset = BGFX_RESET_VSYNC; - - bgfx::init(args.m_type, args.m_pciId); - bgfx::reset(width, height, reset); - - // Enable debug text. - bgfx::setDebug(debug); - - // Set view 0 clear state. - bgfx::setViewClear(0 - , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH - , 0x303030ff - , 1.0f - , 0 - ); - - imguiCreate(); - - NVGcontext* nvg = nvgCreate(1, 0); - bgfx::setViewSeq(0, true); - - DemoData data; - loadDemoData(nvg, &data); - - bndSetFont(nvgCreateFont(nvg, "droidsans", "font/droidsans.ttf") ); - bndSetIconImage(nvgCreateImage(nvg, "images/blender_icons16.png", 0) ); - - int64_t timeOffset = bx::getHPCounter(); - - entry::MouseState mouseState; - while (!entry::processEvents(width, height, debug, reset, &mouseState) ) + void init(int _argc, char** _argv) BX_OVERRIDE { - int64_t now = bx::getHPCounter(); - const double freq = double(bx::getHPFrequency() ); - float time = (float)( (now-timeOffset)/freq); + Args args(_argc, _argv); - // Set view 0 default viewport. - bgfx::setViewRect(0, 0, 0, width, height); + m_width = 1280; + m_height = 720; + m_debug = BGFX_DEBUG_TEXT; + m_reset = BGFX_RESET_VSYNC; - // This dummy draw call is here to make sure that view 0 is cleared - // if no other draw calls are submitted to view 0. - bgfx::touch(0); + bgfx::init(args.m_type, args.m_pciId); + bgfx::reset(m_width, m_height, m_reset); - // Use debug font to print information about this example. - bgfx::dbgTextClear(); - bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/20-nanovg"); - bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: NanoVG is small antialiased vector graphics rendering library."); + // Enable debug text. + bgfx::setDebug(m_debug); - nvgBeginFrame(nvg, width, height, 1.0f); + // Set view 0 clear state. + bgfx::setViewClear(0 + , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH + , 0x303030ff + , 1.0f + , 0 + ); - renderDemo(nvg, float(mouseState.m_mx), float(mouseState.m_my), float(width), float(height), time, 0, &data); + imguiCreate(); - nvgEndFrame(nvg); + m_nvg = nvgCreate(1, 0); + bgfx::setViewSeq(0, true); - // Advance to next frame. Rendering thread will be kicked to - // process submitted rendering primitives. - bgfx::frame(); + loadDemoData(m_nvg, &m_data); + + bndSetFont(nvgCreateFont(m_nvg, "droidsans", "font/droidsans.ttf") ); + bndSetIconImage(nvgCreateImage(m_nvg, "images/blender_icons16.png", 0) ); + + m_timeOffset = bx::getHPCounter(); } - freeDemoData(nvg, &data); + int shutdown() BX_OVERRIDE + { + freeDemoData(m_nvg, &m_data); - nvgDelete(nvg); + nvgDelete(m_nvg); - imguiDestroy(); + imguiDestroy(); - // Shutdown bgfx. - bgfx::shutdown(); + // Shutdown bgfx. + bgfx::shutdown(); - return 0; -} + return 0; + } + + bool update() BX_OVERRIDE + { + if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) ) + { + int64_t now = bx::getHPCounter(); + const double freq = double(bx::getHPFrequency() ); + float time = (float)( (now-m_timeOffset)/freq); + + // Set view 0 default viewport. + bgfx::setViewRect(0, 0, 0, m_width, m_height); + + // This dummy draw call is here to make sure that view 0 is cleared + // if no other draw calls are submitted to view 0. + bgfx::touch(0); + + // Use debug font to print information about this example. + bgfx::dbgTextClear(); + bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/20-nanovg"); + bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: NanoVG is small antialiased vector graphics rendering library."); + + nvgBeginFrame(m_nvg, m_width, m_height, 1.0f); + + renderDemo(m_nvg, float(m_mouseState.m_mx), float(m_mouseState.m_my), float(m_width), float(m_height), time, 0, &m_data); + + nvgEndFrame(m_nvg); + + // Advance to next frame. Rendering thread will be kicked to + // process submitted rendering primitives. + bgfx::frame(); + + return true; + } + + return false; + } + + uint32_t m_width; + uint32_t m_height; + uint32_t m_debug; + uint32_t m_reset; + + entry::MouseState m_mouseState; + + int64_t m_timeOffset; + + NVGcontext* m_nvg; + DemoData m_data; +}; + +ENTRY_IMPLEMENT_MAIN(ExampleNanoVG);