Skip to content

Commit

Permalink
refactor: Update CoreRendererOptions platform type to include CorePla…
Browse files Browse the repository at this point in the history
…tform
  • Loading branch information
wouterlucas committed Oct 29, 2024
1 parent 12b62db commit fa7d8ab
Show file tree
Hide file tree
Showing 18 changed files with 511 additions and 258 deletions.
15 changes: 13 additions & 2 deletions src/core/CoreTextureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { NoiseTexture } from './textures/NoiseTexture.js';
import { SubTexture } from './textures/SubTexture.js';
import { RenderTexture } from './textures/RenderTexture.js';
import type { Texture } from './textures/Texture.js';
import type { CorePlatform } from './platforms/CorePlatform.js';
import type { WebPlatform } from './platforms/web/WebPlatform.js';

/**
* Augmentable map of texture class types
Expand Down Expand Up @@ -139,6 +141,10 @@ export interface TextureOptions {
}

export class CoreTextureManager {
private platform: CorePlatform | WebPlatform;
public createImageBitmap: typeof createImageBitmap;
public hasCreateImageBitmap = false;

/**
* Map of textures by cache key
*/
Expand All @@ -155,8 +161,8 @@ export class CoreTextureManager {
txConstructors: Partial<TextureMap> = {};

imageWorkerManager: ImageWorkerManager | null = null;
hasCreateImageBitmap = !!self.createImageBitmap;
hasWorker = !!self.Worker;

/**
* Renderer that this texture manager is associated with
*
Expand All @@ -177,7 +183,12 @@ export class CoreTextureManager {
*/
frameTime = 0;

constructor(numImageWorkers: number) {
constructor(numImageWorkers: number, platform: CorePlatform | WebPlatform) {
this.platform = platform;

this.createImageBitmap = platform.createImageBitmap;
this.hasCreateImageBitmap = !!this.createImageBitmap;

// Register default known texture types
if (this.hasCreateImageBitmap && this.hasWorker && numImageWorkers > 0) {
this.imageWorkerManager = new ImageWorkerManager(numImageWorkers);
Expand Down
10 changes: 7 additions & 3 deletions src/core/Stage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class Stage {
private frameEventQueue: [name: string, payload: unknown][] = [];
private fontResolveMap: Record<string, CanvasTextRenderer | SdfTextRenderer> =
{};
private platform: CorePlatform;
private platform: CorePlatform | WebPlatform | null = null;

/// Debug data
contextSpy: ContextSpy | null = null;
Expand Down Expand Up @@ -152,7 +152,6 @@ export class Stage {
this.shManager = new CoreShaderManager();
this.animationManager = new AnimationManager();
this.contextSpy = enableContextSpy ? new ContextSpy() : null;
this.platform = platform;

let bm = [0, 0, 0, 0] as [number, number, number, number];
if (boundsMargin) {
Expand All @@ -162,6 +161,11 @@ export class Stage {
}
this.boundsMargin = bm;

assertTruthy(
platform !== null,
'A CorePlatform is not provided in the options',
);

const rendererOptions: CoreRendererOptions = {
stage: this,
platform,
Expand Down Expand Up @@ -264,7 +268,7 @@ export class Stage {
}

updateFrameTime() {
const newFrameTime = this.platform.getTimeStamp();
const newFrameTime = this.platform?.getTimeStamp() || Date.now();
this.lastFrameTime = this.currentFrameTime;
this.currentFrameTime = newFrameTime;
this.deltaTime = !this.lastFrameTime
Expand Down
34 changes: 34 additions & 0 deletions src/core/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,37 @@ export function convertUrlToAbsolute(url: string): string {
const absoluteUrl = new URL(url, self.location.href);
return absoluteUrl.href;
}

/**
* Compare two arrays for equality.
*
* @remarks
* This function will not try to compare nested arrays or Float32Arrays and
* instead will always return false when they are encountered.
*
* @param a
* @param b
* @returns
*/
export function compareArrays<T>(a: T[], b: T[]): boolean {
if (a.length !== b.length) {
return false;
}

let result = false;
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i]) || a[i] instanceof Float32Array) {
result = false;
break;
}

if (a[i] !== b[i]) {
result = false;
break;
}

result = true;
}

return result;
}
Loading

0 comments on commit fa7d8ab

Please sign in to comment.