diff --git a/tiny-doc/src/docs/asciidoc/sample/_engine.lua b/tiny-doc/src/docs/asciidoc/sample/_engine.lua index a24307c3..b3a6e352 100644 --- a/tiny-doc/src/docs/asciidoc/sample/_engine.lua +++ b/tiny-doc/src/docs/asciidoc/sample/_engine.lua @@ -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 diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameEngine.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameEngine.kt index 477775b4..a9df78e6 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameEngine.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameEngine.kt @@ -88,6 +88,8 @@ class GameEngine( private var numberOfResources: Int = 0 + private val debugMessages = mutableListOf() + private lateinit var scripts: Array private lateinit var spriteSheets: Array private lateinit var levels: Array @@ -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( diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameResourceAccess.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameResourceAccess.kt index 6bf38a4d..9499fbcc 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameResourceAccess.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/engine/GameResourceAccess.kt @@ -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 */ @@ -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 } diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SprLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SprLib.kt index e1e9295f..d4cd9b0b 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SprLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SprLib.kt @@ -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() diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt index bde796de..cce434ae 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/StdLib.kt @@ -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 @@ -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) diff --git a/tiny-engine/src/commonMain/resources/_engine.lua b/tiny-engine/src/commonMain/resources/_engine.lua index a24307c3..b3a6e352 100644 --- a/tiny-engine/src/commonMain/resources/_engine.lua +++ b/tiny-engine/src/commonMain/resources/_engine.lua @@ -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 diff --git a/tiny-web-editor/src/jsMain/resources/_engine.lua b/tiny-web-editor/src/jsMain/resources/_engine.lua index a24307c3..b3a6e352 100644 --- a/tiny-web-editor/src/jsMain/resources/_engine.lua +++ b/tiny-web-editor/src/jsMain/resources/_engine.lua @@ -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