Skip to content

Commit

Permalink
Add examples on the shape and gfx lib
Browse files Browse the repository at this point in the history
  • Loading branch information
dwursteisen committed Jan 4, 2024
1 parent 83926f5 commit bb015ff
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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).")
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
"""

0 comments on commit bb015ff

Please sign in to comment.