Skip to content

Commit

Permalink
Moved LetterboxingViewport from app to graphics. #183
Browse files Browse the repository at this point in the history
  • Loading branch information
czyzby committed Mar 26, 2020
1 parent 3aab80b commit bca5540
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
- `profile` inlined function allows to profile an operation with the LibGDX `PerformanceCounter`.
- `PerformanceCounter.profile` inlined extension method eases usage of `PerformanceCounter` API.
- `PerformanceCounter.prettyPrint` allows to print basic performance data after profiling.
- **[CHANGE]** (`ktx-app`) `LetterboxingViewport` moved from `ktx-app` to `ktx-graphics`.
- **[FEATURE]** (`ktx-ashley`) Added `Entity.contains` (`in` operator) that checks if an `Entity` has a `Component`.
- **[FEATURE]** (`ktx-async`) Added `RenderingScope` factory function for custom scopes using rendering thread dispatcher.
- **[FEATURE]** (`ktx-graphics`) Added `LetterboxingViewport` from `ktx-app`.
- **[FEATURE]** (`ktx-graphics`) Added `takeScreenshot` utility function that allows to save a screenshot of the application.
- **[FEATURE]** (`ktx-math`) Added `lerp` and `interpolate` extension functions for `Float` ranges.
- **[FEATURE]** (`ktx-preferences`) Added a new KTX module: Preferences API extensions.
Expand Down
14 changes: 0 additions & 14 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ being a class like `com.badlogic.gdx.InputAdapter`.

- `clearScreen` is an inlined utility function that hides the OpenGL calls, allowing to clear the screen with a chosen
color.
- `LetterboxingViewport` combines `ScreenViewport` and `FitViewport` behavior: it targets a specific aspect ratio and
applies letterboxing like `FitViewport`, but it does not scale rendered objects when resized, keeping them in fixed size
similarly to `ScreenViewport`. Thanks to customizable target PPI value, it is ideal for GUIs and can easily support
different screen sizes.
- `emptyScreen` provides no-op implementations of `Screen`.

#### Profiling
Expand Down Expand Up @@ -125,16 +121,6 @@ class MyInputListener : KtxInputAdapter {
}
```

Creating and customizing a new `LetterboxingViewport`:

```Kotlin
import ktx.app.LetterboxingViewport

val viewport: Viewport = LetterboxingViewport(targetPpiX = 96f, targetPpiY = 96f, aspectRatio = 4f / 3f)
// Updating viewport on resize:
viewport.update(Gdx.graphics.width, Gdx.graphics.height, true)
```

Profiling an operation:

```Kotlin
Expand Down
24 changes: 24 additions & 0 deletions graphics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ original `ShapeRenderer` methods and perform the same actions. The methods inclu
A `use` inlined extension method is also available for `ShapeRenderer`, ensuring you won't have unbalanced `begin()`
and `end()` calls.

#### Cameras and viewports

- `LetterboxingViewport` combines `ScreenViewport` and `FitViewport` behavior: it targets a specific aspect ratio and
applies letterboxing like `FitViewport`, but it does not scale rendered objects when resized, keeping them in fixed size
similarly to `ScreenViewport`. Thanks to customizable target PPI value, it is ideal for GUIs and can easily support
different screen sizes.

### Usage examples

Using a `Batch`:
Expand Down Expand Up @@ -172,6 +179,23 @@ import ktx.graphics.takeScreenshot
takeScreenshot(Gdx.files.external("mygame/screenshot.png"))
```

Creating and customizing a new `LetterboxingViewport`:

```Kotlin
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.utils.viewport.Viewport
import ktx.graphics.LetterboxingViewport

class Application: ApplicationAdapter() {
val viewport: Viewport = LetterboxingViewport(targetPpiX = 96f, targetPpiY = 96f, aspectRatio = 4f / 3f)

override fun resize(width: Int, height: Int) {
// Updating viewport to the new screen size:
viewport.update(width, height, true)
}
}
```

#### Synergy

Use [`ktx-math`](../math) for `Vector2` and `Vector3` extensions, including idiomatic Kotlin factory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktx.app
package ktx.graphics

import com.badlogic.gdx.Application.ApplicationType.Android
import com.badlogic.gdx.Application.ApplicationType.iOS
Expand All @@ -22,16 +22,16 @@ import com.badlogic.gdx.utils.viewport.ScalingViewport
* FitViewport is excellent for the actual (2D) game rendering.
* @param targetPpiX this is the targeted pixel per inch ratio on X axis, which allows to scale the viewport
* correctly on different devices. Usually about 96 for desktop and WebGL platforms, 160 for mobiles.
* Make sure to call [updateScale] after changing this variable.
* correctly on different devices. Usually about 96 for desktop and WebGL platforms, 160 for mobiles.
* Make sure to call [updateScale] after changing this variable.
* @param targetPpiY targeted pixel per inch ratio on Y axis. Usually about 96 for desktop and WebGL platforms, 160
* for mobiles. Make sure to call [updateScale] after changing this variable.
* for mobiles. Make sure to call [updateScale] after changing this variable.
* @param aspectRatio width divided by height. Will preserve this aspect ratio by applying letterboxing.
*/
class LetterboxingViewport(
var targetPpiX: Float = defaultTargetPpi,
var targetPpiY: Float = defaultTargetPpi,
var aspectRatio: Float = 4f / 3f) : ScalingViewport(Scaling.fit, 0f, 0f) {
var targetPpiX: Float = defaultTargetPpi,
var targetPpiY: Float = defaultTargetPpi,
var aspectRatio: Float = 4f / 3f) : ScalingViewport(Scaling.fit, 0f, 0f) {
/** You can directly modify unit per pixel ratio (bypassing PPI check) by modifying this value.
* @see updateScale */
var scaleX = 0f
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ktx.app
package ktx.graphics

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.backends.lwjgl.LwjglNativesLoader
Expand Down

0 comments on commit bca5540

Please sign in to comment.