add function to make text vec to its own module so it can be reused across different modes

This commit is contained in:
2025-02-20 06:13:33 +00:00
parent 027c8be541
commit 9feae2bb0f
5 changed files with 125 additions and 125 deletions

102
fcore/make-text-vec.sml Normal file
View File

@@ -0,0 +1,102 @@
structure MakeTextVec =
struct
fun helpGetTextVec
( x
, y
, fontSize
, fontSpace
, windowWidth
, windowHeight
, pos
, str
, acc
, r
, g
, b
) =
if pos = String.size str then
acc
else
let
val chr = String.sub (str, pos)
val chrFun = Vector.sub (CozetteAscii.asciiTable, Char.ord chr)
val hd = chrFun
(x, y, fontSize, fontSize, windowWidth, windowHeight, r, g, b)
val acc = hd :: acc
in
helpGetTextVec
( x + fontSpace
, y
, fontSize
, fontSpace
, windowWidth
, windowHeight
, pos + 1
, str
, acc
, r
, g
, b
)
end
fun getTextWidth text = String.size text * Constants.fontSpace
(* x coordinate that will let us place this text on centre of screen *)
fun getTextCentreX text =
let val textWidth = getTextWidth text
in (Constants.worldWidth - textWidth) div 2
end
fun make (x, y, width, height, str, r, g, b, acc) =
let
val wratio = width / Constants.worldWidthReal
val hratio = height / Constants.worldHeightReal
in
if wratio < hratio then
let
val scale = Constants.worldHeightReal * wratio
val yOffset =
if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0
else 0.0
val x = Real32.fromInt x * wratio
val y = Real32.fromInt y * wratio + yOffset
val x = Real32.toInt IEEEReal.TO_NEAREST x
val y = Real32.toInt IEEEReal.TO_NEAREST y
val fontSize = Constants.fontSize * wratio
val fontSpace = Real32.fromInt Constants.fontSpace * wratio
val fontSpace = Real32.toInt IEEEReal.TO_NEAREST fontSpace
in
helpGetTextVec
(x, y, fontSize, fontSpace, width, height, 0, str, acc, r, g, b)
end
else
let
val scale = Constants.worldWidthReal * hratio
val xOffset =
if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0
else 0.0
val x = Real32.fromInt x * hratio + xOffset
val y = Real32.fromInt y * hratio
val x = Real32.toInt IEEEReal.TO_NEAREST x
val y = Real32.toInt IEEEReal.TO_NEAREST y
val fontSize = Constants.fontSize * hratio
val fontSpace = Real32.fromInt Constants.fontSpace * hratio
val fontSpace = Real32.toInt IEEEReal.TO_NEAREST fontSpace
in
helpGetTextVec
(x, y, fontSize, fontSpace, width, height, 0, str, acc, r, g, b)
end
end
end

View File

@@ -12,10 +12,8 @@ struct
else else
let let
val titleState = val titleState =
if #downHeld input then if #downHeld input then {focus = OPTIONS_BUTTON}
{focus = OPTIONS_BUTTON} else titleState
else
titleState
in in
GameType.TITLE titleState GameType.TITLE titleState
end end
@@ -31,10 +29,7 @@ struct
else else
let let
val titleState = val titleState =
if #upHeld input then if #upHeld input then {focus = START_BUTTON} else titleState
{focus = START_BUTTON}
else
titleState
in in
GameType.TITLE titleState GameType.TITLE titleState
end end

View File

@@ -2,129 +2,29 @@ structure TitleVec =
struct struct
open TitleType open TitleType
fun helpGetTextVec
( x
, y
, fontSize
, fontSpace
, windowWidth
, windowHeight
, pos
, str
, acc
, r
, g
, b
) =
if pos = String.size str then
acc
else
let
val chr = String.sub (str, pos)
val chrFun = Vector.sub (CozetteAscii.asciiTable, Char.ord chr)
val hd = chrFun
(x, y, fontSize, fontSize, windowWidth, windowHeight, r, g, b)
val acc = hd :: acc
in
helpGetTextVec
( x + fontSpace
, y
, fontSize
, fontSpace
, windowWidth
, windowHeight
, pos + 1
, str
, acc
, r
, g
, b
)
end
fun getTextWidth text = String.size text * Constants.fontSpace
(* x coordinate that will let us place this text on centre of screen *)
fun getTextCentreX text =
let val textWidth = getTextWidth text
in (Constants.worldWidth - textWidth) div 2
end
fun getTextVec (x, y, width, height, str, r, g, b, acc) =
let
val wratio = width / Constants.worldWidthReal
val hratio = height / Constants.worldHeightReal
in
if wratio < hratio then
let
val scale = Constants.worldHeightReal * wratio
val yOffset =
if height > scale then (height - scale) / 2.0
else if height < scale then (scale - height) / 2.0
else 0.0
val x = Real32.fromInt x * wratio
val y = Real32.fromInt y * wratio + yOffset
val x = Real32.toInt IEEEReal.TO_NEAREST x
val y = Real32.toInt IEEEReal.TO_NEAREST y
val fontSize = Constants.fontSize * wratio
val fontSpace = Real32.fromInt Constants.fontSpace * wratio
val fontSpace = Real32.toInt IEEEReal.TO_NEAREST fontSpace
in
helpGetTextVec
(x, y, fontSize, fontSpace, width, height, 0, str, acc, r, g, b)
end
else
let
val scale = Constants.worldWidthReal * hratio
val xOffset =
if width > scale then (width - scale) / 2.0
else if width < scale then (scale - width) / 2.0
else 0.0
val x = Real32.fromInt x * hratio + xOffset
val y = Real32.fromInt y * hratio
val x = Real32.toInt IEEEReal.TO_NEAREST x
val y = Real32.toInt IEEEReal.TO_NEAREST y
val fontSize = Constants.fontSize * hratio
val fontSpace = Real32.fromInt Constants.fontSpace * hratio
val fontSpace = Real32.toInt IEEEReal.TO_NEAREST fontSpace
in
helpGetTextVec
(x, y, fontSize, fontSpace, width, height, 0, str, acc, r, g, b)
end
end
fun getDrawVec (title: TitleType.title_type, width, height) = fun getDrawVec (title: TitleType.title_type, width, height) =
case #focus title of case #focus title of
START_BUTTON => START_BUTTON =>
let let
val playX = getTextCentreX "Play game" val playX = MakeTextVec.getTextCentreX "Play game"
val acc = val acc = MakeTextVec.make
getTextVec (playX, 500, width, height, "Play game", 0.3, 0.3, 0.7, []) (playX, 500, width, height, "Play game", 0.3, 0.3, 0.7, [])
val optionsX = getTextCentreX "Options" val optionsX = MakeTextVec.getTextCentreX "Options"
val acc = val acc = MakeTextVec.make
getTextVec (optionsX, 600, width, height, "Options", 0.0, 0.0, 0.0, acc) (optionsX, 600, width, height, "Options", 0.0, 0.0, 0.0, acc)
in in
Vector.concat acc Vector.concat acc
end end
| OPTIONS_BUTTON => | OPTIONS_BUTTON =>
let let
val playX = getTextCentreX "Play game" val playX = MakeTextVec.getTextCentreX "Play game"
val acc = val acc = MakeTextVec.make
getTextVec (playX, 500, width, height, "Play game", 0.0, 0.0, 0.0, []) (playX, 500, width, height, "Play game", 0.0, 0.0, 0.0, [])
val optionsX = getTextCentreX "Options" val optionsX = MakeTextVec.getTextCentreX "Options"
val acc = val acc = MakeTextVec.make
getTextVec (optionsX, 600, width, height, "Options", 0.3, 0.3, 0.7, acc) (optionsX, 600, width, height, "Options", 0.3, 0.3, 0.7, acc)
in in
Vector.concat acc Vector.concat acc
end end

View File

@@ -22,6 +22,7 @@ in
fcore/field.sml fcore/field.sml
fcore/level/chain-edge.sml fcore/level/chain-edge.sml
end end
fcore/make-text-vec.sml
fcore/level/wall.sml fcore/level/wall.sml
fcore/level/platform.sml fcore/level/platform.sml

View File

@@ -250,13 +250,15 @@ struct
shellState shellState
end end
fun helpDrawTitle (shellState: t) = drawPlayer shellState
fun drawTitle (shellState: t, title) = fun drawTitle (shellState: t, title) =
let let
val width = InputState.getWidth () val width = InputState.getWidth ()
val height = InputState.getHeight () val height = InputState.getHeight ()
val vec = TitleVec.getDrawVec (title, width, height) val vec = TitleVec.getDrawVec (title, width, height)
val shellState = uploadPlayer (shellState, vec) val shellState = uploadPlayer (shellState, vec)
val () = helpDrawLevel shellState val () = helpDrawTitle shellState
in in
shellState shellState
end end