int loadDemoData(struct NVGcontext* vg, struct DemoData* data) { int i; if (vg == NULL) return -1; for (i = 0; i < 12; i++) { char file[128]; snprintf(file, 128, "../example/images/image%d.jpg", i+1); data->images[i] = nvgCreateImage(vg, file); if (data->images[i] == 0) { printf("Could not load %s.\n", file); return -1; } } data->fontIcons = nvgCreateFont(vg, "icons", "../example/entypo.ttf"); if (data->fontIcons == -1) { printf("Could not add font icons.\n"); return -1; } data->fontNormal = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf"); if (data->fontNormal == -1) { printf("Could not add font italic.\n"); return -1; } data->fontBold = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf"); if (data->fontBold == -1) { printf("Could not add font bold.\n"); return -1; } return 0; }
//Initialize the renderer bool init() { m_vg = nvgCreateGL2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); m_font = nvgCreateFont(m_vg, "sans", "Roboto-Regular.ttf"); nvgCreateFont(m_vg, "sans-bold", "Roboto-Bold.ttf"); nvgCreateFont(m_vg, "sans-light", "Roboto-Light.ttf"); m_inited = true; return true; }
bool Application::initGL(){ NVGcontext* context = NVG::instance()->context(); int res = nvgCreateFont(context, "sans", RESOURCE_PATH("Roboto-Regular.ttf").c_str()); if (res == -1) { printf("Could not add font regular.\n"); return false; } res = nvgCreateFont(context, "sans-bold", RESOURCE_PATH("Roboto-Bold.ttf").c_str()); if (res == -1) { printf("Could not add font bold.\n"); return false; } return true; }
int loadFonts(NVGcontext* vg) { int font; font = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf"); if (font == -1) { printf("Could not add font regular.\n"); return -1; } font = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf"); if (font == -1) { printf("Could not add font bold.\n"); return -1; } return 0; }
void setup() { size(640, 320); glfwSetKeyCallback(window, keyevent); glfwSetCharCallback(window, charevent); uictx = uiCreateContext(); uiMakeCurrent(uictx); bndSetFont(nvgCreateFont(vg, "system", "../3rdparty/blendish/DejaVuSans.ttf")); bndSetIconImage(nvgCreateImage(vg, "../3rdparty/blendish/blender_icons16.png", NVG_IMAGE_GENERATE_MIPMAPS)); }
int loadDemoData(struct NVGcontext* vg, struct DemoData* data) { for (uint32_t ii = 0; ii < 12; ++ii) { char file[128]; bx::snprintf(file, 128, "images/image%d.jpg", ii+1); data->images[ii] = nvgCreateImage(vg, file, 0); if (data->images[ii] == 0) { printf("Could not load %s.\n", file); return -1; } } data->fontIcons = nvgCreateFont(vg, "icons", "font/entypo.ttf"); if (data->fontIcons == -1) { printf("Could not add font icons.\n"); return -1; } data->fontNormal = nvgCreateFont(vg, "sans", "font/roboto-regular.ttf"); if (data->fontNormal == -1) { printf("Could not add font italic.\n"); return -1; } data->fontBold = nvgCreateFont(vg, "sans-bold", "font/roboto-bold.ttf"); if (data->fontBold == -1) { printf("Could not add font bold.\n"); return -1; } return 0; }
JNIEXPORT jint JNICALL Java_firststep_internal_NVG_createFont (JNIEnv *e, jclass c, jlong ctx, jstring jname, jstring jpath) { checkJO(jname,"\ncreateFont was passed a NULL name string\n"); checkJO(jpath,"\ncreateFont was passed a NULL path string\n"); const char *name = (*e)->GetStringUTFChars(e, jname, 0); const char *path = (*e)->GetStringUTFChars(e, jpath, 0); int r = nvgCreateFont((NVGcontext*)ctx, name, path); (*e)->ReleaseStringUTFChars(e, jname, name); (*e)->ReleaseStringUTFChars(e, jpath, path); return r; }
void init(NVGcontext *vg) { bndSetFont(nvgCreateFont(vg, "system", "DejaVuSans.ttf")); bndSetIconImage(nvgCreateImage(vg, "blender_icons16.png", 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; }
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() { 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; }
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; }
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; }
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; }
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 }
Font::Font(const std::string& name, const std::string& file, AlloyContext* context) : nvg(context->nvgContext), handle(0), name(name), file(file) { handle = nvgCreateFont(nvg, name.c_str(), file.c_str()); }
void init(NVGcontext* vg) { fontNormal = nvgCreateFont(vg, "sans", "assets/Roboto-Regular.ttf"); assert(fontNormal != -1); }