123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- //
- // Copyright (c) 2013 Mikko Mononen memon@inside.org
- //
- // This software is provided 'as-is', without any express or implied
- // warranty. In no event will the authors be held liable for any damages
- // arising from the use of this software.
- // Permission is granted to anyone to use this software for any purpose,
- // including commercial applications, and to alter it and redistribute it
- // freely, subject to the following restrictions:
- // 1. The origin of this software must not be misrepresented; you must not
- // claim that you wrote the original software. If you use this software
- // in a product, an acknowledgment in the product documentation would be
- // appreciated but is not required.
- // 2. Altered source versions must be plainly marked as such, and must not be
- // misrepresented as being the original software.
- // 3. This notice may not be removed or altered from any source distribution.
- //
- #include <stdio.h>
- #ifdef NANOVG_GLEW
- # include <GL/glew.h>
- #endif
- #ifdef __APPLE__
- # define GLFW_INCLUDE_GLCOREARB
- #endif
- #ifdef NANOVG_GLAD
- # include <glad/glad.h>
- #else
- # define GLFW_INCLUDE_GLEXT
- #endif
- #include <GLFW/glfw3.h>
- #ifndef DEMO_ANTIALIAS
- # define DEMO_ANTIALIAS 1
- #endif
- #ifndef DEMO_STENCIL_STROKES
- # define DEMO_STENCIL_STROKES 1
- #endif
- #ifndef DEMO_MSAA
- # define DEMO_MSAA 0
- #endif
- #include "nanovg.h"
- #define NANOVG_GL3_IMPLEMENTATION
- #include "nanovg_gl.h"
- #include "demo.h"
- #include "perf.h"
- void errorcb(int error, const char* desc)
- {
- printf("GLFW error %d: %s\n", error, desc);
- }
- int blowup = 0;
- int screenshot = 0;
- int premult = 0;
- static void key(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);
- if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
- blowup = !blowup;
- if (key == GLFW_KEY_S && action == GLFW_PRESS)
- screenshot = 1;
- if (key == GLFW_KEY_P && action == GLFW_PRESS)
- premult = !premult;
- }
- int main()
- {
- GLFWwindow* window;
- DemoData data;
- NVGcontext* vg = NULL;
- GPUtimer gpuTimer;
- 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
- glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, 1);
- #if DEMO_MSAA
- glfwWindowHint(GLFW_SAMPLES, 4);
- #endif
- window = glfwCreateWindow(1000, 600, "NanoVG Example OpenGL 3.2", NULL, NULL);
- // window = glfwCreateWindow(1000, 600, "NanoVG Example OpenGL 3.2", 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 consumeP it here.
- glGetError();
- #endif
- #ifdef NANOVG_GLAD
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
- printf("Could not init glad.\n");
- return -1;
- }
- #endif
- int flags = 0;
- #ifndef NDEBUG
- flags |= NVG_DEBUG;
- #endif
- #if !DEMO_MSAA && DEMO_ANTIALIAS
- flags |= NVG_ANTIALIAS;
- #endif
- #if DEMO_STENCIL_STROKES
- flags |= NVG_STENCIL_STROKES;
- #endif
- vg = nvgCreateGL3(flags);
- 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);
- 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;
- }
|