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 }
bool setupGraphics(int _w, int _h) { w = _w; h = _h; printGLString("Version", GL_VERSION); printGLString("Vendor", GL_VENDOR); printGLString("Renderer", GL_RENDERER); printGLString("Extensions", GL_EXTENSIONS); LOGI("setupGraphics(%d, %d)", w, h); vg = nvgCreateGLES2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); glViewport(0, 0, w, h); checkGlError("glViewport"); glClearColor(r,g,b,a); checkGlError("glClearColor"); return true; }
QNanoPainter::QNanoPainter() : m_nvgContext(nullptr) , m_textAlign(QNanoPainter::ALIGN_LEFT) , m_textBaseline(QNanoPainter::BASELINE_ALPHABETIC) , m_devicePixelRatio(1.0) , m_fontSet(false) , m_pixelAlign(QNanoPainter::PIXEL_ALIGN_NONE) , m_pixelAlignText(QNanoPainter::PIXEL_ALIGN_NONE) { // Initialize NanoVG for correct GL version // TODO: Allow to enable/disable NVG_DEBUG, possibly some own general debug define #ifdef QT_OPENGL_ES_2 m_nvgContext = nvgCreateGLES2(NVG_ANTIALIAS | NVG_DEBUG); #else m_nvgContext = nvgCreateGL2(NVG_ANTIALIAS | NVG_DEBUG); #endif Q_ASSERT_X(m_nvgContext, "QNanoPainter::QNanoPainter", "Could not init nanovg!"); }
int main(int argc, char *argv[]) { /* Platform */ int width, height; const char *font_path; gui_size font_height; SDL_Window *win; SDL_GLContext glContext; NVGcontext *vg = NULL; unsigned int started; unsigned int dt; /* GUI */ struct gui_input in; struct gui_font font; struct demo_gui gui; if (argc < 3) { fprintf(stdout,"Missing TTF Font file/height argument: nanovg <path> <height>\n"); exit(EXIT_FAILURE); } font_path = argv[1]; font_height = (gui_size)MAX(0, atoi(argv[2])); /* SDL */ SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); win = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN); glContext = SDL_GL_CreateContext(win); SDL_GetWindowSize(win, &width, &height); /* OpenGL */ glewExperimental = 1; if (glewInit() != GLEW_OK) die("[GLEW] failed setup\n"); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); /* nanovg */ vg = nvgCreateGLES2(NVG_ANTIALIAS|NVG_DEBUG); if (!vg) die("[NVG]: failed to init\n"); nvgCreateFont(vg, "fixed", font_path); nvgFontFace(vg, "fixed"); nvgFontSize(vg, font_height); nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE); /* GUI */ memset(&in, 0, sizeof in); memset(&gui, 0, sizeof gui); gui.memory = malloc(MAX_MEMORY); font.userdata.ptr = vg; nvgTextMetrics(vg, NULL, NULL, &font.height); font.width = font_get_width; init_demo(&gui, &font); while (gui.running) { /* Input */ SDL_Event evt; started = SDL_GetTicks(); gui_input_begin(&in); while (SDL_PollEvent(&evt)) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) key(&in, &evt, gui_false); else if (evt.type == SDL_KEYDOWN) key(&in, &evt, gui_true); else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&in, &evt, gui_true); else if (evt.type == SDL_MOUSEBUTTONUP) btn(&in, &evt, gui_false); else if (evt.type == SDL_MOUSEMOTION) motion(&in, &evt); else if (evt.type == SDL_TEXTINPUT) text(&in, &evt); else if (evt.type == SDL_MOUSEWHEEL) gui_input_scroll(&in, evt.wheel.y); } gui_input_end(&in); /* GUI */ SDL_GetWindowSize(win, &width, &height); run_demo(&gui, &in); /* Draw */ glClearColor(0.4f, 0.4f, 0.4f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); draw(vg, &gui.stack, width, height); gui.ms = SDL_GetTicks() - started; SDL_GL_SwapWindow(win); /* Timing */ dt = SDL_GetTicks() - started; gui.ms = dt; if (dt < DTIME) SDL_Delay(DTIME - dt); } cleanup: /* Cleanup */ free(gui.memory); nvgDeleteGLES2(vg); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(win); SDL_Quit(); return 0; }
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(int argc, char *argv[]) { /* Platform */ int width, height; const char *font_path; zr_size font_height; SDL_Window *win; SDL_GLContext glContext; NVGcontext *vg = NULL; /* GUI */ struct demo_gui gui; if (argc < 2) { fprintf(stdout,"Missing TTF Font file argument: gui <path>\n"); exit(EXIT_FAILURE); } font_path = argv[1]; font_height = 10; /* SDL */ SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); win = SDL_CreateWindow("Demo", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN); glContext = SDL_GL_CreateContext(win); SDL_GetWindowSize(win, &width, &height); /* OpenGL */ glewExperimental = 1; if (glewInit() != GLEW_OK) die("[GLEW] failed setup\n"); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); /* nanovg */ vg = nvgCreateGLES2(NVG_ANTIALIAS|NVG_DEBUG); if (!vg) die("[NVG]: failed to init\n"); nvgCreateFont(vg, "fixed", font_path); nvgFontFace(vg, "fixed"); nvgFontSize(vg, font_height); nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE); /* GUI */ memset(&gui, 0, sizeof gui); zr_command_queue_init_fixed(&gui.queue, calloc(MAX_MEMORY, 1), MAX_MEMORY); gui.font.userdata = zr_handle_ptr(vg); gui.font.width = font_get_width; nvgTextMetrics(vg, NULL, NULL, &gui.font.height); init_demo(&gui); while (gui.running) { /* Input */ SDL_Event evt; zr_input_begin(&gui.input); while (SDL_PollEvent(&evt)) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) key(&gui.input, &evt, zr_false); else if (evt.type == SDL_KEYDOWN) key(&gui.input, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.input, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.input, &evt, zr_false); else if (evt.type == SDL_MOUSEMOTION) motion(&gui.input, &evt); else if (evt.type == SDL_TEXTINPUT) text(&gui.input, &evt); else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&gui.input,(float)evt.wheel.y); } zr_input_end(&gui.input); /* GUI */ SDL_GetWindowSize(win, &width, &height); run_demo(&gui); /* Draw */ glClearColor(0.4f, 0.4f, 0.4f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); draw(vg, &gui.queue, width, height); SDL_GL_SwapWindow(win); } cleanup: /* Cleanup */ free(zr_buffer_memory(&gui.queue.buffer)); nvgDeleteGLES2(vg); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(win); SDL_Quit(); return 0; }
int main(int argc, char *argv[]) { int x,y,width, height; SDL_Window *win; SDL_GLContext glContext; NVGcontext *vg = NULL; int running = 1; unsigned int started; unsigned int dt; struct zr_user_font font; struct file_browser browser; const char *font_path; int icon_sheet; font_path = argv[1]; if (argc < 2) die("missing argument!: <font> <icons>"); /* SDL */ SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); win = SDL_CreateWindow("File Explorer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN); glContext = SDL_GL_CreateContext(win); SDL_GetWindowSize(win, &width, &height); SDL_GetWindowPosition(win, &x, &y); /* OpenGL */ glewExperimental = 1; if (glewInit() != GLEW_OK) die("[GLEW] failed setup\n"); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); /* nanovg */ vg = nvgCreateGLES2(NVG_ANTIALIAS|NVG_DEBUG); if (!vg) die("[NVG]: failed to init\n"); nvgCreateFont(vg, "fixed", font_path); nvgFontFace(vg, "fixed"); nvgFontSize(vg, 14); nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE); /* GUI */ memset(&browser, 0, sizeof browser); font.userdata.ptr = vg; nvgTextMetrics(vg, NULL, NULL, &font.height); font.width = font_get_width; file_browser_init(&browser, vg, &font, width, height); while (running) { /* Input */ SDL_Event evt; started = SDL_GetTicks(); zr_input_begin(&browser.input); while (SDL_PollEvent(&evt)) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) key(&browser.input, &evt, zr_false); else if (evt.type == SDL_KEYDOWN) key(&browser.input, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&browser.input, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONUP) btn(&browser.input, &evt, zr_false); else if (evt.type == SDL_MOUSEMOTION) motion(&browser.input, &evt); else if (evt.type == SDL_TEXTINPUT) text(&browser.input, &evt); else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&browser.input, evt.wheel.y); } zr_input_end(&browser.input); SDL_GetWindowSize(win, &width, &height); running = file_browser_run(&browser, width, height); /* Draw */ glClearColor(0.4f, 0.4f, 0.4f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); draw(vg, &browser.queue, width, height); SDL_GL_SwapWindow(win); } cleanup: /* Cleanup */ free(browser.memory); nvgDeleteGLES2(vg); SDL_GL_DeleteContext(glContext); SDL_DestroyWindow(win); SDL_Quit(); return 0; }