this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

adding NanoVG example (not included in CMake, requires NanoVG libs)

+136
+136
demos/nanovg/main.c
··· 1 + #include <SDL2/SDL.h> 2 + #include <SDL2/SDL_gpu.h> 3 + 4 + #define GLEW_STATIC // needed for windows only(?) 5 + #include <GL/glew.h> 6 + 7 + #define NANOVG_GL3_IMPLEMENTATION 8 + #include "nanovg/nanovg.h" 9 + #include "nanovg/nanovg_gl.h" 10 + #include "nanovg/nanovg_gl_utils.h" 11 + 12 + /* Create a GPU_Image from a NanoVG Framebuffer */ 13 + GPU_Image* generateFBO(NVGcontext* _vg, const float _w, const float _h, void (*draw)(NVGcontext*, const float, const float, const float, const float)) { 14 + // GPU_FlushBlitBuffer(); // you may want to call this if you're doing this in the middle of SDL_gpu blitting 15 + NVGLUframebuffer* fb = nvgluCreateFramebuffer(_vg, _w, _h, 0); // IMPORTANT: don't run nvgluDeleteFramebuffer 16 + nvgluBindFramebuffer(fb); 17 + glViewport(0, 0, _w, _h); 18 + glClearColor(0, 0, 0, 0); 19 + glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); 20 + nvgBeginFrame(_vg, _w, _h, 1.0f); 21 + draw(_vg, 0, 0, _w, _h); // call the drawing function that was passed as parameter 22 + nvgEndFrame(_vg); 23 + /* nvgluBindFramebuffer(0); // official documentation says to unbind, but I haven't had issues not doing it */ 24 + GPU_ResetRendererState(); // not calling GPU_ResetRendererState can cause problems with SDL_gpu depending on your order of operations 25 + // IMPORTANT: don't run nvgluDeleteFramebuffer, GPU_CreateImageUsingTexture takes the handle 26 + return GPU_CreateImageUsingTexture(fb->texture, false); // should take_ownership be true? 27 + } 28 + 29 + /* Simple Drawing Example */ 30 + void drawNVG(NVGcontext* _vg, const float _x, const float _y, const float _w, const float _h) { 31 + const float square_r = 5.0f; 32 + nvgBeginPath(_vg); 33 + nvgRoundedRect(_vg, _x, _y, _w, _h, square_r); 34 + NVGpaint bg_paint = nvgLinearGradient(_vg, _x, _y, _x+_w, _y+_h, nvgRGBA(255, 255, 255, 255), nvgRGBA(255, 255, 255, 155)); 35 + nvgFillPaint(_vg, bg_paint); 36 + nvgFill(_vg); 37 + } 38 + 39 + /* draw something that takes some awhile */ 40 + void drawComplexNVG(NVGcontext* _vg, const float _x, const float _y, const float _w, const float _h) { 41 + float x = _x; 42 + float y = _y; 43 + nvgBeginPath(_vg); 44 + nvgMoveTo(_vg, x, y); 45 + for (unsigned i = 1; i < 50000; i++) { 46 + nvgBezierTo(_vg, x-10.0f, y+10.0f, x+25, y+25, x,y); 47 + x += 10.0f; 48 + y += 5.0f; 49 + if (x > _w) 50 + x = 0.0f; 51 + if (y > _h) 52 + y = 0.0f; 53 + } 54 + NVGpaint stroke_paint = nvgLinearGradient(_vg, _x, _y, _w, _h, nvgRGBA(255, 255, 255, 20), nvgRGBA(0, 255, 255, 10)); 55 + nvgStrokePaint(_vg, stroke_paint); 56 + nvgStroke(_vg); 57 + } 58 + 59 + /* Can help show STENCIL problems when _arc_radius because concave (convex?) */ 60 + void drawPie(NVGcontext* _vg, const float _x, const float _y, const float _arc_radius) { 61 + const float pie_radius = 100.0f; 62 + nvgBeginPath(_vg); 63 + nvgMoveTo(_vg, _x, _y); 64 + nvgArc(_vg, _x, _y, pie_radius, 0.0f, nvgDegToRad(_arc_radius), NVG_CW); 65 + nvgLineTo(_vg, _x, _y); 66 + nvgFillColor(_vg, nvgRGBA(0xFF,0xFF,0xFF,0xFF)); 67 + nvgFill(_vg); 68 + } 69 + 70 + void main_loop(GPU_Target* _screen, NVGcontext* _vg, const Uint16 _screen_w, const Uint16 _screen_h) { 71 + const float px_ratio = (float)_screen_w / (float)_screen_w; // spoilers: it's 1.0f 72 + 73 + GPU_Rect fbo_simple_rect = { 65.0f, 10.0f, 50.0f, 50.0f}; // Blitting Destination 74 + GPU_Image* fbo_simple = generateFBO(_vg, fbo_simple_rect.w, fbo_simple_rect.h, drawNVG); 75 + 76 + GPU_Rect fbo_complex_rect = { 0.0f, 0.0f, _screen_w, _screen_h}; // Blitting Destination 77 + GPU_Image* fbo_complex = generateFBO(_vg, fbo_complex_rect.w, fbo_complex_rect.h, drawComplexNVG); 78 + 79 + float arc_radius = 0.0f; 80 + 81 + SDL_Event event; 82 + bool loop = true; 83 + do { 84 + /* SDL Event Handling */ 85 + while(SDL_PollEvent(&event)) { 86 + if(event.type == SDL_QUIT) 87 + loop = false; 88 + else if(event.type == SDL_KEYDOWN) { 89 + if(event.key.keysym.sym == SDLK_ESCAPE) 90 + loop = false; 91 + } 92 + } 93 + 94 + /* Animation Pass */ 95 + arc_radius += 1.0f; 96 + 97 + /* SDL_gpu + NanoVG Rendering */ 98 + GPU_ClearRGBA(_screen, 0x00, 0x00, 0x00, 0xFF); // GPU_ClearRGBA clears GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 99 + glClear(GL_STENCIL_BUFFER_BIT); // IMPORTANT: GPU_ClearRGBA does not clear GL_STENCIL_BUFFER_BIT 100 + 101 + /* SDL_gpu Blitting */ 102 + GPU_BlitRectX(fbo_complex, NULL, _screen, &fbo_complex_rect, 0.0f, 0.0f, 0.0f, GPU_FLIP_VERTICAL); // IMPORTANT: GPU_BlitRectX is required to use GPU_FLIP_VERTICAL which is required for NVGLUframebuffer data (why???) 103 + GPU_BlitRectX(fbo_simple, NULL, _screen, &fbo_simple_rect, 0.0f, 0.0f, 0.0f, GPU_FLIP_VERTICAL); // IMPORTANT: GPU_BlitRectX is required to use GPU_FLIP_VERTICAL which is required for NVGLUframebuffer data (why???) 104 + 105 + /* NanoVG Section */ 106 + GPU_FlushBlitBuffer(); // IMPORTANT: run GPU_FlushBlitBuffer before nvgBeginFrame 107 + nvgBeginFrame(_vg, _screen_w, _screen_h, px_ratio); // Do your normal NanoVG stuff 108 + drawNVG(_vg, 10.0f, 10.0f, fbo_simple_rect.w, fbo_simple_rect.h); // run our simple drawing code directly 109 + drawPie(_vg, _screen_w/2, _screen_h/2, arc_radius); // drawing the pie chart will break if you don't have a stencil buffer 110 + /* drawComplexNVG(_vg); */ 111 + nvgEndFrame(_vg); // Finish our NanoVG pass 112 + GPU_ResetRendererState(); // IMPORTANT: run GPU_ResetRendererState after nvgEndFrame 113 + 114 + /* Finish */ 115 + GPU_Flip(_screen); // Render to screen 116 + } while (loop); 117 + 118 + /* Loops over, Cleanup */ 119 + GPU_FreeImage(fbo_simple); 120 + GPU_FreeImage(fbo_complex); 121 + } 122 + 123 + int main() { 124 + const Uint16 screen_w = 500; 125 + const Uint16 screen_h = 500; 126 + 127 + /* Init SDL and our renderer, no error handling! */ 128 + SDL_Init(SDL_INIT_EVERYTHING); // Init SDL 129 + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1); // NanoVG _REQUIRES_ a stencil buffer 130 + GPU_Target* target = GPU_InitRenderer(GPU_RENDERER_OPENGL_3, screen_w, screen_h, 0); // Init SDL_gpu 131 + NVGcontext* vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG); // Init NanoVG 132 + 133 + main_loop(target, vg, screen_w, screen_h); 134 + 135 + return 0; 136 + }