diff --git a/indigo/indigo/src/main/scala/indigo/platform/renderer/Renderer.scala b/indigo/indigo/src/main/scala/indigo/platform/renderer/Renderer.scala index 7e9d1db0f..c2a8a5f25 100644 --- a/indigo/indigo/src/main/scala/indigo/platform/renderer/Renderer.scala +++ b/indigo/indigo/src/main/scala/indigo/platform/renderer/Renderer.scala @@ -21,20 +21,18 @@ trait Renderer: def init(shaders: Set[RawShaderCode]): Unit def drawScene(sceneData: ProcessedSceneData, runningTime: Seconds): Unit def captureScreen( - clippingRect: Rectangle, + clippingRect: Option[Rectangle], excludeLayers: Batch[BindingKey], imageType: ImageType ): ImageData - def captureScreen(): ImageData = captureScreen(Rectangle(Size(screenWidth, screenHeight)), Batch.empty, ImageType.PNG) - def captureScreen(clippingRect: Rectangle): ImageData = captureScreen(clippingRect, Batch.empty, ImageType.PNG) + def captureScreen(): ImageData = captureScreen(None, Batch.empty, ImageType.PNG) + def captureScreen(clippingRect: Rectangle): ImageData = captureScreen(Some(clippingRect), Batch.empty, ImageType.PNG) def captureScreen(clippingRect: Rectangle, imageType: ImageType): ImageData = - captureScreen(clippingRect, Batch.empty, imageType) - def captureScreen(excludeLayers: Batch[BindingKey]): ImageData = - captureScreen(Rectangle(Size(screenWidth, screenHeight)), excludeLayers, ImageType.PNG) + captureScreen(Some(clippingRect), Batch.empty, imageType) + def captureScreen(excludeLayers: Batch[BindingKey]): ImageData = captureScreen(None, excludeLayers, ImageType.PNG) def captureScreen(excludeLayers: Batch[BindingKey], imageType: ImageType): ImageData = - captureScreen(Rectangle(Size(screenWidth, screenHeight)), excludeLayers, imageType) - def captureScreen(imageType: ImageType): ImageData = - captureScreen(Rectangle(Size(screenWidth, screenHeight)), Batch.empty, imageType) + captureScreen(None, excludeLayers, imageType) + def captureScreen(imageType: ImageType): ImageData = captureScreen(None, Batch.empty, imageType) object Renderer: def blackHole = new Renderer { @@ -46,7 +44,7 @@ object Renderer: def init(shaders: Set[RawShaderCode]): Unit = () def drawScene(sceneData: ProcessedSceneData, runningTime: Seconds): Unit = () def captureScreen( - clippingRect: Rectangle, + clippingRect: Option[Rectangle], excludeLayers: Batch[BindingKey], imageType: ImageType ): ImageData = ImageData(0, ImageType.PNG, Array.emptyByteArray) diff --git a/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl1/RendererWebGL1.scala b/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl1/RendererWebGL1.scala index 8ff9ecc8d..2b9cc60a9 100644 --- a/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl1/RendererWebGL1.scala +++ b/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl1/RendererWebGL1.scala @@ -106,15 +106,22 @@ final class RendererWebGL1( private given CanEqual[Option[Int], Option[Int]] = CanEqual.derived def captureScreen( - clippingRect: Rectangle, + clippingRect: Option[Rectangle], excludeLayers: Batch[BindingKey], imageType: ImageType ): ImageData = { val canvas = dom.document.createElement("canvas").asInstanceOf[html.Canvas] val ctx2d = canvas.getContext("2d", cNc.context.getContextAttributes()).asInstanceOf[dom.CanvasRenderingContext2D] + val magnifiedClip = clippingRect match { + case Some(rect) => rect * cNc.magnification + case None => Rectangle(0, 0, screenWidth, screenHeight) + } + + canvas.width = magnifiedClip.width + canvas.height = magnifiedClip.height - canvas.width = clippingRect.width - canvas.height = clippingRect.height + val prevSceneData = _prevSceneData + val prevGameRuntime = _prevGameRuntime drawScene( ProcessedSceneData( @@ -132,16 +139,19 @@ final class RendererWebGL1( _prevGameRuntime ) + _prevSceneData = prevSceneData + _prevGameRuntime = prevGameRuntime + ctx2d.drawImage( cNc.canvas, - clippingRect.x, - clippingRect.y, - clippingRect.width, - clippingRect.height, + magnifiedClip.x, + magnifiedClip.y, + magnifiedClip.width, + magnifiedClip.height, 0, 0, - clippingRect.width, - clippingRect.height + magnifiedClip.width, + magnifiedClip.height ) val dataUrl = canvas.toDataURL(imageType.toString()) canvas.remove() diff --git a/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl2/RendererWebGL2.scala b/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl2/RendererWebGL2.scala index d307ad680..7081a5642 100644 --- a/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl2/RendererWebGL2.scala +++ b/indigo/indigo/src/main/scala/indigo/platform/renderer/webgl2/RendererWebGL2.scala @@ -237,15 +237,22 @@ final class RendererWebGL2( private given CanEqual[Option[Int], Option[Int]] = CanEqual.derived def captureScreen( - clippingRect: Rectangle, + clippingRect: Option[Rectangle], excludeLayers: Batch[BindingKey], imageType: ImageType ): ImageData = { val canvas = dom.document.createElement("canvas").asInstanceOf[html.Canvas] val ctx2d = canvas.getContext("2d", cNc.context.getContextAttributes()).asInstanceOf[dom.CanvasRenderingContext2D] + val magnifiedClip = clippingRect match { + case Some(rect) => rect * cNc.magnification + case None => Rectangle(0, 0, screenWidth, screenHeight) + } + + canvas.width = magnifiedClip.width + canvas.height = magnifiedClip.height - canvas.width = clippingRect.width - canvas.height = clippingRect.height + val prevSceneData = _prevSceneData + val prevGameRuntime = _prevGameRuntime drawScene( ProcessedSceneData( @@ -263,17 +270,19 @@ final class RendererWebGL2( _prevGameRuntime ) - println(s"magnification: ${cNc.magnification}, config magnification: ${config.magnification}") + _prevSceneData = prevSceneData + _prevGameRuntime = prevGameRuntime + ctx2d.drawImage( cNc.canvas, - clippingRect.x * cNc.magnification, - clippingRect.y * cNc.magnification, - clippingRect.width * cNc.magnification, - clippingRect.height * cNc.magnification, + magnifiedClip.x, + magnifiedClip.y, + magnifiedClip.width, + magnifiedClip.height, 0, 0, - clippingRect.width * cNc.magnification, - clippingRect.height * cNc.magnification + magnifiedClip.width, + magnifiedClip.height ) val dataUrl = canvas.toDataURL(imageType.toString()) canvas.remove() diff --git a/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/CaptureScreenScene.scala b/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/CaptureScreenScene.scala index 76d709f22..4aa07e3a9 100644 --- a/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/CaptureScreenScene.scala +++ b/indigo/sandbox/src/main/scala/com/example/sandbox/scenes/CaptureScreenScene.scala @@ -49,7 +49,7 @@ object CaptureScreenScene extends Scene[SandboxStartupData, SandboxGameModel, Sa case MouseEvent.Click(x, y) if x >= 250 && x <= 266 && y >= 165 && y <= 181 => // Open a window with the captured image println(context.captureScreen(Batch(uiKey)).getDataUrl) - println(context.captureScreen(clippingRect).getDataUrl) + println(context.captureScreen(clippingRect.expand(Size(1, 1))).getDataUrl) Outcome(model) case _ => Outcome(model) }