Skip to content

Commit 0422289

Browse files
authored
feat: use only one source of scale (kaplayjs#786)
1 parent bde95ec commit 0422289

File tree

5 files changed

+32
-25
lines changed

5 files changed

+32
-25
lines changed

src/app/appEvents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export function initAppEvents() {
4141
);
4242
_k.gfx.width = _k.gfx.ggl.gl.drawingBufferWidth
4343
/ _k.gfx.pixelDensity
44-
/ _k.gfx.gscale;
44+
/ _k.globalOpt.scale;
4545
_k.gfx.height = _k.gfx.ggl.gl.drawingBufferHeight
4646
/ _k.gfx.pixelDensity
47-
/ _k.gfx.gscale;
47+
/ _k.globalOpt.scale;
4848
}
4949
});
5050

src/core/engine.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import { createFrameRenderer } from "./frameRendering";
1818
export type Engine = ReturnType<typeof createEngine>;
1919

2020
export const createEngine = (gopt: KAPLAYOpt) => {
21-
const canvas = createCanvas(gopt);
21+
// Default options
22+
const opt = Object.assign({
23+
scale: 1,
24+
}, gopt);
25+
26+
const canvas = createCanvas(opt);
2227
const { fontCacheC2d, fontCacheCanvas } = createFontCache();
2328
const app = initApp({ canvas, ...gopt });
2429

@@ -37,24 +42,24 @@ export const createEngine = (gopt: KAPLAYOpt) => {
3742
const gl = canvasContext;
3843

3944
// TODO: Investigate correctly what's the differente between GFX and AppGFX and reduce to 1 method
40-
const gfx = initGfx(gl, gopt);
41-
const appGfx = initAppGfx(gfx, gopt);
42-
const assets = initAssets(gfx, gopt.spriteAtlasPadding ?? 0);
45+
const gfx = initGfx(gl, opt);
46+
const appGfx = initAppGfx(gfx, opt);
47+
const assets = initAssets(gfx, opt.spriteAtlasPadding ?? 0);
4348
const audio = initAudio();
4449
const game = createGame();
4550

4651
// Frame rendering
4752
const frameRenderer = createFrameRenderer(
4853
appGfx,
4954
game,
50-
gopt.pixelDensity ?? 1,
55+
opt.pixelDensity ?? 1,
5156
);
5257

5358
// Debug mode
54-
const debug = createDebug(gopt, app, appGfx, audio, game, frameRenderer);
59+
const debug = createDebug(opt, app, appGfx, audio, game, frameRenderer);
5560

5661
return {
57-
globalOpt: gopt,
62+
globalOpt: opt,
5863
canvas,
5964
app,
6065
ggl: gfx,
@@ -74,7 +79,7 @@ export const createEngine = (gopt: KAPLAYOpt) => {
7479
app,
7580
game,
7681
assets,
77-
gopt,
82+
opt,
7883
frameRenderer,
7984
debug,
8085
);

src/gfx/canvas.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import type { KAPLAYOpt } from "../types";
1+
import type { KAPLAYOpt, MustKAPLAYOpt } from "../types";
22

3-
export const createCanvas = (gopt: KAPLAYOpt) => {
3+
export const createCanvas = (gopt: MustKAPLAYOpt) => {
44
const root = gopt.root ?? document.body;
5-
const gscale = gopt.scale ?? 1;
65
const pixelDensity = gopt.pixelDensity || 1;
76

87
// If root is not defined (which falls back to <body>) we assume user is on a clean page,
@@ -31,8 +30,8 @@ export const createCanvas = (gopt: KAPLAYOpt) => {
3130
// check if isFixed
3231
gopt.width && gopt.height && !gopt.letterbox
3332
) {
34-
canvas.width = gopt.width * gscale;
35-
canvas.height = gopt.height * gscale;
33+
canvas.width = gopt.width * gopt.scale;
34+
canvas.height = gopt.height * gopt.scale;
3635
styles.push(`width: ${canvas.width}px`);
3736
styles.push(`height: ${canvas.height}px`);
3837
}

src/gfx/gfxApp.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {
66
MAX_BATCHED_VERTS,
77
VERTEX_FORMAT,
88
} from "../constants/general";
9+
import { go } from "../game/scenes";
910
import { type Color, rgb } from "../math/color";
1011
import { Mat23 } from "../math/math";
1112
import { Vec2 } from "../math/Vec2";
12-
import type { KAPLAYOpt } from "../types";
13+
import type { KAPLAYOpt, MustKAPLAYOpt } from "../types";
1314
import type { FontAtlas } from "./formatText";
1415
import { FrameBuffer } from "./FrameBuffer";
1516
import { BatchRenderer, type GfxCtx, Texture } from "./gfx";
@@ -32,8 +33,6 @@ export type AppGfxCtx = {
3233
postShaderUniform: Uniform | (() => Uniform) | null;
3334
renderer: BatchRenderer;
3435
pixelDensity: number;
35-
/** This is the scale factor that scales pixel "kaplay({ scale })" */
36-
gscale: number;
3736
transform: Mat23;
3837
transformStack: Mat23[];
3938
transformStackIndex: number;
@@ -66,10 +65,9 @@ export type Viewport = {
6665
scale: number;
6766
};
6867

69-
export const initAppGfx = (gfx: GfxCtx, gopt: KAPLAYOpt): AppGfxCtx => {
68+
export const initAppGfx = (gfx: GfxCtx, gopt: MustKAPLAYOpt): AppGfxCtx => {
7069
const defShader = makeShader(gfx, DEF_VERT, DEF_FRAG);
7170
const pixelDensity = gopt.pixelDensity ?? 1;
72-
const gscale = gopt.scale ?? 1;
7371
const { gl } = gfx;
7472

7573
// a 1x1 white texture to draw raw shapes like rectangles and polygons
@@ -82,8 +80,8 @@ export const initAppGfx = (gfx: GfxCtx, gopt: KAPLAYOpt): AppGfxCtx => {
8280
const frameBuffer = (gopt.width && gopt.height)
8381
? new FrameBuffer(
8482
gfx,
85-
gopt.width * pixelDensity * gscale,
86-
gopt.height * pixelDensity * gscale,
83+
gopt.width * pixelDensity * gopt.scale,
84+
gopt.height * pixelDensity * gopt.scale,
8785
)
8886
: new FrameBuffer(
8987
gfx,
@@ -174,7 +172,6 @@ export const initAppGfx = (gfx: GfxCtx, gopt: KAPLAYOpt): AppGfxCtx => {
174172
postShaderUniform: null as Uniform | (() => Uniform) | null,
175173
renderer: renderer,
176174
pixelDensity: pixelDensity,
177-
gscale,
178175

179176
transform: new Mat23(),
180177
transformStack: transformStack,
@@ -185,9 +182,9 @@ export const initAppGfx = (gfx: GfxCtx, gopt: KAPLAYOpt): AppGfxCtx => {
185182
bgAlpha: bgAlpha,
186183

187184
width: gopt.width
188-
?? gl.drawingBufferWidth / pixelDensity / gscale,
185+
?? gl.drawingBufferWidth / pixelDensity / gopt.scale,
189186
height: gopt.height
190-
?? gl.drawingBufferHeight / pixelDensity / gscale,
187+
?? gl.drawingBufferHeight / pixelDensity / gopt.scale,
191188

192189
viewport: {
193190
x: 0,

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6012,6 +6012,12 @@ export type KGamepad = {
60126012
*/
60136013
export type GameObjInspect = Record<Tag, string | null>;
60146014

6015+
export type MustKAPLAYOpt =
6016+
& {
6017+
[K in keyof Pick<KAPLAYOpt, "scale">]-?: KAPLAYOpt[K];
6018+
}
6019+
& KAPLAYOpt;
6020+
60156021
/**
60166022
* KAPLAY configurations.
60176023
*

0 commit comments

Comments
 (0)