Skip to content

Commit

Permalink
Update the debug method to print values on the screen
Browse files Browse the repository at this point in the history
  • Loading branch information
dwursteisen committed Nov 26, 2023
1 parent a240538 commit cb74f90
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 6 deletions.
5 changes: 5 additions & 0 deletions tiny-doc/src/docs/asciidoc/sample/_engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function popup(logo, text, color, keep)
forever = keep
end

function printDebug(index, text, color)
shape.rectf(0, index * 6, #text * 6 + 6, 6, color)
print(text, 6, index * 6 + 1, "#FFFFFF")
end

function clear()
dt = 0
forever = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class GameEngine(

private var numberOfResources: Int = 0

private val debugMessages = mutableListOf<DebugMessage>()

private lateinit var scripts: Array<GameScript?>
private lateinit var spriteSheets: Array<SpriteSheet?>
private lateinit var levels: Array<GameLevel?>
Expand Down Expand Up @@ -335,11 +337,26 @@ class GameEngine(
popup("screenshot PNG", "#00FF00")
platform.screenshot()
}

debugMessages.forEachIndexed { index, debugMessage ->
val (msg, color) = debugMessage
engineGameScript?.invoke(
"printDebug",
valueOf(index),
valueOf(msg),
valueOf(color),
)
}
debugMessages.clear()
inputManager.reset()
}
}
}

override fun debug(str: DebugMessage) {
debugMessages.add(str)
}

private suspend fun GameEngine.popupError(ex: LuaError) {
val errorLine = ex.errorLine()
logger.warn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.github.minigdx.tiny.resources.GameScript
import com.github.minigdx.tiny.resources.Sound
import com.github.minigdx.tiny.resources.SpriteSheet

data class DebugMessage(val mesage: String, val color: String)

/**
* Descriptor to access the game resource
*/
Expand Down Expand Up @@ -39,4 +41,10 @@ interface GameResourceAccess {
* Find a script by its name.
*/
fun script(name: String): GameScript?

/**
* Print a message over the current screen,
* after the game rendered.
*/
fun debug(str: DebugMessage) = Unit
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class SprLib(val gameOptions: GameOptions, val resourceAccess: GameResourceAcces
return invoke(arrayOf(a, b, c, valueOf(false), valueOf(false))).arg1()
}

@TinyCall("Draw a sprite and allow flip on x or y axis.")
override fun invoke(@TinyArgs(["sprN", "x", "y", "flipX", "flipY"]) args: Varargs): Varargs {
if (args.narg() < 3) return NONE
val sprN = args.arg(1).checkint()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import com.github.mingdx.tiny.doc.TinyArgs
import com.github.mingdx.tiny.doc.TinyCall
import com.github.mingdx.tiny.doc.TinyFunction
import com.github.mingdx.tiny.doc.TinyLib
import com.github.minigdx.tiny.engine.DebugMessage
import com.github.minigdx.tiny.engine.GameOptions
import com.github.minigdx.tiny.engine.GameResourceAccess
import org.luaj.vm2.LuaTable
import org.luaj.vm2.LuaValue
import org.luaj.vm2.Varargs
import org.luaj.vm2.lib.LibFunction
import org.luaj.vm2.lib.OneArgFunction
import org.luaj.vm2.lib.TwoArgFunction
import org.luaj.vm2.lib.VarArgFunction

Expand Down Expand Up @@ -120,13 +120,17 @@ class StdLib(
}

@TinyFunction("Print in the console a value to help debugging")
internal inner class debug : OneArgFunction() {
internal inner class debug : TwoArgFunction() {

@TinyCall("Print in the console a value")
override fun call(arg: LuaValue): LuaValue {
println("🐞 -> $arg")
return NONE
override fun call(arg1: LuaValue, arg2: LuaValue): LuaValue {
val message = arg1.optjstring("")!!
val color = arg2.optjstring("#32CD32")!!
resourceAccess.debug(DebugMessage(message, color))
return NIL
}

@TinyCall("Print in the console a value")
override fun call(arg: LuaValue): LuaValue = super.call(arg)
}

@TinyFunction("Print on the screen a string.", example = STD_PRINT_EXAMPLE)
Expand Down
5 changes: 5 additions & 0 deletions tiny-engine/src/commonMain/resources/_engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function popup(logo, text, color, keep)
forever = keep
end

function printDebug(index, text, color)
shape.rectf(0, index * 6, #text * 6 + 6, 6, color)
print(text, 6, index * 6 + 1, "#FFFFFF")
end

function clear()
dt = 0
forever = false
Expand Down
5 changes: 5 additions & 0 deletions tiny-web-editor/src/jsMain/resources/_engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ function popup(logo, text, color, keep)
forever = keep
end

function printDebug(index, text, color)
shape.rectf(0, index * 6, #text * 6 + 6, 6, color)
print(text, 6, index * 6 + 1, "#FFFFFF")
end

function clear()
dt = 0
forever = false
Expand Down

0 comments on commit cb74f90

Please sign in to comment.