Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
improve debugger (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
bognari committed Mar 13, 2022
1 parent e6cd247 commit 377a559
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 28 deletions.
4 changes: 4 additions & 0 deletions korge/src/commonMain/kotlin/com/soywiz/korge/Korge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ object Korge {
clipBorders: Boolean = true,
bgcolor: RGBA? = Colors.BLACK,
debug: Boolean = false,
debugFontExtraScale: Double = 1.0,
debugFontColor: RGBA = Colors.WHITE,
fullscreen: Boolean? = null,
args: Array<String> = arrayOf(),
gameWindow: GameWindow? = null,
Expand Down Expand Up @@ -185,6 +187,8 @@ object Korge {
override val size = SizeInt(virtualWidth, virtualHeight)
})
views.debugViews = debug
views.debugFontExtraScale = debugFontExtraScale
views.debugFontColor = debugFontColor
views.virtualWidth = virtualWidth
views.virtualHeight = virtualHeight
views.scaleAnchor = scaleAnchor
Expand Down
6 changes: 4 additions & 2 deletions korge/src/commonMain/kotlin/com/soywiz/korge/KorgeHeadless.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ object KorgeHeadless {
clipBorders: Boolean = true,
bgcolor: RGBA? = Colors.BLACK,
debug: Boolean = false,
debugFontExtraScale: Double = 1.0,
debugFontColor: RGBA = Colors.WHITE,
fullscreen: Boolean? = null,
args: Array<String> = arrayOf(),
timeProvider: TimeProvider = TimeProvider,
Expand All @@ -51,8 +53,8 @@ object KorgeHeadless {
val gameWindow = HeadlessGameWindow(width, height, draw = draw)
Korge(
title, width, height, virtualWidth, virtualHeight, icon, iconPath, /*iconDrawable,*/ imageFormats, quality,
targetFps, scaleAnchor, scaleMode, clipBorders, bgcolor, debug, fullscreen, args,
gameWindow, timeProvider, injector,
targetFps, scaleAnchor, scaleMode, clipBorders, bgcolor, debug, debugFontExtraScale, debugFontColor,
fullscreen, args, gameWindow, timeProvider, injector,
blocking = blocking,debugAg = debugAg, entry = entry
)
return gameWindow
Expand Down
33 changes: 25 additions & 8 deletions korge/src/commonMain/kotlin/com/soywiz/korge/input/MouseEvents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.soywiz.korge.internal.*
import com.soywiz.korge.scene.*
import com.soywiz.korgw.*
import com.soywiz.korio.lang.*
import kotlin.math.max
import kotlin.native.concurrent.ThreadLocal
import kotlin.reflect.*

Expand Down Expand Up @@ -59,9 +60,14 @@ class MouseEvents(override val view: View) : MouseComponent, Extra by Extra.Mixi
fun installDebugExtensionOnce(views: Views) {
views.mouseDebugHandlerOnce {
views.debugHandlers += { ctx ->
val scale = ctx.ag.devicePixelRatio
val scale = ctx.ag.computedPixelRatio * ctx.debugExtraFontScale
//val scale = 2.0

val space = max(1 * scale, 2.0)
//println(scale)



var yy = 60.toDouble() * scale
val lineHeight = 8.toDouble() * scale
val mouseHit = mouseHitTest(views)
Expand All @@ -70,23 +76,25 @@ class MouseEvents(override val view: View) : MouseComponent, Extra by Extra.Mixi
renderContext.useBatcher { batch ->
batch.drawQuad(
ctx.getTex(Bitmaps.white),
x = bounds.x.toInt().toFloat(),
y = bounds.y.toInt().toFloat(),
x = bounds.x.toFloat(),
y = bounds.y.toFloat(),
width = bounds.width.toFloat(),
height = bounds.height.toFloat(),
colorMul = RGBA(0xFF, 0, 0, 0x3F),
m = mouseHit.globalMatrix
)
renderContext.drawText(
debugBmpFont,
lineHeight.toDouble(),
mouseHit.toString() + " : " + views.nativeMouseX + "," + views.nativeMouseY,
lineHeight,
"$mouseHit : ${views.nativeMouseX},${views.nativeMouseY}",
x = 0,
y = yy.toInt(),
filtering = false
filtering = false,
colMul = ctx.debugExtraFontColor
)
}
yy += lineHeight
yy += space
}

val mouseHitResultUsed = input.mouseHitResultUsed
Expand All @@ -104,9 +112,18 @@ class MouseEvents(override val view: View) : MouseComponent, Extra by Extra.Mixi
)
var vview = mouseHitResultUsed
while (vview != null) {
renderContext.drawText(debugBmpFont, lineHeight.toDouble(), vview.toString(), x = 0, y = yy.toInt())
vview = vview?.parent
renderContext.drawText(
debugBmpFont,
lineHeight,
vview.toString(),
x = 0,
y = yy.toInt(),
filtering = false,
colMul = ctx.debugExtraFontColor
)
vview = vview.parent
yy += lineHeight
yy += space
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.soywiz.korge.internal.*
import com.soywiz.korge.stat.*
import com.soywiz.korge.view.*
import com.soywiz.korim.bitmap.*
import com.soywiz.korim.color.*
import com.soywiz.korio.async.*
import com.soywiz.korma.geom.*
import kotlin.coroutines.*
Expand Down Expand Up @@ -46,6 +47,8 @@ class RenderContext constructor(
val views: Views? = bp as? Views?

var debugAnnotateView: View? = null
var debugExtraFontScale : Double = 1.0
var debugExtraFontColor : RGBA = Colors.WHITE

var stencilIndex: Int = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal fun ViewsContainer.installFpsDebugOverlay() {
}

views.addDebugRenderer { ctx ->
val scale = ctx.ag.devicePixelRatio
val scale = ctx.ag.computedPixelRatio * ctx.debugExtraFontScale

val fontSize = 8.0 * scale
val currentTime = PerformanceCounter.reference
Expand All @@ -63,7 +63,7 @@ internal fun ViewsContainer.installFpsDebugOverlay() {

fun drawTextWithShadow(text: String, x: Int, y: Int) {
ctx.drawText(debugBmpFont, fontSize, text, x = x + 1, y = y + 1, colMul = Colors.BLACK, filtering = false)
ctx.drawText(debugBmpFont, fontSize, text, x = x, y = y, colMul = Colors.WHITE, filtering = false)
ctx.drawText(debugBmpFont, fontSize, text, x = x, y = y, colMul = ctx.debugExtraFontColor, filtering = false)
}

drawTextWithShadow("FPS: " +
Expand Down
2 changes: 2 additions & 0 deletions korge/src/commonMain/kotlin/com/soywiz/korge/view/Views.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ class Views constructor(

var supportTogglingDebug = true
var debugViews = false
var debugFontExtraScale by renderContext::debugExtraFontScale
var debugFontColor by renderContext::debugExtraFontColor
val debugHandlers = FastArrayList<Views.(RenderContext) -> Unit>()

fun addDebugRenderer(block: Views.(RenderContext) -> Unit) {
Expand Down
11 changes: 10 additions & 1 deletion korgw/src/commonMain/kotlin/com/soywiz/korag/AG.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,20 @@ abstract class AG : AGFeatures, Extra by Extra.Mixin() {
open val maxTextureSize = Size(2048, 2048)

open val devicePixelRatio: Double = 1.0
open val pixelsPerInch: Double get() = 96.0
open val pixelsPerLogicalInchRatio: Double = 1.0
open val pixelsPerInch: Double = defaultPixelsPerInch
// Use this in the debug handler, while allowing people to access raw devicePixelRatio without the noise of window scaling
// I really dont know if "/" or "*" or right but in my mathematical mind "pixelsPerLogicalInchRatio" must increase and not decrease the scale
// maybe it is pixelsPerLogicalInchRatio / devicePixelRatio ?
open val computedPixelRatio: Double get() = devicePixelRatio * pixelsPerLogicalInchRatio

open fun beforeDoRender() {
}

companion object {
const val defaultPixelsPerInch : Double = 96.0
}

inline fun doRender(block: () -> Unit) {
beforeDoRender()
mainRenderBuffer.init()
Expand Down
2 changes: 0 additions & 2 deletions korgw/src/commonMain/kotlin/com/soywiz/korag/OpenglAG.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ abstract class AGOpengl : AG() {
open val webgl: Boolean get() = false
open val webgl2: Boolean get() = false

override var devicePixelRatio: Double = 1.0

override fun contextLost() {
Console.info("AG.contextLost()", this, gl, gl.root)
contextVersion++
Expand Down
2 changes: 1 addition & 1 deletion korgw/src/jsMain/kotlin/com/soywiz/korag/GlExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ open class AGWebgl(val config: AGConfig, val glDecorator: (KmlGl) -> KmlGl = { i

override val nativeComponent: Any = canvas
val tDevicePixelRatio get() = window.devicePixelRatio.toDouble()
override var devicePixelRatio = 1.0; get() = when {
override val devicePixelRatio get() = when {
tDevicePixelRatio <= 0.0 -> 1.0
tDevicePixelRatio.isNaN() -> 1.0
tDevicePixelRatio.isInfinite() -> 1.0
Expand Down
48 changes: 36 additions & 12 deletions korgw/src/jvmMain/kotlin/com/soywiz/korgw/awt/AwtAg.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.soywiz.korgw.awt

import com.soywiz.kgl.*
import com.soywiz.korag.*
import com.soywiz.korgw.osx.*
import com.soywiz.korgw.win32.*
import com.soywiz.korgw.x11.*
import com.soywiz.korio.util.*
import java.awt.*
import com.soywiz.korag.AG
import com.soywiz.korag.AGOpengl
import com.soywiz.korgw.osx.MacKmlGL
import com.soywiz.korgw.win32.Win32KmlGl
import com.soywiz.korgw.x11.X11KmlGl
import com.soywiz.korio.util.OS
import java.awt.Component
import java.awt.GraphicsEnvironment
import java.awt.Toolkit

class AwtAg(override val nativeComponent: Any, private val checkGl: Boolean, logGl: Boolean, val cacheGl: Boolean = false) : AGOpengl() {
override val gles: Boolean = true
Expand All @@ -15,13 +18,34 @@ class AwtAg(override val nativeComponent: Any, private val checkGl: Boolean, log
private var baseLazyGlWithLog: LogKmlGlProxy? = null
private var lazyGl: KmlGlFastProxy? = null

override var devicePixelRatio: Double = 1.0
get() {
// https://stackoverflow.com/questions/20767708/how-do-you-detect-a-retina-display-in-java
val config = (nativeComponent as? Component)?.graphicsConfiguration
?: GraphicsEnvironment.getLocalGraphicsEnvironment().defaultScreenDevice.defaultConfiguration
return config.defaultTransform.scaleX
private val localGraphicsEnvironment : GraphicsEnvironment by lazy(LazyThreadSafetyMode.PUBLICATION) {
GraphicsEnvironment.getLocalGraphicsEnvironment()
}

override val devicePixelRatio: Double get() {
if (GraphicsEnvironment.isHeadless()) {
return super.devicePixelRatio
}
// transform
// https://stackoverflow.com/questions/20767708/how-do-you-detect-a-retina-display-in-java
val config = (nativeComponent as? Component)?.graphicsConfiguration
?: localGraphicsEnvironment.defaultScreenDevice.defaultConfiguration
return config.defaultTransform.scaleX
}

override val pixelsPerInch: Double by lazy(LazyThreadSafetyMode.PUBLICATION) {
if (GraphicsEnvironment.isHeadless()) {
return@lazy Companion.defaultPixelsPerInch
}
// maybe this is not just windows specific :
// https://stackoverflow.com/questions/32586883/windows-scaling
// somehow this value is not update when you change the scaling in the windows settings while the jvm is running :(
return@lazy Toolkit.getDefaultToolkit().screenResolution.toDouble()
}

override val pixelsPerLogicalInchRatio: Double by lazy(LazyThreadSafetyMode.PUBLICATION) {
pixelsPerInch / defaultPixelsPerInch
}

var logGl: Boolean = logGl
set(value) {
Expand Down

0 comments on commit 377a559

Please sign in to comment.