Add sdl2 and glfw examples. Build working for windows now

This commit is contained in:
Joey Yakimowich-Payne 2019-10-20 11:30:28 -06:00
commit 9983e298a0
90 changed files with 58339 additions and 215 deletions

189
examples/common.nim Normal file
View file

@ -0,0 +1,189 @@
import os
import nanovg
type
WindowConfig* = object
cornerRadiusTopLeft*: float
cornerRadiusTopRight*: float
cornerRadiusBottomLeft*: float
cornerRadiusBottomRight*: float
# const ICON_TRASH: cint = 0x0000f1f8
const ICON_TRASH* = ""
# const ICON_TRASH = "\uf1f8"
#const ICON_TRASH: cint = 0xE729
proc currentSourceDir*(): string {.inline.} =
return currentSourcePath.parentDir()
# Config
proc drawLabel*(text: string, x, y, w, h: float) =
nanovg.setFontSize(50.0)
nanovg.setFont("sans")
nanovg.setFillColor(1.0, 1.0, 1.0, 1.0)
nanovg.textAlign("left middle")
nanovg.text(x, y+h*0.5, text)
proc drawWindow*(title: string, x, y, w, h: float) =
let cornerRadius = 3.0
withState:
nanovg.beginPath()
nanovg.pathRoundedRect(x, y, w, h, cornerRadius)
#nanovg.setFillColor(RGBA(255,255,255,192))
nanovg.setFillColor(RGBA(28,30,34,192))
nanovg.pathFill()
nanovg.setFontSize(30.0)
nanovg.setFont("sans")
nanovg.textAlign("center middle")
nanovg.setFontBlur(2)
nanovg.setFillColor(RGBA(0,0,0,128))
nanovg.text(x+w/2, y+16+1, title)
nanovg.setFontBlur(0)
nanovg.setFillColor(RGBA(220,220,220,160))
nanovg.text(x+w/2,y+16, title)
proc isBlack*(color: Color): bool =
result = true
for n, v in color.fieldPairs:
if v != 0:
return false
proc cpToUtf8*(code: int): string =
var n = 0
var cp = code
if cp < 0x80: n = 1
elif cp < 0x800: n = 2
elif cp < 0x10000: n = 3
elif cp < 0x200000: n = 4
elif cp < 0x4000000: n = 5
elif cp <= 0x7fffffff: n = 6
var res = newStringOfCap(n)
res.setLen(n)
if n >= 6:
res[5] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x4000000
if n >= 5:
res[4] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x200000
if n >= 4:
res[3] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x10000
if n >= 3:
res[2] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x800
if n >= 2:
res[1] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0xc0
if n >= 1:
res[0] = char(cp)
return res
proc drawButton*(text: string, x, y, width, height: float) =
let bg = linearGradient(
x, y, x, y+height,
RGBA(255, 255, 255, 16),
RGBA(100, 100, 100, 16)
)
let color = RGBA(29, 100, 210, 255)
let shadowColor = RGBA(0, 0, 0, 90)
let highlightColor = RGBA(255, 255, 255, 100)
let radius = 3.0
let lightWidth = 0.5
let shadowOffset = 3.0
let drawShadow = true
if drawShadow:
nanovg.beginPath()
nanovg.pathRoundedRect(x+1+shadowOffset, y+1+shadowOffset, width-2, height-2, radius)
nanovg.setFillColor(shadowColor)
nanovg.pathFill()
nanovg.beginPath()
nanovg.pathRoundedRect(x+1, y+1, width-2, height-2, radius)
nanovg.setFillColor(color)
nanovg.pathFill()
nanovg.setFillPaint(bg)
nanovg.pathFill()
nanovg.beginPath()
nanovg.pathMoveTo(x+radius, y+lightWidth+1)
nanovg.setStrokeWidth(lightWidth)
nanovg.setStrokeColor(highlightColor)
nanovg.pathLineTo(x+width-(radius), y+lightWidth+1)
nanovg.pathStroke()
proc drawButton*(preicon: string, text: string, x, y, w, h: float, color: Color) =
var
bg: Paint
cornerRadius = 4.0
tb: TextBounds
ib: TextBounds
tw = 0.0
iw = 0.0
bg = nanovg.linearGradient(
x,y,x,y+h,
RGBA(255,255,255, if isBlack(color): 16 else:32),
RGBA(0,0,0, if isBlack(color): 16 else:32)
)
nanovg.beginPath()
nanovg.pathRoundedRect(x+1, y+1, w-2, h-2, cornerRadius-1)
if not isBlack(color):
nanovg.setFillColor(color)
nanovg.pathFill()
nanovg.setFillPaint(bg)
nanovg.pathFill()
nanovg.beginPath()
nanovg.pathRoundedRect(x+0.5, y+0.5, w-1, h-1, cornerRadius-0.5)
nanovg.setStrokeColor(RGBA(0,0,0,48))
nanovg.pathStroke()
nanovg.setFontSize(20.0)
nanovg.setFont("sans-bold")
tb = nanovg.textBounds(0.0, 0.0, text)
tw = tb.horizontalAdvance
nanovg.setFontSize(h*0.5)
nanovg.setFont("fa")
ib = nanovg.textBounds(0,0, preicon)
iw = ib.horizontalAdvance
iw += h*0.15
nanovg.setFontSize(h*0.5)
nanovg.setFont("fa")
nanovg.setFillColor(RGBA(255,255,255,96))
nanovg.textAlign("left middle")
nanovg.text(x+w*0.5-tw*0.5-iw*0.75, y+h*0.5, preicon)
nanovg.setFontSize(20.0)
nanovg.setFont("sans-bold")
nanovg.textAlign("left middle")
nanovg.setFillColor(RGBA(0,0,0,160))
nanovg.text(x+w*0.5-tw*0.5+iw*0.25, y+h*0.5-1, text)
nanovg.setFillColor(RGBA(255,255,255,160))
nanovg.text(x+w*0.5-tw*0.5+iw*0.25, y+h*0.5, text)
proc glewInit*() {.header:"<GL/glew.h>".}

View file

@ -2,200 +2,15 @@ import os
import nanovg
import glfw, opengl
import perf
type
WindowConfig = object
cornerRadiusTopLeft: float
cornerRadiusTopRight: float
cornerRadiusBottomLeft: float
cornerRadiusBottomRight: float
# const ICON_TRASH: cint = 0x0000f1f8
const ICON_TRASH = ""
# const ICON_TRASH = "\uf1f8"
#const ICON_TRASH: cint = 0xE729
proc currentSourceDir(): string {.inline.} =
return currentSourcePath.parentDir()
# Config
proc drawLabel(text: string, x, y, w, h: float) =
nanovg.setFontSize(50.0)
nanovg.setFont("sans")
nanovg.setFillColor(1.0, 1.0, 1.0, 1.0)
nanovg.textAlign("left middle")
nanovg.text(x, y+h*0.5, text)
proc drawWindow(title: string, x, y, w, h: float) =
let cornerRadius = 3.0
withState:
nanovg.beginPath()
nanovg.pathRoundedRect(x, y, w, h, cornerRadius)
#nanovg.setFillColor(RGBA(255,255,255,192))
nanovg.setFillColor(RGBA(28,30,34,192))
nanovg.pathFill()
nanovg.setFontSize(30.0)
nanovg.setFont("sans")
nanovg.textAlign("center middle")
nanovg.setFontBlur(2)
nanovg.setFillColor(RGBA(0,0,0,128))
nanovg.text(x+w/2, y+16+1, title)
nanovg.setFontBlur(0)
nanovg.setFillColor(RGBA(220,220,220,160))
nanovg.text(x+w/2,y+16, title)
proc isBlack(color: Color): bool =
result = true
for n, v in color.fieldPairs:
if v != 0:
return false
proc cpToUtf8*(code: int): string =
var n = 0
var cp = code
if cp < 0x80: n = 1
elif cp < 0x800: n = 2
elif cp < 0x10000: n = 3
elif cp < 0x200000: n = 4
elif cp < 0x4000000: n = 5
elif cp <= 0x7fffffff: n = 6
var res = newStringOfCap(n)
res.setLen(n)
if n >= 6:
res[5] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x4000000
if n >= 5:
res[4] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x200000
if n >= 4:
res[3] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x10000
if n >= 3:
res[2] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0x800
if n >= 2:
res[1] = char(0x80 or (cp and 0x3f))
cp = cp shr 6
cp = cp or 0xc0
if n >= 1:
res[0] = char(cp)
return res
proc drawButton*(text: string, x, y, width, height: float) =
let bg = linearGradient(
x, y, x, y+height,
RGBA(255, 255, 255, 16),
RGBA(100, 100, 100, 16)
)
let color = RGBA(29, 100, 210, 255)
let shadowColor = RGBA(0, 0, 0, 90)
let highlightColor = RGBA(255, 255, 255, 100)
let radius = 3.0
let lightWidth = 0.5
let shadowOffset = 3.0
let drawShadow = true
if drawShadow:
nanovg.beginPath()
nanovg.pathRoundedRect(x+1+shadowOffset, y+1+shadowOffset, width-2, height-2, radius)
nanovg.setFillColor(shadowColor)
nanovg.pathFill()
nanovg.beginPath()
nanovg.pathRoundedRect(x+1, y+1, width-2, height-2, radius)
nanovg.setFillColor(color)
nanovg.pathFill()
nanovg.setFillPaint(bg)
nanovg.pathFill()
nanovg.beginPath()
nanovg.pathMoveTo(x+radius, y+lightWidth+1)
nanovg.setStrokeWidth(lightWidth)
nanovg.setStrokeColor(highlightColor)
nanovg.pathLineTo(x+width-(radius), y+lightWidth+1)
nanovg.pathStroke()
proc drawButton*(preicon: string, text: string, x, y, w, h: float, color: Color) =
var
bg: Paint
cornerRadius = 4.0
tb: TextBounds
ib: TextBounds
tw = 0.0
iw = 0.0
bg = nanovg.linearGradient(
x,y,x,y+h,
RGBA(255,255,255, if isBlack(color): 16 else:32),
RGBA(0,0,0, if isBlack(color): 16 else:32)
)
nanovg.beginPath()
nanovg.pathRoundedRect(x+1, y+1, w-2, h-2, cornerRadius-1)
if not isBlack(color):
nanovg.setFillColor(color)
nanovg.pathFill()
nanovg.setFillPaint(bg)
nanovg.pathFill()
nanovg.beginPath()
nanovg.pathRoundedRect(x+0.5, y+0.5, w-1, h-1, cornerRadius-0.5)
nanovg.setStrokeColor(RGBA(0,0,0,48))
nanovg.pathStroke()
nanovg.setFontSize(20.0)
nanovg.setFont("sans-bold")
tb = nanovg.textBounds(0.0, 0.0, text)
tw = tb.horizontalAdvance
nanovg.setFontSize(h*0.5)
nanovg.setFont("fa")
ib = nanovg.textBounds(0,0, preicon)
iw = ib.horizontalAdvance
iw += h*0.15
nanovg.setFontSize(h*0.5)
nanovg.setFont("fa")
nanovg.setFillColor(RGBA(255,255,255,96))
nanovg.textAlign("left middle")
nanovg.text(x+w*0.5-tw*0.5-iw*0.75, y+h*0.5, preicon)
nanovg.setFontSize(20.0)
nanovg.setFont("sans-bold")
nanovg.textAlign("left middle")
nanovg.setFillColor(RGBA(0,0,0,160))
nanovg.text(x+w*0.5-tw*0.5+iw*0.25, y+h*0.5-1, text)
nanovg.setFillColor(RGBA(255,255,255,160))
nanovg.text(x+w*0.5-tw*0.5+iw*0.25, y+h*0.5, text)
import common
proc main =
glfw.initialize()
var c = DefaultOpenglWindowConfig
c.version = glv32
c.title = "Minimal Nim-GLFW example"
c.forwardCompat = true
c.profile = opCoreProfile
c.profile = opCompatProfile
c.debugContext = false
let
@ -204,14 +19,14 @@ proc main =
var w = newWindow(c)
swapInterval(0)
setTime(0)
glewInit()
nanovg.init()
nanovg.loadFont(currentSourceDir()/"resources/Roboto-Regular.ttf", "sans")
nanovg.loadFont(currentSourceDir()/"resources/Roboto-Bold.ttf", "sans-bold")
nanovg.loadFont(currentSourceDir()/"resources/fa.ttf", "fa")
nanovg.loadFont(currentSourceDir()/"resources/entypo.ttf", "icons")
swapInterval(0)
setTime(0)
var prevt = getTime()
while not w.shouldClose():

114
examples/demo_sdl2.nim Normal file
View file

@ -0,0 +1,114 @@
import sdl2/sdl except Color
import sdl2/sdl_image as img, opengl
import os
import nanovg
import perf
import common
proc events(): bool =
result = false
var e: sdl.Event
while sdl.pollEvent(addr(e)) != 0:
# Quit requested
if e.kind == sdl.Quit:
return true
# Key pressed
elif e.kind == sdl.KeyDown:
# Exit on Escape key press
if e.key.keysym.sym == sdl.K_Escape:
return true
proc frameBufferSize*(window: Window): tuple[w, h: int32] =
var
fbWidth: cint
fbHeight: cint
window.glGetDrawableSize(fbWidth.addr, fbHeight.addr)
return (fbWidth.int32, fbHeight.int32)
proc size*(window: Window): tuple[w, h: int32] =
var
width: cint
height: cint
window.getWindowSize(width.addr, height.addr)
return (width.int32, height.int32)
proc main() =
let
fps = newGraph(RenderStyle.FPS, "Frame Time")
cpuGraph = newGraph(RenderStyle.MS, "CPU Time")
let
windowFlags: uint32 = sdl.WINDOW_OPENGL or sdl.WINDOW_ALLOW_HIGHDPI or sdl.WINDOW_SHOWN
screenWidth = 640
screenHeight = 480
discard sdl.glSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3)
discard sdl.glSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 2)
discard sdl.init(sdl.INIT_EVERYTHING)
let window = sdl.createWindow(
"SDL2 Test",
sdl.WINDOWPOS_CENTERED,
sdl.WINDOWPOS_CENTERED,
screenWidth, screenHeight, windowFlags
)
let context = glCreateContext(window)
discard sdl.glSetSwapInterval(0)
var done = false
var prevt: float = sdl.getTicks().float/1000.0
glewInit()
nanovg.init()
nanovg.loadFont(currentSourceDir()/"resources/Roboto-Regular.ttf", "sans")
nanovg.loadFont(currentSourceDir()/"resources/Roboto-Bold.ttf", "sans-bold")
nanovg.loadFont(currentSourceDir()/"resources/fa.ttf", "fa")
nanovg.loadFont(currentSourceDir()/"resources/entypo.ttf", "icons")
while not done:
let fbSize = window.frameBufferSize()
glViewPort(0, 0, fbSize.w, fbSize.h)
glCullFace(GL_BACK)
glFrontFace(GL_CCW)
glClearColor(0.3, 0.3, 0.32, 1.0)
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT)
withFrame(window):
let x = glyphPositions("Some仮名thing is up", 0, 0)
drawWindow("Title", 50, 50, 300, 400)
drawLabel("Hello!", 10, 10, 280, 20)
drawButton(ICON_TRASH, "Delete", 100, 200, 160, 28, RGBA(128, 16, 8, 255))
drawButton(ICON_TRASH, "Deleter", 100, 250, 160, 28, RGBA(12, 130, 80, 255))
drawButton("Testing", 100, 300, 160, 28)
fps.render(5, 5)
cpuGraph.render(210, 5)
let t: float = sdl.getTicks().float/1000.0
let dt = t - prevt
prevt = t
window.glSwapWindow()
done = events()
let cpuTime: float = sdl.getTicks().float/1000.0 - t
fps.update(dt)
cpuGraph.update(cpuTime)
sdl.glDeleteContext(context)
window.destroyWindow()
img.quit()
sdl.quit()
main()

Binary file not shown.

Binary file not shown.

View file

@ -1 +1 @@
--path:"../src/"
--path:"../src/"