int main() { GLFWwindow* window; DemoData data; NVGcontext* vg = NULL; PerfGraph fps; double prevt = 0; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); vg = nvgCreateGLES2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } if (loadDemoData(vg, &data) == -1) return -1; glfwSwapInterval(0); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; t = glfwGetTime(); dt = t - prevt; prevt = t; updateGraph(&fps, dt); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); if (premult) glClearColor(0,0,0,0); else glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderGraph(vg, 5,5, &fps); nvgEndFrame(vg); if (screenshot) { screenshot = 0; saveScreenShot(fbWidth, fbHeight, premult, "dump.png"); } glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } freeDemoData(vg, &data); nvgDeleteGLES2(vg); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; struct DemoData data; struct NVGcontext* vg = NULL; struct GPUtimer gpuTimer; struct PerfGraph fps, cpuGraph, gpuGraph; double prevt = 0, cpuTime = 0; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time"); initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time"); glfwSetErrorCallback(errorcb); #ifndef _WIN32 // don't require this on win32, and works with more cards glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif #ifdef DEMO_MSAA glfwWindowHint(GLFW_SAMPLES, 4); #endif window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here. glGetError(); #endif #ifdef DEMO_MSAA vg = nvgCreateGL3(512, 512, 0); #else vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS); #endif if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } if (loadDemoData(vg, &data) == -1) return -1; glfwSwapInterval(0); initGPUTimer(&gpuTimer); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; float gpuTimes[3]; int i, n; t = glfwGetTime(); dt = t - prevt; prevt = t; startGPUTimer(&gpuTimer); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); if (premult) glClearColor(0,0,0,0); else glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio, premult ? NVG_PREMULTIPLIED_ALPHA : NVG_STRAIGHT_ALPHA); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderGraph(vg, 5,5, &fps); renderGraph(vg, 5+200+5,5, &cpuGraph); if (gpuTimer.supported) renderGraph(vg, 5+200+5+200+5,5, &gpuGraph); nvgEndFrame(vg); // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU) cpuTime = glfwGetTime() - t; updateGraph(&fps, dt); updateGraph(&cpuGraph, cpuTime); // We may get multiple results. n = stopGPUTimer(&gpuTimer, gpuTimes, 3); for (i = 0; i < n; i++) updateGraph(&gpuGraph, gpuTimes[i]); if (screenshot) { screenshot = 0; saveScreenShot(fbWidth, fbHeight, premult, "dump.png"); } glfwSwapBuffers(window); glfwPollEvents(); } freeDemoData(vg, &data); nvgDeleteGL3(vg); printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f); printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f); printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; NVGcontext* vg = NULL; GPUtimer gpuTimer; PerfGraph fps, cpuGraph, gpuGraph; double prevt = 0, cpuTime = 0; NVGLUframebuffer* fb = NULL; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initGraph(&fps, GRAPH_RENDER_FPS, "Frame Time"); initGraph(&cpuGraph, GRAPH_RENDER_MS, "CPU Time"); initGraph(&gpuGraph, GRAPH_RENDER_MS, "GPU Time"); glfwSetErrorCallback(errorcb); #ifndef _WIN32 // don't require this on win32, and works with more cards glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #endif glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); #ifdef DEMO_MSAA glfwWindowHint(GLFW_SAMPLES, 4); #endif window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here. glGetError(); #endif #ifdef DEMO_MSAA vg = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_DEBUG); #else vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); #endif if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } // Create hi-dpi FBO for hi-dpi screens. glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // The image pattern is tiled, set repeat on x and y. fb = nvgluCreateFramebuffer(vg, (int)(100*pxRatio), (int)(100*pxRatio), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY); if (fb == NULL) { printf("Could not create FBO.\n"); return -1; } if (loadFonts(vg) == -1) { printf("Could not load fonts\n"); return -1; } glfwSwapInterval(0); initGPUTimer(&gpuTimer); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; float gpuTimes[3]; int i, n; t = glfwGetTime(); dt = t - prevt; prevt = t; startGPUTimer(&gpuTimer); glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; renderPattern(vg, fb, t, pxRatio); // Update and render glViewport(0, 0, fbWidth, fbHeight); glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, winWidth, winHeight, pxRatio); // Use the FBO as image pattern. if (fb != NULL) { NVGpaint img = nvgImagePattern(vg, 0, 0, 100, 100, 0, fb->image, 1.0f); nvgSave(vg); for (i = 0; i < 20; i++) { nvgBeginPath(vg); nvgRect(vg, 10 + i*30,10, 10, winHeight-20); nvgFillColor(vg, nvgHSLA(i/19.0f, 0.5f, 0.5f, 255)); nvgFill(vg); } nvgBeginPath(vg); nvgRoundedRect(vg, 140 + sinf(t*1.3f)*100, 140 + cosf(t*1.71244f)*100, 250, 250, 20); nvgFillPaint(vg, img); nvgFill(vg); nvgStrokeColor(vg, nvgRGBA(220,160,0,255)); nvgStrokeWidth(vg, 3.0f); nvgStroke(vg); nvgRestore(vg); } renderGraph(vg, 5,5, &fps); renderGraph(vg, 5+200+5,5, &cpuGraph); if (gpuTimer.supported) renderGraph(vg, 5+200+5+200+5,5, &gpuGraph); nvgEndFrame(vg); // Measure the CPU time taken excluding swap buffers (as the swap may wait for GPU) cpuTime = glfwGetTime() - t; updateGraph(&fps, dt); updateGraph(&cpuGraph, cpuTime); // We may get multiple results. n = stopGPUTimer(&gpuTimer, gpuTimes, 3); for (i = 0; i < n; i++) updateGraph(&gpuGraph, gpuTimes[i]); glfwSwapBuffers(window); glfwPollEvents(); } nvgluDeleteFramebuffer(fb); nvgDeleteGL3(vg); printf("Average Frame Time: %.2f ms\n", getGraphAverage(&fps) * 1000.0f); printf(" CPU Time: %.2f ms\n", getGraphAverage(&cpuGraph) * 1000.0f); printf(" GPU Time: %.2f ms\n", getGraphAverage(&gpuGraph) * 1000.0f); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; struct NVGcontext* vg = NULL; char search[64] = "Foob-foob"; double t = 0; /* int blending = 0; float opacity = 0.5f; float position[3] = {100.0f, 120.0f, 234.0f}; float color[4] = {255/255.0f,192/255.0f,0/255.0f,255/255.0f}; float iterations = 42; int cull = 1; char name[64] = "Mikko"; const char* choices[] = { "Normal", "Minimum Color", "Screen Door", "Maximum Velocity" }; float scroll = 30;*/ float iconScale = 21.0f/1000.0f; printf("start\n"); if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(1000, 600, "mg", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, keycb); glfwSetCharCallback(window, charcb); glfwSetMouseButtonCallback(window, buttoncb); glfwMakeContextCurrent(window); vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS); if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } miInit(vg); if (miCreateFont(MI_FONT_NORMAL, "../example/fonts/Roboto-Regular.ttf")) { printf("Could not add font italic.\n"); return -1; } if (miCreateFont(MI_FONT_ITALIC, "../example/fonts/Roboto-Italic.ttf")) { printf("Could not add font italic.\n"); return -1; } if (miCreateFont(MI_FONT_BOLD, "../example/fonts/Roboto-Bold.ttf")) { printf("Could not add font bold.\n"); return -1; } if (miCreateIconImage("check", "../example/icons/check.svg", iconScale)) { printf("Could not create icon 'check'.\n"); return -1; } if (miCreateIconImage("arrow-combo", "../example/icons/arrow-combo.svg", iconScale)) { printf("Could not create icon 'arrow-combo'.\n"); return -1; } if (miCreateIconImage("tools", "../example/icons/tools.svg", iconScale)) { printf("Could not create icon 'tool'.\n"); return -1; } if (miCreateIconImage("cancel-circled", "../example/icons/cancel-circled.svg", iconScale)) { printf("Could not create icon 'cancel-circled'.\n"); return -1; } if (miCreateIconImage("plus", "../example/icons/plus.svg", iconScale)) { printf("Could not create icon 'plus'.\n"); return -1; } if (miCreateIconImage("search", "../example/icons/search.svg", iconScale)) { printf("Could not create icon 'search'.\n"); return -1; } glfwSetTime(0); MIcanvasState canvas = {0}; float value = 0.15f; while (!glfwWindowShouldClose(window)) { double mx, my; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; double dt; dt = glfwGetTime() - t; t += dt; // float t = glfwGetTime(); // float x,y,popy; // struct MGhit* hit; glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, winWidth, winHeight, pxRatio, NVG_STRAIGHT_ALPHA); input.mx = mx; input.my = my; miFrameBegin(winWidth, winHeight, &input, (float)dt); // input.nkeys = 0; // input.mbut = 0; // miInput(panel, &input); /* float s = slider; if (miSync(panel, "slider", &s)) { slider = s; } if (miValue(panel, "slider", &s)) { slider = s; } if (miClicked(panel, "footer-add")) { } */ // miRender(panel, vg); miPanelBegin(50,50, 250,450); miDockBegin(MI_TOP_BOTTOM); miText("Materials"); float cols[3] = {25, -1, 25}; miDivsBegin(MI_LEFT_RIGHT, 3, cols); miRowHeight(25); miText("S"); miInput(search, sizeof(search)); miText("X"); miText("Q"); miDivsEnd(); miSliderValue(&value, -1.0f, 1.0f); /* miLayoutBegin(); miRowHeight(25); miPack(MI_LEFT_RIGHT); miText("S"); miPack(MI_RIGHT_LEFT); miText("X"); miPack(MI_FILLX); miInput(search, sizeof(search)); miLayoutEnd();*/ miDockEnd(); miDockBegin(MI_BOTTOM_TOP); float cols2[3] = {-1, 60, 40}; miDivsBegin(MI_LEFT_RIGHT, 3, cols2); miRowHeight(20); miSpacer(); miButton("Add"); miButton("Delete"); miDivsEnd(); miDockEnd(); // miLayoutBegin(); // miRowHeight(20); /* miPack(MI_LEFT_RIGHT); miText("Ins"); miPack(MI_RIGHT_LEFT); miButton("Delete"); miButton("Add");*/ // miLayoutEnd(); miDockBegin(MI_FILLY); float cols3[2] = {50, -1}; miDivsBegin(MI_LEFT_RIGHT, 2, cols3); miRowHeight(50); miText("IMG"); float rows[4] = {-1, 20, 15, -1}; miDivsBegin(MI_TOP_BOTTOM, 4, rows); miSpacer(); miText("Plastic"); miLayoutBegin(MI_LEFT_RIGHT); miPack(MI_LEFT_RIGHT); miText("very shiny"); miPack(MI_RIGHT_LEFT); miText("7kB"); miLayoutEnd(); miDivsEnd(); miDivsEnd(); miLayoutBegin(MI_LEFT_RIGHT); miRowHeight(50); miText("IMG"); miLayoutBegin(MI_TOP_BOTTOM); miText("Plastic"); miText("very shiny"); miLayoutEnd(); miLayoutEnd(); miDockEnd(); /* miText("Text 1"); float cols[3] = {25, -1, 25}; miDivsBegin(MI_LEFT_RIGHT, 3, cols); // miLayoutBegin(MI_LEFT_RIGHT); miPack(MI_LEFT_RIGHT); miText("Text 2.1"); miButton("Text 2.2"); miText("Text 2.3"); miDivsEnd(); // miLayoutEnd(); miText("Text 3");*/ /* miPack(MI_BOTTOM_TOP); miText("BOTTOM"); miPack(MI_LEFT_RIGHT); miText("LEFT"); miPack(MI_RIGHT_LEFT); miText("RIGHT");*/ /* MIhandle button = miButton("Popup"); MIhandle popup = miPopupBegin(button, MI_ONCLICK, MI_BELOW); miText("Popup..."); miCanvasBegin(&canvas, MI_FIT, 50); miCanvasEnd(); if (miClicked(miButton("Close"))) { printf("Close popup\n"); miPopupHide(popup); } MIhandle button2 = miButton("Popup 2"); miPopupBegin(button2, MI_ONHOVER, MI_RIGHT); miText("Popup 2"); miPopupEnd(); MIhandle button3 = miButton("Popup 3"); miPopupBegin(button3, MI_ONHOVER, MI_RIGHT); miText("Popup 3"); miPopupEnd(); miPopupEnd(); miSlider(&value, -1.0f, 1.0f); miSliderValue(&value, -1.0f, 1.0f); miText("Foobar"); float divs[] = {50,100}; miDivsBegin(MI_ROW, divs, 2, 30, 5); miButton("Tab 1"); miButton("Tab 2"); miDivsBegin(MI_COL, divs, 2, 30, 0); miButton("Tab 4.1"); miDivsBegin(MI_ROW, divs, 2, 30, 0); miButton("Tab 4.2.1"); miButton("Tab 4.2.2"); miButton("Tab 4.2.3"); miDivsEnd(); miDivsEnd(); miButton("Tab 3"); miButton("Tab 5"); miStackBegin(MI_COL, 30, 5); miText("Tab 6.1"); miStackBegin(MI_ROW, 30, 5); miText("Tab 6.2.1"); miText("Tab 6.2.2"); miText("Tab 6.2.3"); miStackEnd(); miText("Tab 6.3"); miStackEnd(); miDivsEnd(); miText("Foofoo"); miButtonRowBegin(4); miButton("A"); miButton("B"); miButton("C"); miButton("D"); miButtonRowEnd(); miInput(search, sizeof(search)); miPanelBegin(250,250, 250,40); miText("Another one..."); miPanelEnd();*/ miPanelEnd(); miFrameEnd(); nvgEndFrame(vg); glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } nvgDeleteGL3(vg); miTerminate(); glfwTerminate(); return 0; }
int main() { GLFWwindow* window; struct DemoData data; struct NVGcontext* vg = NULL; struct FPScounter fps; double prevt = 0; if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } initFPS(&fps); glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } #endif vg = nvgCreateGL3(512,512); if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } if (loadDemoData(vg, &data) == -1) return -1; glfwSwapInterval(0); glfwSetTime(0); prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; int width, height; t = glfwGetTime(); dt = t - prevt; prevt = t; updateFPS(&fps, dt); glfwGetCursorPos(window, &mx, &my); glfwGetFramebufferSize(window, &width, &height); // Update and render glViewport(0, 0, width, height); glClearColor(0.3f, 0.3f, 0.32f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); nvgBeginFrame(vg, width, height); renderDemo(vg, mx,my, width,height, t, blowup, &data); renderFPS(vg, 5,5, &fps); glEnable(GL_DEPTH_TEST); glfwSwapBuffers(window); glfwPollEvents(); } freeDemoData(vg, &data); nvgDeleteGL3(vg); glfwTerminate(); return 0; }
int _main_(int _argc, char** _argv) { 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) ) { int64_t now = bx::getHPCounter(); const double freq = double(bx::getHPFrequency() ); float time = (float)( (now-timeOffset)/freq); // Set view 0 default viewport. bgfx::setViewRect(0, 0, 0, width, 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(nvg, width, height, 1.0f); renderDemo(nvg, float(mouseState.m_mx), float(mouseState.m_my), float(width), float(height), time, 0, &data); nvgEndFrame(nvg); // Advance to next frame. Rendering thread will be kicked to // process submitted rendering primitives. bgfx::frame(); } freeDemoData(nvg, &data); nvgDelete(nvg); imguiDestroy(); // Shutdown bgfx. bgfx::shutdown(); return 0; }
void Renderer2D::prepareRender( void ) { nvgBeginFrame( _context2D, _window->getWidth(), _window->getHeight(), 1.f ); }
static void draw(NVGcontext *nvg, struct zr_command_queue *queue, int width, int height) { const struct zr_command *cmd; glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_CULL_FACE); glDisable(GL_DEPTH_TEST); glEnable(GL_SCISSOR_TEST); glEnable(GL_TEXTURE_2D); nvgBeginFrame(nvg, width, height, ((float)width/(float)height)); zr_foreach_command(cmd, queue) { switch (cmd->type) { case ZR_COMMAND_NOP: break; case ZR_COMMAND_SCISSOR: { const struct zr_command_scissor *s = zr_command(scissor, cmd); nvgScissor(nvg, s->x, s->y, s->w, s->h); } break; case ZR_COMMAND_LINE: { const struct zr_command_line *l = zr_command(line, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, l->begin.x, l->begin.y); nvgLineTo(nvg, l->end.x, l->end.y); nvgFillColor(nvg, nvgRGBA(l->color.r, l->color.g, l->color.b, l->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_CURVE: { const struct zr_command_curve *q = zr_command(curve, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, q->begin.x, q->begin.y); nvgBezierTo(nvg, q->ctrl[0].x, q->ctrl[0].y, q->ctrl[1].x, q->ctrl[1].y, q->end.x, q->end.y); nvgStrokeColor(nvg, nvgRGBA(q->color.r, q->color.g, q->color.b, q->color.a)); nvgStroke(nvg); } break; case ZR_COMMAND_RECT: { const struct zr_command_rect *r = zr_command(rect, cmd); nvgBeginPath(nvg); nvgRoundedRect(nvg, r->x, r->y, r->w, r->h, r->rounding); nvgFillColor(nvg, nvgRGBA(r->color.r, r->color.g, r->color.b, r->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_CIRCLE: { const struct zr_command_circle *c = zr_command(circle, cmd); nvgBeginPath(nvg); nvgCircle(nvg, c->x + (c->w/2.0f), c->y + c->w/2.0f, c->w/2.0f); nvgFillColor(nvg, nvgRGBA(c->color.r, c->color.g, c->color.b, c->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_TRIANGLE: { const struct zr_command_triangle *t = zr_command(triangle, cmd); nvgBeginPath(nvg); nvgMoveTo(nvg, t->a.x, t->a.y); nvgLineTo(nvg, t->b.x, t->b.y); nvgLineTo(nvg, t->c.x, t->c.y); nvgLineTo(nvg, t->a.x, t->a.y); nvgFillColor(nvg, nvgRGBA(t->color.r, t->color.g, t->color.b, t->color.a)); nvgFill(nvg); } break; case ZR_COMMAND_TEXT: { const struct zr_command_text *t = zr_command(text, cmd); nvgBeginPath(nvg); nvgRoundedRect(nvg, t->x, t->y, t->w, t->h, 0); nvgFillColor(nvg, nvgRGBA(t->background.r, t->background.g, t->background.b, t->background.a)); nvgFill(nvg); nvgBeginPath(nvg); nvgFillColor(nvg, nvgRGBA(t->foreground.r, t->foreground.g, t->foreground.b, t->foreground.a)); nvgTextAlign(nvg, NVG_ALIGN_MIDDLE); nvgText(nvg, t->x, t->y + t->h * 0.5f, t->string, &t->string[t->length]); nvgFill(nvg); } break; case ZR_COMMAND_IMAGE: { const struct zr_command_image *i = zr_command(image, cmd); NVGpaint imgpaint; imgpaint = nvgImagePattern(nvg, i->x, i->y, i->w, i->h, 0, i->img.handle.id, 1.0f); nvgBeginPath(nvg); nvgRoundedRect(nvg, i->x, i->y, i->w, i->h, 0); nvgFillPaint(nvg, imgpaint); nvgFill(nvg); } break; case ZR_COMMAND_ARC: default: break; } } zr_command_queue_clear(queue); nvgResetScissor(nvg); nvgEndFrame(nvg); glPopAttrib(); }
void Application::drawDebugUI() { NVGcontext* nvg = context->nvgContext; nvgBeginFrame(nvg, context->getScreenWidth(), context->getScreenHeight(),1.0f); nvgResetScissor(nvg); rootRegion.drawDebug(context.get()); Region* onTop = context->getOnTopRegion(); if (onTop != nullptr) { onTop->drawDebug(context.get()); } float cr = context->theme.CORNER_RADIUS; if (context->getViewport().contains(context->cursorPosition)) { nvgFontSize(nvg, 15); nvgFontFaceId(nvg, context->getFontHandle(FontType::Bold)); /* int alignment = 0; if (context->cursorPosition.x < context->width() * 0.5f) { alignment = NVG_ALIGN_LEFT; } else { alignment = NVG_ALIGN_RIGHT; } if (context->cursorPosition.y < context->height() * 0.5f) { alignment |= NVG_ALIGN_TOP; } else { alignment |= NVG_ALIGN_BOTTOM; } std::string txt = MakeString() << std::setprecision(4) << " " << context->cursorPosition; nvgTextAlign(nvg, alignment); nvgFillColor(nvg, Color(0, 0, 0, 128)); if (context->hasFocus) { drawText(nvg, context->cursorPosition, txt, FontStyle::Outline, Color(255), Color(64, 64, 64)); } */ nvgTextAlign(nvg, NVG_ALIGN_TOP); float yoffset = 5; std::string txt = context->hasFocus ? "Window Has Focus" : "Window Lost Focus"; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; if (context->mouseOverRegion != nullptr) { txt = MakeString() << "Mouse Over [" << context->mouseOverRegion->name << "] " << context->cursorPosition; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; } if (context->mouseDownRegion != nullptr) { txt = MakeString() << "Mouse Down [" << context->mouseDownRegion->name << "] " << context->cursorDownPosition; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; } if (context->mouseFocusRegion != nullptr) { txt = MakeString() << "Mouse Focus [" << context->mouseFocusRegion->name << "]"; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; } if (context->onTopRegion != nullptr) { txt = MakeString() << "On Top [" << context->onTopRegion->name << ": " << (context->onTopRegion->isVisible() ? "Visible" : "Hidden") << "]"; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; } if (context->leftMouseButton) { txt = "Left Mouse Button Down"; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; } if (context->rightMouseButton) { txt = "Right Mouse Button Down"; drawText(nvg, 5, yoffset, txt.c_str(), FontStyle::Outline, Color(255), Color(64, 64, 64)); yoffset += 16; } if (context->hasFocus) { nvgBeginPath(nvg); nvgLineCap(nvg, NVG_ROUND); nvgStrokeWidth(nvg, 2.0f); nvgStrokeColor(nvg, Color(255, 255, 255, 255)); nvgMoveTo(nvg, context->cursorPosition.x - cr, context->cursorPosition.y); nvgLineTo(nvg, context->cursorPosition.x + cr, context->cursorPosition.y); nvgMoveTo(nvg, context->cursorPosition.x, context->cursorPosition.y - cr); nvgLineTo(nvg, context->cursorPosition.x, context->cursorPosition.y + cr); nvgStroke(nvg); nvgBeginPath(nvg); nvgFillColor(nvg, Color(255, 255, 255, 255)); nvgCircle(nvg, context->cursorPosition.x, context->cursorPosition.y, 3.0f); nvgFill(nvg); nvgBeginPath(nvg); nvgFillColor(nvg, Color(255, 64, 32, 255)); nvgCircle(nvg, context->cursorPosition.x, context->cursorPosition.y, 1.5f); nvgFill(nvg); } } nvgEndFrame(nvg); }
void Screen::drawWidgets() { if (!mVisible) return; Vector2i oldFBSize(mFBSize); glfwMakeContextCurrent(mGLFWWindow); glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]); glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]); glViewport(0, 0, mFBSize[0], mFBSize[1]); if (oldFBSize != mFBSize) framebufferSizeChanged(); /* Calculate pixel ratio for hi-dpi devices. */ mPixelRatio = (float) mFBSize[0] / (float) mSize[0]; nvgBeginFrame(mNVGContext, mSize[0], mSize[1], mPixelRatio); nvgTranslate(mNVGContext, -2, -2); draw(mNVGContext); double elapsed = glfwGetTime() - mLastInteraction; if (elapsed > 0.5f) { /* Draw tooltips */ const Widget *widget = findWidget(mMousePos); if (widget && !widget->tooltip().empty()) { int tooltipWidth = 150; float bounds[4]; nvgFontFace(mNVGContext, "sans"); nvgFontSize(mNVGContext, 15.0f); nvgTextAlign(mNVGContext, NVG_ALIGN_CENTER | NVG_ALIGN_TOP); nvgTextLineHeight(mNVGContext, 1.1f); Vector2i pos = widget->absolutePosition() + Vector2i(widget->width() / 2, widget->height() + 10); nvgTextBoxBounds(mNVGContext, pos.x(), pos.y(), tooltipWidth, widget->tooltip().c_str(), nullptr, bounds); nvgGlobalAlpha(mNVGContext, std::min(1.0, 2 * (elapsed - 0.5f)) * 0.8); nvgBeginPath(mNVGContext); nvgFillColor(mNVGContext, Color(0, 255)); int h = (bounds[2] - bounds[0]) / 2; nvgRoundedRect(mNVGContext, bounds[0] - 4 - h, bounds[1] - 4, (int) (bounds[2] - bounds[0]) + 8, (int) (bounds[3] - bounds[1]) + 8, 3); int px = (int) ((bounds[2] + bounds[0]) / 2) - h; nvgMoveTo(mNVGContext, px, bounds[1] - 10); nvgLineTo(mNVGContext, px + 7, bounds[1] + 1); nvgLineTo(mNVGContext, px - 7, bounds[1] + 1); nvgFill(mNVGContext); nvgFillColor(mNVGContext, Color(255, 255)); nvgFontBlur(mNVGContext, 0.0f); nvgTextBox(mNVGContext, pos.x() - h, pos.y(), tooltipWidth, widget->tooltip().c_str(), nullptr); } } nvgEndFrame(mNVGContext); }
int main() { constexpr auto width = 1200u; constexpr auto height = 800u; // init ny app auto& backend = ny::Backend::choose(); if(!backend.vulkan()) { dlg_error("ny backend has no vulkan support!"); return 0; } auto ac = backend.createAppContext(); // basic vpp init auto iniExtensions = ac->vulkanExtensions(); iniExtensions.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME); vk::ApplicationInfo appInfo ("vpp-intro", 1, "vpp", 1, VK_API_VERSION_1_0); vk::InstanceCreateInfo instanceInfo; instanceInfo.pApplicationInfo = &appInfo; instanceInfo.enabledExtensionCount = iniExtensions.size(); instanceInfo.ppEnabledExtensionNames = iniExtensions.data(); #ifdef WithLayers constexpr auto layer = "VK_LAYER_LUNARG_standard_validation"; instanceInfo.enabledLayerCount = 1; instanceInfo.ppEnabledLayerNames = &layer; #endif vpp::Instance instance(instanceInfo); #ifdef WithLayers vpp::DebugCallback debugCallback(instance); #endif // ny init auto run = true; auto listener = MyWindowListener {}; listener.run = &run; auto vkSurface = vk::SurfaceKHR {}; auto ws = ny::WindowSettings {}; ws.surface = ny::SurfaceType::vulkan; ws.listener = &listener; ws.size = {width, height}; ws.vulkan.instance = (VkInstance) instance.vkHandle(); ws.vulkan.storeSurface = &(std::uintptr_t&) (vkSurface); auto wc = ac->createWindowContext(ws); // further vpp init const vpp::Queue* presentQueue; vpp::Device device(instance, vkSurface, presentQueue); vpp::Swapchain swapchain(device, vkSurface, {width, height}, {}); // vvg setup auto nvgContext = vvg::createContext(swapchain); auto font = nvgCreateFont(nvgContext, "sans", "Roboto-Regular.ttf"); using Clock = std::chrono::high_resolution_clock; auto lastFrameTimer = Clock::now(); unsigned int framesCount = 0; std::string fpsString = "420 fps"; // main loop while(run) { if(!ac->dispatchEvents()) break; nvgBeginFrame(nvgContext, width, height, width / (float) height); nvgBeginPath(nvgContext); nvgMoveTo(nvgContext, 10, 10); nvgLineTo(nvgContext, 10, 400); nvgLineTo(nvgContext, 100, 400); nvgQuadTo(nvgContext, 100, 50, 400, 120); nvgLineTo(nvgContext, 450, 10); nvgClosePath(nvgContext); nvgFillColor(nvgContext, nvgRGBAf(0.5, 0.8, 0.7, 1.0)); nvgFill(nvgContext); nvgBeginPath(nvgContext); nvgFontFaceId(nvgContext, font); nvgFontSize(nvgContext, 100.f); nvgFontBlur(nvgContext, .8f); nvgFillColor(nvgContext, nvgRGBAf(1.0, 1.0, 1.0, 1.0)); nvgTextBox(nvgContext, 200, 200, width - 200, "Hello Vulkan Vector Graphics World", nullptr); nvgFontSize(nvgContext, 30.f); nvgFontBlur(nvgContext, .2f); nvgText(nvgContext, 10, height - 20, fpsString.c_str(), nullptr); nvgBeginPath(nvgContext); nvgRect(nvgContext, 700, 400, 300, 300); nvgPathWinding(nvgContext, NVG_HOLE); nvgRect(nvgContext, 750, 450, 50, 50); // auto paint = nvgRadialGradient(nvgContext, 750, 425,20, 50, nvgRGB(0, 0, 200), nvgRGB(200, 200, 0)); // auto paint = nvgRadialGradient(nvgContext, 0.0, 0.0, 0.2, 100.0, nvgRGB(0, 0, 200), nvgRGB(200, 200, 0)); auto paint = nvgLinearGradient(nvgContext, 700, 400, 800, 450, nvgRGB(0, 0, 200), nvgRGB(200, 200, 0)); nvgFillPaint(nvgContext, paint); // nvgFillColor(nvgContext, nvgRGBA(200, 200, 0, 200)); nvgClosePath(nvgContext); nvgFill(nvgContext); nvgEndFrame(nvgContext); // only refresh frame timer every second framesCount++; if(Clock::now() - lastFrameTimer >= std::chrono::seconds(1)) { fpsString = std::to_string(framesCount) + " fps"; lastFrameTimer = Clock::now(); framesCount = 0; } } vvg::destroyContext(*nvgContext); }
int main() { GLFWwindow* window; NVGcontext *vg; if(!glfwInit()) { fprintf(stderr, "Failed to start glfw\n"); return -1; } //Set OpenGL Revision glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); //Create Window window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); if (!window) { fprintf(stderr, "Failed to make window\n"); glfwTerminate(); return -1; } //Setup Context glfwSetKeyCallback(window, key); glfwSetMouseButtonCallback(window, mouse_event); glfwSetCursorPosCallback(window, mouse_hover); glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { fprintf(stderr, "Failed to start GLEW\n"); return -1; } vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); nvgCreateFont(vg, "sans", "Roboto-Regular.ttf"); nvgCreateFont(vg, "icons", "entypo.ttf"); glfwSwapInterval(0); glfwSetTime(0); long prevt = glfwGetTime(); while (!glfwWindowShouldClose(window)) { double mx, my; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; glfwGetCursorPos(window, &mx, &my); glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; // Update and render glViewport(0, 0, fbWidth, fbHeight); //glClearColor(0x06/255.0, 0x27/255.0, 0x37/255.0, 1.0f); glClearColor(0, 0, 0, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); //#define M_PI 3.14159 nvgBeginFrame(vg, winWidth, winHeight, pxRatio); //float in[100] = {0.0, 0.0, 0.5, 0.2, 0.3, 0.7,0.9,0.8,0.0,1.0}; //drawHarmonicPlot(vg, in, 100, 0,0,winWidth, winHeight); //drawPowButton(vg, 0,0,winWidth, winHeight); drawPanDial(vg, "100", 0.4, 0, 0, winWidth, winHeight); //nvgBeginPath(vg); //float cx = 30.0, cy = 30.0, h = 80; //nvgArc(vg, cx, cy, 0.4*h, 0, 1.0/2.0*M_PI, 1); //nvgArc(vg, cx, cy, 0.2*h, 1.0/2.0*M_PI, 0, 2); //nvgClosePath(vg); //nvgFillColor(vg, nvgRGBA(0x11,0x45,0x75,255)); //nvgFill(vg); nvgEndFrame(vg); // nvgBeginFrame(vg, winWidth, winHeight, pxRatio); // // //viewReverb(vg, 0,0, winWidth, winHeight); // viewModule(vg, "afilter", 0,0,winWidth, winHeight); // //viewLFO(vg, 0,0, winWidth, winHeight); // //viewFilterEnv(vg, 0,0, winWidth, winHeight); // //y x y x y x y x y x // //float in[10] = {0.0, 0.0, 0.5, 0.2, 0.3, 0.7,-0.9,0.8,0.0,1.0}; // //drawEnvEdit(vg, in, 5, 3, 0, 0, winWidth, winHeight); // //#if 0 // dial_t dial = {60, 0, 0, 100, "label"}; // renderDial(vg, dial); // drawButton(vg, "banana", 100, 25, 100, 50); // drawOptButton(vg, "opt", 200, 25, 100, 50); // drawButtonGrid(vg, 4,4, 300, 0, 100, 100); // drawAltDial(vg, 400, 0, 100, 100); // drawGrid(vg, 8, 8, 500, 0, 200, 200); // drawSin(vg, 500, 0, 200, 200); //#endif // // nvgEndFrame(vg); glfwSwapBuffers(window); glfwPollEvents(); } nvgDeleteGL2(vg); glfwTerminate(); return 0; }
JNIEXPORT void JNICALL Java_firststep_internal_NVG_beginFrame (JNIEnv *e, jclass c, jlong ctx, jint wid, jint hgt, jfloat r) { nvgBeginFrame((NVGcontext*)ctx, wid, hgt, r); }