Skip to content

Commit

Permalink
Fixes clipping support when capturing the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
hobnob committed Oct 17, 2024
1 parent 6fa99c9 commit 014f92f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 014f92f

Please sign in to comment.