Skip to content

Commit

Permalink
spr.pset and spr.pget have now the same behaviour (ignoring no valid …
Browse files Browse the repository at this point in the history
…x/y coordinates)
  • Loading branch information
dwursteisen committed Jan 6, 2024
1 parent 8f8f296 commit a1dba55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction()
arg.checkColorIndex()
}
resourceAccess.frameBuffer.clear(color)
return NONE
return NIL
}
}

Expand All @@ -58,7 +58,7 @@ class GfxLib(private val resourceAccess: GameResourceAccess) : TwoArgFunction()
@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 {
resourceAccess.frameBuffer.pixel(arg1.checkint(), arg2.checkint(), arg3.checkint())
return NONE
return NIL
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.mingdx.tiny.doc.TinyFunction
import com.github.mingdx.tiny.doc.TinyLib
import com.github.minigdx.tiny.engine.GameOptions
import com.github.minigdx.tiny.engine.GameResourceAccess
import com.github.minigdx.tiny.graphic.PixelArray
import org.luaj.vm2.LuaTable
import org.luaj.vm2.LuaValue
import org.luaj.vm2.Varargs
Expand Down Expand Up @@ -45,9 +46,8 @@ class SprLib(val gameOptions: GameOptions, val resourceAccess: GameResourceAcces
val x = arg1.checkint()
val y = arg2.checkint()

if (x in 0 until pixelArray.width && y in 0 until pixelArray.height) {
if (isInPixelArray(pixelArray, x, y)) {
val index = pixelArray.get(x, y)

val colorIndex = index.get(0)
return valueOf(colorIndex)
} else {
Expand All @@ -56,6 +56,10 @@ class SprLib(val gameOptions: GameOptions, val resourceAccess: GameResourceAcces
}
}

private fun isInPixelArray(pixelArray: PixelArray, x: Int, y: Int): Boolean {
return x in 0 until pixelArray.width && y in 0 until pixelArray.height
}

@TinyFunction(
"Set the color index at the coordinate (x,y) in the current spritesheet.",
example = SPR_PSET_EXAMPLE,
Expand All @@ -68,12 +72,15 @@ class SprLib(val gameOptions: GameOptions, val resourceAccess: GameResourceAcces
@TinyArg("y") arg2: LuaValue,
@TinyArg("color") arg3: LuaValue,
): LuaValue {
resourceAccess.spritesheet(currentSpritesheet)?.pixels?.set(
arg1.checkint(),
arg2.checkint(),
arg3.checkint(),
)
return arg3
val x = arg1.checkint()
val y = arg2.checkint()
val pixels = resourceAccess.spritesheet(currentSpritesheet)?.pixels ?: return NIL
return if (isInPixelArray(pixels, x, y)) {
pixels.set(x, y, arg3.checkint())
arg3
} else {
NIL
}
}
}

Expand Down

0 comments on commit a1dba55

Please sign in to comment.