Skip to content

Commit

Permalink
fix: Errant warning when using pixelRatio to upscale games
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Feb 12, 2024
1 parent e422f9f commit 164768d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- Fixes an issue where a collider that was part of a contact that was deleted did not fire a collision end event, this was unexpected
- Fixed errant warning about resolution when using `pixelRatio` on low res games to upscale
- Fixes an issue where a collider that was part of a contact that was deleted did not fire a collision end event, this was unexpected
- Fixes an issue where you may want to have composite colliders behave as constituent colliders for the purposes of start/end collision events. A new property is added to physics config, the current behavior is the default which is `'together'`, this means the whole composite collider is treated as 1 collider for onCollisionStart/onCollisionEnd. Now you can configure a `separate` which will fire onCollisionStart/onCollisionEnd for every separate collider included in the composite (useful if you are building levels or things with gaps that you need to disambiguate). You can also configure this on a per composite level to mix and match `CompositeCollider.compositeStrategy`
- Fixed issue where particles would have an errant draw if using a particle sprite
- Fixed issue where a null/undefined graphics group member graphic would cause a crash, now logs a warning.
Expand Down
4 changes: 4 additions & 0 deletions src/engine/Director/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,14 @@ export class Loader extends DefaultLoader {
await this.showPlayButton();
}

private _configuredPixelRatio: number | null = null;
public override async onBeforeLoad(): Promise<void> {
this._configuredPixelRatio = this.screen.pixelRatioOverride;
// Push the current user entered resolution/viewport
this.screen.pushResolutionAndViewport();
// Configure resolution for loader, it expects resolution === viewport
this.screen.resolution = this.screen.viewport;
this.screen.pixelRatioOverride = 1;
this.screen.applyResolutionAndViewport();

this.canvas.width = this.engine.canvas.width;
Expand All @@ -283,6 +286,7 @@ export class Loader extends DefaultLoader {

// eslint-disable-next-line require-await
public override async onAfterLoad(): Promise<void> {
this.screen.pixelRatioOverride = this._configuredPixelRatio;
this.screen.popResolutionAndViewport();
this.screen.applyResolutionAndViewport();
this.dispose();
Expand Down
16 changes: 16 additions & 0 deletions src/engine/Screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ export class Screen {
// Asking the window.devicePixelRatio is expensive we do it once
private _devicePixelRatio = this._calculateDevicePixelRatio();

/**
* Returns the computed pixel ratio, first using any override, then the device pixel ratio
*/
public get pixelRatio(): number {
if (this._pixelRatioOverride) {
return this._pixelRatioOverride;
Expand All @@ -389,6 +392,19 @@ export class Screen {
return this._devicePixelRatio;
}

/**
* Get or set the pixel ratio override
*
* You will need to call applyResolutionAndViewport() affect change on the screen
*/
public get pixelRatioOverride(): number | undefined {
return this._pixelRatioOverride;
}

public set pixelRatioOverride(value: number | undefined) {
this._pixelRatioOverride = value;
}

public get isHiDpi() {
return this.pixelRatio !== 1;
}
Expand Down

0 comments on commit 164768d

Please sign in to comment.