Skip to content

Commit bb015ff

Browse files
committed
Add examples on the shape and gfx lib
1 parent 83926f5 commit bb015ff

File tree

4 files changed

+123
-12
lines changed

4 files changed

+123
-12
lines changed

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLib.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction()
3636
return func
3737
}
3838

39-
@TinyFunction("clear the screen")
39+
@TinyFunction("clear the screen", example = GFX_CLS_EXAMPLE)
4040
internal inner class cls : OneArgFunction() {
4141
@TinyCall("Clear the screen with a default color.")
4242
override fun call(): LuaValue = super.call()
@@ -53,7 +53,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction()
5353
}
5454
}
5555

56-
@TinyFunction("Set the color index at the coordinate (x,y).")
56+
@TinyFunction("Set the color index at the coordinate (x,y).", example = GFX_PSET_EXAMPLE)
5757
internal inner class pset : ThreeArgFunction() {
5858
@TinyCall("set the color index at the coordinate (x,y).")
5959
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()
6262
}
6363
}
6464

65-
@TinyFunction("Get the color index at the coordinate (x,y).")
65+
@TinyFunction("Get the color index at the coordinate (x,y).", example = GFX_PGET_EXAMPLE)
6666
internal inner class pget : TwoArgFunction() {
6767
@TinyCall("get the color index at the coordinate (x,y).")
6868
override fun call(@TinyArg("x")arg1: LuaValue, @TinyArg("y")arg2: LuaValue): LuaValue {

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/GfxLibExamples.kt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,48 @@ function _draw()
6666
spr.sheet(0)
6767
spr.sdraw()
6868
end"""
69+
70+
//language=Lua
71+
const val GFX_PSET_EXAMPLE = """
72+
function _draw()
73+
local pos = ctrl.touching(0)
74+
if pos ~= nil then
75+
-- set the pixel with the color 9 when the mouse is pressed
76+
gfx.pset(pos.x, pos.y, 9)
77+
end
78+
end"""
79+
80+
//language=Lua
81+
const val GFX_PGET_EXAMPLE = """
82+
function _draw()
83+
gfx.cls()
84+
local index = 0
85+
for x=0, 240, 16 do
86+
for y=0, 240, 16 do
87+
shape.rectf(x, y, 16, 16, index)
88+
index = index + 1
89+
end
90+
end
91+
92+
local pos = ctrl.touch()
93+
local color = gfx.pget(pos.x, pos.y)
94+
if color ~= nil then
95+
shape.rectf(0, 0, 80, 6, 13)
96+
print("color index: "..color)
97+
end
98+
99+
100+
shape.circlef(pos.x - 2, pos.y - 2, 4, 0)
101+
end"""
102+
103+
//language=Lua
104+
const val GFX_CLS_EXAMPLE = """
105+
function _draw()
106+
if ctrl.pressed(keys.space) then
107+
gfx.cls()
108+
end
109+
110+
print("Press space to clear the screen")
111+
local pos = ctrl.touch()
112+
shape.circlef(pos.x, pos.y, 4, math.rnd())
113+
end"""

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLib.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
9595
return shp
9696
}
9797

98-
@TinyFunction("Draw a rectangle.")
98+
@TinyFunction("Draw a rectangle.", example = SHAPE_RECTF_EXAMPLE)
9999
internal inner class rect : LibFunction() {
100100
@TinyCall("Draw a rectangle.")
101101
override fun invoke(@TinyArgs(["x", "y", "width", "height", "color"]) args: Varargs): Varargs {
@@ -124,7 +124,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
124124
): LuaValue = super.call(a, b)
125125
}
126126

127-
@TinyFunction("Draw an oval.")
127+
@TinyFunction("Draw an oval.", example = SHAPE_OVALF_EXAMPLE)
128128
internal inner class oval : LibFunction() {
129129
@TinyCall("Draw an oval using the default color.")
130130
override fun call(
@@ -198,7 +198,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
198198
}
199199
}
200200

201-
@TinyFunction("Draw an oval filled.")
201+
@TinyFunction("Draw an oval filled.", example = SHAPE_OVALF_EXAMPLE)
202202
internal inner class ovalf : LibFunction() {
203203
@TinyCall("Draw a filled oval using the default color.")
204204
override fun call(
@@ -266,7 +266,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
266266
p += 2 * radiusY * radiusY * x - 2 * radiusX * radiusX * y + radiusX * radiusX
267267
}
268268
}
269-
return NONE
269+
return NIL
270270
}
271271
}
272272

@@ -346,11 +346,11 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
346346
}
347347
x++
348348
}
349-
return NONE
349+
return NIL
350350
}
351351
}
352352

353-
@TinyFunction("Draw a line.")
353+
@TinyFunction("Draw a line.", example = SHAPE_LINE_EXAMPLE)
354354
internal inner class line : LibFunction() {
355355

356356
@TinyCall("Draw a line.")
@@ -414,7 +414,7 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
414414
}
415415
}
416416

417-
@TinyFunction("Draw a circle.")
417+
@TinyFunction("Draw a circle.", example = SHAPE_CIRCLEF_EXAMPLE)
418418
internal inner class circle : LibFunction() {
419419

420420
@TinyCall("Draw a circle with the default color.")
@@ -460,7 +460,10 @@ class ShapeLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction(
460460
}
461461
}
462462

463-
@TinyFunction("Draw a filled triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.")
463+
@TinyFunction(
464+
"Draw a filled triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.",
465+
example = SHAPE_TRIANGLEF_EXAMPLE,
466+
)
464467
inner class trianglef : LibFunction() {
465468

466469
@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(
513516
}
514517
}
515518

516-
@TinyFunction("Draw a triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.")
519+
@TinyFunction(
520+
"Draw a triangle using the coordinates of (x1, y1), (x2, y2) and (x3, y3) and color.",
521+
example = SHAPE_TRIANGLEF_EXAMPLE,
522+
)
517523
inner class triangle : LibFunction() {
518524

519525
private val line = line()

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/ShapeLibExamples.kt

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,23 @@ function _draw()
5050
end
5151
"""
5252

53+
//language=Lua
54+
const val SHAPE_OVALF_EXAMPLE = """
55+
function _draw()
56+
gfx.cls()
57+
58+
local pos = ctrl.touch()
59+
local w = math.max(0, pos.x)
60+
local h = math.max(0, pos.y)
61+
62+
shape.ovalf(w, h, w, h, 5)
63+
shape.oval(256 - w, 256 - h, w, h, 1)
64+
65+
66+
print("size w: "..w.." h: "..h)
67+
end
68+
"""
69+
5370
//language=Lua
5471
const val SHAPE_GRADIENT_EXAMPLE = """
5572
function _draw()
@@ -65,3 +82,46 @@ function _draw()
6582
6683
end
6784
"""
85+
86+
//language=Lua
87+
const val SHAPE_LINE_EXAMPLE = """
88+
function _draw()
89+
gfx.cls()
90+
91+
local i =0
92+
for x =16, 240, 16 do
93+
for y =16, 240, 16 do
94+
shape.line(x, y, 256 - x, 256 - y, i)
95+
i = i + 1
96+
end
97+
end
98+
end
99+
"""
100+
101+
//language=Lua
102+
const val SHAPE_TRIANGLEF_EXAMPLE = """
103+
function tri(f, fill)
104+
local x1 = 128 + math.cos(f) * 64
105+
local y1 = 128 + math.sin(f) * 64
106+
107+
local x2 = 128 + math.cos(f + math.pi * 1/3) * 64
108+
local y2 = 128 + math.sin(f + math.pi * 1/3) * 64
109+
110+
local x3 = 128 + math.cos(f+ math.pi * 2/3) * 64
111+
local y3 = 128 + math.sin(f +math.pi * 2/3) * 64
112+
113+
if fill then
114+
shape.trianglef(x3, y3, x2, y2, x1, y1, 8)
115+
else
116+
shape.triangle(x1, y1, x2, y2, x3, y3, 8)
117+
end
118+
end
119+
120+
function _draw()
121+
gfx.cls()
122+
local f = tiny.frame * 0.01
123+
124+
tri(f, false)
125+
tri(f*2, true)
126+
end
127+
"""

0 commit comments

Comments
 (0)