2024-02-05 18:06:13 +00:00
|
|
|
; Copyright (c) 2019-2024 David Vogel
|
2019-10-20 14:32:48 +00:00
|
|
|
;
|
|
|
|
; This software is released under the MIT License.
|
|
|
|
; https://opensource.org/licenses/MIT
|
|
|
|
|
2024-02-08 15:12:55 +00:00
|
|
|
EnableExplicit
|
|
|
|
|
2019-10-20 14:32:48 +00:00
|
|
|
UsePNGImageEncoder()
|
2019-10-20 14:28:17 +00:00
|
|
|
|
|
|
|
Declare Worker(*Dummy)
|
|
|
|
|
|
|
|
Structure QueueElement
|
2019-11-01 01:40:21 +00:00
|
|
|
img.i
|
|
|
|
x.i
|
|
|
|
y.i
|
2022-07-24 20:05:34 +00:00
|
|
|
sx.i
|
|
|
|
sy.i
|
2019-10-20 14:28:17 +00:00
|
|
|
EndStructure
|
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
Structure GLViewportDims
|
2024-02-05 18:06:13 +00:00
|
|
|
x.i
|
|
|
|
y.i
|
|
|
|
width.i
|
|
|
|
height.i
|
2024-01-04 18:36:36 +00:00
|
|
|
EndStructure
|
2019-11-01 01:40:21 +00:00
|
|
|
|
2024-02-05 18:00:39 +00:00
|
|
|
Structure WorkerInfo
|
2024-02-05 18:06:13 +00:00
|
|
|
workerNumber.i
|
2024-02-05 18:00:39 +00:00
|
|
|
EndStructure
|
|
|
|
|
2024-02-08 15:12:55 +00:00
|
|
|
#Workers = 8
|
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
; Returns the size of the main OpenGL rendering output.
|
|
|
|
ProcedureDLL GetGLViewportSize(*dims.GLViewportDims)
|
|
|
|
If Not *dims
|
|
|
|
ProcedureReturn #False
|
2019-11-01 01:40:21 +00:00
|
|
|
EndIf
|
2024-01-04 18:36:36 +00:00
|
|
|
|
|
|
|
glGetIntegerv_(#GL_VIEWPORT, *dims)
|
|
|
|
|
|
|
|
ProcedureReturn #True
|
2019-11-01 01:40:21 +00:00
|
|
|
EndProcedure
|
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
; Returns the size of the main OpenGL rendering output as a windows RECT.
|
2019-11-01 01:40:21 +00:00
|
|
|
ProcedureDLL GetRect(*rect.RECT)
|
|
|
|
If Not *rect
|
|
|
|
ProcedureReturn #False
|
|
|
|
EndIf
|
2024-02-05 18:06:13 +00:00
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
Protected dims.GLViewportDims
|
|
|
|
glGetIntegerv_(#GL_VIEWPORT, dims)
|
2024-02-05 18:06:13 +00:00
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
*rect\left = dims\x
|
|
|
|
*rect\top = dims\y
|
|
|
|
*rect\right = dims\x + dims\width
|
|
|
|
*rect\bottom = dims\y + dims\height
|
2019-11-01 01:40:21 +00:00
|
|
|
|
|
|
|
ProcedureReturn #True
|
|
|
|
EndProcedure
|
|
|
|
|
2019-10-20 14:28:17 +00:00
|
|
|
ProcedureDLL AttachProcess(Instance)
|
2019-11-01 01:40:21 +00:00
|
|
|
Global Semaphore = CreateSemaphore()
|
|
|
|
Global Mutex = CreateMutex()
|
|
|
|
Global NewList Queue.QueueElement()
|
2019-10-25 18:25:41 +00:00
|
|
|
|
2019-11-01 01:40:21 +00:00
|
|
|
CreateDirectory("mods/noita-mapcap/output/")
|
2019-10-25 18:25:41 +00:00
|
|
|
|
2024-02-08 15:12:55 +00:00
|
|
|
Static Dim WorkerInfos.WorkerInfo(#Workers-1)
|
|
|
|
Protected i
|
|
|
|
For i = 0 To #Workers-1
|
2024-02-05 18:06:13 +00:00
|
|
|
WorkerInfos(i)\workerNumber = i
|
2024-02-05 18:00:39 +00:00
|
|
|
CreateThread(@Worker(), @WorkerInfos(i))
|
2019-11-01 01:40:21 +00:00
|
|
|
Next
|
2019-10-20 14:28:17 +00:00
|
|
|
EndProcedure
|
|
|
|
|
2024-02-05 18:00:39 +00:00
|
|
|
Procedure Worker(*workerInfo.WorkerInfo)
|
2024-02-08 15:12:55 +00:00
|
|
|
Protected img, x, y, sx, sy
|
2019-11-01 01:40:21 +00:00
|
|
|
|
|
|
|
Repeat
|
|
|
|
WaitSemaphore(Semaphore)
|
|
|
|
|
|
|
|
LockMutex(Mutex)
|
|
|
|
FirstElement(Queue())
|
|
|
|
img = Queue()\img
|
|
|
|
x = Queue()\x
|
|
|
|
y = Queue()\y
|
2022-07-24 20:05:34 +00:00
|
|
|
sx = Queue()\sx
|
|
|
|
sy = Queue()\sy
|
2019-11-01 01:40:21 +00:00
|
|
|
DeleteElement(Queue())
|
|
|
|
UnlockMutex(Mutex)
|
2024-01-04 18:36:36 +00:00
|
|
|
|
2022-07-24 20:05:34 +00:00
|
|
|
If sx > 0 And sy > 0
|
|
|
|
ResizeImage(img, sx, sy)
|
|
|
|
EndIf
|
2019-11-01 01:40:21 +00:00
|
|
|
|
2024-02-05 18:06:13 +00:00
|
|
|
; Save image temporary, and only move it once it's fully exported.
|
|
|
|
; This prevents images getting corrupted when the main process crashes.
|
2024-02-05 18:00:39 +00:00
|
|
|
If SaveImage(img, "mods/noita-mapcap/output/worker_" + *workerInfo\workerNumber + ".tmp", #PB_ImagePlugin_PNG)
|
2024-02-05 18:06:13 +00:00
|
|
|
RenameFile("mods/noita-mapcap/output/worker_" + *workerInfo\workerNumber + ".tmp", "mods/noita-mapcap/output/" + x + "," + y + ".png")
|
|
|
|
; We can't really do anything when either SaveImage or RenameFile fails, so just silently fail.
|
2024-02-05 18:00:39 +00:00
|
|
|
EndIf
|
2019-11-01 01:40:21 +00:00
|
|
|
|
|
|
|
FreeImage(img)
|
|
|
|
ForEver
|
2019-10-20 14:28:17 +00:00
|
|
|
EndProcedure
|
|
|
|
|
2022-07-24 20:05:34 +00:00
|
|
|
; Takes a screenshot of the client area of this process' active window.
|
2024-01-04 18:36:36 +00:00
|
|
|
; The portion of the client area that is captured is described by capRect, which is in viewport coordinates.
|
2022-07-24 20:05:34 +00:00
|
|
|
; x and y defines the top left position of the captured rectangle in scaled world coordinates. The scale depends on the window to world pixel ratio.
|
|
|
|
; sx and sy defines the final dimensions that the screenshot will be resized to. No resize will happen if set to 0.
|
|
|
|
ProcedureDLL Capture(*capRect.RECT, x.l, y.l, sx.l, sy.l)
|
2024-01-04 18:36:36 +00:00
|
|
|
Protected viewportRect.RECT
|
|
|
|
If Not GetRect(@viewportRect)
|
2020-10-17 22:39:58 +00:00
|
|
|
ProcedureReturn #False
|
|
|
|
EndIf
|
2019-11-01 01:40:21 +00:00
|
|
|
|
2024-02-08 15:12:55 +00:00
|
|
|
Protected imageID, hDC, *pixelBuffer
|
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
; Limit the desired capture area to the actual client area of the viewport.
|
2022-07-24 20:05:34 +00:00
|
|
|
If *capRect\left < 0 : *capRect\left = 0 : EndIf
|
|
|
|
If *capRect\top < 0 : *capRect\top = 0 : EndIf
|
2024-01-04 18:36:36 +00:00
|
|
|
If *capRect\right < *capRect\left : *capRect\right = *capRect\left : EndIf
|
|
|
|
If *capRect\bottom < *capRect\top : *capRect\bottom = *capRect\top : EndIf
|
|
|
|
If *capRect\right > viewportRect\right : *capRect\right = viewportRect\right : EndIf
|
|
|
|
If *capRect\bottom > viewportRect\bottom : *capRect\bottom = viewportRect\bottom : EndIf
|
2024-02-05 18:06:13 +00:00
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
Protected capWidth = *capRect\right - *capRect\left
|
|
|
|
Protected capHeight = *capRect\bottom - *capRect\top
|
2024-02-05 18:06:13 +00:00
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
imageID = CreateImage(#PB_Any, capWidth, capHeight)
|
2019-11-01 01:40:21 +00:00
|
|
|
If Not imageID
|
|
|
|
ProcedureReturn #False
|
|
|
|
EndIf
|
2024-02-05 18:06:13 +00:00
|
|
|
|
2019-11-01 01:40:21 +00:00
|
|
|
hDC = StartDrawing(ImageOutput(imageID))
|
|
|
|
If Not hDC
|
|
|
|
FreeImage(imageID)
|
|
|
|
ProcedureReturn #False
|
|
|
|
EndIf
|
2024-02-05 18:06:13 +00:00
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
*pixelBuffer = DrawingBuffer()
|
2024-02-05 18:06:13 +00:00
|
|
|
glReadPixels_(*capRect\left, *capRect\top, capWidth, capHeight, #GL_BGR_EXT, #GL_UNSIGNED_BYTE, *pixelBuffer)
|
2024-04-11 09:39:35 +00:00
|
|
|
If glGetError_() <> #GL_NO_ERROR
|
|
|
|
StopDrawing()
|
|
|
|
FreeImage(imageID)
|
|
|
|
ProcedureReturn #False
|
|
|
|
EndIf
|
2019-11-01 01:40:21 +00:00
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
StopDrawing()
|
2019-11-01 01:40:21 +00:00
|
|
|
|
|
|
|
LockMutex(Mutex)
|
2022-07-24 20:05:34 +00:00
|
|
|
; Check if the queue has too many elements, if so, wait. (Emulate go's channels)
|
|
|
|
While ListSize(Queue()) > 1
|
2019-11-01 01:40:21 +00:00
|
|
|
UnlockMutex(Mutex)
|
2022-07-24 20:05:34 +00:00
|
|
|
Delay(1)
|
2019-11-01 01:40:21 +00:00
|
|
|
LockMutex(Mutex)
|
|
|
|
Wend
|
|
|
|
LastElement(Queue())
|
|
|
|
AddElement(Queue())
|
|
|
|
Queue()\img = imageID
|
2022-07-24 20:05:34 +00:00
|
|
|
Queue()\x = x
|
|
|
|
Queue()\y = y
|
|
|
|
Queue()\sx = sx
|
|
|
|
Queue()\sy = sy
|
2019-11-01 01:40:21 +00:00
|
|
|
UnlockMutex(Mutex)
|
|
|
|
|
|
|
|
SignalSemaphore(Semaphore)
|
|
|
|
|
|
|
|
ProcedureReturn #True
|
2019-10-20 14:28:17 +00:00
|
|
|
EndProcedure
|
|
|
|
|
2019-11-01 01:40:21 +00:00
|
|
|
; #### Test
|
|
|
|
;AttachProcess(0)
|
|
|
|
;OpenWindow(0, 100, 200, 195, 260, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
|
|
|
|
;Delay(1000)
|
|
|
|
;Capture(123, 123)
|
|
|
|
;Delay(1000)
|
|
|
|
|
2024-01-04 18:36:36 +00:00
|
|
|
; IDE Options = PureBasic 6.04 LTS (Windows - x64)
|
2019-10-20 14:28:17 +00:00
|
|
|
; ExecutableFormat = Shared dll
|
2024-04-11 09:39:35 +00:00
|
|
|
; CursorPosition = 99
|
|
|
|
; FirstLine = 72
|
2024-01-04 18:36:36 +00:00
|
|
|
; Folding = -
|
2022-07-24 20:05:34 +00:00
|
|
|
; Optimizer
|
2019-10-20 14:28:17 +00:00
|
|
|
; EnableThread
|
|
|
|
; EnableXP
|
|
|
|
; Executable = capture.dll
|
2024-02-05 18:00:39 +00:00
|
|
|
; DisableDebugger
|
|
|
|
; Compiler = PureBasic 6.04 LTS - C Backend (Windows - x86)
|