int main(int argc, char **argv) { if (!glfwInit()) { printf("Failed to init GLFW."); return -1; } glfwSetErrorCallback(errorcb); glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1); GLFWwindow* window = glfwCreateWindow(1024, 768, "scanner", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwSetKeyCallback(window, key); glfwSetCharCallback(window, charevent); glfwSetCursorPosCallback(window, cursorpos); glfwSetMouseButtonCallback(window, mousebutton); glfwSetScrollCallback(window, scrollevent); glfwMakeContextCurrent(window); glfwSwapInterval(1); // vsync glewExperimental = GL_TRUE; if(glewInit() != GLEW_OK) { printf("Could not init glew.\n"); return -1; } glGetError(); // GLEW generates GL error because it calls glGetString(GL_EXTENSIONS). struct NVGcontext* vg = nvgCreateGL2(NVG_ANTIALIAS); if (vg == NULL) { printf("Could not init nanovg.\n"); return -1; } UIcontext *uictx = uiCreateContext(4096, 1 << 20); uiMakeCurrent(uictx); uiSetHandler(ui_handler); uiInit(vg, "oui-blendish/DejaVuSans.ttf", "oui-blendish/blender_icons16.png"); gui_init(vg); glfwSetTime(0); while (!glfwWindowShouldClose(window)) { double mx, my; glfwGetCursorPos(window, &mx, &my); int winWidth, winHeight; glfwGetWindowSize(window, &winWidth, &winHeight); int fbWidth, fbHeight; glfwGetFramebufferSize(window, &fbWidth, &fbHeight); float pxRatio = (float)fbWidth / (float)winWidth; gui_frame(vg, winWidth, winHeight, pxRatio, glfwGetTime()); glfwSwapBuffers(window); glfwPollEvents(); } gui_quit(vg); uiDestroyContext(uictx); nvgDeleteGL2(vg); glfwTerminate(); return 0; }
int column(int parent) { int item = uiItem(); uiSetHandler(item, columnhandler, UI_APPEND); uiAppend(parent, item); return item; }
int row(int parent) { int item = uiItem(); uiSetHandler(item, rowhandler, UI_APPEND); uiAppend(parent, item); return item; }
int vgroup(int parent) { int item = uiItem(); UIData *data = (UIData *)uiAllocData(item, sizeof(UIData)); data->subtype = ST_COLUMN; uiSetHandler(item, vgrouphandler, UI_APPEND); uiAppend(parent, item); return item; }
int radio(int parent, UIhandle handle, int iconid, const char *label, int *value) { int item = uiItem(); uiSetHandle(item, handle); uiSetSize(item, label?0:BND_TOOL_WIDTH, BND_WIDGET_HEIGHT); { UIRadioData *data = (UIRadioData *)uiAllocData(item, sizeof(UIRadioData)); data->head.subtype = ST_RADIO; data->iconid = iconid; data->label = label; data->value = value; } uiSetHandler(item, radiohandler, UI_BUTTON0_DOWN); uiAppend(parent, item); return item; }
int textbox(int parent, UIhandle handle, char *text, int maxsize) { int item = uiItem(); UITextData *data; uiSetHandle(item, handle); uiSetSize(item, 0, BND_WIDGET_HEIGHT); uiSetHandler(item, textboxhandler, UI_BUTTON0_DOWN | UI_KEY_DOWN | UI_CHAR); // store some custom data with the button that we use for styling // and logic, e.g. the pointer to the data we want to alter. data = (UITextData *)uiAllocData(item, sizeof(UITextData)); data->head.subtype = ST_TEXT; data->text = text; data->maxsize = maxsize; uiAppend(parent, item); return item; }
int check(int parent, UIhandle handle, const char *label, int *option) { // create new ui item int item = uiItem(); // set persistent handle for item that is used // to track activity over time uiSetHandle(item, handle); // set size of wiget; horizontal size is dynamic, vertical is fixed uiSetSize(item, 0, BND_WIDGET_HEIGHT); // attach event handler e.g. demohandler above uiSetHandler(item, checkhandler, UI_BUTTON0_DOWN); { // store some custom data with the button that we use for styling UICheckData *data = (UICheckData *)uiAllocData(item, sizeof(UICheckData)); data->head.subtype = ST_CHECK; data->label = label; data->option = option; } uiAppend(parent, item); return item; }
int slider(int parent, UIhandle handle, const char *label, float *progress) { // create new ui item int item = uiItem(); // set persistent handle for item that is used // to track activity over time uiSetHandle(item, handle); // set size of wiget; horizontal size is dynamic, vertical is fixed uiSetSize(item, 0, BND_WIDGET_HEIGHT); // attach our slider event handler and capture two classes of events uiSetHandler(item, sliderhandler, UI_BUTTON0_DOWN | UI_BUTTON0_CAPTURE); { // store some custom data with the button that we use for styling // and logic, e.g. the pointer to the data we want to alter. UISliderData *data = (UISliderData *)uiAllocData(item, sizeof(UISliderData)); data->head.subtype = ST_SLIDER; data->label = label; data->progress = progress; } uiAppend(parent, item); return item; }
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; }