You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- start at 100 to guarantee trigger both of them at first frame
@@ -1272,12 +1272,10 @@ function love.run()
1272
1272
1273
1273
-- Main loop
1274
1274
returnfunction()
1275
-
localtime=timer()
1275
+
-- Loop start time
1276
+
localloopT=timer()
1276
1277
STEP()
1277
1278
1278
-
-- local loopDT=time-lastLoopTime
1279
-
lastLoopTime=time
1280
-
1281
1279
-- EVENT
1282
1280
ifPUMPthen
1283
1281
PUMP()
@@ -1296,8 +1294,8 @@ function love.run()
1296
1294
ifupdateCounter>=100then
1297
1295
updateCounter=updateCounter-100
1298
1296
1299
-
localupdateDT=time-lastUpdateTime
1300
-
lastUpdateTime=time
1297
+
localupdateDT=loopT-lastUpdateTime
1298
+
lastUpdateTime=loopT
1301
1299
1302
1300
ifmouseShowthenmouse_update(updateDT) end
1303
1301
ifnext(jsState) thengp_update(jsState[1],updateDT) end
@@ -1321,8 +1319,8 @@ function love.run()
1321
1319
ifdrawCounter>=100then
1322
1320
drawCounter=drawCounter-100
1323
1321
1324
-
localdrawDT=time-lastDrawTime
1325
-
lastDrawTime=time
1322
+
localdrawDT=loopT-lastDrawTime
1323
+
lastDrawTime=loopT
1326
1324
1327
1325
gc_replaceTransform(SCR.origin)
1328
1326
BG._draw()
@@ -1337,7 +1335,7 @@ function love.run()
1337
1335
gc_replaceTransform(xOy)
1338
1336
SYSFX._draw()
1339
1337
TEXT.draw(TEXT)
1340
-
ifmouseShowthenglobalEvent.drawCursor(mx,my,time) end
1338
+
ifmouseShowthenglobalEvent.drawCursor(mx,my,loopT) end
1341
1339
gc_replaceTransform(SCR.xOy_ul)
1342
1340
globalEvent.drawSysInfo()
1343
1341
gc_replaceTransform(xOy)
@@ -1402,9 +1400,9 @@ function love.run()
1402
1400
end
1403
1401
1404
1402
-- Check screen size
1405
-
iftime-lastScreenCheckTime>1.26and (gc.getWidth()~=SCR.worgc.getHeight()~=SCR.h) then
1403
+
ifloopT-lastScreenCheckTime>1and (gc.getWidth()~=SCR.worgc.getHeight()~=SCR.h) then
1406
1404
love.resize(gc.getWidth(),gc.getHeight())
1407
-
lastScreenCheckTime=time
1405
+
lastScreenCheckTime=loopT
1408
1406
end
1409
1407
1410
1408
-- Slow devmode
@@ -1416,9 +1414,9 @@ function love.run()
1416
1414
end
1417
1415
end
1418
1416
1419
-
localcurFrameInterval=timer()-lastLoopTime
1420
-
ifcurFrameInterval<sleepInterval*.9626thenSLEEP(sleepInterval*.9626-curFrameInterval) end
1421
-
whiletimer()-lastLoopTime<sleepIntervaldoend
1417
+
localtimeRemain=loopT+mainLoopInterval-timer()
1418
+
iftimeRemain>sleepDurationErrorthenSLEEP(timeRemain-sleepDurationError) end
1419
+
whiletimer()-loopT<mainLoopIntervaldoend
1422
1420
end
1423
1421
end
1424
1422
@@ -1500,10 +1498,30 @@ function ZENITHA.setCleanCanvas(bool)
1500
1498
end
1501
1499
1502
1500
---Set the max update rate of main loop cycle
1503
-
---@paramfpsnumber Default to 60
1504
-
functionZENITHA.setMaxFPS(fps)
1505
-
assert(type(fps)=='number' andfps>0,"ZENITHA.setMaxFPS(fps): Need >0")
1506
-
sleepInterval=1/fps
1501
+
---@paramlpsnumber Loop/sec, default to 60
1502
+
functionZENITHA.setMainLoopSpeed(lps)
1503
+
assert(type(lps)=='number' andlps>0,"ZENITHA.setMainLoopSpeed(lps): Need >0")
1504
+
mainLoopInterval=1/lps
1505
+
end
1506
+
1507
+
---Set the sleep duration error to balance accuracy & performance of main-loop-frequency
1508
+
---
1509
+
---Recommend value:
1510
+
---| Mode \| | Value |
1511
+
---| -: | :-: |
1512
+
---| Accuracy \| | 🪟1.0, 🐧0.5 |
1513
+
---| Normal \| | 0 |
1514
+
---| Performance \| | -0.5 |
1515
+
---| Power-Saving \| | -1.0 |
1516
+
---
1517
+
---How this works: Because `love.timer.sleep(t)` is not accurate enough (always a bit more time), so we can sleep `[setting value] LESS`, then busy-wait to obtain the exact time interval.
1518
+
---
1519
+
---But `sleep()` actually only accept integer microsecond value, so when we need to sleep 1.5ms, doing `sleep(1.5ms)` is same as `sleep(1ms)`, so busy-wait will still work for ~0.5ms.
1520
+
---That's why we accept negative number. Setting error to -1ms means we will do `sleep(2.5ms)` when we need 1.5, so busy-wait is guaranteed not to be triggered, saving more resource.
1521
+
---@parammsnumber in [-1,1], default to 0 (ms)
1522
+
functionZENITHA.setSleepDurationError(ms)
1523
+
assert(type(ms)=='number' andms>-1andms<1,"ZENITHA.setSleepFault(ms): Need in [-1,1]")
0 commit comments