Add sdl_gpu demo, add more glyph positions and text rendering
This commit is contained in:
parent
458d0ea598
commit
ee2f3065b8
12 changed files with 312 additions and 78 deletions
|
|
@ -24,7 +24,7 @@ proc drawLabel*(text: string, x, y, w, h: float) =
|
|||
nanovg.setFillColor(1.0, 1.0, 1.0, 1.0)
|
||||
|
||||
nanovg.textAlign("left middle")
|
||||
nanovg.text(x, y+h*0.5, text)
|
||||
nanovg.text(text, x, y+h*0.5)
|
||||
|
||||
proc drawWindow*(title: string, x, y, w, h: float) =
|
||||
let cornerRadius = 3.0
|
||||
|
|
@ -41,11 +41,11 @@ proc drawWindow*(title: string, x, y, w, h: float) =
|
|||
|
||||
nanovg.setFontBlur(2)
|
||||
nanovg.setFillColor(RGBA(0,0,0,128))
|
||||
nanovg.text(x+w/2, y+16+1, title)
|
||||
nanovg.text(title, x+w/2, y+16+1)
|
||||
|
||||
nanovg.setFontBlur(0)
|
||||
nanovg.setFillColor(RGBA(220,220,220,160))
|
||||
nanovg.text(x+w/2,y+16, title)
|
||||
nanovg.text(title, x+w/2,y+16)
|
||||
|
||||
proc isBlack*(color: Color): bool =
|
||||
result = true
|
||||
|
|
@ -163,12 +163,12 @@ proc drawButton*(preicon: string, text: string, x, y, w, h: float, color: Color)
|
|||
|
||||
nanovg.setFontSize(20.0)
|
||||
nanovg.setFont("sans-bold")
|
||||
tb = nanovg.textBounds(0.0, 0.0, text)
|
||||
tb = nanovg.textBounds(text, 0.0, 0.0)
|
||||
tw = tb.horizontalAdvance
|
||||
|
||||
nanovg.setFontSize(h*0.5)
|
||||
nanovg.setFont("fa")
|
||||
ib = nanovg.textBounds(0,0, preicon)
|
||||
ib = nanovg.textBounds(preicon, 0, 0)
|
||||
iw = ib.horizontalAdvance
|
||||
iw += h*0.15
|
||||
|
||||
|
|
@ -176,14 +176,14 @@ proc drawButton*(preicon: string, text: string, x, y, w, h: float, color: Color)
|
|||
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.text(preicon, x+w*0.5-tw*0.5-iw*0.75, y+h*0.5)
|
||||
|
||||
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.text(text, x+w*0.5-tw*0.5+iw*0.25, y+h*0.5-1)
|
||||
nanovg.setFillColor(RGBA(255,255,255,160))
|
||||
nanovg.text(x+w*0.5-tw*0.5+iw*0.25, y+h*0.5, text)
|
||||
nanovg.text(text, x+w*0.5-tw*0.5+iw*0.25, y+h*0.5)
|
||||
|
||||
proc glewInit*() {.header:"<GL/glew.h>".}
|
||||
|
|
@ -5,7 +5,6 @@ import perf
|
|||
import common
|
||||
|
||||
proc main =
|
||||
glfw.initialize()
|
||||
var c = DefaultOpenglWindowConfig
|
||||
c.version = glv32
|
||||
c.title = "Minimal Nim-GLFW example"
|
||||
|
|
@ -18,6 +17,7 @@ proc main =
|
|||
cpuGraph = newGraph(RenderStyle.MS, "CPU Time")
|
||||
|
||||
|
||||
glfw.initialize()
|
||||
var w = newWindow(c)
|
||||
swapInterval(0)
|
||||
setTime(0)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import sdl2/sdl except Color
|
||||
import sdl2/sdl_image as img, opengl
|
||||
import sdl2/sdl2 as sdl
|
||||
import sdl2/sdl2_image as img, opengl
|
||||
|
||||
import os
|
||||
import nanovg
|
||||
|
|
@ -13,16 +13,16 @@ proc events(): bool =
|
|||
while sdl.pollEvent(addr(e)) != 0:
|
||||
|
||||
# Quit requested
|
||||
if e.kind == sdl.Quit:
|
||||
if e.kind.EventType == sdl.EventQuit:
|
||||
return true
|
||||
|
||||
# Key pressed
|
||||
elif e.kind == sdl.KeyDown:
|
||||
elif e.kind.EventType == sdl.EventKeyDown:
|
||||
# Exit on Escape key press
|
||||
if e.key.keysym.sym == sdl.K_Escape:
|
||||
if e.key.keysym.sym == sdl.KeycodeEscape:
|
||||
return true
|
||||
|
||||
proc frameBufferSize*(window: Window): tuple[w, h: int32] =
|
||||
proc frameBufferSize*(window: ptr Window): tuple[w, h: int32] =
|
||||
var
|
||||
fbWidth: cint
|
||||
fbHeight: cint
|
||||
|
|
@ -31,7 +31,7 @@ proc frameBufferSize*(window: Window): tuple[w, h: int32] =
|
|||
|
||||
return (fbWidth.int32, fbHeight.int32)
|
||||
|
||||
proc size*(window: Window): tuple[w, h: int32] =
|
||||
proc size*(window: ptr Window): tuple[w, h: int32] =
|
||||
var
|
||||
width: cint
|
||||
height: cint
|
||||
|
|
@ -45,22 +45,22 @@ proc main() =
|
|||
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
|
||||
windowFlags = sdl.WINDOW_OPENGL or sdl.WINDOW_ALLOW_HIGHDPI or sdl.WINDOW_SHOWN
|
||||
screenWidth = 640
|
||||
screenHeight = 480
|
||||
|
||||
discard sdl.init(sdl.INIT_EVERYTHING)
|
||||
discard sdl.init(sdl.INIT_EVERYTHING.uint32)
|
||||
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_FLAGS, sdl.GL_CONTEXT_FORWARD_COMPATIBLE_FLAG)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE.cint)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_FLAGS, sdl.GL_CONTEXT_FORWARD_COMPATIBLE_FLAG.cint)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 2)
|
||||
|
||||
let window = sdl.createWindow(
|
||||
"SDL2 Test",
|
||||
sdl.WINDOWPOS_CENTERED,
|
||||
sdl.WINDOWPOS_CENTERED,
|
||||
screenWidth, screenHeight, windowFlags
|
||||
sdl.WINDOWPOS_CENTERED.cint,
|
||||
sdl.WINDOWPOS_CENTERED.cint,
|
||||
screenWidth.cint, screenHeight.cint, windowFlags.uint32
|
||||
)
|
||||
|
||||
let context = glCreateContext(window)
|
||||
|
|
|
|||
156
examples/demo_sdl2_gpu.nim
Normal file
156
examples/demo_sdl2_gpu.nim
Normal file
|
|
@ -0,0 +1,156 @@
|
|||
import sdl2 as sdl
|
||||
import sdl2/sdl2_gpu as gpu
|
||||
import 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.EventType == sdl.EventQuit:
|
||||
return true
|
||||
|
||||
# Key pressed
|
||||
elif e.kind.EventType == sdl.EventKeyDown:
|
||||
# Exit on Escape key press
|
||||
if e.key.keysym.sym == sdl.KeycodeEscape:
|
||||
return true
|
||||
|
||||
proc frameBufferSize*(window: ptr Window): tuple[w, h: int32] =
|
||||
var
|
||||
fbWidth: cint
|
||||
fbHeight: cint
|
||||
|
||||
window.glGetDrawableSize(fbWidth.addr, fbHeight.addr)
|
||||
|
||||
return (fbWidth.int32, fbHeight.int32)
|
||||
|
||||
proc size*(window: ptr Window): tuple[w, h: int32] =
|
||||
var
|
||||
width: cint
|
||||
height: cint
|
||||
|
||||
window.getWindowSize(width.addr, height.addr)
|
||||
|
||||
return (width.int32, height.int32)
|
||||
|
||||
template generateFbo(width, height: float, code: untyped): gpu.Image =
|
||||
let fb = createFrameBuffer(width, height, ImageFlagsGL.NO_DELETE)
|
||||
fb.binde()
|
||||
|
||||
glViewPort(0, 0, width.int, height.int)
|
||||
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)
|
||||
beginFrame(width, height, 1.0)
|
||||
code
|
||||
endFrame()
|
||||
gpu.resetRendererState()
|
||||
echo fb.texture
|
||||
let res = gpu.createImageUsingTexture(cast[TextureHandle](fb.texture), false)
|
||||
fb.delete()
|
||||
res
|
||||
|
||||
proc windowID*(target: ptr Target): uint32 {.
|
||||
cdecl, importc: "GPU_getWindowID".}
|
||||
|
||||
proc main() =
|
||||
let
|
||||
fps = newGraph(RenderStyle.FPS, "Frame Time")
|
||||
cpuGraph = newGraph(RenderStyle.MS, "CPU Time")
|
||||
|
||||
discard sdl.init(sdl.INIT_EVERYTHING.uint32)
|
||||
discard sdl.glSetAttribute(sdl.GL_STENCIL_SIZE, 1)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE.cint)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_FLAGS, sdl.GL_CONTEXT_FORWARD_COMPATIBLE_FLAG.cint)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3)
|
||||
discard sdl.glSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 2)
|
||||
var w, h: cint
|
||||
|
||||
# let
|
||||
# vert = gpu.loadShader(gpu.VERTEX_SHADER, currentSourceDir()/"resources/untextured.vert")
|
||||
# frag = gpu.loadShader(gpu.FRAGMENT_SHADER, currentSourceDir()/"resources/untextured.frag")
|
||||
# program = gpu.linkShaders(vert, frag)
|
||||
|
||||
# glUseProgram(program)
|
||||
|
||||
# let
|
||||
# vertLoc = gpu.getAttributeLocation(program, "gpu_Vertex")
|
||||
# colorLoc = gpu.getAttributeLocation(program, "gpu_Color")
|
||||
# mvpLoc = gpu.getUniformLocation(program, "gpu_ModelViewProjectionMatrix")
|
||||
|
||||
let screen = gpu.initRenderer(gpu.RENDERER_OPENGL_3, 640, 480, sdl.WINDOW_ALLOW_HIGHDPI)
|
||||
gpu.setRequiredFeatures(gpu.FEATURE_BASIC_SHADERS)
|
||||
|
||||
|
||||
nanovg.init()
|
||||
discard sdl.glSetSwapInterval(0)
|
||||
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")
|
||||
|
||||
var done = false
|
||||
var prevt: float = sdl.getTicks().float/1000.0
|
||||
|
||||
var image = gpu.loadImage(currentSourceDir()/"resources/howl.png")
|
||||
|
||||
let window = sdl.getWindowFromID(screen.context.windowID)
|
||||
|
||||
while not done:
|
||||
gpu.clearRGBA(screen, 0, 0, 0, 0xFF)
|
||||
|
||||
# do gpu drawing
|
||||
|
||||
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)
|
||||
|
||||
gpu.blit(image, nil, screen, 400, 100)
|
||||
|
||||
gpu.flushBlitBuffer()
|
||||
|
||||
withFrame(window):
|
||||
let x = glyphPositions("Some仮名thing is up", 0, 0, 10)
|
||||
echo x
|
||||
text("Some仮名thing is up", 100, 100, 100)
|
||||
drawWindow("Title", 50, 50, 300, 400)
|
||||
drawLabel("Hello!", 10, 10, 500, 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)
|
||||
|
||||
gpu.resetRendererState()
|
||||
|
||||
fps.render(5, 5)
|
||||
cpuGraph.render(210, 5)
|
||||
|
||||
|
||||
gpu.flip(screen)
|
||||
|
||||
let t: float = sdl.getTicks().float/1000.0
|
||||
let dt = t - prevt
|
||||
prevt = t
|
||||
|
||||
let cpuTime: float = sdl.getTicks().float/1000.0 - t
|
||||
|
||||
fps.update(dt)
|
||||
cpuGraph.update(cpuTime)
|
||||
|
||||
done = events()
|
||||
gpu.freeImage(image)
|
||||
gpu.quit()
|
||||
|
||||
main()
|
||||
|
|
@ -96,7 +96,7 @@ proc render*(graph: PerfGraph, x, y: float) =
|
|||
nanovg.setFontSize(14)
|
||||
nanovg.textAlign("top left")
|
||||
nanovg.setFillColor(240, 240, 240, 192)
|
||||
nanovg.text(x+3, y+1, graph.name)
|
||||
nanovg.text(graph.name, x+3, y+1)
|
||||
|
||||
nanovg.setFontSize(18)
|
||||
nanovg.textAlign("top right")
|
||||
|
|
@ -106,18 +106,18 @@ proc render*(graph: PerfGraph, x, y: float) =
|
|||
of RenderStyle.FPS:
|
||||
var str = fmt"{1.0/avg:.2f} FPS"
|
||||
|
||||
nanovg.text(x+width-3, y+1, str)
|
||||
nanovg.text(str, x+width-3, y+1)
|
||||
nanovg.setFontSize(15)
|
||||
nanovg.textAlign("bottom right")
|
||||
nanovg.setFillColor(240, 240, 240, 160)
|
||||
|
||||
str = fmt"{avg*1000:.2f} ms"
|
||||
nanovg.text(x+width-3, y+height-1, str)
|
||||
nanovg.text(str, x+width-3, y+height-1)
|
||||
|
||||
of RenderStyle.PERCENT:
|
||||
let str = fmt"{avg:.1f} %"
|
||||
nanovg.text(x+width-3, y+1, str)
|
||||
nanovg.text(str, x+width-3, y+1)
|
||||
|
||||
of RenderStyle.MS:
|
||||
let str = fmt"{avg*1000:.2f} ms"
|
||||
nanovg.text(x+width-3, y+1, str)
|
||||
nanovg.text(str, x+width-3, y+1)
|
||||
|
|
|
|||
BIN
examples/resources/howl.png
Normal file
BIN
examples/resources/howl.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
BIN
examples/resources/umeboshi.ttf
Normal file
BIN
examples/resources/umeboshi.ttf
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue