Make nanovg work with mac

This commit is contained in:
Joey Yakimowich-Payne 2020-09-09 05:07:15 -06:00
commit 86630827e3
6 changed files with 86 additions and 45 deletions

View file

@ -22,15 +22,20 @@ proc main =
fps = newGraph(RenderStyle.FPS, "Frame Time")
cpuGraph = newGraph(RenderStyle.MS, "CPU Time")
discard glfw.init()
initHint(CONTEXT_VERSION_MAJOR, 3)
initHint(CONTEXT_VERSION_MINOR, 2)
when defined(macosx):
initHint(OPENGL_FORWARD_COMPAT, 1)
initHint(OPENGL_PROFILE, OPENGL_COMPAT_PROFILE)
else:
initHint(OPENGL_PROFILE, OPENGL_CORE_PROFILE)
initHint(OPENGL_PROFILE, OPENGL_CORE_PROFILE)
discard glfw.init()
windowHint(CONTEXT_VERSION_MAJOR, 3)
windowHint(CONTEXT_VERSION_MINOR, 2)
when defined(macosx):
windowHint(OPENGL_FORWARD_COMPAT, 1)
windowHint(OPENGL_PROFILE, OPENGL_CORE_PROFILE)
var w = createWindow(800, 600, "Minimal Nim-GLFW Example", nil, nil)
w.makeContextCurrent()

View file

@ -73,6 +73,7 @@ proc main() =
discard glew.init()
let vg = nanovg.newContext()
vg.loadFont(currentSourceDir()/"resources"/"Roboto-Regular.ttf", "sans")
vg.loadFont(currentSourceDir()/"resources"/"umeboshi.ttf", "umeboshi")
vg.loadFont(currentSourceDir()/"resources"/"Roboto-Bold.ttf", "sans-bold")
vg.loadFont(currentSourceDir()/"resources"/"fa.ttf", "fa")
vg.loadFont(currentSourceDir()/"resources"/"entypo.ttf", "icons")
@ -87,6 +88,7 @@ proc main() =
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT or GL_STENCIL_BUFFER_BIT)
vg.withFrame(window):
vg.setFont("umeboshi")
vg.text("Some仮名thing is up", 100, 100, 100)
vg.drawWindow("Title", 50, 50, 300, 400)
vg.drawLabel("Hello!", 10, 10, 280, 20)
@ -116,4 +118,4 @@ proc main() =
img.quit()
sdl.quit()
main()
main()

View file

@ -1,6 +1,6 @@
# Package
version = "0.1.0"
version = "0.2.0"
author = "Joey Yakimowich-Payne"
description = "A wrapper around nanovg"
license = "MIT"
@ -10,11 +10,10 @@ srcDir = "src"
requires "nim >= 1.0.6",
"https://github.com/jyapayne/nimterop#367ec05",
"regex >= 0.14.1",
"opengl >= 1.2.6",
"sdl2#head",
"https://github.com/jyapayne/nim-glew#15383cf",
"https://github.com/jyapayne/nim-glfw#22bbdf6"
"https://github.com/jyapayne/nim-glew#c6a3e51",
"https://github.com/jyapayne/nim-glfw#8414225"
import os

View file

@ -2,7 +2,7 @@ import macros
import sequtils
import strutils
import strformat
import nanovg/wrapper
import nanovg/nanovg
converter intToCuchar*(source: uint8 | int): cuchar =
return source.cuchar
@ -470,25 +470,25 @@ proc `globalAlpha=`*(context: Context, alpha: float) =
# Transforms
proc resetTransform*(context: Context) {.inline.} =
wrapper.resetTransform(context)
nanovg.resetTransform(context)
proc transform*(context: Context, a, b, c, d, e, f: float) =
wrapper.transform(context, a, b, c, d, e, f)
nanovg.transform(context, a, b, c, d, e, f)
proc translate*(context: Context, x, y: float) {.inline.} =
wrapper.translate(context, x, y)
nanovg.translate(context, x, y)
proc rotate*(context: Context, angle: float) {.inline.} =
wrapper.rotate(context, angle)
nanovg.rotate(context, angle)
proc skewX*(context: Context, angle: float) {.inline.} =
wrapper.skewX(context, angle)
nanovg.skewX(context, angle)
proc skewY*(context: Context, angle: float) {.inline.} =
wrapper.skewY(context, angle)
nanovg.skewY(context, angle)
proc scale*(context: Context, x, y: float) {.inline.} =
wrapper.scale(context, x, y)
nanovg.scale(context, x, y)
proc copyArray[T, U](src: T): U =
for i in 0..<src.len:
@ -601,16 +601,16 @@ proc pathStroke*(context: Context) =
context.stroke()
proc linearGradient*(context: Context, startX, startY, endX, endY: float, startColor, endColor: Color): Paint =
result = wrapper.linearGradient(context, startX, startY, endX, endY, startColor, endColor)
result = nanovg.linearGradient(context, startX, startY, endX, endY, startColor, endColor)
proc boxGradient*(context: Context, x, y, width, height, radius, feather: float, startColor, endColor: Color): Paint =
result = wrapper.boxGradient(context, x, y, width, height, radius, feather, startColor, endColor)
result = nanovg.boxGradient(context, x, y, width, height, radius, feather, startColor, endColor)
proc radialGradient*(context: Context, centerX, centerY, innerRadius, outerRadius: float, startColor, endColor: Color): Paint =
result = wrapper.radialGradient(context, centerX, centerY, innerRadius, outerRadius, startColor, endColor)
result = nanovg.radialGradient(context, centerX, centerY, innerRadius, outerRadius, startColor, endColor)
proc imagePattern*(context: Context, originX, originY, renderSizeX, renderSizeY, angle: float, image: Image, alpha: float): Paint =
result = wrapper.imagePattern(context, originX, originY, renderSizeX, renderSizeY, angle, image, alpha)
result = nanovg.imagePattern(context, originX, originY, renderSizeX, renderSizeY, angle, image, alpha)
proc setScissor*(context: Context, x, y, width, height: float) =
context.scissor(x, y, width, height)

View file

@ -0,0 +1,57 @@
import macros, nimterop / plugin
import strutils
template camelCase(str: string): string =
var res = newStringOfCap(str.len)
var i = 0
while i < str.len:
if str[i] == '_' and i < str.len - 1:
res.add(str[i+1].toUpperAscii)
i += 1
else:
res.add(str[i])
i += 1
res
template lowerFirstLetter(str, rep: string): string =
if str.startsWith(rep):
var res = str[rep.len .. ^1]
res[0] = res[0].toLowerAscii
res
else:
str
template removeBeginning(str, rep: string): string =
if str.startsWith(rep):
str[rep.len .. ^1]
else:
str
const replacements = [
"glnvg__",
"nvg__",
"nvglu",
"nvgl",
"nvg",
]
# Symbol renaming examples
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
for rep in replacements:
if sym.kind == nskProc:
sym.name = lowerFirstLetter(sym.name, rep)
elif sym.kind == nskType:
if sym.name.startsWith(rep):
sym.name = camelCase(removeBeginning(sym.name, rep))
else:
sym.name = removeBeginning(sym.name, rep)
if sym.kind == nskField:
sym.name = camelCase(sym.name)
if sym.name == "type":
sym.name = "kind"
# Get rid of leading and trailing underscores
if sym.kind == nskProc or sym.kind == nskType or sym.kind == nskConst:
if sym.name != "_":
sym.name = sym.name.strip(chars={'_'}).replace("__", "_").strip(chars={'_'})

View file

@ -11,6 +11,7 @@ import nimterop/[cimport, build]
const
baseDir = currentSourcePath.parentDir().parentDir().parentDir()
srcDir = baseDir/"build"/"nanovg"
symbolPluginPath = currentSourcePath.parentDir() / "cleansymbols.nim"
defs = """
nanovgGit
@ -116,30 +117,7 @@ elif defined(windows):
cCompile(srcDir/"src/*.c")
# Use cPlugin() to make any symbol changes
cPlugin:
import strutils, regex
const replacements = [
re"^glnvg__(.)",
re"^nvg__(.)",
re"^nvglu(.)",
re"^nvgl(.)",
re"^nvg(.)",
]
proc firstLetterLower(m: RegexMatch, s: string): string =
if m.groupsCount > 0 and m.group(0).len > 0:
return s[m.group(0)[0]].toLowerAscii
# Symbol renaming examples
proc onSymbol*(sym: var Symbol) {.exportc, dynlib.} =
# Get rid of leading and trailing underscores
sym.name = sym.name.strip(chars = {'_'})
# Remove prefixes or suffixes from procs
if sym.kind == nskProc:
for rep in replacements:
sym.name = sym.name.replace(rep, firstLetterLower)
cPluginPath(symbolPluginPath)
# Finally import wrapped header file. Recurse if #include files should also
# be wrapped. Set dynlib if binding to dynamic library.