From bb015ff75cf6945b2e84251e264deab03cf19c60 Mon Sep 17 00:00:00 2001 From: David Wursteisen Date: Thu, 4 Jan 2024 15:00:38 +0100 Subject: [PATCH] Add examples on the shape and gfx lib --- .../com/github/minigdx/tiny/lua/GfxLib.kt | 6 +- .../github/minigdx/tiny/lua/GfxLibExamples.kt | 45 ++++++++++++++ .../com/github/minigdx/tiny/lua/ShapeLib.kt | 24 +++++--- .../minigdx/tiny/lua/ShapeLibExamples.kt | 60 +++++++++++++++++++ 4 files changed, 123 insertions(+), 12 deletions(-) diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLib.kt index d1629521..0c9a0fcf 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLib.kt @@ -36,7 +36,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction() return func } - @TinyFunction("clear the screen") + @TinyFunction("clear the screen", example = GFX_CLS_EXAMPLE) internal inner class cls : OneArgFunction() { @TinyCall("Clear the screen with a default color.") override fun call(): LuaValue = super.call() @@ -53,7 +53,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction() } } - @TinyFunction("Set the color index at the coordinate (x,y).") + @TinyFunction("Set the color index at the coordinate (x,y).", example = GFX_PSET_EXAMPLE) internal inner class pset : ThreeArgFunction() { @TinyCall("set the color index at the coordinate (x,y).") override fun call(@TinyArg("x")arg1: LuaValue, @TinyArg("y")arg2: LuaValue, @TinyArg("color")arg3: LuaValue): LuaValue { @@ -62,7 +62,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction() } } - @TinyFunction("Get the color index at the coordinate (x,y).") + @TinyFunction("Get the color index at the coordinate (x,y).", example = GFX_PGET_EXAMPLE) internal inner class pget : TwoArgFunction() { @TinyCall("get the color index at the coordinate (x,y).") override fun call(@TinyArg("x")arg1: LuaValue, @TinyArg("y")arg2: LuaValue): LuaValue { diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLibExamples.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLibExamples.kt index 6dbca874..26d33693 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLibExamples.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLibExamples.kt @@ -66,3 +66,48 @@ function _draw() spr.sheet(0) spr.sdraw() end""" + +//language=Lua +const val GFX_PSET_EXAMPLE = """ +function _draw() + local pos = ctrl.touching(0) + if pos ~= nil then + -- set the pixel with the color 9 when the mouse is pressed + gfx.pset(pos.x, pos.y, 9) + end +end""" + +//language=Lua +const val GFX_PGET_EXAMPLE = """ +function _draw() + gfx.cls() + local index = 0 + for x=0, 240, 16 do + for y=0, 240, 16 do + shape.rectf(x, y, 16, 16, index) + index = index + 1 + end + end + + local pos = ctrl.touch() + local color = gfx.pget(pos.x, pos.y) + if color ~= nil then + shape.rectf(0, 0, 80, 6, 13) + print("color index: "..color) + end + + + shape.circlef(pos.x - 2, pos.y - 2, 4, 0) +end""" + +//language=Lua +const val GFX_CLS_EXAMPLE = """ +function _draw() + if ctrl.pressed(keys.space) then + gfx.cls() + end + + print("Press space to clear the screen") + local pos = ctrl.touch() + shape.circlef(pos.x, pos.y, 4, math.rnd()) +end""" diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLib.kt index 61c33e94..4ed562d8 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLib.kt @@ -95,7 +95,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( return shp } - @TinyFunction("Draw a rectangle.") + @TinyFunction("Draw a rectangle.", example = SHAPE_RECTF_EXAMPLE) internal inner class rect : LibFunction() { @TinyCall("Draw a rectangle.") override fun invoke(@TinyArgs(["x", "y", "width", "height", "color"]) args: Varargs): Varargs { @@ -124,7 +124,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( ): LuaValue = super.call(a, b) } - @TinyFunction("Draw an oval.") + @TinyFunction("Draw an oval.", example = SHAPE_OVALF_EXAMPLE) internal inner class oval : LibFunction() { @TinyCall("Draw an oval using the default color.") override fun call( @@ -198,7 +198,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( } } - @TinyFunction("Draw an oval filled.") + @TinyFunction("Draw an oval filled.", example = SHAPE_OVALF_EXAMPLE) internal inner class ovalf : LibFunction() { @TinyCall("Draw a filled oval using the default color.") override fun call( @@ -266,7 +266,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( p += 2 * radiusY * radiusY * x - 2 * radiusX * radiusX * y + radiusX * radiusX } } - return NONE + return NIL } } @@ -346,11 +346,11 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( } x++ } - return NONE + return NIL } } - @TinyFunction("Draw a line.") + @TinyFunction("Draw a line.", example = SHAPE_LINE_EXAMPLE) internal inner class line : LibFunction() { @TinyCall("Draw a line.") @@ -414,7 +414,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( } } - @TinyFunction("Draw a circle.") + @TinyFunction("Draw a circle.", example = SHAPE_CIRCLEF_EXAMPLE) internal inner class circle : LibFunction() { @TinyCall("Draw a circle with the default color.") @@ -460,7 +460,10 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( } } - @TinyFunction("Draw a filled triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.") + @TinyFunction( + "Draw a filled triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.", + example = SHAPE_TRIANGLEF_EXAMPLE, + ) inner class trianglef : LibFunction() { @TinyCall("Draw a filled triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3).") @@ -513,7 +516,10 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction( } } - @TinyFunction("Draw a triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.") + @TinyFunction( + "Draw a triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.", + example = SHAPE_TRIANGLEF_EXAMPLE, + ) inner class triangle : LibFunction() { private val line = line() diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLibExamples.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLibExamples.kt index 8ede019b..23ee36e8 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLibExamples.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLibExamples.kt @@ -50,6 +50,23 @@ function _draw() end """ +//language=Lua +const val SHAPE_OVALF_EXAMPLE = """ +function _draw() + gfx.cls() + + local pos = ctrl.touch() + local w = math.max(0, pos.x) + local h = math.max(0, pos.y) + + shape.ovalf(w, h, w, h, 5) + shape.oval(256 - w, 256 - h, w, h, 1) + + + print("size w: "..w.." h: "..h) +end +""" + //language=Lua const val SHAPE_GRADIENT_EXAMPLE = """ function _draw() @@ -65,3 +82,46 @@ function _draw() end """ + +//language=Lua +const val SHAPE_LINE_EXAMPLE = """ +function _draw() + gfx.cls() + + local i =0 + for x =16, 240, 16 do + for y =16, 240, 16 do + shape.line(x, y, 256 - x, 256 - y, i) + i = i + 1 + end + end +end +""" + +//language=Lua +const val SHAPE_TRIANGLEF_EXAMPLE = """ +function tri(f, fill) + local x1 = 128 + math.cos(f) * 64 + local y1 = 128 + math.sin(f) * 64 + + local x2 = 128 + math.cos(f + math.pi * 1/3) * 64 + local y2 = 128 + math.sin(f + math.pi * 1/3) * 64 + + local x3 = 128 + math.cos(f+ math.pi * 2/3) * 64 + local y3 = 128 + math.sin(f +math.pi * 2/3) * 64 + + if fill then + shape.trianglef(x3, y3, x2, y2, x1, y1, 8) + else + shape.triangle(x1, y1, x2, y2, x3, y3, 8) + end +end + +function _draw() + gfx.cls() + local f = tiny.frame * 0.01 + + tri(f, false) + tri(f*2, true) +end +"""