void Screen::initialize(GLFWwindow *window, bool shutdownGLFWOnDestruct) { mGLFWWindow = window; mShutdownGLFWOnDestruct = shutdownGLFWOnDestruct; glfwGetWindowSize(mGLFWWindow, &mSize[0], &mSize[1]); glfwGetFramebufferSize(mGLFWWindow, &mFBSize[0], &mFBSize[1]); #ifdef NDEBUG mNVGContext = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS); #else mNVGContext = nvgCreateGL3(NVG_STENCIL_STROKES | NVG_ANTIALIAS | NVG_DEBUG); #endif mVisible = glfwGetWindowAttrib(window, GLFW_VISIBLE) != 0; mTheme = new Theme(mNVGContext); mMousePos = Vector2i::Zero(); mMouseState = mModifiers = 0; mDragActive = false; mLastInteraction = glfwGetTime(); mProcessEvents = true; mBackground = Vector3f(0.3f, 0.3f, 0.32f); __nanogui_screens[mGLFWWindow] = this; for (int i=0; i < (int) Cursor::CursorCount; ++i) mCursors[i] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR + i); }
int main() { glfwInit(); glfwDefaultWindowHints(); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); GLFWwindow *window = glfwCreateWindow(800, 600, "nanovg Demo", nullptr, nullptr); NVGcontext *vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); nvgBeginFrame(vg, 800, 600, 1); nvgBeginPath(vg); nvgRoundedRect(vg, 12, 12, 800-12*2, 600-12*2, 4); nvgFillColor(vg, nvgRGBA(255, 255, 255, 192)); nvgFill(vg); nvgEndFrame(vg); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); return 0; }
Gui::Gui(size_t _width, size_t _height) : width(_width), height(_height) { vg = nvgCreateGL3(width, height, NVG_ANTIALIAS); assert(vg); data.init(vg); fps.setContext(vg); Label::for_each([this](Label& l) { l.setContext(vg); }); }
Context createContextGL(bool antiAlias, bool stencilStrokes) { int flags = (antiAlias ? NVG_ANTIALIAS : 0) | (stencilStrokes ? NVG_STENCIL_STROKES : 0); #if defined(NANOVG_GLES3) return { nvgCreateGLES3(flags), nvgDeleteGLES3 }; #elif defined(NANOVG_GLES2) return { nvgCreateGLES2(flags), nvgDeleteGLES2 }; #else return { nvgCreateGL3(flags), nvgDeleteGL3 }; #endif }
AlloyContext::AlloyContext(int width, int height, const std::string& title, const Theme& theme) : nvgContext(nullptr), window(nullptr), theme(theme) { threadId = std::this_thread::get_id(); if (glfwInit() != GL_TRUE) { throw std::runtime_error("Could not initialize GLFW."); } glfwSetErrorCallback( [](int error, const char* desc) { std::cout << "GLFW Error [" << error << "] " << desc << std::endl; }); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); glfwWindowHint(GLFW_VISIBLE, 0); window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL); if (!window) { glfwTerminate(); throw std::runtime_error("Could not create window."); } glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { throw std::runtime_error("Could not initialize GLEW."); } glGetError(); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); viewSize = int2(width, height); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); glfwWindowHint(GLFW_VISIBLE, 0); offscreenWindow = glfwCreateWindow(width, height, "Offscreen", NULL, NULL); if (!offscreenWindow) { glfwTerminate(); throw std::runtime_error("Could not create window."); } glfwMakeContextCurrent(offscreenWindow); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { throw std::runtime_error("Could not initialize GLEW."); } glGetError(); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glViewport(0, 0, width, height); glfwMakeContextCurrent(window); nvgContext = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); const float2 TextureCoords[6] = { float2(1.0f, 0.0f), float2(0.0f, 0.0f), float2(0.0f, 1.0f), float2(0.0f, 1.0f), float2(1.0f, 1.0f), float2( 1.0f, 0.0f) }; const float3 PositionCoords[6] = { float3(1.0f, 1.0f, 0.0f), float3(0.0f, 1.0f, 0.0f), float3(0.0f, 0.0f, 0.0f), float3(0.0f, 0.0f, 0.0f), float3(1.0f, 0.0f, 0.0f), float3(1.0f, 1.0f, 0.0f) }; glGenVertexArrays(1, &vaoImageOnScreen.vao); glBindVertexArray(vaoImageOnScreen.vao); glGenBuffers(1, &vaoImageOnScreen.positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOnScreen.positionBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, PositionCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &vaoImageOnScreen.uvBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOnScreen.uvBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 6, TextureCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glfwHideWindow(offscreenWindow); glfwMakeContextCurrent(offscreenWindow); glGenVertexArrays(1, &vaoImageOffScreen.vao); glBindVertexArray(vaoImageOffScreen.vao); glGenBuffers(1, &vaoImageOffScreen.positionBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOffScreen.positionBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, PositionCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &vaoImageOffScreen.uvBuffer); glBindBuffer(GL_ARRAY_BUFFER, vaoImageOffScreen.uvBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * 6, TextureCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); glfwMakeContextCurrent(window); int widthMM, heightMM; GLFWmonitor* monitor = glfwGetPrimaryMonitor(); if (monitor == nullptr) throw std::runtime_error("Could not find monitor."); const GLFWvidmode* mode = glfwGetVideoMode(monitor); if (mode == nullptr) throw std::runtime_error("Could not find video monitor."); glfwGetMonitorPhysicalSize(monitor, &widthMM, &heightMM); dpmm = double2(mode->width / (double) widthMM, mode->height / (double) heightMM); int winWidth, winHeight, fbWidth, fbHeight; glfwGetWindowSize(window, &winWidth, &winHeight); glfwGetFramebufferSize(window, &fbWidth, &fbHeight); // Calculate pixel ration for hi-dpi devices. screenSize = int2(winWidth,winHeight); viewSize = int2(fbWidth,fbHeight); pixelRatio = pixel((float) fbWidth / (float) winWidth); lastAnimateTime = std::chrono::steady_clock::now(); lastCursorTime = std::chrono::steady_clock::now(); lastUpdateTime = std::chrono::steady_clock::now(); cursor = &Cursor::Normal; }
int main() { GLFWwindow* window; UIcontext *uictx; uictx = uiCreateContext(4096, 1<<20); uiMakeCurrent(uictx); uiSetHandler(ui_handler); if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } 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); window = glfwCreateWindow(650, 650, "OUI Blendish Demo", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwSetCharCallback(window, charevent); glfwSetCursorPosCallback(window, cursorpos); glfwSetMouseButtonCallback(window, mousebutton); glfwSetScrollCallback(window, scrollevent); 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 //_vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); _vg = nvgCreateGL3(NVG_ANTIALIAS); if (_vg == NULL) { printf("Could not init nanovg.\n"); return -1; } init(_vg); printf("sizeof(UIitem)=%lu\n", sizeof(UIitem)); glfwSwapInterval(0); glfwSetTime(0); double c = 0.0; int total = 0; int peak_items = 0; unsigned int peak_alloc = 0; 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(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); double t = glfwGetTime(); nvgBeginFrame(_vg, winWidth, winHeight, pxRatio); draw(_vg, winWidth, winHeight); peak_items = (peak_items > uiGetItemCount())?peak_items:uiGetItemCount(); peak_alloc = (peak_alloc > uiGetAllocSize())?peak_alloc:uiGetAllocSize(); nvgEndFrame(_vg); double t2 = glfwGetTime(); c += (t2 - t); total++; if (total > (1*60)) { printf("%fms\n", (c / (double)total)*1000.0); total = 0; c = 0.0; } glfwSwapBuffers(window); glfwPollEvents(); } printf("Peak item count: %i (%lu bytes)\nPeak allocated handles: %u bytes\n", peak_items, peak_items * sizeof(UIitem), peak_alloc); uiDestroyContext(uictx); nvgDeleteGL3(_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; } #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); 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, 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); glEnable(GL_DEPTH_TEST); // 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; }
NVGRenderer::NVGRenderer() : m_inFrame(false) { m_context = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); }
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; 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 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; }
void init() { #ifdef GRAPHICS if (!glfwInit()) { LOG("Failed to init GLFW."); exit(-1); } initGraph(&fps_graph, GRAPH_RENDER_FPS, "Frame Time"); initGraph(&cpu_graph, GRAPH_RENDER_MS, "CPU Time"); initGraph(&gpu_graph, GRAPH_RENDER_MS, "GPU Time"); glfwSetErrorCallback([](int error, const char* desc) {LOG("GLFW error %d: %s\n", error, desc); }); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); int height = glfwGetVideoMode(glfwGetPrimaryMonitor())->height*0.6, width = height*double(world::width) / double(world::height); window = glfwCreateWindow(width, height, "PedestrianSimulator", NULL, NULL); if (!window) { glfwTerminate(); exit(-1); } glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) { NVG_NOTUSED(scancode); NVG_NOTUSED(mods); if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GL_TRUE); }); glfwSetMouseButtonCallback(window, [](GLFWwindow* wind, int button, int action, int mods){ static vec_t start_pos; vecd_t pos; glfwGetCursorPos(wind, &pos.x, &pos.y); vec_t world_pos = screen2world({ pos.x, pos.y }); if (action == GLFW_RELEASE) { if (button == GLFW_MOUSE_BUTTON_1) { world::add_objective({ world_pos, 0 }); } else if (button == GLFW_MOUSE_BUTTON_2) { world::add_objective({ world_pos, 1 }); } else if (button==GLFW_MOUSE_BUTTON_3) LOG("click at screen (%.2lf, %.2lf), world (%.2f, %.2f)", pos.x, pos.y, world_pos.x, world_pos.y); } else if (action == GLFW_PRESS) { if (button == GLFW_MOUSE_BUTTON_2) { start_pos = world_pos; } } }); glfwMakeContextCurrent(window); glewExperimental = GL_TRUE; if (glewInit() != GLEW_OK) { LOG("Could not init glew.\n"); exit(-1); } glGetError(); vg = nvgCreateGL3(512, 512, NVG_ANTIALIAS | NVG_STENCIL_STROKES); if (vg == NULL) { LOG("Could not init nanovg.\n"); exit(-1); } nvgCreateFont(vg, "sans", "roboto.ttf"); glfwSwapInterval(0); initGPUTimer(&gpu_timer); glfwSetTime(0); prev_time = glfwGetTime(); #endif }
int main() { if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } glfwSetErrorCallback(errorcb); #ifdef __APPLE__ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #else glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); #endif #ifdef DEMO_MSAA glfwWindowHint(GLFW_SAMPLES, 4); #endif window = glfwCreateWindow(600, 400, "Alpha clock", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwSetWindowSizeCallback(window, windowSize); glfwMakeContextCurrent(window); if (!glInit()) { printf("Error: can't initialize GL3 API\n"); glfwTerminate(); return -1; } #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; } glfwSwapInterval(0); glfwSetTime(0); nvgCreateFont(vg, "bold", "fonts/Walkway_Bold.ttf"); nvgCreateFont(vg, "black", "fonts/Walkway_Black.ttf"); while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; glfwGetCursorPos(window, &mx, &my); draw(); glfwPollEvents(); } nvgDeleteGL3(vg); glfwTerminate(); return 0; }
NVGcontext * make_nanovg_context(int flags) { return nvgCreateGL3(flags); }
JNIEXPORT jlong JNICALL Java_firststep_internal_NVG_create (JNIEnv *e, jclass c, jint flags) { return (jlong)(NVGcontext*) nvgCreateGL3(flags); }