Skip to content

Commit 343d9d2

Browse files
authored
Switch to "Windex" (#77)
* more cleanup * switch to windex * update timers
1 parent 4ec5301 commit 343d9d2

File tree

8 files changed

+43
-82
lines changed

8 files changed

+43
-82
lines changed

figuro.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ requires "msgpack4nim"
2828
requires "stack_strings"
2929
requires "micros"
3030
requires "stylus"
31-
requires "https://github.com/elcritch/windy#macos-keyinput-fixes"
31+
requires "https://github.com/elcritch/windex >= 0.1.0"
3232
requires "libfswatch >= 0.1.0"
3333
requires "chronicles >= 0.10.3"

src/figuro/common/fontutils.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import std/isolation
44
import pkg/vmath
55
import pkg/pixie
66
import pkg/pixie/fonts
7-
import pkg/windy
7+
import pkg/windex
88
import pkg/threading/channels
99

1010
import glyphs, extras, shared

src/figuro/renderer/opengl.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import std/[options, unicode, hashes, strformat, strutils, tables, times]
22
import std/terminal
33

44
import pkg/pixie
5-
import pkg/windy
5+
import pkg/windex
66
import pkg/sigils/weakrefs
77

88
import ../commons
@@ -68,7 +68,7 @@ proc configureWindowEvents(renderer: Renderer) =
6868
uxInput.mouse.wheelDelta = window.scrollDelta().descaled()
6969
discard renderer.uxInputList.trySend(uxInput)
7070

71-
window.onButtonPress = proc(button: windy.Button) =
71+
window.onButtonPress = proc(button: windex.Button) =
7272
let uxInput = window.copyInputs()
7373
when defined(debugEvents):
7474
stdout.styledWriteLine(

src/figuro/renderer/opengl/renderer.nim

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export tables
33

44
import pkg/threading/atomics
55
import pkg/chroma
6-
import pkg/windy
6+
import pkg/windex
77
import pkg/opengl
88
from pixie import Image
99
import pkg/sigils
@@ -293,8 +293,6 @@ proc renderFrame*(renderer: Renderer) =
293293
ctx.scale(ctx.pixelScale)
294294

295295
# draw root
296-
# withLock(renderer.lock):
297-
# ctx.renderRoot(renderer.nodes)
298296
ctx.renderRoot(renderer.nodes)
299297

300298
ctx.restoreTransform()
@@ -330,10 +328,11 @@ proc pollAndRender*(renderer: Renderer, updated = false, poll = true) =
330328

331329
if renderer.window.closeRequested:
332330
renderer.frame[].running = false
331+
app.running = false
333332
return
334333

335334
if poll:
336-
windy.pollEvents()
335+
windex.pollEvents()
337336

338337
var cmd: RenderCommands
339338
while renderer.rendInputList.tryRecv(cmd):
@@ -342,19 +341,23 @@ proc pollAndRender*(renderer: Renderer, updated = false, poll = true) =
342341
renderer.nodes = nlayers
343342
update = true
344343
RenderQuit:
344+
echo "QUITTING"
345345
renderer.frame[].running = false
346+
app.running = false
346347
return
347348
RenderSetTitle(name):
348349
renderer.window.title = name
349350

350351
if update:
351-
renderer.updated.store false
352352
renderAndSwap(renderer)
353353

354354
proc runRendererLoop*(renderer: Renderer) =
355355
threadEffects:
356356
RenderThread
357-
while app.running and renderer[].frame[].running:
358-
timeIt(renderAvgTime):
359-
renderer.pollAndRender(false)
360-
os.sleep(renderer.duration.inMilliseconds)
357+
while app.running:
358+
let time =
359+
timeItVar(renderAvgTime):
360+
renderer.pollAndRender(false)
361+
362+
let avgMicros = time.micros.toInt() div 1_000
363+
os.sleep(renderer.duration.inMilliseconds - avgMicros)

src/figuro/renderer/opengl/utils.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ proc openglDebug*() =
3535
cast[cstring](glGetString(GL_SHADING_LANGUAGE_VERSION))
3636

3737
proc setOpenGlHints*() =
38-
# these don't work in windy?
38+
# these don't work in windex?
3939
when defined(setOpenGlHintsEnabled):
4040
if msaa != msaaDisabled:
4141
windowHint(SAMPLES, msaa.cint)

src/figuro/renderer/opengl/window.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import std/[strformat, times, strutils]
22

33
import pkg/[chroma, pixie]
44
import pkg/opengl
5-
import pkg/windy
5+
import pkg/windex
66

77
import utils
88
import glcommons
@@ -23,11 +23,11 @@ when defined(glDebugMessageCallback):
2323
import strformat, strutils
2424

2525
static:
26-
## compile check to ensure windy buttons don't change on us
27-
for i in 0 .. windy.Button.high().int:
26+
## compile check to ensure windex buttons don't change on us
27+
for i in 0 .. windex.Button.high().int:
2828
assert $Button(i) == $UiButton(i)
2929

30-
proc toUi*(wbtn: windy.ButtonView): UiButtonView =
30+
proc toUi*(wbtn: windex.ButtonView): UiButtonView =
3131
when defined(nimscript):
3232
for b in set[Button](wbtn):
3333
result.incl UiButton(b.int)

src/figuro/runtime/utils/timers.nim

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,38 @@ type
88
proc logTiming(name, time: string) =
99
info "timings", name = name, time = time
1010

11-
macro timeIt*(timer, blk: untyped) =
11+
const
12+
timeItSmoothing {.intdefine.} = 10
13+
alpha {.intdefine.} = 1.0 / timeItSmoothing.toFloat
14+
15+
proc timeItImpl*(retVar: bool, timer, blk: NimNode): NimNode =
1216
let name = newStrLitNode timer.repr()
1317
if defined(printDebugTimings):
14-
quote:
15-
var timer {.global, threadvar.}: TimeIt
18+
let timer = genSym(nskVar, "timer")
19+
result = quote do:
20+
var `timer` {.global, threadvar.}: TimeIt
1621
let a = getMonoTime()
1722
`blk`
1823
let b = getMonoTime()
1924
let res = b - a
20-
let micros = res.inMicroseconds
21-
timer.micros = 0.99 * timer.micros + 0.01 * micros.toBiggestFloat
22-
timer.count.inc
23-
if timer.count mod 1_000 == 0:
24-
let num = timer.micros / 1_000.0
25+
let micros = res.inMicroseconds.toFloat
26+
`timer`.micros = alpha * micros + (1.0-alpha) * `timer`.micros
27+
`timer`.count.inc
28+
if `timer`.count mod 1_000 == 0:
29+
let num = `timer`.micros / 1_000.0
2530
logTiming($`name`, $num.formatBiggestFloat(ffDecimal, 3) & " ms")
31+
if retVar:
32+
result.add quote do:
33+
`timer`
2634
else:
27-
quote:
35+
result = quote do:
2836
`blk`
2937

38+
macro timeIt*(timer, blk: untyped) =
39+
result = timeItImpl(false, timer, blk)
40+
macro timeItVar*(timer, blk: untyped): TimeIt =
41+
result = timeItImpl(true, timer, blk)
42+
3043
proc runEveryMillis*(ms: int, repeat: int, code: proc(idx: FrameIdx): bool) =
3144
when false:
3245
var

src/figuro/ui/utils.nim

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,6 @@ type WidgetArgs* =
3131
blk: NimNode,
3232
]
3333

34-
proc parseWidgetArgs*(args: NimNode): WidgetArgs =
35-
## Parses widget args looking for options:
36-
## - `state(int)`
37-
## - `captures(i, x)`
38-
args.expectKind(nnkArgList)
39-
40-
# echo "parseWidgetArgs: ", args.treeRepr
41-
result.id = args[0]
42-
result.id.expectKind(nnkStrLit)
43-
result.blk = args[^1]
44-
45-
for arg in args[1 ..^ 2]:
46-
## iterate through the widget args looking for
47-
## `state(int)` or `captures(i, x)`
48-
##
49-
# if arg.kind == nnkCall:
50-
# let fname = arg[0]
51-
# # if fname.repr == "state":
52-
# # if arg.len() != 2:
53-
# # error "only one type var allowed"
54-
# # result.stateArg = arg[1]
55-
# if fname.repr == "expose":
56-
# if arg.len() > 2:
57-
# error "only no arg or a single name allowed"
58-
# result.bindsArg = newLit(true)
59-
# elif fname.repr == "captures":
60-
# result.capturedVals = nnkBracket.newTree()
61-
# result.capturedVals.add arg[1..^1]
62-
# else:
63-
# error("unexpected paren arguement: " & arg.repr, arg)
64-
if arg.kind == nnkExprEqExpr:
65-
let fname = arg[0]
66-
case fname.repr
67-
of "parent":
68-
result.parentArg = arg[1]
69-
of "captures":
70-
if arg[1].kind == nnkBracket:
71-
result.capturedVals = arg[1]
72-
else:
73-
result.capturedVals = nnkBracket.newTree()
74-
result.capturedVals.add arg[1]
75-
of "expose":
76-
echo "EXP: ", arg.treeRepr
77-
result.bindsArg = newLit(true)
78-
else:
79-
error("unexpected named arguement: " & arg.treerepr, arg)
80-
else:
81-
error("unexpected arguement: " & arg.treerepr, arg)
82-
83-
if result.stateArg.isNil:
84-
result.stateArg = ident"void"
85-
if result.parentArg.isNil:
86-
result.parentArg = ident"node"
87-
# echo "parseWidgetArgs:res: ", result.repr
88-
8934
template wrapCaptures*(hasCaptures, capturedVals, body: untyped): untyped =
9035
when hasCaptures:
9136
capture `capturedVals`:

0 commit comments

Comments
 (0)