Browse Source

added example

updated for newest nanovg API
pull/1/head
Leonard Ritter 11 years ago
parent
commit
72d9be0ec2
4 changed files with 318 additions and 7 deletions
  1. +1
    -1
      .hgignore
  2. +8
    -6
      blendish.h
  3. +273
    -0
      example.cpp
  4. +36
    -0
      premake4.lua

+ 1
- 1
.hgignore View File

@@ -9,4 +9,4 @@ syntax: glob
syntax: regexp

^build
^dist
^nanovg

+ 8
- 6
blendish.h View File

@@ -35,7 +35,7 @@ extern "C" {

/*

Revision 1 (2014-07-08)
Revision 2 (2014-07-08)

Summary
-------
@@ -177,6 +177,8 @@ typedef enum BNDcornerFlags {

// default widget height
#define BND_WIDGET_HEIGHT 21
// default toolbutton width (if icon only)
#define BND_TOOL_WIDTH 20

////////////////////////////////////////////////////////////////////////////////

@@ -397,6 +399,7 @@ void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color);

#ifdef BLENDISH_IMPLEMENTATION

#include <memory.h>
#include <math.h>

#ifdef _MSC_VER
@@ -779,7 +782,6 @@ void bndMenuLabel(NVGcontext *ctx,
void bndMenuItem(NVGcontext *ctx,
float x, float y, float w, float h, BNDwidgetState state,
int iconid, const char *label) {
NVGcolor shade_top, shade_down;
if (state != BND_DEFAULT) {
bndInnerBox(ctx,x,y,w,h,0,0,0,0,
bndOffsetColor(bnd_theme.menuItemTheme.innerSelectedColor,
@@ -876,7 +878,7 @@ void bndIcon(NVGcontext *ctx, float x, float y, int iconid) {
nvgImagePattern(ctx,x-u,y-v,
BND_ICON_SHEET_WIDTH,
BND_ICON_SHEET_HEIGHT,
0,bnd_icon_image,0));
0,bnd_icon_image,0,1));
nvgFill(ctx);
}

@@ -979,15 +981,15 @@ void bndIconLabel(NVGcontext *ctx, float x, float y, float w, float h,
nvgBeginPath(ctx);
nvgFillColor(ctx, color);
if (value) {
float label_width = nvgTextBounds(ctx, label, NULL, NULL);
float sep_width = nvgTextBounds(ctx,
float label_width = nvgTextBounds(ctx, 1, 1, label, NULL, NULL);
float sep_width = nvgTextBounds(ctx, 1, 1,
BND_LABEL_SEPARATOR, NULL, NULL);
nvgTextAlign(ctx, NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE);
x += pleft;
if (align == BND_CENTER) {
float width = label_width + sep_width
+ nvgTextBounds(ctx, value, NULL, NULL);
+ nvgTextBounds(ctx, 1, 1, value, NULL, NULL);
x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f;
}
y += h-6;


+ 273
- 0
example.cpp View File

@@ -0,0 +1,273 @@
//
// based on NanoVG's example code by Mikko Mononen

#include <stdio.h>
#ifdef NANOVG_GLEW
# include <GL/glew.h>
#endif
#ifdef __APPLE__
# define GLFW_INCLUDE_GLCOREARB
#endif
#include <GLFW/glfw3.h>
#include "nanovg.h"
#define NANOVG_GL3_IMPLEMENTATION
#include "nanovg_gl.h"

#define BLENDISH_IMPLEMENTATION
#include "blendish.h"

////////////////////////////////////////////////////////////////////////////////

void init(NVGcontext *vg) {
bndSetFont(nvgCreateFont(vg, "system", "droidsans.ttf"));
bndSetIconImage(nvgCreateImage(vg, "blender_icons.png"));
}

void draw(NVGcontext *vg, float w, float h) {

bndBackground(vg, 0, 0, w, h);
int x = 10;
int y = 10;
bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
BND_ICONID(6,3),"Default");
y += 25;
bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
BND_ICONID(6,3),"Hovered");
y += 25;
bndToolButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
BND_ICONID(6,3),"Active");
y += 40;
bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
-1,"Default");
y += 25;
bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
-1,"Hovered");
y += 25;
bndRadioButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
-1,"Active");

y += 40;
bndLabel(vg,x,y,120,BND_WIDGET_HEIGHT,-1,"Label:");
y += BND_WIDGET_HEIGHT;
bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
-1, "Default");
y += 25;
bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
-1, "Hovered");
y += 25;
bndChoiceButton(vg,x,y,80,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
-1, "Active");
y = 10;
x += 130;
bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_DEFAULT,"Default");
y += 25;
bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_HOVER,"Hovered");
y += 25;
bndOptionButton(vg,x,y,120,BND_WIDGET_HEIGHT,BND_ACTIVE,"Active");

y += 40;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_DOWN,BND_DEFAULT,
"Top","100");
y += BND_WIDGET_HEIGHT-2;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
"Center","100");
y += BND_WIDGET_HEIGHT-2;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_TOP,BND_DEFAULT,
"Bottom","100");

int mx = x-30;
int my = y-12;
int mw = 120;
bndMenuBackground(vg,mx,my,mw,240,BND_CORNER_TOP);
bndMenuLabel(vg,mx,my,mw,BND_WIDGET_HEIGHT,-1,"Menu Title");
my += BND_WIDGET_HEIGHT-2;
bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_DEFAULT,
BND_ICONID(17,3),"Default");
my += BND_WIDGET_HEIGHT-2;
bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_HOVER,
BND_ICONID(18,3),"Hovered");
my += BND_WIDGET_HEIGHT-2;
bndMenuItem(vg,mx,my,mw,BND_WIDGET_HEIGHT,BND_ACTIVE,
BND_ICONID(19,3),"Active");
y = 10;
x += 130;
int ox = x;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
"Default","100");
y += 25;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
"Hovered","100");
y += 25;
bndNumberField(vg,x,y,120,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
"Active","100");
y += 40;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,BND_DEFAULT,
-1,"One");
x += 60-1;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
-1,"Two");
x += 60-1;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_ALL,BND_DEFAULT,
-1,"Three");
x += 60-1;
bndRadioButton(vg,x,y,60,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,BND_ACTIVE,
-1,"Butts");
x = ox;
y += 40;
float progress_value = fmodf(glfwGetTime()/10.0,1.0);
char progress_label[32];
sprintf(progress_label, "%d%%", int(progress_value*100+0.5f));
bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_DEFAULT,
progress_value,"Default",progress_label);
y += 25;
bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_HOVER,
progress_value,"Hovered",progress_label);
y += 25;
bndSlider(vg,x,y,240,BND_WIDGET_HEIGHT,BND_CORNER_NONE,BND_ACTIVE,
progress_value,"Active",progress_label);
x = ox;
y += 40;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
BND_DEFAULT,BND_ICONID(0,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(1,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(2,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(3,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(4,10),NULL);
x += BND_TOOL_WIDTH-1;
bndToolButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
BND_DEFAULT,BND_ICONID(5,10),NULL);
x += BND_TOOL_WIDTH-1;
x += 5;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_RIGHT,
BND_DEFAULT,BND_ICONID(0,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(1,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(2,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_DEFAULT,BND_ICONID(3,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_ALL,
BND_ACTIVE,BND_ICONID(4,11),NULL);
x += BND_TOOL_WIDTH-1;
bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT,
BND_DEFAULT,BND_ICONID(5,11),NULL);
}

////////////////////////////////////////////////////////////////////////////////

void errorcb(int error, const char* desc)
{
printf("GLFW error %d: %s\n", error, desc);
}

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);
}

int main()
{
GLFWwindow* window;
struct NVGcontext* vg = NULL;

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(1000, 600, "Blendish Demo", NULL, 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 consume it here.
glGetError();
#endif

vg = nvgCreateGL3(NVG_ANTIALIAS | NVG_STENCIL_STROKES);
if (vg == NULL) {
printf("Could not init nanovg.\n");
return -1;
}
init(vg);

glfwSwapInterval(0);

glfwSetTime(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);

nvgBeginFrame(vg, winWidth, winHeight, pxRatio);

draw(vg, winWidth, winHeight);

nvgEndFrame(vg);

glfwSwapBuffers(window);
glfwPollEvents();
}

nvgDeleteGL3(vg);

glfwTerminate();
return 0;
}

+ 36
- 0
premake4.lua View File

@@ -0,0 +1,36 @@

local action = _ACTION or ""

solution "blendish"
location ( "build" )
configurations { "Debug", "Release" }
platforms {"native", "x64", "x32"}

project "example"
kind "ConsoleApp"
language "C++"
files { "example.cpp", "nanovg/src/nanovg.c" }
includedirs { "nanovg/src" }
targetdir("build")

configuration { "linux" }
linkoptions { "`pkg-config --libs glfw3 --static`" }
links { "GL", "GLU", "m", "GLEW" }
defines { "NANOVG_GLEW" }

configuration { "windows" }
links { "glfw3", "gdi32", "winmm", "user32", "GLEW", "glu32","opengl32" }
defines { "NANOVG_GLEW" }

configuration { "macosx" }
links { "glfw3" }
linkoptions { "-framework OpenGL", "-framework Cocoa", "-framework IOKit", "-framework CoreVideo" }

configuration "Debug"
defines { "DEBUG" }
flags { "Symbols", "ExtraWarnings"}

configuration "Release"
defines { "NDEBUG" }
flags { "Optimize", "ExtraWarnings"}


Loading…
Cancel
Save