Skip to content

Commit

Permalink
feat: deprecate GameViewport.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
david-lebl-adastra committed Oct 30, 2024
1 parent 96e7db9 commit f6560cc
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object ConfigGen {
|
| val config: GameConfig =
| GameConfig(
| viewport = GameViewport(${indigoOptions.metadata.width}, ${indigoOptions.metadata.height}),
| viewport = Size(${indigoOptions.metadata.width}, ${indigoOptions.metadata.height}),
| frameRateLimit = Option(FPS.`60`),
| clearColor = ${extractBgColor(indigoOptions.metadata.backgroundColor)},
| magnification = 1,
Expand Down
2 changes: 1 addition & 1 deletion indigo/indigo/src/main/scala/indigo/IndigoShader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ trait IndigoShader extends GameLauncher[IndigoShaderModel, IndigoShaderModel, Un
model: IndigoShaderModel
): GlobalEvent => Outcome[IndigoShaderModel] = {
case ViewportResize(vp) =>
Outcome(model.copy(viewport = vp.size))
Outcome(model.copy(viewport = vp))

case KeyboardEvent.KeyUp(Key.KEY_F) =>
Outcome(model, Batch(ToggleFullScreen))
Expand Down
7 changes: 4 additions & 3 deletions indigo/indigo/src/main/scala/indigo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ object syntax:
def point: Point = Point(t._1, t._2)
def size: Size = Size(t._1, t._2)

extension (s: Size)
def giveDimensions(magnification: Int): Rectangle =
Rectangle(0, 0, s.width / magnification, s.height / magnification)

extension [A](values: scalajs.js.Array[A]) def toBatch: Batch[A] = Batch.fromJSArray(values)
extension [A](values: Array[A]) def toBatch: Batch[A] = Batch.fromArray(values)
extension [A](values: List[A]) def toBatch: Batch[A] = Batch.fromList(values)
Expand Down Expand Up @@ -557,9 +561,6 @@ val ResizePolicy: shared.config.ResizePolicy.type = shared.config.ResizePolicy
type GameConfig = shared.config.GameConfig
val GameConfig: shared.config.GameConfig.type = shared.config.GameConfig

type GameViewport = shared.config.GameViewport
val GameViewport: shared.config.GameViewport.type = shared.config.GameViewport

type AdvancedGameConfig = shared.config.AdvancedGameConfig
val AdvancedGameConfig: shared.config.AdvancedGameConfig.type = shared.config.AdvancedGameConfig

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import indigo.platform.renderer.shared.ContextAndCanvas
import indigo.platform.renderer.shared.LoadedTextureAsset
import indigo.platform.renderer.shared.TextureLookupResult
import indigo.platform.renderer.shared.WebGLHelper
import indigo.shared.config.GameViewport
import indigo.shared.config.RenderingTechnology
import indigo.shared.datatypes.Radians
import indigo.shared.datatypes.Size
import indigo.shared.datatypes.mutable.CheapMatrix4
import indigo.shared.display.DisplayEntity
import indigo.shared.display.DisplayGroup
Expand Down Expand Up @@ -255,7 +255,7 @@ final class RendererWebGL1(

gl.viewport(0, 0, actualWidth.toDouble, actualHeight.toDouble)

globalEventStream.pushGlobalEvent(ViewportResize(GameViewport(actualWidth, actualHeight)))
globalEventStream.pushGlobalEvent(ViewportResize(Size(actualWidth, actualHeight)))

()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import indigo.platform.renderer.shared.LoadedTextureAsset
import indigo.platform.renderer.shared.TextureLookupResult
import indigo.platform.renderer.shared.WebGLHelper
import indigo.shared.QuickCache
import indigo.shared.config.GameViewport
import indigo.shared.config.RenderingTechnology
import indigo.shared.datatypes.RGBA
import indigo.shared.datatypes.Radians
import indigo.shared.datatypes.Size
import indigo.shared.datatypes.mutable.CheapMatrix4
import indigo.shared.events.ViewportResize
import indigo.shared.platform.ProcessedSceneData
Expand All @@ -32,7 +32,7 @@ import org.scalajs.dom.WebGLBuffer
import org.scalajs.dom.WebGLFramebuffer
import org.scalajs.dom.WebGLProgram
import org.scalajs.dom.WebGLRenderingContext
import org.scalajs.dom.WebGLRenderingContext._
import org.scalajs.dom.WebGLRenderingContext.*
import org.scalajs.dom.html

import scala.scalajs.js.Dynamic
Expand Down Expand Up @@ -419,7 +419,7 @@ final class RendererWebGL2(

gl.viewport(0, 0, actualWidth.toDouble, actualHeight.toDouble)

globalEventStream.pushGlobalEvent(ViewportResize(GameViewport(actualWidth, actualHeight)))
globalEventStream.pushGlobalEvent(ViewportResize(Size(actualWidth, actualHeight)))

()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import indigo.shared.datatypes.RGBA
import indigo.shared.datatypes.Rectangle
import indigo.shared.datatypes.Size
import indigo.shared.time.FPS
import indigo.syntax.*

import scala.annotation.targetName

Expand All @@ -26,7 +27,7 @@ import scala.annotation.targetName
* Additional settings to help tune your game.
*/
final case class GameConfig(
viewport: GameViewport,
viewport: Size,
frameRateLimit: Option[FPS],
clearColor: RGBA,
magnification: Int,
Expand All @@ -53,11 +54,9 @@ final case class GameConfig(
|""".stripMargin

def withViewport(width: Int, height: Int): GameConfig =
this.copy(viewport = GameViewport(width, height))
this.copy(viewport = Size(width, height))
def withViewport(size: Size): GameConfig =
this.copy(viewport = GameViewport(size.width, size.height))
def withViewport(newViewport: GameViewport): GameConfig =
this.copy(viewport = newViewport)
this.copy(viewport = size)

def withFrameRateLimit(limit: FPS): GameConfig =
this.copy(frameRateLimit = Option(limit))
Expand Down Expand Up @@ -104,7 +103,7 @@ object GameConfig:

val default: GameConfig =
GameConfig(
viewport = GameViewport(550, 400),
viewport = Size(550, 400),
frameRateLimit = Option(FPS.`60`),
clearColor = RGBA.Black,
magnification = 1,
Expand All @@ -115,7 +114,7 @@ object GameConfig:

def apply(width: Int, height: Int): GameConfig =
GameConfig(
viewport = GameViewport(width, height),
viewport = Size(width, height),
frameRateLimit = Option(FPS.`60`),
clearColor = RGBA.Black,
magnification = 1,
Expand All @@ -124,7 +123,7 @@ object GameConfig:
advanced = AdvancedGameConfig.default
)

def apply(viewport: GameViewport, clearColor: RGBA, magnification: Int): GameConfig =
def apply(viewport: Size, clearColor: RGBA, magnification: Int): GameConfig =
GameConfig(
viewport = viewport,
frameRateLimit = Option(FPS.`60`),
Expand All @@ -137,7 +136,7 @@ object GameConfig:

def apply(width: Int, height: Int, clearColor: RGBA, magnification: Int): GameConfig =
GameConfig(
viewport = GameViewport(width, height),
viewport = Size(width, height),
frameRateLimit = Option(FPS.`60`),
clearColor = clearColor,
magnification = magnification,
Expand Down
124 changes: 67 additions & 57 deletions indigo/indigo/src/main/scala/indigo/shared/config/GameViewport.scala
Original file line number Diff line number Diff line change
@@ -1,57 +1,67 @@
package indigo.shared.config

import indigo.shared.datatypes.Point
import indigo.shared.datatypes.Rectangle
import indigo.shared.datatypes.Size

/** Respresents the initial size of the game's viewport.
*
* @param width
* Width in pixels
* @param height
* Height in pixels
*/
final case class GameViewport(size: Size) derives CanEqual:
val width: Int = size.width
val height: Int = size.height
val horizontalMiddle: Int = width / 2
val verticalMiddle: Int = height / 2
val center: Point = Point(horizontalMiddle, verticalMiddle)

@deprecated("use 'toRectangle' instead")
def asRectangle: Rectangle =
toRectangle
def toRectangle: Rectangle =
Rectangle(Point.zero, size)

def toPoint: Point =
size.toPoint

def toSize: Size =
size

def bounds: Rectangle =
toRectangle

def giveDimensions(magnification: Int): Rectangle =
Rectangle(0, 0, width / magnification, height / magnification)

object GameViewport:

def apply(width: Int, height: Int): GameViewport =
GameViewport(Size(width, height))

val atWUXGA: GameViewport =
GameViewport(1920, 1200)
val atWUXGABy2: GameViewport =
GameViewport(960, 600)

val at1080p: GameViewport =
GameViewport(1920, 1080)
val at1080pBy2: GameViewport =
GameViewport(960, 540)

val at720p: GameViewport =
GameViewport(1280, 720)
val at720pBy2: GameViewport =
GameViewport(640, 360)
//package indigo.shared.config
//
//import indigo.shared.datatypes.Point
//import indigo.shared.datatypes.Rectangle
//import indigo.shared.datatypes.Size
//
////opaque type GameViewport = Size
////object GameViewport:
//// def apply(size: Size): GameViewport = size
////
//// extension (g: GameViewport)
//// def toSize: Size = g
////
////
//
///** Respresents the initial size of the game's viewport.
// *
// * @param width
// * Width in pixels
// * @param height
// * Height in pixels
// */
//final case class GameViewport(size: Size) derives CanEqual:
// export size.*
//// val width: Int = size.width
//// val height: Int = size.height
//// val horizontalMiddle: Int = width / 2
//// val verticalMiddle: Int = height / 2
//// val center: Point = Point(horizontalMiddle, verticalMiddle)
//
//// @deprecated("use 'toRectangle' instead")
//// def asRectangle: Rectangle =
//// toRectangle
//// def toRectangle: Rectangle =
//// Rectangle(Point.zero, size)
////
//// def toPoint: Point =
//// size.toPoint
////
//// def toSize: Size =
//// size
////
//// def bounds: Rectangle =
//// toRectangle
////
// def giveDimensions(magnification: Int): Rectangle =
// Rectangle(0, 0, width / magnification, height / magnification)
//
//object GameViewport:
//
// def apply(width: Int, height: Int): GameViewport =
// GameViewport(Size(width, height))
//
// val atWUXGA: GameViewport =
// GameViewport(1920, 1200)
// val atWUXGABy2: GameViewport =
// GameViewport(960, 600)
//
// val at1080p: GameViewport =
// GameViewport(1920, 1080)
// val at1080pBy2: GameViewport =
// GameViewport(960, 540)
//
// val at720p: GameViewport =
// GameViewport(1280, 720)
// val at720pBy2: GameViewport =
// GameViewport(640, 360)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import indigo.shared.assets.AssetName
import indigo.shared.assets.AssetType
import indigo.shared.audio.Volume
import indigo.shared.collections.Batch
import indigo.shared.config.GameViewport
import indigo.shared.config.RenderingTechnology
import indigo.shared.constants.Key
import indigo.shared.datatypes.BindingKey
Expand Down Expand Up @@ -65,7 +64,7 @@ case object FrameTick extends GlobalEvent
* @param gameViewPort
* The actual size in pixels, you can ask it to apply magnification.
*/
final case class ViewportResize(gameViewPort: GameViewport) extends ViewEvent
final case class ViewportResize(gameViewPort: Size) extends ViewEvent

/** Attempt to enter or exit full screen mode
*/
Expand Down
18 changes: 4 additions & 14 deletions indigo/indigo/src/main/scala/indigo/shared/scenegraph/Camera.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package indigo.shared.scenegraph

import indigo.shared.config.GameViewport
import indigo.shared.datatypes.Point
import indigo.shared.datatypes.Radians
import indigo.shared.datatypes.Rectangle
Expand All @@ -10,12 +9,9 @@ import indigo.shared.datatypes.Size
*/
sealed trait Camera:
def position: Point
def topLeft(viewport: GameViewport): Point
def topLeft(viewport: Size): Point
def bounds(viewport: GameViewport): Rectangle
def bounds(viewport: Size): Rectangle
def frustum(viewport: GameViewport): Rectangle = bounds(viewport)
def frustum(viewport: Size): Rectangle = bounds(viewport)
def frustum(viewport: Size): Rectangle = bounds(viewport)
def zoom: Zoom
def rotation: Radians
def isLookAt: Boolean
Expand All @@ -31,11 +27,9 @@ object Camera:
* while controlling the position, zoom and rotation.
*/
final case class Fixed(position: Point, zoom: Zoom, rotation: Radians) extends Camera:
def topLeft(viewport: GameViewport): Point = position
def topLeft(viewport: Size): Point = position
def bounds(viewport: GameViewport): Rectangle = Rectangle(position, viewport.size)
def bounds(viewport: Size): Rectangle = Rectangle(position, viewport)
val isLookAt: Boolean = false
def topLeft(viewport: Size): Point = position
def bounds(viewport: Size): Rectangle = Rectangle(position, viewport)
val isLookAt: Boolean = false

def withX(newX: Int): Fixed =
this.copy(position = position.withX(newX))
Expand Down Expand Up @@ -78,12 +72,8 @@ object Camera:
final case class LookAt(target: Point, zoom: Zoom, rotation: Radians) extends Camera:
val isLookAt: Boolean = true
val position: Point = target
def topLeft(viewport: GameViewport): Point =
topLeft(viewport.size)
def topLeft(viewport: Size): Point =
target - (viewport.toPoint / 2) / zoom.toDouble.toInt
def bounds(viewport: GameViewport): Rectangle =
bounds(viewport.size)
def bounds(viewport: Size): Rectangle =
Rectangle(topLeft(viewport), viewport)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package indigo.shared.scenegraph
import indigo.Point
import indigo.Rectangle
import indigo.Size
import indigo.shared.config.GameViewport

class CameraTests extends munit.FunSuite {

Expand Down
2 changes: 1 addition & 1 deletion indigo/perf/src/main/scala/com/example/perf/PerfGame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object PerfGame extends IndigoDemo[Unit, Dude, DudeModel, Unit] {
BootResult
.noData(
GameConfig(
viewport = GameViewport(viewportWidth, viewportHeight),
viewport = Size(viewportWidth, viewportHeight),
frameRateLimit = None,
clearColor = RGBA(0.4, 0.2, 0.5, 1),
magnification = magnificationLevel,
Expand Down
2 changes: 1 addition & 1 deletion indigo/physics/src/main/scala/example/IndigoPhysics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object IndigoPhysics extends IndigoGame[Unit, Unit, Model, Unit]:
def boot(flags: Map[String, String]): Outcome[BootResult[Unit, Model]] =
Outcome(
BootResult
.noData(Config.config)
.noData(Config.config.withViewport(Config.config.viewport + Config.config.viewport))
.withSubSystems(FPSCounter(Point(10)))
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ object SandboxGame extends IndigoGame[SandboxBootData, SandboxStartupData, Sandb
val gameViewport =
(flags.get("width"), flags.get("height")) match {
case (Some(w), Some(h)) =>
GameViewport(w.toInt, h.toInt)
Size(w.toInt, h.toInt)

case _ =>
GameViewport(viewportWidth, viewportHeight)
Size(viewportWidth, viewportHeight)
}

Outcome(
Expand Down Expand Up @@ -275,7 +275,7 @@ final case class Dude(
sprite: Sprite[Material.ImageEffects],
clips: Map[CycleLabel, Clip[Material.Bitmap]]
)
final case class SandboxBootData(message: String, gameViewport: GameViewport)
final case class SandboxBootData(message: String, gameViewport: Size)
final case class SandboxStartupData(dude: Dude, viewportCenter: Point)
final case class SandboxViewModel(
offset: Point,
Expand Down
Loading

0 comments on commit f6560cc

Please sign in to comment.