Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #78: Add SDF helpers #123

Merged
merged 9 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
# Run from root.

sbt crossCleanAll crossTestAllNoClean "scalafix --check" scalafmtCheckAll

cd sandbox

sbt compile fastLinkJS
2 changes: 1 addition & 1 deletion sandbox/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.10.1
sbt.version=1.10.6
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.sandbox

import com.example.sandbox.scenes.*
import com.example.sandbox.shaders.*
import indigo.*
import indigo.json.Json
import indigo.scenes.*
Expand All @@ -11,7 +12,13 @@ import indigoextras.ui.*
import scala.scalajs.js.annotation.*

@JSExportTopLevel("IndigoGame")
object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, SandboxGameModel, SandboxViewModel]:
object SandboxGame
extends IndigoGame[
SandboxBootData,
SandboxStartupData,
SandboxGameModel,
SandboxViewModel
]:

val magnificationLevel: Int = 2
val gameWidth: Int = 228
Expand All @@ -22,15 +29,19 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
def initialScene(bootData: SandboxBootData): Option[SceneName] =
Some(ShadersScene.name)

def scenes(bootData: SandboxBootData): NonEmptyList[Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]] =
def scenes(bootData: SandboxBootData): NonEmptyList[
Scene[SandboxStartupData, SandboxGameModel, SandboxViewModel]
] =
NonEmptyList(
OriginalScene,
ShadersScene
)

val eventFilters: EventFilters = EventFilters.Permissive

def boot(flags: Map[String, String]): Outcome[BootResult[SandboxBootData, SandboxGameModel]] = {
def boot(
flags: Map[String, String]
): Outcome[BootResult[SandboxBootData, SandboxGameModel]] = {
val gameViewport =
(flags.get("width"), flags.get("height")) match {
case (Some(w), Some(h)) =>
Expand All @@ -47,7 +58,10 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
clearColor = RGBA(0.4, 0.2, 0.5, 1),
magnification = magnificationLevel
),
SandboxBootData(flags.getOrElse("key", "No entry for 'key'."), gameViewport)
SandboxBootData(
flags.getOrElse("key", "No entry for 'key'."),
gameViewport
)
).withAssets(
SandboxAssets.assets ++
Shaders.assets
Expand All @@ -62,7 +76,12 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
Shaders.circle,
Shaders.external,
Shaders.sea,
CustomShader.shader
BoxShader.shader,
CircleShader.shader,
HexagonShader.shader,
SegmentShader.shader,
StarShader.shader,
TriangleShader.shader
)
)
}
Expand All @@ -89,8 +108,10 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
aseprite,
spriteAndAnimations.sprite
.withDepth(Depth(3))
.withRef(16, 16) // Initial offset, so when talk about his position it's the center of the sprite
.moveTo(screenCenter) // Also place him in the middle of the screen initially
.withRef(16, 16) // Initial offset, so when talk about his position it's the center of the sprite
.moveTo(
screenCenter
) // Also place him in the middle of the screen initially
.withMaterial(SandboxAssets.dudeMaterial),
clips
),
Expand All @@ -100,10 +121,15 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
.addAnimations(spriteAndAnimations.animations)

val res: Option[Startup.Success[SandboxStartupData]] = for {
json <- assetCollection.findTextDataByName(AssetName(SandboxAssets.dudeName.toString + "-json"))
aseprite <- Json.asepriteFromJson(json)
spriteAndAnimations <- aseprite.toSpriteAndAnimations(dice, SandboxAssets.dudeName)
clips <- aseprite.toClips(SandboxAssets.dudeName)
json <- assetCollection.findTextDataByName(
AssetName(SandboxAssets.dudeName.toString + "-json")
)
aseprite <- Json.asepriteFromJson(json)
spriteAndAnimations <- aseprite.toSpriteAndAnimations(
dice,
SandboxAssets.dudeName
)
clips <- aseprite.toClips(SandboxAssets.dudeName)
} yield makeStartupData(aseprite, spriteAndAnimations, clips)

Outcome(res.getOrElse(Startup.Failure("Failed to load the dude")))
Expand All @@ -112,19 +138,43 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
def initialModel(startupData: SandboxStartupData): Outcome[SandboxGameModel] =
Outcome(SandboxModel.initialModel(startupData))

def initialViewModel(startupData: SandboxStartupData, model: SandboxGameModel): Outcome[SandboxViewModel] = {
def initialViewModel(
startupData: SandboxStartupData,
model: SandboxGameModel
): Outcome[SandboxViewModel] = {
val assets =
InputFieldAssets(
Text("placeholder", 0, 0, 0, Fonts.fontKey, SandboxAssets.fontMaterial).alignLeft,
Graphic(0, 0, 16, 16, 2, Material.ImageEffects(SandboxAssets.smallFontName).withTint(RGB(0, 0, 1)))
Text(
"placeholder",
0,
0,
0,
Fonts.fontKey,
SandboxAssets.fontMaterial
).alignLeft,
Graphic(
0,
0,
16,
16,
2,
Material
.ImageEffects(SandboxAssets.smallFontName)
.withTint(RGB(0, 0, 1))
)
.withCrop(188, 78, 14, 23)
)

Outcome(
SandboxViewModel(
Point.zero,
InputField("single", assets).withKey(BindingKey("single")).makeSingleLine,
InputField("multi\nline", assets).withKey(BindingKey("multi")).makeMultiLine.moveTo(5, 5),
InputField("single", assets)
.withKey(BindingKey("single"))
.makeSingleLine,
InputField("multi\nline", assets)
.withKey(BindingKey("multi"))
.makeMultiLine
.moveTo(5, 5),
true
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.sandbox.scenes
import com.example.sandbox.SandboxGameModel
import com.example.sandbox.SandboxStartupData
import com.example.sandbox.SandboxViewModel
import com.example.sandbox.shaders.ShaderPrograms
import com.example.sandbox.shaders.*
import indigo.*
import indigo.ShaderPrimitive.*
import indigo.scenes.*
Expand Down Expand Up @@ -49,44 +49,20 @@ object ShadersScene extends Scene[SandboxStartupData, SandboxGameModel, SandboxV
Outcome(
SceneUpdateFragment(
Layer(
CustomShader(
20,
20,
100,
100,
Depth.zero,
ShaderData(CustomShader.shaderId)
)
BlankEntity(100, 100, ShaderData(BoxShader.shader.id))
.moveTo(0, 0),
BlankEntity(100, 100, ShaderData(CircleShader.shader.id))
.moveTo(100, 0),
BlankEntity(100, 100, ShaderData(HexagonShader.shader.id))
.moveTo(200, 0),
BlankEntity(100, 100, ShaderData(SegmentShader.shader.id))
.moveTo(0, 100),
BlankEntity(100, 100, ShaderData(StarShader.shader.id))
.moveTo(100, 100),
BlankEntity(100, 100, ShaderData(TriangleShader.shader.id))
.moveTo(200, 100)
).withMagnification(1)
)
)

}

final case class CustomShader(x: Int, y: Int, width: Int, height: Int, depth: Depth, shader: ShaderData)
extends EntityNode[CustomShader]:
val flip: Flip = Flip.default
val position: Point = Point(x, y)
val size: Size = Size(width, height)
val ref: Point = Point.zero
val rotation: Radians = Radians.zero
val scale: Vector2 = Vector2.one
lazy val toShaderData: ShaderData = shader

def withDepth(newDepth: Depth): CustomShader =
this.copy(depth = newDepth)

val eventHandlerEnabled: Boolean = false
def eventHandler: ((CustomShader, GlobalEvent)) => Option[GlobalEvent] = Function.const(None)

object CustomShader:

val shaderId: ShaderId =
ShaderId("custom shader")

// println(ShaderPrograms.frag2)

val shader: EntityShader.Source =
EntityShader
.Source(shaderId)
.withFragmentProgram(ShaderPrograms.frag2)
Loading
Loading