progress fixing bugs in generated export string

This commit is contained in:
2025-07-13 01:40:57 +01:00
parent 31bc8485f3
commit 0e141174b2
5 changed files with 133 additions and 58 deletions

View File

@@ -500,67 +500,55 @@ struct
fun intToRealString num =
let
val result = Real32.fromInt num
val result = Real32.toString result
val result = Real.fromInt num
val result = Real.toString result
in
if String.isSubstring "." result then result else result ^ ".0"
end
fun colToRealString col =
let
val result = Real32.fromInt col / 255.0
val result = Real32.toString result
val result = Real.fromInt col / 255.0
val result = Real.toString result
in
if String.isSubstring "." result then result else result ^ ".0"
end
fun makeXString x =
let val x = intToRealString x
in "xToNdc (xOffset, " ^ x ^ ", scale, halfWidth)"
end
fun makeEndXString (startX, endX) =
let val endX = intToRealString endX
in "endXToNdc (xOffset, " ^ startX ^ ", " ^ endX ^ ", scale, halfWidth)"
end
fun makeYString y =
let val y = intToRealString y
in "yToNdc (yOffset, " ^ y ^ ", scale, halfHeight)"
end
fun makeEndYString (startY, endY) =
let val endY = intToRealString endY
in "endYToNdc (yOffset, " ^ startY ^ ", " ^ endY ^ ", scale, halfHeight)"
end
fun toExportStringFolder ({x, ex, y, ey, data = {r, g, b, a}}, acc) =
let
val x = intToRealString x
val y = intToRealString y
val ex = intToRealString ex
val ey = intToRealString ey
val x = makeXString x
val y = makeYString y
val ex = makeEndXString (x, ex)
val ey = makeYString ey
val r = colToRealString r
val g = colToRealString g
val b = colToRealString b
val x = String.concat ["(((x + ", x, ") - halfWidth) / halfWidth)"]
val y = String.concat ["((~(y + ", y, ") - halfHeight) / halfHeight)"]
val ex = String.concat
[ "((((("
, "("
, ex
, "+ x)"
, " - "
, x
, ") "
, "* scale) + "
, x
, ") - halfWidth) / halfWidth)"
]
val ey = String.concat
[ "("
, "("
, "("
, "("
, "("
, "("
, ey
, "+ y)"
, " - "
, y
, ")"
, "* scale) + "
, y
, ") - halfHeight) / halfHeight)"
]
in
(* based on triangle order formed by `Ndc.ltrbToVertexRgb` function *)
x :: ey :: r :: g :: b :: ex :: ey :: r :: g :: b :: x :: y :: r :: g :: b
:: x :: y :: r :: g :: b :: ex :: ey :: r :: g :: b :: ex :: y :: r :: g
:: b :: acc
val item = String.concatWith ",\n"
[x, ey, ex, ey, x, y, x, y, ex, ey, ex, y]
in
item :: acc
end
fun toExportString (squares, canvasWidth, canvasHeight) =
@@ -571,19 +559,35 @@ struct
val coords = BinTree.foldr (toExportStringFolder, bintree, [])
val coords = String.concatWith ",\n" coords
val header = String.concat
in
String.concat
[ "structure AAA = \nstruct\n"
, " fun lerp (x, y, scale, windowWidth, windowHeight) =\n"
, " fun xToNdc (xOffset, xpos, scale, halfWidth) =\n"
, " ((xpos * scale + xOffset) - halfWidth) / halfWidth\n\n"
, " fun endXToNdc (xOffset, startX, endX, scale, halfWidth) =\n"
, " (((endX - startX) * scale + xOffset) - halfWidth) / halfWidth\n\n"
, " fun yToNdc (yOffset, ypos, scale, halfHeight) =\n"
, " ~(((ypos * scale + yOffset) - halfHeight) / halfHeight)\n\n"
, " fun endYToNdc (yOffset, startY, endY, scale, halfHeight) =\n"
, " ~((((endY - startY) * scale + yOffset) - halfHeight) / halfHeight)\n\n"
, " fun lerp (xOffset, yOffset, scale, windowWidth, windowHeight) =\n"
, " let\n"
, " val halfWidth = Real32.fromInt windowWidth / 2.0\n"
, " val halfHeight = Real32.fromInt windowHeight / 2.0\n"
, " val windowWidth = Real32.fromInt windowWidth\n"
, " val halfWidth = windowWidth / 2.0\n"
, " val windowHeight = Real32.fromInt windowHeight\n"
, " val halfHeight = windowHeight / 2.0\n"
, " in\n"
, " #[\n"
, coords
, "\n"
, " ]\n"
, " end\n"
, "end\n"
]
val footer = String.concat ["\n ]\n", " end\n", "end\n"]
in
String.concat [header, coords, footer]
end
end