Skip to content

Commit 44605b9

Browse files
committed
Move coroutine wakeup back into OnWorldPreUpdate
1 parent d5cd88a commit 44605b9

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

.vscode/settings.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"downscaling",
1414
"DPMM",
1515
"executables",
16+
"framebuffer",
17+
"framebuffers",
1618
"Fullscreen",
1719
"goarch",
1820
"gridify",

files/capture.lua

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright (c) 2019-2023 David Vogel
1+
-- Copyright (c) 2019-2024 David Vogel
22
--
33
-- This software is released under the MIT License.
44
-- https://opensource.org/licenses/MIT
@@ -108,9 +108,13 @@ local function captureScreenshot(pos, ensureLoaded, dontOverwrite, ctx, outputPi
108108
-- Suspend UI drawing for 1 frame.
109109
UI:SuspendDrawing(1)
110110

111-
--wait(0)
111+
-- First we wait one frame for the current state to be drawn.
112+
wait(0)
113+
114+
-- At this point the needed frame is fully drawn, but the framebuffers are swapped.
112115

113116
-- Recalculate capture position and rectangle if we are not forcing any capture position.
117+
-- We are in the `OnWorldPreUpdate` hook, this means that `CameraAPI.GetPos` return the position of the last frame.
114118
if not pos then
115119
topLeftCapture, bottomRightCapture, topLeftWorld, bottomRightWorld = calculateCaptureRectangle(pos)
116120
if outputPixelScale > 0 then
@@ -120,8 +124,9 @@ local function captureScreenshot(pos, ensureLoaded, dontOverwrite, ctx, outputPi
120124
end
121125
end
122126

123-
-- Wait for two frames.
124-
wait(1)
127+
-- Wait another frame.
128+
-- After this `wait` the framebuffer will be swapped again, and we can grab the correct frame.
129+
wait(0)
125130

126131
-- The top left world position needs to be upscaled by the pixel scale.
127132
-- Otherwise it's not possible to stitch the images correctly.

init.lua

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Copyright (c) 2022 David Vogel
1+
-- Copyright (c) 2022-2024 David Vogel
22
--
33
-- This software is released under the MIT License.
44
-- https://opensource.org/licenses/MIT
@@ -109,19 +109,13 @@ end
109109
function OnWorldPreUpdate()
110110
Message:CatchException("OnWorldPreUpdate", function()
111111
-- Coroutines aren't run every frame in this lua sandbox, do it manually here.
112-
--wake_up_waiting_threads(1)
112+
wake_up_waiting_threads(1)
113113

114114
end)
115115
end
116116

117117
---Called *every* time the game has finished updating the world.
118118
function OnWorldPostUpdate()
119-
Message:CatchException("OnWorldPreUpdate", function()
120-
-- Coroutines aren't run every frame in this lua sandbox, do it manually here.
121-
wake_up_waiting_threads(1)
122-
123-
end)
124-
125119
Message:CatchException("OnWorldPostUpdate", function()
126120
-- Reload mod every 60 frames.
127121
-- This allows live updates to the mod while Noita is running.

0 commit comments

Comments
 (0)