Skip to content

Instantly share code, notes, and snippets.

@jonbro
Created January 5, 2016 05:06
Show Gist options
  • Select an option

  • Save jonbro/b745a101b977ca61466e to your computer and use it in GitHub Desktop.

Select an option

Save jonbro/b745a101b977ca61466e to your computer and use it in GitHub Desktop.
#include <SDL.h>
#include <time.h>
#include <emscripten.h>
#include "SDL/SDL_opengl.h"
#include "nanovg.h"
#define NANOVG_GLES2_IMPLEMENTATION
#include "nanovg_gl.h"
#include "nanovg_gl_utils.h"
NVGcontext* vg = NULL;
SDL_Window* window;
SDL_Renderer* renderer;
float randomFloat()
{
float r = (float)rand()/(float)RAND_MAX;
return r;
}
void drawMush(float cx, float cy, float size)
{
// draw the shadow
nvgBeginPath(vg);
nvgEllipse(vg, cx, cy+size*0.25, size*0.9, size*0.9);
nvgFillColor(vg, nvgRGBA(0,0,0,90));
nvgFill(vg);
// draw the mushroom base
// draw the main mush
NVGpaint gloss = nvgRadialGradient(vg, cx, cy-size*0.15, size*0.1, size*0.9, nvgRGBA(163,254,193,255), nvgRGBA(32,157,185,255));
nvgBeginPath(vg);
nvgEllipse(vg, cx, cy, size, size);
nvgFillPaint(vg, gloss);
nvgFill(vg);
// draw some circles
for(int i=0;i<20;i++){
nvgBeginPath(vg);
float rs = randomFloat();
float rx = randomFloat();
float ry = randomFloat();
nvgEllipse(vg, cx-size*rx+size*0.5, cy-size*ry+size*0.5, size*rs*0.2, size*rs*0.2);
nvgFillColor(vg, nvgRGBA(255,255,255,255));
nvgFill(vg);
}
}
void frame()
{
nvgBeginFrame(vg, 640,480, 1);
// draw the background
nvgBeginPath(vg);
nvgRect(vg, 0,0,640,480);
nvgFillColor(vg, nvgRGBA(165,155,102,255));
nvgFill(vg);
drawMush(319,200,75);
// nvgFill(vg);
nvgEndFrame(vg);
}
int main()
{
srand(time(NULL));
SDL_GLContext context;
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("test", 0, 0, 640, 480, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
vg = nvgCreateGLES2(NVG_ANTIALIAS | NVG_STENCIL_STROKES | NVG_DEBUG);
emscripten_set_main_loop(frame, -1, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment