From 9feae2bb0fd6710ec5ec0b5a1f8f9e80a9ca1123 Mon Sep 17 00:00:00 2001 From: Humza Shahid Date: Thu, 20 Feb 2025 06:13:33 +0000 Subject: [PATCH] add function to make text vec to its own module so it can be reused across different modes --- fcore/make-text-vec.sml | 102 ++++++++++++++++++++++++++++ fcore/title/title-update.sml | 15 ++-- fcore/title/title-vec.sml | 128 ++++------------------------------- oms.mlb | 1 + shell/gl-draw.sml | 4 +- 5 files changed, 125 insertions(+), 125 deletions(-) create mode 100644 fcore/make-text-vec.sml diff --git a/fcore/make-text-vec.sml b/fcore/make-text-vec.sml new file mode 100644 index 0000000..d658c7d --- /dev/null +++ b/fcore/make-text-vec.sml @@ -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 diff --git a/fcore/title/title-update.sml b/fcore/title/title-update.sml index a4761ba..9fb0121 100644 --- a/fcore/title/title-update.sml +++ b/fcore/title/title-update.sml @@ -9,13 +9,11 @@ struct val mode = if #attackHeld input orelse #jumpHeld input then GameType.LEVEL LevelType.initial - else + else let val titleState = - if #downHeld input then - {focus = OPTIONS_BUTTON} - else - titleState + if #downHeld input then {focus = OPTIONS_BUTTON} + else titleState in GameType.TITLE titleState end @@ -24,17 +22,14 @@ struct end | OPTIONS_BUTTON => let - val mode = + val mode = if #attackHeld input orelse #jumpHeld input then (* placeholder: go to configure screen instead once that is implemented *) GameType.TITLE titleState else let val titleState = - if #upHeld input then - {focus = START_BUTTON} - else - titleState + if #upHeld input then {focus = START_BUTTON} else titleState in GameType.TITLE titleState end diff --git a/fcore/title/title-vec.sml b/fcore/title/title-vec.sml index 108b3c1..71e5472 100644 --- a/fcore/title/title-vec.sml +++ b/fcore/title/title-vec.sml @@ -2,129 +2,29 @@ structure TitleVec = struct 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) = case #focus title of START_BUTTON => - let - val playX = getTextCentreX "Play game" - val acc = - getTextVec (playX, 500, width, height, "Play game", 0.3, 0.3, 0.7, []) + let + val playX = MakeTextVec.getTextCentreX "Play game" + val acc = MakeTextVec.make + (playX, 500, width, height, "Play game", 0.3, 0.3, 0.7, []) - val optionsX = getTextCentreX "Options" - val acc = - getTextVec (optionsX, 600, width, height, "Options", 0.0, 0.0, 0.0, acc) + val optionsX = MakeTextVec.getTextCentreX "Options" + val acc = MakeTextVec.make + (optionsX, 600, width, height, "Options", 0.0, 0.0, 0.0, acc) in Vector.concat acc end | OPTIONS_BUTTON => - let - val playX = getTextCentreX "Play game" - val acc = - getTextVec (playX, 500, width, height, "Play game", 0.0, 0.0, 0.0, []) + let + val playX = MakeTextVec.getTextCentreX "Play game" + val acc = MakeTextVec.make + (playX, 500, width, height, "Play game", 0.0, 0.0, 0.0, []) - val optionsX = getTextCentreX "Options" - val acc = - getTextVec (optionsX, 600, width, height, "Options", 0.3, 0.3, 0.7, acc) + val optionsX = MakeTextVec.getTextCentreX "Options" + val acc = MakeTextVec.make + (optionsX, 600, width, height, "Options", 0.3, 0.3, 0.7, acc) in Vector.concat acc end diff --git a/oms.mlb b/oms.mlb index 148c954..669969c 100644 --- a/oms.mlb +++ b/oms.mlb @@ -22,6 +22,7 @@ in fcore/field.sml fcore/level/chain-edge.sml end +fcore/make-text-vec.sml fcore/level/wall.sml fcore/level/platform.sml diff --git a/shell/gl-draw.sml b/shell/gl-draw.sml index 785d393..486dbb1 100644 --- a/shell/gl-draw.sml +++ b/shell/gl-draw.sml @@ -250,13 +250,15 @@ struct shellState end + fun helpDrawTitle (shellState: t) = drawPlayer shellState + fun drawTitle (shellState: t, title) = let val width = InputState.getWidth () val height = InputState.getHeight () val vec = TitleVec.getDrawVec (title, width, height) val shellState = uploadPlayer (shellState, vec) - val () = helpDrawLevel shellState + val () = helpDrawTitle shellState in shellState end